From c8268413bbbbf7f0b5600c6610f51d288581fbac Mon Sep 17 00:00:00 2001 From: Jose Luis Rivero Date: Mon, 8 Apr 2024 19:24:40 +0200 Subject: [PATCH] Implement --pre-cleaup option Signed-off-by: Jose Luis Rivero --- .github/workflows/main.yml | 12 ++++++++--- plugins/repository.py | 42 ++++++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cde5f2b..6f8f4ec 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -31,11 +31,17 @@ jobs: - name: Test plugins _TEST.py run: | python3 plugins/*_TEST.py - - name: Smoke system tests + - name: Smoke system tests for repository module run: | - sudo env "PYTHONPATH=$pythonLocation/lib/python${{ matrix.python-version}}/site-packages" \ - python3 ./gzdev.py repository enable osrf stable python3 gzdev.py repository list + sudo env "PYTHONPATH=$pythonLocation/lib/python${{ matrix.python-version}}/site-packages" \ + python3 ./gzdev.py repository enable osrf stable + sudo env "PYTHONPATH=$pythonLocation/lib/python${{ matrix.python-version}}/site-packages" \ + python3 ./gzdev.py repository --pre-cleanup enable osrf stable 2>log + grep /etc/apt/sources.list.d/_gzdev_osrf_stable.list + grep /usr/share/keyrings/_gzdev_osrf_stable.gpg + - name: Smoke system tests for ign-docker module + run: | python3 gzdev.py ign-docker-env citadel python3 gzdev.py ign-docker-env dome --linux-distro ubuntu:bionic python3 gzdev.py ign-docker-env dome --linux-distro ubuntu:focal --vol /tmp:/foo::/tmp:/bar --rocker-args '--dev-helpers' diff --git a/plugins/repository.py b/plugins/repository.py index 415c237..a93e2ca 100644 --- a/plugins/repository.py +++ b/plugins/repository.py @@ -6,7 +6,7 @@ Usage: gzdev repository (ACTION) [] [] [--project=] [--force-linux-distro=] - [--keyserver=] [--gpg-check] + [--keyserver=] [--gpg-check] [--pre-cleanup] gzdev repository list gzdev repository (-h | --help) gzdev repository --version @@ -22,6 +22,8 @@ --gpg-check Do run a gpg check for validating the key downloaded in enable action (need the gpg binary) + --pre-cleanup Remove all repositories and keys installed + by gzdev from the system before proceding """ import distro @@ -37,6 +39,9 @@ from docopt import docopt +GZDEV_FILE_PREFIX = '_gzdev_' + + def _check_call(cmd): print('') print("Invoking '%s'" % ' '.join(cmd)) @@ -128,13 +133,13 @@ def get_repo_url(repo_name, repo_type, config): def get_sources_list_file_path(repo_name, repo_type): - filename = '_gzdev_' + repo_name + '_' + repo_type + '.list' + filename = GZDEV_FILE_PREFIX + repo_name + '_' + repo_type + '.list' directory = '/etc/apt/sources.list.d' return directory + '/' + filename def key_filepath(repo_name, repo_type): - return f"/usr/share/keyrings/_gzdev_{repo_name}_{repo_type}.gpg" + return f"/usr/share/keyrings/{GZDEV_FILE_PREFIX}_{repo_name}_{repo_type}.gpg" def assert_key_in_file(key, key_path): @@ -219,13 +224,15 @@ def normalize_args(args): project = args['--project'] force_linux_distro = args['--force-linux-distro'] gpg_check = args['--gpg_check'] if '--gpg_check' in args else False + pre_cleanup = args['--pre-cleanup'] if '--pre-cleanup' in args else False if force_linux_distro: linux_distro = force_linux_distro else: linux_distro = distro.codename() - if '--keyserver' in args: + if '--keyserver' in args and args['--keyserver']: warn('--keyserver option is deprecated. It is safe to remove it') - return action, repo_name, repo_type, project, linux_distro, gpg_check + return action, repo_name, repo_type, project, linux_distro, gpg_check, \ + pre_cleanup def validate_input(args): @@ -249,7 +256,10 @@ def process_project_install(project, config, linux_distro, gpg_check, def process_input(args, config): - action, repo_name, repo_type, project, linux_distro, gpg_check = args + action, repo_name, repo_type, project, linux_distro, gpg_check, \ + pre_cleanup = args + + remove_all_installed() if pre_cleanup else None if (action == 'enable'): if project: @@ -269,6 +279,26 @@ def process_input(args, config): disable_repo(repo_name) +def remove_file_by_pattern(directory, pattern): + for filename in os.listdir(directory): + if pattern.match(filename): + filepath = os.path.join(directory, filename) + try: + os.remove(filepath) + print(f'Removed: {filepath}') + except OSError as e: + print(f'Error: {filepath} - {e}') + + +def remove_all_installed(): + # Remove installed apt directories + remove_file_by_pattern('/etc/apt/sources.list.d/', + re.compile(r'^' + GZDEV_FILE_PREFIX + '(.*)\\.list')) + # Remove installed keys + remove_file_by_pattern('/usr/share/keyrings/', + re.compile(r'^' + GZDEV_FILE_PREFIX + '(.*)\\.gpg')) + + def main(): try: args = normalize_args(docopt(__doc__,