From 734dcd38fe235284d0033fbecf8d8e94d33f7fa2 Mon Sep 17 00:00:00 2001 From: RichardG867 Date: Wed, 13 Apr 2022 22:14:11 -0300 Subject: [PATCH] Switch BIOSExtractor header file copying to hardlinks where supported --- biostools/extractors.py | 11 +++++------ biostools/util.py | 14 +++++++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/biostools/extractors.py b/biostools/extractors.py index a38e6e2..0f6ed7e 100644 --- a/biostools/extractors.py +++ b/biostools/extractors.py @@ -406,12 +406,11 @@ class BIOSExtractor(Extractor): # treat it as a big chunk of data. open(os.path.join(dest_dir_0, ':combined:'), 'wb').close() - # Copy any header file to extracted directory, for identifying Intel BIOSes. - # See AMIAnalyzer.can_handle for more information. - try: - shutil.copy(os.path.join(os.path.dirname(file_path_abs), ':header:'), os.path.join(dest_dir_0, ':header:')) - except: - pass + # Hardlink or copy any header file to extracted directory, to help with + # identifying Intel BIOSes. See AMIAnalyzer.can_handle for more information. + parent_header = os.path.join(os.path.dirname(file_path_abs), ':header:') + if os.path.exists(parent_header): + util.hardlink_or_copy(parent_header, os.path.join(dest_dir_0, ':header:')) # Remove BIOS file. try: diff --git a/biostools/util.py b/biostools/util.py index 86ecf70..542b42d 100644 --- a/biostools/util.py +++ b/biostools/util.py @@ -15,7 +15,7 @@ # # Copyright 2021 RichardG. # -import multiprocessing, os, math, re, traceback, urllib.request +import multiprocessing, os, math, re, shutil, traceback, urllib.request from biostools.pciutil import * date_pattern_mmddyy = re.compile('''(?P[0-9]{2})/(?P[0-9]{2})/(?P[0-9]{2,4})''') @@ -131,6 +131,18 @@ def date_lt(date1, date2, pattern): Date format set by the given pattern.""" return date_cmp(date1, date2, pattern) < 0 +def hardlink_or_copy(src, dest): + """Attempt to hardlink or copy src to dest. + Returns True if either operation was successful.""" + try: + os.link(src, dest) + except: + try: + shutil.copy2(src, dest) + except: + return False + return True + def log_traceback(*args): """Log to biostools_error.log, including any outstanding traceback."""