Skip to content

Commit

Permalink
Added --output-dir flag in download command
Browse files Browse the repository at this point in the history
Ticket: CFE-3684
Signed-off-by: Victor Moene <[email protected]>
  • Loading branch information
victormlg committed Dec 19, 2024
1 parent a5c1588 commit 62eb52a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
20 changes: 16 additions & 4 deletions cf_remote/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
cf_remote_packages_dir,
)
from cf_remote.utils import (
copy_file,
mkdir,
save_file,
strip_user,
read_json,
Expand Down Expand Up @@ -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))

Expand All @@ -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
Expand All @@ -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():
Expand Down
8 changes: 7 additions & 1 deletion cf_remote/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cf_remote/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions cf_remote/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
import sys
import re
import json
Expand Down Expand Up @@ -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)

0 comments on commit 62eb52a

Please sign in to comment.