Skip to content

Commit

Permalink
Merge pull request #33 from kosma/handle-yaml-os-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
haboustak committed Sep 30, 2023
2 parents 50fa457 + cbd0524 commit ed0bd37
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
18 changes: 13 additions & 5 deletions art/_yaml.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
# -*- coding: utf-8 -*-

import click
import yaml


def load(path):
with open(path, 'r') as stream:
return yaml.safe_load(stream=stream)

try:
with open(path, 'r') as stream:
return yaml.safe_load(stream=stream)
except FileNotFoundError:
return None
except OSError as exc:
raise click.ClickException('Failed to read file: %s' % exc)

def save(path, obj):
with open(path, 'w') as stream:
yaml.safe_dump(obj, stream=stream, default_flow_style=False)
try:
with open(path, 'w') as stream:
yaml.safe_dump(obj, stream=stream, default_flow_style=False)
except OSError as exc:
raise click.ClickException('Failed to write file: %s' % exc)
7 changes: 7 additions & 0 deletions art/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def update():
raise _config.ConfigException('token_type', 'A job token cannot be used to update artifacts')

artifacts = _yaml.load(_paths.artifacts_file)
if not artifacts:
raise click.ClickException('The %s file was not found or did not contain any entries' % _paths.artifacts_file)

for entry in artifacts:
fail_msg = 'Failed to get last successful "%s" job for "%s" ref "%s"' % (
entry['job'],
Expand All @@ -176,6 +179,8 @@ def download():

gitlab = get_gitlab()
artifacts_lock = _yaml.load(_paths.artifacts_lock_file)
if not artifacts_lock:
raise click.ClickException('No entries in %s file. Run "art update" first.' % _paths.artifacts_lock_file)

for entry in artifacts_lock:
filename = zip_name(entry)
Expand All @@ -192,6 +197,8 @@ def install(keep_empty_dirs):

gitlab = get_gitlab()
artifacts_lock = _yaml.load(_paths.artifacts_lock_file)
if not artifacts_lock:
raise click.ClickException('No entries in %s file. Run "art update" first.' % _paths.artifacts_lock_file)

for entry in artifacts_lock:
# dictionary of src:dest pairs representing artifacts to install
Expand Down

0 comments on commit ed0bd37

Please sign in to comment.