-
Notifications
You must be signed in to change notification settings - Fork 0
/
caws.py
executable file
·95 lines (65 loc) · 2.68 KB
/
caws.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
#!/usr/bin/env python3
"""Interactavly upgrade Python packages."""
import json
import subprocess
import sys
VERSION = '0.5.3'
APP_NAME = 'caws'
PIP_X = 'pip3'
def pip_upgrade(ask):
"""External call to the pip package manager."""
syscall = subprocess.run([PIP_X, 'list', '--outdated',
'--format=json'], stdout=subprocess.PIPE)
outdated = json.loads(syscall.stdout)
if not outdated:
print('All Python packages are up to date.')
else:
to_upgrade = list()
for rec in outdated:
to_upgrade.append(rec.get('name'))
print('\nOutdated packages: {0:s}'.format(', '.join(to_upgrade)))
for rec in outdated:
choice = str()
if ask:
choice = input('\nUpdate {0:s} from {1:s} to {2:s}? [Y/n] '
.format(rec.get('name'),
rec.get('version'),
rec.get('latest_version')))
if choice.lower() == 'y' or choice == '' or not ask:
pkg = rec.get('name')
try:
syscall = subprocess.run([PIP_X, 'install', '-U', pkg])
except subprocess.SubprocessError:
sys.exit()
if __name__ == '__main__':
if len(sys.argv) > 1:
switch = sys.argv[1]
else:
switch = None
if switch == '-h' or switch == '--help':
print('usage: caws [option]\n')
print('optional arguments:')
print('-h, --help Help on commandline switches')
print('-v, --version Print version string')
print('-l, --list Show outdated packages')
print('-u, --update Interactive updater')
print('-a, --all Batch uprgade all packages')
elif switch == '-v' or switch == '--version':
syscall = subprocess.run([PIP_X, '--version'], stdout=subprocess.PIPE)
print('The Cheese Shop Updater (version {0:s})'.format(VERSION))
print('(c) 2018 YarraRiver\n')
print(syscall.stdout.decode('utf-8'))
elif switch == '-l' or switch == '--list':
syscall = subprocess.run([PIP_X, 'list', '--outdated',
'--format=columns'], stdout=subprocess.PIPE)
outdated = syscall.stdout.decode('utf-8')
if not outdated:
print('All Python packages are up to date.')
else:
print(outdated)
elif switch == '-u' or switch == '--upgrade' or not switch:
pip_upgrade(ask=True)
elif switch == '-a' or switch == '--all':
pip_upgrade(ask=False)
else:
print('{0:s}: illegal option {1:s}'.format(APP_NAME, switch))