-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist-py-packages
executable file
·50 lines (36 loc) · 1.33 KB
/
list-py-packages
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
#!/usr/bin/env bash
source /etc/profile.d/modules.sh
module --silent purge
module --silent load gcc-libs
module --silent load python3/recommended
python <<'EOF'
import pkg_resources
import re
import json
import sys
import datetime
re_name = re.compile("^Name: (.*)$", re.MULTILINE)
re_version = re.compile("^Version: (.*)$", re.MULTILINE)
re_summary = re.compile("^Summary: (.*)$", re.MULTILINE)
packages = list()
for pkg in pkg_resources.working_set:
metadata_text = pkg.get_metadata(pkg.PKG_INFO)
match = re_name.search(metadata_text)
pkg_name = match.group(1)
match = re_version.search(metadata_text)
pkg_version = match.group(1)
match = re_summary.search(metadata_text)
pkg_summary = match.group(1)
# These are named slightly weirdly to match the format we had to get from R
pkg_info = {"Package": pkg_name, "Version": pkg_version, "Title": pkg_summary}
packages.append(pkg_info)
package_key = lambda x: x["Package"].lower()
packages.sort(key=package_key)
info = dict()
info["version"] = f"Python {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
utc_dt = datetime.datetime.now(datetime.timezone.utc) # UTC time
dt = utc_dt.astimezone() # local time
info["generated"] = dt.strftime("%Y-%m-%dT%H:%M:%S%z")
info["packages"] = packages
print(json.dumps(info, indent=3))
EOF