-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate.py
More file actions
56 lines (45 loc) · 1.54 KB
/
Copy pathupdate.py
File metadata and controls
56 lines (45 loc) · 1.54 KB
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
def b(): import ipdb; ipdb.set_trace()
import argparse
import subprocess
import sys
try:
import importlib.metadata as importlib_metadata # in Python 3.8 and later
except ImportError:
import importlib_metadata
def get_installed_version(package_name):
try:
return importlib_metadata.version(package_name)
except importlib_metadata.PackageNotFoundError:
return None
def main(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
package_versions = {}
for line in lines:
line = line.strip()
if not line:
continue
if '==' in line:
package_name, _ = line.split('==')
else:
package_name = line
installed_version = get_installed_version(package_name)
if not installed_version:
subprocess.check_call([
sys.executable,
"-m",
"pip",
"install",
package_name,
])
installed_version = get_installed_version(package_name)
package_versions[package_name] = f"{package_name}=={installed_version}\n"
sorted_packages = sorted(package_versions.keys())
output_lines = [package_versions[package] for package in sorted_packages]
with open(file_path, 'w') as file:
file.writelines(output_lines)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Update package versions in a file with the installed versions.')
parser.add_argument('--file_path', default='.python/pip.txt', type=str, help='The path to the file containing package names and versions.')
args = parser.parse_args()
main(args.file_path)