forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpip_upgrade.py
More file actions
executable file
·58 lines (42 loc) · 1.39 KB
/
pip_upgrade.py
File metadata and controls
executable file
·58 lines (42 loc) · 1.39 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
57
#!/usr/bin/env python3
# Upgrade packages in a virtualenv -- but only the packages that
# are in that virtualenv, not site packages.
import os, sys
from pathlib import Path
import subprocess
import re
VENVLOC = os.environ["VIRTUAL_ENV"]
if not VENVLOC:
print("Not in a virtualenv!")
sys.exit(1)
def update_virtualenv():
packagenames, versions = get_installed_packages()
for pkg, version in zip(packagenames, versions):
print(pkg, version)
cmd = ["pip", "install", "-U", *packagenames]
print("Calling:", cmd)
subprocess.call(cmd)
def get_installed_packages():
package_dir = find_package_dir()
packagenames = []
packageversions = []
for infodir in package_dir.glob("*-*-info"):
parts = infodir.name.split('-')
if not parts[1].endswith("dist"):
print("bad parts!")
continue
version = parts[1][:-4]
if version.endswith('.'):
version = version[:-1]
if Path(package_dir, parts[0]).exists():
packagenames.append(parts[0])
packageversions.append(version)
return packagenames, packageversions
def find_package_dir():
for pydir in Path(VENVLOC, "lib").glob("python*"):
site_packages = Path(pydir, "site-packages")
if site_packages.exists():
return site_packages
return None
if __name__ == '__main__':
update_virtualenv()