From 62eb52af71f5c42e80c236ef473e392961f34d03 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Wed, 11 Dec 2024 10:34:04 +0100 Subject: [PATCH] Added --output-dir flag in download command Ticket: CFE-3684 Signed-off-by: Victor Moene --- cf_remote/commands.py | 20 ++++++++++++++++---- cf_remote/main.py | 8 +++++++- cf_remote/paths.py | 1 + cf_remote/utils.py | 11 +++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/cf_remote/commands.py b/cf_remote/commands.py index 4bc2b83..b84eb85 100644 --- a/cf_remote/commands.py +++ b/cf_remote/commands.py @@ -22,6 +22,8 @@ cf_remote_packages_dir, ) from cf_remote.utils import ( + copy_file, + mkdir, save_file, strip_user, read_json, @@ -277,7 +279,7 @@ def install( return errors -def _iterate_over_packages(tags=None, version=None, edition=None, download=False): +def _iterate_over_packages(tags=None, version=None, edition=None, download=False, output_dir=None): releases = Releases(edition) print("Available releases: {}".format(releases)) @@ -296,7 +298,17 @@ def _iterate_over_packages(tags=None, version=None, edition=None, download=False else: for artifact in artifacts: if download: - download_package(artifact.url, checksum=artifact.checksum) + package_path= download_package(artifact.url, checksum=artifact.checksum) + if output_dir : + output_dir = os.path.normpath(output_dir) + parent = os.path.dirname(output_dir) + if not os.path.exists(parent): + user_error("'{}' doesn't exist. Make sure this path is correct and exists.".format(parent)) + mkdir(output_dir) + + filename = os.path.basename(artifact.url) + output_path = os.path.join(output_dir, filename) + copy_file(package_path, output_path) else: print(artifact.url) return 0 @@ -307,8 +319,8 @@ def list_command(tags=None, version=None, edition=None): return _iterate_over_packages(tags, version, edition, False) -def download(tags=None, version=None, edition=None): - return _iterate_over_packages(tags, version, edition, True) +def download(tags=None, version=None, edition=None, output_dir=None): + return _iterate_over_packages(tags, version, edition, True, output_dir) def _get_aws_creds_from_env(): diff --git a/cf_remote/main.py b/cf_remote/main.py index 28240de..c7171fb 100644 --- a/cf_remote/main.py +++ b/cf_remote/main.py @@ -138,6 +138,12 @@ def _get_arg_parser(): ) sp.add_argument("tags", metavar="TAG", nargs="*") + sp.add_argument("--output-dir", + "-o", + help="Where to download", + type=str + ) + sp = subp.add_parser( "run", help="Run the command given as arguments on the given hosts" ) @@ -311,7 +317,7 @@ def run_command_with_args(command, args): ) elif command == "download": return commands.download( - tags=args.tags, version=args.version, edition=args.edition + tags=args.tags, version=args.version, edition=args.edition, output_dir=args.output_dir ) elif command == "run": return commands.run(hosts=args.hosts, raw=args.raw, command=args.remote_command) diff --git a/cf_remote/paths.py b/cf_remote/paths.py index e80cd2b..e12efdb 100644 --- a/cf_remote/paths.py +++ b/cf_remote/paths.py @@ -11,6 +11,7 @@ def cfengine_dir(subdir=None): override_dir = os.getenv("CF_REMOTE_DIR") if override_dir: + override_dir = os.path.normpath(override_dir) parent = os.path.dirname(override_dir) if not os.path.exists(parent): diff --git a/cf_remote/utils.py b/cf_remote/utils.py index d0522a1..1729e4a 100644 --- a/cf_remote/utils.py +++ b/cf_remote/utils.py @@ -1,4 +1,5 @@ import os +import shutil import sys import re import json @@ -202,3 +203,13 @@ def whoami(): def print_progress_dot(*args): print(".", end="") sys.stdout.flush() # STDOUT is line-buffered + +def copy_file(input_path, output_path) : + filename = os.path.basename(input_path) + output_dir = os.path.dirname(output_path) + + tmp_filename = '.{}.tmp'.format(filename) + tmp_output_path = os.path.join(output_dir, tmp_filename) + + shutil.copyfile(input_path, tmp_output_path) + os.rename(tmp_output_path, output_path)