-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathupgrade_pip.py
112 lines (81 loc) · 3.11 KB
/
upgrade_pip.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import datetime
import os
import pip
from subprocess import call
# joincfe.com/blog/
# joincfe.com/blog/upgrade-all-pip-python-packages-store-an-archive/
def get_outdated():
list_command = pip.commands.list.ListCommand()
options, args = list_command.parse_args([])
packages = pip.utils.get_installed_distributions()
return list_command.get_outdated(packages, options)
def upgrade_oudated(all_pkgs=False):
packages_upgraded = []
if all_pkgs:
to_upgrade = get_packages(upgrade=True)
else:
to_upgrade = get_outdated()
for dist in to_upgrade:
call("pip install --upgrade {pack_name}".format(pack_name=dist.project_name), shell=True)
packages_upgraded.append(dist.project_name)
if len(packages_upgraded) > 0:
print("Upgraded the folllowing pacakges:\n")
for pkg in packages_upgraded:
print(" - " + str(pkg))
print("\n")
# upgrade_oudated()
def set_archive_filepath(requirements_path, filepath=None, item=0):
if filepath is None:
base_filepath = os.path.join(requirements_path, "archive.txt")
else:
if item >= 0:
item += 1
today = str(datetime.date.today())
base_filepath = os.path.join(requirements_path, "archive__%s__%i.txt" %(today, item))
if os.path.exists(base_filepath):
return set_archive_filepath(requirements_path, filepath=base_filepath, item=item)
return base_filepath
def save_archive(archive_list):
requirements_path = os.path.join(os.getcwd(), "requirements")
if not os.path.exists(requirements_path):
os.mkdir(requirements_path)
#filepath = os.path.join(requirements_path, "requirements-archive.txt")
filepath = set_archive_filepath(requirements_path)
with open(filepath, "w+") as archive:
for rq in sorted(archive_list, key=str.lower):
archive.write(str(rq) + "\n")
print("Archive done")
def make_archive():
print("Making archive...")
current = get_packages()
save_archive(current)
def get_packages(upgrade=False):
print("Getting packages...")
current = []
for dist in pip.get_installed_distributions():
if upgrade:
current.append(dist)
else:
current.append(str(dist.as_requirement()))
return current
def get_requirements_location(next_to=None):
if next_to is not None:
location = None
for root, dirs, files in os.walk(os.getcwd()):
for file in files:
if str(file) == str(next_to):
location = os.path.join(root, "requirements.txt")
return location
return None
def save_requirements(next_to='manage.py'):
packages = get_packages()
print("Saving requirements.txt")
location = get_requirements_location(next_to=next_to) # os.path.join(os.getcwd(), "requirements.txt")
if location is None:
location = os.path.join(os.getcwd(), "requirements.txt")
with open(location, "w") as f:
for pkg in sorted(packages, key=str.lower):
f.write(str(pkg) + "\n")
make_archive()
upgrade_oudated(all_pkgs=False)
save_requirements()