-
Notifications
You must be signed in to change notification settings - Fork 92
/
generate-packages-list.py
executable file
·64 lines (55 loc) · 2.44 KB
/
generate-packages-list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
"""
Generate human readable packages.txt file from a conda-lock file.
"""
import argparse
def parse_lock_line(line):
"""
Parse a single conda-lock line and return name
Supports both conda and pip pins.
Returns name of package and version as tuple
"""
if line.startswith("# pip"):
# Get just the URL
full_url = line.split()[-1]
# Remove hash from URL
url = full_url.split('#', 1)[0]
# Get just the name of file we're getting
filename = url.split('/')[-1]
if filename.endswith('.whl'):
# Wheels are in the format <package-name>-<version>-<tags>
underscored_pkg_name, version, _ = filename.split('-', 2)
elif filename.endswith('.tar.gz'):
# tarballs are just <package>-<version_.tgz
basename = filename.rsplit('.', 2)[0]
underscored_pkg_name, version = basename.split('-', 1)
else:
raise RuntimeError(f'Found unknown file {filename} in conda-lock file')
# PyPI has files with '_' when the package name has '-' in it
pkg_name = underscored_pkg_name.replace('_', '-')
else:
# Given something like this:
# https://conda.anaconda.org/conda-forge/noarch/nomkl-1.0-h5ca1d4c_0.tar.bz2#9a66894dfd07c4510beb6b3f9672ccc0
# it should return nomkl, 1.0, h5ca14dc_0
# Remove hash from the URL
url = line.split('#', 1)[0]
# Find last component of URL
full_pkg = url.rsplit('/', 1)[-1]
# Full pakage names are of form <pkg-name>-<version>-<build>.
# Since <pkg-name> can have dashes, use rsplit to split the whole
# thing into 3 components, starting from the right.
pkg_name, version, _ = full_pkg.rsplit('-', 2)
return pkg_name, version
def main():
argparser = argparse.ArgumentParser()
argparser.add_argument('lock_file', help='Path to conda-lock file to parse', default='conda-linux-64.lock')
args = argparser.parse_args()
print('# List of packages and versions installed in the environment')
print(f'# Generated by parsing {args.lock_file}, please use that as source of truth')
with open(args.lock_file) as f:
# Skip first four lines, as they only have metadata
lines = f.readlines()[4:]
for pkg_name, version in sorted(parse_lock_line(l) for l in lines if l.strip()):
print(f'{pkg_name}=={version}')
if __name__ == '__main__':
main()