Skip to content

Commit

Permalink
Implement --pre-cleaup option
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Luis Rivero <[email protected]>
  • Loading branch information
j-rivero committed Apr 8, 2024
1 parent 982bf15 commit c826841
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
42 changes: 36 additions & 6 deletions plugins/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Usage:
gzdev repository (ACTION) [<repo-name>] [<repo-type>]
[--project=<project_name>] [--force-linux-distro=<distro>]
[--keyserver=<keyserver>] [--gpg-check]
[--keyserver=<keyserver>] [--gpg-check] [--pre-cleanup]
gzdev repository list
gzdev repository (-h | --help)
gzdev repository --version
Expand All @@ -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
Expand All @@ -37,6 +39,9 @@
from docopt import docopt


GZDEV_FILE_PREFIX = '_gzdev_'


def _check_call(cmd):
print('')
print("Invoking '%s'" % ' '.join(cmd))
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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__,
Expand Down

0 comments on commit c826841

Please sign in to comment.