-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroot_tools.py
executable file
·120 lines (100 loc) · 3.88 KB
/
root_tools.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
#!/usr/bin/python
"""
ROS Installer root utils
Handle the setup and installation of ROS (www.ros,org) in this module.
Needs to run with super user permissions as it needs to make changes in /etc/apt/sources.list.d and installs
packages via apt
Usage:
root_tools.py <server> <ros_version> <packages>...
root_tools.py (-h | --help)
root_tools.py (-v | --version)
Options:
-h --help Show this screen.
-v --version Show version.
"""
import os
import apt
import sys
import lsb_release
import subprocess
import datetime
from docopt import docopt
__author__ = 'Markus Bajones'
__license__ = 'GPL'
__version__ = '1.0.0'
__email__ = '[email protected]'
def install_packages(pkgs):
print("installing {pkgs}".format(pkgs=pkgs))
cache = apt.cache.Cache()
cache.update()
try:
# prepare all packages for installation
for i in pkgs:
pkg = cache[i]
if pkg.is_installed:
print("{pkg_name} already installed.".format(pkg_name=i))
else:
print("{pkg_name} will be installed.".format(pkg_name=i))
pkg.mark_install()
cache.commit()
except Exception as arg:
print("Sorry, package installation failed [{err}]".format(err=str(arg)))
return
def write_source_list(server, file):
if not 'ubuntu' in lsb_release.get_distro_information()['ID'].lower():
print("Non Ubuntu detected.")
return False
codename = lsb_release.get_distro_information()['CODENAME']
content = 'deb '+server+'/ros/ubuntu '+codename+' main'
try:
if not os.path.exists(file) or not content in open(file).read():
with open(file, 'w+') as f:
f.write(content)
print("{file} written successfully.".format(file=file))
else:
print("Mirror already in {file}".format(file=file))
except IOError as e:
print("Sorry, unable to write file [{err}]".format(err=str(e)))
return True
def add_key_to_system(keyserver, key):
print("add gpg key to system")
try:
subprocess.check_call(["/usr/bin/apt-key", "adv", "--keyserver", keyserver, "--recv-key", key])
except subprocess.CalledProcessError as e:
print("apt-key executed with errors. [{err}]".format(err=str(e)))
def check_key_installed(key):
print("check if gpg key is installed")
try:
proc = subprocess.Popen(["/usr/bin/apt-key", "list"], stdout=subprocess.PIPE)
for line in iter(proc.stdout.readline, ''):
if key.split('0x')[1] in line: # remoce '0x' from the key as it is not shown in apt-key list output
print('{key} is already installed on this system.'.format(key=key))
return True
return False
except Exception as e:
print('Error while trying to check for installed GPG key')
print('Error was: {err}'.format(err=e))
return False
def init_rosdep():
if not os.path.exists('/etc/ros/rosdep/sources.list.d/20-default.list'):
try:
subprocess.check_call(['/usr/bin/rosdep', 'init'])
except subprocess.CalledProcessError as e:
print("rosdep executed with errors. [{err}]".format(err=str(e)))
if __name__ == '__main__':
arguments = docopt(__doc__, version='ROS Installer root utils 0.1')
ros_version = arguments['<ros_version>']
packages = arguments['<packages>']
server = arguments['<server>']
print("parameters: [{}, {}, {}]".format(ros_version, server, packages))
sources_list = '/etc/apt/sources.list.d/ros-latest.list'
ros_key = '0xB01FA116'
ros_keyserver = 'hkp://pool.sks-keyservers.net:80'
if not os.geteuid() == 0:
sys.exit('Script must be run as root')
write_source_list(server, sources_list)
if not check_key_installed(ros_key):
add_key_to_system(ros_keyserver, ros_key)
install_packages(packages)
init_rosdep()
exit()