-
Notifications
You must be signed in to change notification settings - Fork 0
/
BatchUpdatePackage.py
208 lines (123 loc) · 5.8 KB
/
BatchUpdatePackage.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import os
import sys
import re
import subprocess as sp
import argparse
PYTHON = sys.executable
parser = argparse.ArgumentParser()
parser.add_argument('--exclude', type=str, default='',
help='Packages that do not require updates. e.g "package1 package2 ..."')
parser.add_argument('--extype', type=str, default='',
help='The type of package that does not need to be updated')
ARGS = parser.parse_args()
def update_pip():
cmd = [PYTHON, '-m', 'pip install', '-U', 'pip']
p = sp.Popen(' '.join(cmd), stdin=sp.PIPE,
stdout=sp.PIPE, shell=True)
return None
def get_all_package():
cmd = [PYTHON, '-m', 'pip list']
p = sp.Popen(' '.join(cmd), stdin=sp.PIPE,
stdout=sp.PIPE, shell=True)
p_info = p.stdout.readlines()
packages = []
for info in p_info[2:]:
packages.append(info.decode().split())
return packages
def get_outdated_package():
cmd = [PYTHON, '-m', 'pip list', '--outdated']
p = sp.Popen(' '.join(cmd), stdin=sp.PIPE,
stdout=sp.PIPE, shell=True)
p_info = p.stdout.readlines()
packages = []
for info in p_info[2:]:
packages.append(info.decode().split())
return packages
def update_package():
outdated_packages = get_outdated_package()
exclude_packages = ARGS.exclude.split()
print(f'The packages that need to be excluded: {ARGS.exclude}')
updated_packages = []
roll_back_infos = []
if ARGS.exclude:
# Delete packages that do not need to be updated
for outdated_package in outdated_packages:
package = outdated_package[0]
Type = outdated_package[-1]
if package not in exclude_packages and Type != ARGS.extype:
updated_packages.append(outdated_package)
else:
updated_packages = outdated_packages
print(f'The packages that need to be updated:')
for updated_info in updated_packages:
print(f'{" ".join(updated_info)}')
print('---------------------------------------')
for updated_package in updated_packages:
Package, Version, Latest, Type = updated_package
upcmd = [PYTHON, '-m', 'pip install', '-U', f'{Package}', '--user']
up = sp.Popen(' '.join(upcmd), stderr=sp.PIPE, shell=True)
# read error info
err_infos = up.stderr.readlines()
# need_install_package_dict = {}
for err in err_infos:
err_info = err.decode()
# automatically detects whether there is a conflict
if 'incompatible' in err_info:
# roll back version
rbcmd = [PYTHON, '-m', 'pip install', '-U',
f'{Package}=={Version}', '--user']
rbp = sp.Popen(' '.join(rbcmd), stderr=sp.PIPE, shell=True)
roll_back_infos.append([Package, Latest, Version])
break
for roll_back_info in roll_back_infos:
package, latest, version = roll_back_info
print(f'Roll back {package} version: {latest}->{version}')
# if 'not installed' in err_info:
# # TODO automatically install other dependency packages
# no_install_info_list = re.split('[, ]', err_info)
# try:
# idx = no_install_info_list.index('requires') + 1
# except ValueError:
# continue
# target_package = no_install_info_list[idx]
# if is_str_include_num(target_package):
# separator = ''
# for i in target_package:
# if i == '<':
# separator += '<'
# if i == '=':
# separator += '='
# if i == '>':
# separator += '>'
# target_package_name, target_package_version = target_package.split(
# separator)
# if '=' in separator:
# if target_package_name not in need_install_package_dict:
# need_install_package_dict[target_package_name] = target_package_version
# pass
# else:
# if '<' in separator:
# if target_package_version <= need_install_package_dict[target_package_name]:
# need_install_package_dict[target_package_name] = target_package_version
# if '>' in separator:
# if target_package_version >= need_install_package_dict[target_package_name]:
# need_install_package_dict[target_package_name] = target_package_version
# else:
# print(f'{target_package_name}:{target_package_version}')
# else:
# # TODO find target package info with other method
# continue
# pass
# # install dependency package
# for pack, pack_version in need_install_package_dict.items():
# install_cmd = [PYTHON, '-m', 'pip install',
# f'{pack}=={pack_version}', '--user']
# install_p = sp.Popen(' '.join(install_cmd), stderr=sp.PIPE, shell=True)
return None
def is_str_include_num(s: str):
for i in s:
if i.isdigit():
return True
return False
if __name__ == '__main__':
update_package()