Skip to content

Commit 1688b94

Browse files
committed
Factor more shared code to avoid duplicates
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 8eb5f44 commit 1688b94

4 files changed

Lines changed: 54 additions & 55 deletions

File tree

etc/scripts/7z.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from distutils.dir_util import copy_tree
1212
import os
1313
import shutil
14-
import subprocess
1514
import sys
1615

1716
import shared_utils
@@ -23,15 +22,6 @@
2322
TRACE_DEEP = False
2423

2524

26-
def extract_7zip(location, target_dir):
27-
"""
28-
Extract a 7z archive at `location` in the `target_dir` directory.
29-
"""
30-
out = subprocess.check_output(['7z', 'x', location], cwd=target_dir)
31-
if not b'Everything is Ok' in out:
32-
raise Exception(out)
33-
34-
3525
def install_files(extracted_dir, install_dir, copies):
3626
"""
3727
Install libraries and licenses from the extracted_dir
@@ -58,19 +48,6 @@ def install_files(extracted_dir, install_dir, copies):
5848
os.makedirs(dst, exist_ok=True)
5949
shutil.copy2(src, dst)
6050

61-
def extract_in_place(location):
62-
"""
63-
Extract an archive at `location` in a directory created side-by-side with
64-
the archive.
65-
Return the directory where the files are extracted
66-
"""
67-
target_dir = location.replace('.exe', '')
68-
if os.path.exists(target_dir):
69-
shutil.rmtree(target_dir)
70-
os.makedirs(target_dir, exist_ok=True)
71-
extract_7zip(location, target_dir)
72-
return target_dir
73-
7451

7552
def fetch_package(name, cache_dir):
7653
"""
@@ -102,7 +79,7 @@ def fetch_package(name, cache_dir):
10279
fetched_binary_loc = shared_utils.fetch_file(url=bin_url, dir_location=bin_cache_dir)
10380
shared_utils.verify(fetched_binary_loc, bin_sha256)
10481

105-
extracted_dir = extract_in_place(fetched_binary_loc)
82+
extracted_dir = shared_utils.extract_in_place(fetched_binary_loc)
10683
install_files(extracted_dir, install_dir, copies)
10784

10885
# also fetch sources

etc/scripts/homebrew.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def process_package(package, osarch, install_dir, copies, bin_cache_dir, src_cac
615615
# fetch the binary for the requested osarch
616616
package_binary_download = package.download_urls[osarch]
617617
fetched_binary_loc = package_binary_download.fetch(dir_location=bin_cache_dir)
618-
extracted_dir, _extracted_locations = shared_utils.extract_in_place(location=fetched_binary_loc)
618+
extracted_dir = shared_utils.extract_in_place(fetched_binary_loc)
619619

620620
# fetch the upstream formula and collect extra sources/patches:
621621
# formula_loc = package.formula_download_url.fetch(dir_location=src_cache_dir)

etc/scripts/msys2.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,11 @@ def update_package(name, version=None, repo='mingw64', cache_dir=None,
728728
except:
729729
fetched_binary_loc = shared_utils.fetch_file(url=root_package.download_url, dir_location=bin_cache_dir, force=True)
730730
shared_utils.verify(fetched_binary_loc, root_package.sha256)
731-
extracted_dir, extracted_locations = shared_utils.extract_in_place(fetched_binary_loc)
731+
extracted_dir = shared_utils.extract_in_place(fetched_binary_loc)
732732
install_files(extracted_dir, install_dir, package_short_name=root_package.short_name, copies=copies)
733733

734+
extracted_locs = [extracted_dir]
735+
734736
# also fetch sources
735737
shared_utils.fetch_file(url=root_package.source_package.download_url, dir_location=src_cache_dir)
736738

@@ -751,24 +753,19 @@ def update_package(name, version=None, repo='mingw64', cache_dir=None,
751753
except:
752754
fetched_binary_loc = shared_utils.fetch_file(url=dep.download_url, dir_location=bin_cache_dir, indent=3, force=True)
753755
shared_utils.verify(fetched_binary_loc, dep.sha256)
754-
extracted_dir, dep_extracted_locations = shared_utils.extract_in_place(fetched_binary_loc)
756+
extracted_dir = shared_utils.extract_in_place(fetched_binary_loc)
755757
install_files(extracted_dir, install_dir, package_short_name=dep.short_name, copies=copies)
756758

757759
# also fetch sources
758760
shared_utils.fetch_file(url=dep.source_package.download_url, dir_location=src_cache_dir, indent=5)
759761

760-
extracted_locations.extend(dep_extracted_locations)
762+
extracted_locs.append(extracted_dir)
761763

762764
check_installed_files(install_dir, copies, root_package)
763765

764766
# finally cleanup after thyself, removing extracted locations
765-
766-
for exloc in extracted_locations:
767-
if os.path.exists(exloc):
768-
if os.path.isdir(exloc):
769-
shutil.rmtree(exloc, False)
770-
else:
771-
os.remove(exloc)
767+
for loc in extracted_locs:
768+
shutil.rmtree(loc, False)
772769

773770

774771
def main(argv):

etc/scripts/shared_utils.py

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
# Copyright (c) 2020 nexB Inc.
12

3+
import hashlib
24
import os
5+
import shutil
36
import subprocess
47
import tarfile
8+
import zipfile
59

610
import requests
7-
import shutil
8-
import hashlib
911

1012
REQUEST_TIMEOUT = 60
1113

@@ -14,8 +16,6 @@
1416
TRACE_INSTALL = False
1517

1618

17-
18-
1919
def fetch_file(url, dir_location, file_name=None, force=False, indent=1):
2020
"""
2121
Fetch the file at `url` and save it in `dir_location`.
@@ -36,46 +36,74 @@ def fetch_file(url, dir_location, file_name=None, force=False, indent=1):
3636
return location
3737

3838

39-
4039
def extract_tar(location, target_dir):
4140
"""
4241
Extract a tar archive at `location` in the `target_dir` directory.
4342
Return a list of extracted locations (either directories or files)
4443
"""
45-
locations = []
44+
temp_extraction = None
4645
if location.endswith('.tar.zst'):
4746
# rare and "new" but used in msys
4847
subprocess.check_call(['unzstd', '-q', '-k', '-f', location])
49-
location, _, _ = location.rpartition('.zst')
50-
locations.append(location)
48+
temp_extraction, _, _ = location.rpartition('.zst')
49+
location = temp_extraction
5150
with open(location, 'rb') as input_tar:
5251
with tarfile.open(fileobj=input_tar) as tar:
5352
members = tar.getmembers()
5453
for tarinfo in members:
5554
tarinfo.mode = 0o755
5655
tar.extractall(target_dir, members=members)
57-
locations.append(target_dir)
58-
return locations
59-
56+
if temp_extraction:
57+
os.remove(temp_extraction)
6058

6159

6260
def extract_in_place(location):
6361
"""
6462
Extract a tar archive at `location` in a directory created side-by-side with
6563
the archive and named after the archive stripped from it's extension.
6664
Remove this directory if it already exists.
67-
68-
Return the directory where the files are extracted, and a list of all
69-
extracted_locations.
65+
66+
Return the directory where the files are extracted.
7067
"""
71-
target_dir = location.replace('.tar.xz', '').replace('.tar.gz', '').replace('.tar.zst', '')
68+
if '.tar' in location:
69+
extractor = extract_tar
70+
# split from tar.gz/xz/lzma/zst/bz2
71+
target_dir, _, _ = location.rpartition('.tar')
72+
73+
elif location.endswith('.zip'):
74+
extractor = extract_zip
75+
target_dir = location.replace('.zip', '')
76+
77+
elif location.endswith('.exe'):
78+
# for 7z self extracting exe
79+
extractor = extract_7zip
80+
target_dir = location.replace('.exe', '')
81+
7282
if os.path.exists(target_dir):
7383
shutil.rmtree(target_dir)
84+
7485
os.makedirs(target_dir, exist_ok=True)
75-
extracted_locations = extract_tar(location, target_dir)
76-
return target_dir, extracted_locations
86+
87+
extractor(location, target_dir)
88+
return target_dir
89+
90+
91+
def extract_zip(location, target_dir):
92+
"""
93+
Extract a zip archive file at location to the `target_dir` directory.
94+
"""
95+
with zipfile.ZipFile(location) as zipf:
96+
zipf.extractall(path=target_dir)
7797

7898

99+
def extract_7zip(location, target_dir):
100+
"""
101+
Extract a 7z archive at `location` to the `target_dir` directory.
102+
"""
103+
out = subprocess.check_output(['7z', 'x', location], cwd=target_dir)
104+
if not b'Everything is Ok' in out:
105+
raise Exception(out)
106+
79107

80108
def verify(fetched_location, expected_sha256=None):
81109
"""
@@ -88,6 +116,3 @@ def verify(fetched_location, expected_sha256=None):
88116
with open (fetched_location, 'rb') as f:
89117
fsha256 = hashlib.sha256(f.read()).hexdigest()
90118
assert fsha256 == expected_sha256, f'Invalid SHA256 for: {fetched_location}'
91-
92-
93-

0 commit comments

Comments
 (0)