Skip to content

Commit 2769a70

Browse files
committed
Minor code updates, moved JSON loads to Result constructor
1 parent 46960a9 commit 2769a70

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

arca/_arca.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ class Arca:
3939

4040
def __init__(self, backend: BackendDefinitionType=NOT_SET,
4141
settings=None,
42-
single_pull=None,
43-
base_dir=None,
44-
ignore_cache_errors=None) -> None:
42+
single_pull=NOT_SET,
43+
base_dir=NOT_SET,
44+
ignore_cache_errors=NOT_SET) -> None:
4545
self.settings: Settings = Settings(settings)
4646

47-
if ignore_cache_errors is not None:
47+
if ignore_cache_errors is not NOT_SET:
4848
self.ignore_cache_errors = bool(ignore_cache_errors)
4949

50-
if single_pull is not None:
50+
if single_pull is not NOT_SET:
5151
self.single_pull = bool(single_pull)
5252

53-
if base_dir is not None:
53+
if base_dir is not NOT_SET:
5454
self.base_dir = base_dir
5555

5656
self.region: CacheRegion = self.make_region()
@@ -378,9 +378,7 @@ def run(self, repo: str, branch: str, task: Task, *,
378378

379379
logger.info("Running Arca task %r for repo '%s' in branch '%s'", task, repo, branch)
380380

381-
git_repo, repo_path = self.get_files(repo, branch,
382-
depth=depth,
383-
reference=reference)
381+
git_repo, repo_path = self.get_files(repo, branch, depth=depth, reference=reference)
384382

385383
def create_value():
386384
logger.debug("Value not in cache, creating.")
@@ -428,9 +426,7 @@ def static_filename(self, repo: str, branch: str, relative_path: Union[str, Path
428426
if not isinstance(relative_path, Path):
429427
relative_path = Path(relative_path)
430428

431-
_, repo_path = self.get_files(repo, branch,
432-
depth=depth,
433-
reference=reference)
429+
_, repo_path = self.get_files(repo, branch, depth=depth, reference=reference)
434430

435431
result = repo_path / relative_path
436432
result = result.resolve()

arca/_runner.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" This file is the code which actually launches the tasks, the serialized jsons.
1+
""" This file is the code which actually launches the tasks, the serialized JSONs.
22
"""
33
import json
44
import traceback
@@ -46,7 +46,6 @@ def run(filename):
4646
# KeyError: keys can be missing from the parsed json
4747
# TypeError: task_definition["entry_point"] is a list
4848
# AttributeError: task_definition["entry_point"] isn't a dict
49-
5049
return {"success": False, "reason": "corrupted_definition", "error": traceback.format_exc()}
5150

5251
try:

arca/backend/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import hashlib
2-
import json
32
import re
43
import subprocess
54
from pathlib import Path
@@ -168,7 +167,7 @@ def run(self, repo: str, branch: str, task: Task, git_repo: Repo, repo_path: Pat
168167
logger.debug("stdout output from the command")
169168
logger.debug(out_output)
170169

171-
return Result(json.loads(out_output))
170+
return Result(out_output)
172171
except BuildError: # can be raised by :meth:`Result.__init__`
173172
raise
174173
except Exception as e:

arca/backend/docker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ def run(self, repo: str, branch: str, task: Task, git_repo: Repo, repo_path: Pat
747747
f"/srv/scripts/{task_filename}"],
748748
tty=True)
749749

750-
return Result(json.loads(res.output))
750+
return Result(res.output)
751751
except BuildError: # can be raised by :meth:`Result.__init__`
752752
raise
753753
except Exception as e:

arca/backend/vagrant.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import re
32
import subprocess
43
from pathlib import Path
@@ -69,7 +68,7 @@ def validate_settings(self):
6968
* ``provider`` format
7069
* ``use_registry_name`` is set
7170
"""
72-
super(VagrantBackend, self).validate_settings()
71+
super().validate_settings()
7372

7473
if self.use_registry_name is None:
7574
raise ArcaMisconfigured("Push to registry setting is required for VagrantBackend")
@@ -213,7 +212,7 @@ def run(self, repo: str, branch: str, task: Task, git_repo: Repo, repo_path: Pat
213212
try:
214213
res = api.execute(self.fabric_task, container_name=container_name, definition_filename=task_filename)
215214

216-
return Result(json.loads(res[vagrant.user_hostname_port()].stdout))
215+
return Result(res[vagrant.user_hostname_port()].stdout)
217216
except BuildError: # can be raised by :meth:`Result.__init__`
218217
raise
219218
except Exception as e:

arca/result.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Dict, Any
1+
import json
2+
from typing import Dict, Any, Union
23

34
from arca.exceptions import BuildError
45

@@ -7,7 +8,14 @@ class Result:
78
""" For storing results of the tasks. So far only has one attribute, :attr:`output`.
89
"""
910

10-
def __init__(self, result: Dict[str, Any]) -> None:
11+
def __init__(self, result: Union[str, Dict[str, Any]]) -> None:
12+
if isinstance(result, (str, bytes, bytearray)):
13+
try:
14+
result = json.loads(result)
15+
except ValueError:
16+
raise BuildError("The build failed (the output was corrupted, "
17+
"possibly by the callable printing something)")
18+
1119
if not isinstance(result, dict):
1220
raise BuildError("The build failed (the value returned from the runner was not valid)")
1321

0 commit comments

Comments
 (0)