-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
executable file
·1060 lines (812 loc) · 36.2 KB
/
update.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import yaml
import sys
import os
import shlex
import time
from shutil import rmtree, copytree
from threading import Thread
import re
from subprocess import check_output, PIPE, Popen, DEVNULL, CalledProcessError
import argparse
base_path = os.path.dirname(os.path.abspath(__file__))
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.description = 'Updater for ECN / ROS virtual machine.'
parser.add_argument('-u', metavar='module', type=str, nargs='+', help='Which modules to upgrade (all installed, if empty)', default=())
parser.add_argument('-n', '--no_upgrade', action='store_true', default=False, help='Does not perform apt upgrade')
parser.add_argument('-t', '--test', action='store_true', default=False, help='Run in interactive mode (from a Python console)')
parser.add_argument('-a', '--all', action='store_true', default=False, help='Install all available modules')
parser.add_argument('-c', '--clean', action='store_true', default=False, help='Clean listed modules that are not required')
parser.add_argument('-f', '--force_compile', action='store_true', default=False, help='Force recompilation of ROS workspaces')
parser.add_argument('-g', '--force_git', action='store_true', default=False, help='Force git pull on suitable folders')
parser.add_argument('--nopip', action='store_true', default=False, help='Ignore pip dependencies')
parser.add_argument('-p', '--poweroff', action='store_true', default=False, help='Poweroff after installation / upgrade')
# add empty space after -u if no modules
if '-u' in sys.argv:
sys.argv.insert(sys.argv.index('-u')+1, '')
args = parser.parse_args()
# check we do not run as root
if os.geteuid() == 0 or 'SUDO_UID' in os.environ:
print('\n Do not run this script as root or sudo')
import sys
sys.exit(0)
def fuse(src1: dict, src2: dict):
keys = set(src1.keys()).union(src2.keys())
dst = {}
for key in keys:
if key not in src2:
dst[key] = src1[key]
elif key not in src1:
dst[key] = src2[key]
elif isinstance(src1[key], str):
dst[key] = src1[key]
elif isinstance(src1[key], list):
dst[key] = src1[key] + src2[key]
else:
dst[key] = fuse(src1[key], src2[key])
return dst
class Display:
user = os.environ['USER']
show = False
running = True
cmd = ''
delay = 0.2
@staticmethod
def init():
Display.user_offset = max(4, len(Display.user)) + 3 - len(Display.user)
Display.sudo_offset = max(4, len(Display.user)) - 1
# run blit thread
Display.thread = Thread(target=Display.blit)
Display.thread.start()
@staticmethod
def to_msg(cmd, sudo):
if sudo:
return f'\033[93m [sudo]{Display.sudo_offset*" "}\033[1;37;0m {cmd}'
else:
return f'\033[96m [{Display.user}]{Display.user_offset*" "}\033[1;37;0m {cmd}'
@staticmethod
def msg(cmd, sudo = False):
if type(cmd) == list:
Display.cmd = [Display.to_msg(cmd[0],sudo), Display.to_msg(' -> ' + cmd[1],sudo)]
else:
Display.cmd = Display.to_msg(cmd, sudo)
@staticmethod
def blit():
animations = ['◜◝◞◟','◣◤◥◢','▤▥▦▧▨▩', '▲►▼◄', '/-\\|']
done_smb = '..done' # '✓'
# animation = choice(animations)
animation = animations[-1]
idx = 0
prev_cmd = ''
while Display.running:
if prev_cmd != Display.cmd and prev_cmd != '':
print(prev_cmd, done_smb)
# animation = choice(animations)
idx = 0
if type(Display.cmd) == list:
print(Display.cmd[0])
Display.cmd = Display.cmd[1]
if Display.cmd != '':
print(Display.cmd, animation[idx], end="\r")
idx = (idx+1) % len(animation)
prev_cmd = Display.cmd
time.sleep(Display.delay)
@staticmethod
def endl():
Display.cmd = ''
@staticmethod
def stop(exit = True):
Display.cmd = ''
time.sleep(2*Display.delay)
Display.running = False
print('All set!')
if exit:
sys.exit(0)
Display.init()
def get_file(name):
return base_path + '/' + name
def run(cmd, cwd=None,show=False):
if show:
Display.msg(cmd)
if type(cmd) == list:
cmd = cmd[0]
try:
return check_output(shlex.split(cmd), stderr=PIPE, cwd=cwd).decode('utf-8').splitlines()
except CalledProcessError:
return None
distro = run('lsb_release -cs')[0]
ros1 = run(f'grep ros1_workspaces skel/{distro}/.bashrc', cwd=base_path)[0]
ros1 = ros1.replace('/',' ').split()[3]
ros2 = run(f'grep ros2_workspaces skel/{distro}/.bashrc', cwd=base_path)[0]
ros2 = ros2.replace('/',' ').split()[3]
gz = run(f'grep GZ_VERSION skel/{distro}/.bashrc', cwd=base_path)[0].split('=')[1].strip()
GZ = 'IGNITION' if gz == 'fortress' else 'GZ'
using_ecn_vm = os.uname().nodename == 'ecn-'+distro
# after 4 years finally some custom hacks are needed
if using_ecn_vm:
if run('grep foxy .bashrc', cwd=os.environ['HOME']):
args.force_compile = True
run(f'{base_path}/scripts/vm_update.sh')
class Sudo:
def __init__(self,gui=False):
print('Retrieving system state...')
if using_ecn_vm:
self.passwd = 'ecn'.encode()
else:
self.passwd = None
ask_passwd = True
import getpass
while ask_passwd:
self.passwd = getpass.getpass('Enter admin password: ').encode()
# check passwd
proc = Popen(['sudo','-S','-l'],stdin=PIPE,stdout=PIPE,stderr=PIPE)
out = proc.communicate(self.passwd)
if 'incorrect' not in out[1].decode():
ask_passwd = False
self.run('apt update -qy')
def run(self, cmd, cwd=None,show=True):
if show:
Display.msg(cmd, True)
if type(cmd) == list:
cmd = cmd[0]
proc = Popen(['sudo','-S'] + shlex.split(cmd), stdin=PIPE, stderr=PIPE, stdout=PIPE if show else DEVNULL,cwd=cwd)
proc.communicate(self.passwd)
proc.wait()
def apt_install(self, pkgs):
if not len(pkgs):
return
# enable OSRF repos if needed
ros1_specs = (f'ros-{ros1}', 'https://raw.githubusercontent.com/ros/rosdistro/master/ros.key', 'ros-latest.list', 'http://packages.ros.org/ros/ubuntu')
ros2_specs = (f'ros-{ros2}', 'https://raw.githubusercontent.com/ros/rosdistro/master/ros.key', 'ros2-latest.list', 'http://packages.ros.org/ros2/ubuntu')
ign_specs = ('ignition-', 'https://packages.osrfoundation.org/gazebo.gpg', 'gazebo-latest.list', 'http://packages.osrfoundation.org/gazebo/ubuntu-stable')
gz_specs = ('gz-', 'https://packages.osrfoundation.org/gazebo.gpg', 'gazebo-latest.list', 'http://packages.osrfoundation.org/gazebo/ubuntu-stable')
refresh_src = False
for start, key_url, repo_file, repo_url in (ros1_specs, ros2_specs, ign_specs, gz_specs):
if not any(pkg.startswith(start) for pkg in pkgs):
continue
if start == f'ros-{ros1}' and distro > 'focal':
# trying to install ROS 1 packages on 22.04+
print('ROS 1 is not available on Ubuntu 22.04 or later, current install needs ' + ' '.join(pkg for pkg in pkgs if pkg.startswith(start)))
sys.exit(0)
key_file = '/etc/apt/trusted.gpg.d/' + os.path.basename(key_url)
if not os.path.exists(key_file):
self.run(f'wget {key_url} -O {key_file}')
refresh_src = True
repo_file = '/etc/apt/sources.list.d/' + repo_file
if not os.path.exists(repo_file):
content = f'deb [arch=$(dpkg --print-architecture) signed-by={key_file}] {repo_url} {distro} main'
self.run(f"sh -c 'echo \"{content}\" > {repo_file}'")
self.run(f'chmod 664 {repo_file}')
refresh_src = True
if refresh_src:
self.run('apt update -qy')
self.run(['apt install -qy ' + ' '.join(pkgs), 'Installing packages'])
def apt_remove(self,pkgs,kept):
if not len(pkgs):
return
# simulate removal of each element
actually_removed = []
Display.msg('Listing useless packages', True)
for pkg in pkgs:
out = run(f'apt remove {pkg} --dry-run',show=False)
for line in out:
if line.startswith('Remv'):
other = line.split('[')[0][4:].strip()
if other in kept:
break
else:
actually_removed.append(pkg)
if len(actually_removed):
self.run(['apt purge -qy ' + ' '.join(actually_removed), 'Removing packages'])
self.run('apt autoremove --purge -qy')
def deb_install(self, url):
dst = os.path.basename(url).split('=')[-1]
run(f'wget "{url}" -O /tmp/{dst}')
self.run(f'apt install /tmp/{dst}')
poweroff = False
if args.poweroff:
r = input('Will poweroff computer after update [y/N] ')
if r in ('Y','y'):
poweroff = True
sudo = Sudo()
def src_type(src, dst):
return '_'.join([src,dst]).upper().strip('_')
class Enum(object):
def __init__(self, *keys):
self.__dict__.update(dict([(key.upper(), i) for i,key in enumerate(keys)]))
def values(self):
return self.__dict__.values()
Action = Enum('Remove', 'Keep', 'Install')
Source = Enum(*([src_type(src,dst) for src in ('apt','pip','deb','git') for dst in ('', 'ros', 'ros2')] + ['script']))
Status = Enum('Absent', 'Old', 'Installed')
actions = {Action.REMOVE: 'Remove', Action.KEEP: 'Keep as it is', Action.INSTALL: 'Install / update'}
status = {Status.ABSENT: 'Not installed', Status.OLD: 'Needs update', Status.INSTALLED: 'Up-to-date'}
special_modules = {'cleanup': Action.REMOVE, 'base': Action.INSTALL}
class Depend:
packages = {}
packages_old = []
def __init__(self, pkg, src):
self.pkg, self.src = Depend.resolve(pkg, src)
self.status = self.check()
self.result = Action.KEEP
self.pending = {}
self.cmake = ''
def cmake_flag(self, flag):
if self.cmake == '':
self.cmake = flag
def need_install(self):
return self.result == Action.INSTALL and self.status != Status.INSTALLED
def need_remove(self):
return self.result == Action.REMOVE and self.status != Status.ABSENT
def keep_apt(self):
return self.status != Status.ABSENT and self.result != Action.REMOVE and self.src == Source.APT
@staticmethod
def init_folders(folder):
Depend.folders = {Source.GIT: folder,
Source.GIT_ROS: folder+'/ros1',
Source.GIT_ROS2: folder+'/ros2'}
@staticmethod
def split_deb(src):
pkg,ver_ext = os.path.basename(src.split('=')[-1]).split('_',1)
pkg = pkg.split('[')[0]
ver = re.search('[0-9]+\\.[0-9]+\\.[0-9]+(-[0-9])?',ver_ext)
ver = ver_ext[ver.start():ver.end()]
return pkg,ver
@staticmethod
def resolve(pkg, src):
if src == Source.APT_ROS:
return f'ros-{ros1}-{pkg}', Source.APT
elif src == Source.APT_ROS2:
return f'ros-{ros2}-{pkg}', Source.APT
elif src in (Source.GIT_ROS, Source.GIT_ROS2, Source.GIT):
if not pkg.startswith('http'):
owner,repo = pkg.split(':',1)
pkg = f'https://github.com/{owner}/{repo}'
elif src == Source.DEB:
pkg = pkg.replace('[]', f'[{distro}]')
elif src == Source.SCRIPT:
for cand in (pkg, f'scripts/{pkg}', f'scripts/{pkg}.bash'):
if os.path.exists(get_file(cand)):
pkg = get_file(cand)
break
return pkg, src
def matches(self, pkg, src):
pkg, src = Depend.resolve(pkg, src)
return pkg == self.pkg and src == self.src
def configure(self, module, action):
self.pending[module] = action
self.result = max(self.pending.values())
#print(f'{module} wants {self.pkg} to be: {actions[action]} -> {actions[self.result]}')
#print('', self.pending)
def pending_status(self):
if self.result == Action.INSTALL:
return Status.INSTALLED
elif self.result == Action.REMOVE:
return Status.ABSENT
# Action.KEEP
return self.status
def abs_folder(self):
pkg = self.pkg.split(':')[1]
return self.parent_folder() + '/' + os.path.splitext(os.path.basename(pkg))[0]
def parent_folder(self):
return Depend.folders[self.src] + ('' if self.src == Source.GIT else '/src')
def check(self):
if not Depend.packages:
out = run('apt list --installed',show=True)
for line in out:
if '/' not in line:
continue
pkg,ver,_ = line.split(' ',2)
pkg = pkg[:pkg.find('/')]
Depend.packages[pkg] = ver
out = run('apt list --upgradable')
for line in out:
if '/' not in line:
continue
pkg,_ = line.split(' ',1)
Depend.packages_old.append(pkg[:pkg.find('/')])
if self.src == Source.PIP:
try:
out = run('pip3 show ' + self.pkg)
return Status.INSTALLED if out else Status.ABSENT
except CalledProcessError:
return Status.ABSENT
except FileNotFoundError:
return Status.ABSENT
if self.src == Source.APT:
if self.pkg not in Depend.packages:
return Status.ABSENT
if self.pkg in Depend.packages_old:
return Status.OLD
return Status.INSTALLED
if self.src == Source.DEB: # package[distro]_X.X.X*.deb
pkg,ver = Depend.split_deb(self.pkg)
if pkg in Depend.packages:
return Status.INSTALLED if Depend.packages[pkg] >= ver else Status.OLD
return Status.ABSENT
if self.src == Source.SCRIPT:
# delegate to script
result = run(self.pkg + ' -c')[0]
return getattr(Status, result.upper())
base_dir = self.abs_folder()
if not os.path.exists(base_dir):
return Status.ABSENT
# check GIT wrt upstream
run('git fetch',cwd=base_dir)
git_status = run('git status', cwd=base_dir)
if git_status is None:
return Status.OLD
if 'behind' not in git_status[1] and not args.force_git:
return Status.INSTALLED
return Status.OLD
def update(self):
if not self.need_install():
return None
if self.src == Source.PIP:
try:
sudo.run('pip3 install ' + self.pkg, show=True)
except CalledProcessError:
print('Re-run the process after pip3 was installed')
return self.src
if self.src == Source.APT:
return self.src
if self.src == Source.DEB:
sudo.deb_install(self.pkg)
return self.src
if self.src == Source.SCRIPT:
sudo.run(self.pkg + ' -i')
# git-based, may also be inside ros1 or ros2 local ws
root = self.parent_folder()
if not os.path.exists(root):
sudo.run(f'mkdir -p {root}')
perms = run(f'stat {Depend.folders[Source.GIT]}')
for line in perms:
if line.startswith('Access'):
if 'root' in line:
user = os.environ['USER']
sudo.run(f'chown {user} {Depend.folders[Source.GIT]} -R',show=False)
break
base_dir = self.abs_folder()
if os.path.exists(base_dir):
# uninstall previous files in case new commit removes any
self.uninstall()
# update repo
Display.msg('Refreshing repo ' + base_dir)
run('git pull --recurse-submodules', cwd=base_dir,show=False)
else:
# clone
if self.pkg.count(':') == 2:
# branch is specified
url,branch = self.pkg.rsplit(':',1)
# branch may be ros-specific
branch = branch.replace('<distro>', distro)
run(f'git clone --recursive {url} -b {branch}', cwd=root,show=True)
else:
run('git clone --recursive ' + self.pkg, cwd=root,show=True)
# install if not ROS
if self.src == Source.GIT and os.path.exists(base_dir + '/CMakeLists.txt'):
build_dir = base_dir + '/build'
if os.path.exists(build_dir):
run(f' rm -rf {build_dir}', show=False)
Display.msg(f'Compiling + installing {base_dir}')
run(f'mkdir -p {build_dir}',show=False)
run('cmake {} ..'.format(self.cmake),cwd=build_dir,show=False)
sudo.run('make install -j4', cwd=build_dir, show=False)
return self.src
def uninstall(self):
base_dir = self.abs_folder()
pkgs = []
if self.src == Source.SCRIPT:
sudo.run(self.pkg + ' -r')
return pkgs
def remove_if_here(path):
if os.path.exists(path):
sudo.run(f'rm -rf {path}',show=False)
installed_files = []
if self.src == Source.GIT:
manifest = base_dir + '/build/install_manifest.txt'
if os.path.exists(manifest):
# purge installed files
with open(manifest) as f:
installed_files = f.read().splitlines()
else: # ROS1 / ROS2
# find all packages defined in this clone
for root, subdirs, files in os.walk(base_dir):
if 'package.xml' in files:
with open(root + '/package.xml') as f:
xml = f.read()
pkgs.append(xml[xml.find('<name>')+6:xml.find('</name>')].strip())
subdirs[:] = []
if self.src == Source.GIT_ROS:
for pkg in pkgs:
manifest = f'{Depend.folders[self.src]}/build/{pkg}/install_manifest.txt'
if os.path.exists(manifest):
with open(manifest) as f:
raw_manifest = f.read().splitlines()
installed_files += [f for f in raw_manifest if not os.path.dirname(f).endswith('/install')]
elif self.src == Source.GIT_ROS2:
# just remove install folder
for pkg in pkgs:
remove_if_here(f'{Depend.folders[self.src]}/install/{pkg}')
# clean files from manifest
folders = set()
for f in installed_files:
folder = f
while folder not in ('/',''):
folder = os.path.dirname(folder)
if os.path.exists(folder):
folders.add(folder)
remove_if_here(f)
# cleanup created directories
empty_folders = True
while empty_folders:
empty_folders = [folder for folder in folders if not os.listdir(folder)]
for folder in empty_folders:
remove_if_here(folder)
folders.remove(folder)
return pkgs
def remove(self):
if not self.need_remove():
return None
# deb packages are removed in a single call to apt
if self.src == Source.APT:
return self.pkg
if self.src == Source.DEB:
return Depend.split_deb(self.pkg)[0]
# git packages are removed right here
ros_pkgs = self.uninstall()
# also remove build artefacts and source
base_dir = self.abs_folder()
if self.src in (Source.GIT_ROS, Source.GIT_ROS2):
if os.path.exists(f'{Depend.folders[self.src]}/.catkin_tools'):
# catkin-based clean if used
for pkg in ros_pkgs:
run(f'catkin clean {pkg}', cwd=Depend.folders[self.src])
# colcon-based clean
for root in ('build','log','logs','install'):
for pkg in ros_pkgs:
folder = f'{Depend.folders[self.src]}/{root}/{pkg}'
if os.path.exists(folder):
rmtree(folder)
# destroy git clone, has to use sudo because of build artifacts during install
sudo.run(f'rm -rf {base_dir}',show=False)
return None
class Module:
depends = []
def add_depend(self, pkg, src):
for dep in Module.depends:
if dep.matches(pkg, src):
break
else:
dep = Depend(pkg, src)
Module.depends.append(dep)
self.add_depends(dep)
if 'description' in self.config:
dep.configure(self.name, Action.KEEP)
if 'cmake' in self.config:
dep.cmake_flag(self.config['cmake'])
def __init__(self, name, config):
self.name = name
for dep in ('ros','ros2'):
if name != dep and dep in config:
if 'mod' not in config:
config['mod'] = []
config['mod'].append(dep)
self.config = config
if 'pip' in config and args.nopip:
config.pop('pip')
print(' ignoring pip dependencies')
self.parse_depends()
if name in special_modules:
self.configure(special_modules[name])
def is_default(self):
return 'description' in self.config and self.status != Status.ABSENT
def check_status(self, pending = False):
if len(self.all_deps()) == 0:
self.status = Status.INSTALLED
if pending:
return self.status
elif pending:
self.status = min(dep.pending_status() for dep in self.all_deps())
return self.status
else:
self.status = min(dep.status for dep in self.all_deps())
if 'description' in self.config:
# auto clean deps if module is not here
self.configure(Action.REMOVE if (self.status == Status.ABSENT and args.clean) else Action.KEEP)
def sync_depends(self, modules):
try:
if 'mod' in self.config:
for name in self.config.pop('mod'):
for level,deps in modules[name].sync_depends(modules).items():
self.add_depends(deps, level+1)
self.check_status()
except KeyError as err:
if self.name == 'cleanup':
print('Invalid dependency in cleanup: ', err)
else:
print('While parsing dependencies of',self.name)
raise(err)
return self.deps
def all_deps(self):
return [dep for deps in self.deps.values() for dep in deps]
def add_depends(self, deps, level = 0):
if level not in self.deps:
self.deps[level] = set()
if isinstance(deps, set):
for dep in deps:
self.deps[level].add(dep)
else:
self.deps[level].add(deps)
def parse_depends(self):
self.deps = {}
for key in Source.__dict__:
src = key.lower().split('_')
if len(src) == 2:
src,dst = src
if dst not in self.config:
continue
sub = self.config[dst]
else:
src,dst = src[0], ''
sub = self.config
if src not in sub:
continue
for pkg in sub[src]:
self.add_depend(pkg, getattr(Source, key))
def description(self):
return self.name.upper() + ' (' + self.config['description'] + ')'
def configure(self, action):
if self.name in special_modules:
action = special_modules[self.name]
for dep in self.all_deps():
dep.configure(self.name, action)
# read global + distro-specific modules
info = fuse(yaml.safe_load(open(get_file('modules.yaml'))),
yaml.safe_load(open(get_file(f'modules-{distro}.yaml'))))
# keys with comma are double-keys
keys = list(info.keys())
for key in keys:
if ',' not in key:
continue
detail = info.pop(key)
for mod in key.split(','):
info[mod.strip()] = detail
# erase disabled modules
disable = []
if 'disable' in info:
disabled = info['disable']
info.pop('disable')
groups = [key for key in info if isinstance(info[key], list) and 'ignore' not in key]
for mod in disable:
if mod in info:
info.pop(mod)
for group in groups:
if mod in info[group]:
info[group].pop(mod)
ignore = info['vm_ignore'] if 'vm_ignore' in info and using_ecn_vm else []
Depend.init_folders(info['lib_folder'] if 'lib_folder' in info else '/opt/ecn')
modules = dict((name, Module(name, config)) for name, config in info.items() if isinstance(config, dict))
groups = dict((group, info[group]) for group in groups)
for module in modules.values():
module.sync_depends(modules)
module.check_status()
def extract_cmake_names(path):
def project_name(line):
try:
line = line.lower().split('#')[0]
if 'project' in line:
start,end = line.index('(')+1, line.index(')')
return line[start:end].split()[0]
except ValueError:
pass
return None
projects = {}
for root, dirs, files in os.walk(path):
if 'CMakeLists.txt' in files:
with open(f'{root}/CMakeLists.txt') as cmake:
for line in cmake:
proj = project_name(line)
if proj:
projects[proj] = root
break
return projects
def setup_ignored(root, ros):
projects = extract_cmake_names(root+'/src')
ignore_file = 'CATKIN_IGNORE' if ros == 1 else 'COLCON_IGNORE'
for name in projects:
if name in ignore:
run(f'touch {projects[name]}/{ignore_file}')
def perform_update(action = None, poweroff=False):
'''
Final action
'''
if action is not None:
for m in modules.values():
if 'description' in m.config:
m.configure(action)
if sudo.passwd is None:
return
# install skeleton if ros_management does not appear in bashrc (first run)
skel = f'{base_path}/skel/{distro}'
bashrc = os.environ['HOME'] + '/.bashrc'
with open(bashrc) as f:
if 'ros_management_tools' not in f.read():
copytree(skel + '/', os.environ['HOME'], dirs_exist_ok = True)
# wallpaper
wp = [f for f in os.listdir(base_path + '/images/') if ros2 in f][0]
if not os.path.exists(f'{Depend.folders[Source.GIT]}/{wp}'):
sudo.run(f'cp {base_path}/images/{wp} {Depend.folders[Source.GIT]}')
# remove old ones
pkgs = [dep.remove() for dep in Module.depends]
kept = [dep.pkg for dep in Module.depends if dep.keep_apt()]
sudo.apt_remove([pkg for pkg in pkgs if pkg], kept)
# filter by source APT > DEB
to_install = dict((src, [dep for dep in Module.depends if dep.need_install() and dep.src == src]) for src in Source.values())
# apt-based new packages
pkgs = [dep.pkg for dep in to_install[Source.APT]]
if len(pkgs):
sudo.apt_install(pkgs)
# global ones
if len(Depend.packages_old) and not args.no_upgrade:
sudo.run('apt upgrade -qy')
sudo.run('apt autoremove --purge -qy')
# deb-based
updated = [dep.update() for dep in to_install[Source.DEB]]
if Source.DEB in updated:
sudo.run('apt install -qy --fix-broken --fix-missing')
# pip3-based
for dep in to_install[Source.PIP]:
dep.update()
# git-based
for dep in to_install[Source.GIT]:
dep.update()
# ros ws
need_chmod = False
updated = [dep.update() for dep in to_install[Source.GIT_ROS] + to_install[Source.GIT_ROS2]]
# recompile ros1ws
if Source.GIT_ROS in updated or (args.force_compile and os.path.exists(Depend.folders[Source.GIT_ROS])):
if args.force_compile or not os.path.exists(f'{Depend.folders[Source.GIT_ROS]}/.catkin_tools'):
# purge catkin install
for folder in ('build','install','devel','log'):
if os.path.exists(f'{Depend.folders[Source.GIT_ROS]}/{folder}'):
rmtree(f'{Depend.folders[Source.GIT_ROS]}/{folder}')
run(f'catkin config --init --extend /opt/ros/{ros1} --install -DCATKIN_ENABLE_TESTING=False --make-args -Wno-dev --cmake-args -DCMAKE_BUILD_TYPE=Release', cwd=Depend.folders[Source.GIT_ROS])
setup_ignored(Depend.folders[Source.GIT_ROS], 1)
run([f'catkin build --continue-on-failure', f'Compiling ROS 1 auxiliary workspace @ {Depend.folders[Source.GIT_ROS]}'], cwd=Depend.folders[Source.GIT_ROS], show=True)
need_chmod = True
# recompile ros2ws
if Source.GIT_ROS2 in updated or (args.force_compile and os.path.exists(Depend.folders[Source.GIT_ROS2])):
if args.force_compile:
root = Depend.folders[Source.GIT_ROS2]
run(f'rm -rf {root}/build {root}/install {root}/log')
setup_ignored(Depend.folders[Source.GIT_ROS2], 2)
run([f'bash -c -i "source /opt/ros/{ros2}/setup.bash && {GZ}_VERSION={gz} colcon build --symlink-install --continue-on-error"',
f'Compiling ROS 2 auxiliary workspace @ {Depend.folders[Source.GIT_ROS2]}'],
cwd=Depend.folders[Source.GIT_ROS2], show=True)
need_chmod = True
if need_chmod:
sudo.run([f'chmod a+rX {Depend.folders[Source.GIT]} -R', 'Setting permissions'])
# check chmod for these ones
if os.path.exists('/opt/coppeliaSim'):
sudo.run('chmod a+rwX -R /opt/coppeliaSim',show=False)
# poweroff only if no one else is connected
if poweroff and all(user == Display.user for user in run('users')[0].split()):
Display.stop(False)
sudo.run('poweroff')
else:
Display.stop()
if __name__ != '__main__':
sys.exit(0)
if args.test:
# to test things
Display.stop()
if type(args.u) == list:
# '-u' was given
to_update = [mod for mod in modules if mod in args.u]
if len(to_update) == 0:
# update all existing ones
to_update = [mod for mod in modules if modules[mod].is_default()]
for mod in to_update:
modules[mod].configure(Action.INSTALL)
print(f'Will update / install the following modules: {", ".join(to_update)}')
perform_update(poweroff=poweroff)
if args.all:
# install / update all modules
perform_update(Action.INSTALL,poweroff=poweroff)
sys._excepthook = sys.excepthook
def exception_hook(exctype, value, traceback):
print(exctype, value, traceback)
sys._excepthook(exctype, value, traceback)
sys.exit(1)
sys.excepthook = exception_hook
Display.endl()
# GUI part
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QVBoxLayout,QHBoxLayout,QGridLayout, QLabel, QPushButton, QCheckBox, QComboBox, QSpacerItem, QSizePolicy, QInputDialog, QLineEdit
from PyQt5.QtCore import pyqtSignal as Signal
from PyQt5.QtGui import QFont, QIcon
def Font(size = 10):
return QFont("Helvetica", size, QFont.Bold)
class UpdaterGUI(QWidget):
def __init__(self):
super().__init__()
self.setWindowIcon(QIcon(get_file('images/ecn-rob.png')))
self.setWindowTitle('Virtual Machine updater')
# build GUI
layout = QVBoxLayout(self)
font = Font()
# top bar = groups
groups_layout = QHBoxLayout()
label = QLabel('Groups', self)
label.setFont(Font(12))
groups_layout.addWidget(label)
for group in groups:
groups_layout.addSpacing(10)
groups[group] = {'cb': QCheckBox(self), 'modules': groups[group]}
groups[group]['cb'].setText(group)
groups[group]['cb'].setFont(font)
groups_layout.addWidget(groups[group]['cb'])
groups[group]['cb'].clicked.connect(self.group_update)
layout.addLayout(groups_layout)
layout.addSpacing(10)
mod_layout = QGridLayout()
spacer = QSpacerItem(10, 20, QSizePolicy.Minimum, QSizePolicy.Expanding)
valid = sorted([m for m in modules.values() if 'description' in m.config],
key = lambda m: m.name)
for i,module in enumerate(valid):
mod_layout.addItem(spacer, i, 0)
label = QLabel(module.description(), self)
label.setFont(font)
mod_layout.addWidget(label, i, 1)
mod_layout.addItem(spacer, i, 2)
label = QLabel(status[module.status], self)
label.setFont(font)
mod_layout.addWidget(label, i, 3)