Skip to content

Commit

Permalink
optimize some code and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shokakucarrier committed Mar 6, 2023
1 parent e6466c4 commit 62face4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
7 changes: 5 additions & 2 deletions sidecarinit/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@

logger = logging.getLogger(__name__)


# Decorator function triggers when script runs to define the CLI input options.
# Sets default value for env_yml, an optional argument with a help prompt that is used when it's provided by the user.
@click.command()
@click.argument('env_yml', required=False, default='/opt/indy-sidecar/config/application.yaml')
# help='Target environment, Same as sidecar application.yml'
def run(env_yml):

# Calling read_config() function from the config module to parse a YAML file and load configuration into a named tuple suite.
suite = config.read_config(env_yml)

# Assigning the archive_name variable with the build.config.id environment variable if exists.
archive_name = os.environ.get('build.config.id')

# Downloading from suite.archive_api to suite.local_repository.
logger.info('Downloading from : %s to %s .', suite.archive_api, suite.local_repository)
Path(suite.local_repository).mkdir(parents=True, exist_ok=True)

Expand Down
20 changes: 10 additions & 10 deletions sidecarinit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
ENV_LOCAL_REPOSITORY = 'local-repository'

DEFAULT_PROXY_PORT = 8081
DEFAULT_LOCAL_REPOSITORY = os.environ.get('HOME')+'/preSeedRepo'
DEFAULT_LOCAL_REPOSITORY = os.path.expanduser('~/preSeedRepo')


class Environment:
def __init__(self, env_spec):
self.archive_api = env_spec.get('sidecar').get(ENV_ARCHIVE_API)
self.local_repository = env_spec.get('sidecar').get(ENV_LOCAL_REPOSITORY).replace('${user.home}', os.environ.get('HOME')) or DEFAULT_LOCAL_REPOSITORY
def __init__(self, env_config_dict):
self.archive_api = env_config_dict.get('sidecar').get(ENV_ARCHIVE_API)
self.local_repository = env_config_dict.get('sidecar').get(ENV_LOCAL_REPOSITORY).replace('${user.home}', os.path.expanduser('~')) or DEFAULT_LOCAL_REPOSITORY


def read_config(env_yml):
Expand All @@ -26,16 +26,16 @@ def read_config(env_yml):
"""
errors = []

env_spec = {}
env_config_dict = {}
if env_yml is None:
errors.append(f"Missing test environment config file")
elif os.path.exists(env_yml):
elif not os.path.exists(env_yml):
errors.append(f"Missing test environment config file")
else:
with open(env_yml) as f:
yaml = YAML(typ='safe')
env_spec = yaml.load(f)
else:
errors.append(f"Missing test environment config file")
env_config_dict = yaml.load(f)

env = Environment(env_spec)
env = Environment(env_config_dict)

return env
14 changes: 7 additions & 7 deletions sidecarinit/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from zipfile import ZipFile

def download_archive(url, repository):
archive_name = os.environ.get('build.config.id')
archive_path = "/" + archive_name + '.zip'
build_id = os.environ.get('build.config.id')
archive_path = f"/{build_id}.zip"

with urllib.request.urlopen(url) as response, open(repository + archive_path, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
with urllib.request.urlopen(url) as response:
with open(repository + archive_path, 'wb') as out_file:
out_file.write(response.read())

archive_zip = ZipFile(repository + archive_path)
archive_zip.extractall(repository)
archive_zip.close()
with ZipFile(repository + archive_path) as archive_zip:
archive_zip.extractall(repository)

0 comments on commit 62face4

Please sign in to comment.