Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --output-dir flag in download command #98

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
victormlg marked this conversation as resolved.
Show resolved Hide resolved
if output_dir :
victormlg marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -319,7 +325,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
12 changes: 12 additions & 0 deletions cf_remote/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hashlib
import os
import shutil
import sys
import re
import json
Expand Down Expand Up @@ -204,6 +205,17 @@ 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)


def is_different_checksum(checksum, content) :
assert type(content) == bytes
Expand Down
Loading