-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
install-packages
executable file
·128 lines (106 loc) · 4.1 KB
/
install-packages
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
#!/usr/bin/env python3
# Copyright 2015 Josh Pieper, [email protected]. All rights reserved.
# Copyright 2015 Mikhail Afanasyev. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''%prog [options]
Install packages necessary for mjmech.
'''
import optparse
import re
import subprocess
import sys
# Verbosity level:
# 0 - print summaries and all commands which change state ('sudo' commands)
# 1 - print every command executed
g_verbosity = 0
def split(val):
return [y for y in [x.strip() for x in re.split('\s+', val)]
if y != '']
def call(*args, **kwargs):
if g_verbosity >= 1:
assert 'cwd' not in kwargs, 'not implemented'
print('call:', args)
kwargs['stdout'] = sys.stdout
kwargs['stderr'] = sys.stderr
kwargs['shell'] = True
rv = subprocess.call(*args, **kwargs)
if g_verbosity >= 1:
print('call result:', rv)
return rv
def check_call(cmd, **kwargs):
# always show 'sudo' commands so user knows what he types password for
is_sudo = kwargs.pop('is_sudo', None)
# if command generates lots of output we might as well show the command
# itself
is_chatty = kwargs.pop('is_chatty', None)
if g_verbosity >= 1 or is_sudo or is_chatty:
cmd_v = cmd
if 'cwd' in kwargs:
cmd_v = 'cd %s && %s' % (kwargs['cwd'], cmd_v)
print('check_call:', cmd_v)
kwargs['stdout'] = sys.stdout
kwargs['stderr'] = sys.stderr
kwargs['shell'] = True
return subprocess.check_call(cmd, **kwargs)
def check_output(*args, **kwargs):
if g_verbosity >= 1:
print('check_output:', args)
kwargs['shell'] = True
result = subprocess.check_output(*args, **kwargs).strip()
if g_verbosity >= 1:
print('check_output_result:')
if result == '':
print('', '(empty string)')
else:
print(result)
return result
def main():
global g_verbosity
usage, description = __doc__.split('\n\n', 1)
parser = optparse.OptionParser(usage=usage, description=description)
parser.add_option('--verbose', '-v', action='count', default=0,
help='display additional debugging information')
parser.add_option('--quiet', '-q', action='count', default=0,
help='do not be verbose')
parser.add_option('--yes', '-y', action='store_true',
help='do not ask for confirmation')
parser.add_option('--system', action='store_true',
help='also install system packages')
parser.add_option('--test', '-t', action='store_true',
help='just test if anything needs installing')
options, args = parser.parse_args()
g_verbosity += options.verbose - options.quiet
PKGLIST = split('''
bison flex gcc g++ curl libc6-dev mesa-common-dev libglu1-mesa-dev
libtinfo5 libxrender-dev libxi-dev libx11-xcb-dev
''')
ubuntu_release = subprocess.check_output(["lsb_release", "-cs"]).strip()
if options.verbose:
print('Ubuntu release:', ubuntu_release)
if ubuntu_release == b'focal':
PKGLIST += split('''python-is-python3 libtinfo5''')
if options.yes or call(
"apt-get install --dry-run %s |"
"egrep ' could not be installed|^Conf'" % ' '.join(PKGLIST)) == 0:
print()
print('Need to install some packages')
if options.test:
sys.exit(1)
cmd = ('sudo apt-get install %s %s' % (
('-y' if options.yes else ''), ' '.join(PKGLIST)))
check_call(cmd, is_sudo=True)
else:
print('All packages up to date')
if __name__ == '__main__':
main()