Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 777021a

Browse files
committed
getting artifacts, some rearangement
1 parent c4cede0 commit 777021a

File tree

5 files changed

+110
-9
lines changed

5 files changed

+110
-9
lines changed

paperspace/cli/jobs.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,25 @@ def list_logs(job_id, api_key=None):
106106
command.execute(job_id)
107107

108108

109-
@jobs_group.command("artifactsDestroy", help="Destroy job's artifacts")
109+
@jobs_group.group("artifacts", help="Manage jobs' artifacts", cls=ClickGroup)
110+
def artifacts():
111+
pass
112+
113+
114+
@artifacts.command("destroy", help="Destroy job's artifacts")
110115
@click.argument("job_id")
111116
@click.option("--files", "files")
112117
@api_key_option
113118
def destroy_artifacts(job_id, api_key=None, files=None):
114119
jobs_api = client.API(config.CONFIG_HOST, api_key=api_key)
115120
command = jobs_commands.ArtifactsDestroyCommand(api=jobs_api)
116121
command.execute(job_id, files=files)
122+
123+
124+
@artifacts.command("get", help="Get job's artifacts")
125+
@click.argument("job_id")
126+
@api_key_option
127+
def get_artifacts(job_id, api_key=None):
128+
jobs_api = client.API(config.CONFIG_HOST, api_key=api_key)
129+
command = jobs_commands.ArtifactsGetCommand(api=jobs_api)
130+
command.execute(job_id)

paperspace/commands/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
from collections import OrderedDict
2+
13
from paperspace import logger
24

35

46
class CommandBase(object):
57
def __init__(self, api=None, logger_=logger):
68
self.api = api
79
self.logger = logger_
10+
11+
def _print_dict_recursive(self, input_dict, indent=0, tabulator=" "):
12+
for key, val in input_dict.items():
13+
self.logger.log("%s%s:" % (tabulator * indent, key))
14+
if type(val) is dict:
15+
self._print_dict_recursive(OrderedDict(val), indent + 1)
16+
else:
17+
self.logger.log("%s%s" % (tabulator * (indent + 1), val))

paperspace/commands/jobs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from paperspace import config, client
77
from paperspace.commands import CommandBase
8+
from paperspace.exceptions import BadResponseError
89
from paperspace.utils import get_terminal_lines
910
from paperspace.workspace import S3WorkspaceHandler
1011

@@ -168,3 +169,22 @@ def execute(self, job_id, files=None):
168169
params = {'files': files}
169170
response = self.api.post(url, params=params)
170171
self._log_message(response, "Artifacts destroyed", "Unknown error while destroying artifacts")
172+
173+
174+
class ArtifactsGetCommand(JobsCommandBase):
175+
def execute(self, job_id):
176+
url = '/jobs/artifactsGet'
177+
response = self.api.get(url, params={'jobId': job_id})
178+
179+
self._log_artifacts(response)
180+
181+
def _log_artifacts(self, response):
182+
try:
183+
artifacts_json = response.json()
184+
if response.ok:
185+
self._print_dict_recursive(artifacts_json)
186+
else:
187+
raise BadResponseError(
188+
'{}: {}'.format(artifacts_json['error']['status'], artifacts_json['error']['message']))
189+
except (ValueError, KeyError, BadResponseError) as e:
190+
self.logger.error("Error occurred while getting artifacts: {}".format(str(e)))

tests/functional/test_jobs.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,26 +265,38 @@ class TestJobArtifactsCommands(TestJobs):
265265
@mock.patch("paperspace.client.requests.post")
266266
def test_should_send_valid_post_request_when_destroying_artifacts_with_files_specified(self, post_patched):
267267
post_patched.return_value = MockResponse(status_code=200)
268-
job_id = 'some_job_id'
269-
file_names = 'some_file_names'
270-
result = self.runner.invoke(cli.cli, ['jobs', 'artifactsDestroy', job_id, '--files', file_names, "--apiKey", "some_key"])
268+
job_id = "some_job_id"
269+
file_names = "some_file_names"
270+
result = self.runner.invoke(cli.cli, ["jobs", "artifacts", "destroy", job_id, "--files", file_names, "--apiKey", "some_key"])
271271

272-
post_patched.assert_called_with('{}/jobs/{}/artifactsDestroy'.format(self.URL, job_id),
272+
post_patched.assert_called_with("{}/jobs/{}/artifactsDestroy".format(self.URL, job_id),
273273
files=None,
274274
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
275275
json=None,
276-
params={'files': file_names})
276+
params={"files": file_names})
277277
assert result.exit_code == 0
278278

279279
@mock.patch("paperspace.client.requests.post")
280280
def test_should_send_valid_post_request_when_destroying_artifacts_without_files_specified(self, post_patched):
281281
post_patched.return_value = MockResponse(status_code=200)
282-
job_id = 'some_job_id'
283-
result = self.runner.invoke(cli.cli, ['jobs', 'artifactsDestroy', job_id, "--apiKey", "some_key"])
282+
job_id = "some_job_id"
283+
result = self.runner.invoke(cli.cli, ["jobs", "artifacts", "destroy", job_id, "--apiKey", "some_key"])
284284

285-
post_patched.assert_called_with('{}/jobs/{}/artifactsDestroy'.format(self.URL, job_id),
285+
post_patched.assert_called_with("{}/jobs/{}/artifactsDestroy".format(self.URL, job_id),
286286
files=None,
287287
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
288288
json=None,
289289
params=None)
290290
assert result.exit_code == 0
291+
292+
@mock.patch("paperspace.client.requests.get")
293+
def test_should_send_send_valid_get_request_and_receive_json_response(self, get_patched):
294+
get_patched.return_value = MockResponse(status_code=200)
295+
job_id = "some_job_id"
296+
result = self.runner.invoke(cli.cli, ["jobs", "artifacts", "get", job_id, "--apiKey", "some_key"])
297+
298+
get_patched.assert_called_with("{}/jobs/artifactsGet".format(self.URL),
299+
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
300+
json=None,
301+
params={"jobId": job_id})
302+
assert result.exit_code == 0

tests/unit/test_base.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from collections import OrderedDict
2+
3+
import mock
4+
import pytest
5+
6+
from paperspace.commands import CommandBase
7+
8+
output_response = ""
9+
10+
11+
class TestBaseClass(object):
12+
def test_json_print(self):
13+
global output_response
14+
output_response = ""
15+
16+
def log_to_var(message):
17+
global output_response
18+
output_response = "{}{}\n".format(output_response, message)
19+
20+
logger_ = mock.MagicMock()
21+
logger_.log = log_to_var
22+
23+
input_dict = {
24+
"foo": "bar",
25+
"baz": {
26+
"foo2": "bar2",
27+
'baz2': {
28+
"foo3": "bar3"
29+
}
30+
}
31+
}
32+
expected_string = """foo:
33+
bar
34+
baz:
35+
baz2:
36+
foo3:
37+
bar3
38+
foo2:
39+
bar2
40+
"""
41+
42+
command = CommandBase(logger_=logger_)
43+
command._print_dict_recursive(OrderedDict(input_dict))
44+
45+
assert output_response == expected_string

0 commit comments

Comments
 (0)