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

Commit 87a90c1

Browse files
kossakdandruszak
authored andcommitted
list artifacts
1 parent 541c71b commit 87a90c1

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

paperspace/cli/jobs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,15 @@ def get_artifacts(job_id, api_key=None):
128128
jobs_api = client.API(config.CONFIG_HOST, api_key=api_key)
129129
command = jobs_commands.ArtifactsGetCommand(api=jobs_api)
130130
command.execute(job_id)
131+
132+
133+
@artifacts.command("list", help="List job's artifacts")
134+
@click.argument("job_id")
135+
@click.option("--size", "-s", "size", help="Show file size", is_flag=True)
136+
@click.option("--links", "-l", "links", help="Show file URL", is_flag=True)
137+
@click.option("--files", "files", help="Get only given file (use at the end * as a wildcard)")
138+
@api_key_option
139+
def list_artifacts(job_id, size, links, files, api_key=None):
140+
jobs_api = client.API(config.CONFIG_HOST, api_key=api_key)
141+
command = jobs_commands.ArtifactsListCommand(api=jobs_api)
142+
command.execute(job_id=job_id, size=size, links=links, files=files)

paperspace/commands/jobs.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
from click import style
55

66
from paperspace import config, client
7-
from paperspace.commands import CommandBase
7+
from paperspace.commands import common
88
from paperspace.exceptions import BadResponseError
99
from paperspace.utils import get_terminal_lines
1010
from paperspace.workspace import S3WorkspaceHandler
11-
from paperspace.commands import common
1211

1312

1413
class JobsCommandBase(common.CommandBase):
@@ -171,3 +170,47 @@ def _log_artifacts(self, response):
171170
'{}: {}'.format(artifacts_json['error']['status'], artifacts_json['error']['message']))
172171
except (ValueError, KeyError, BadResponseError) as e:
173172
self.logger.error("Error occurred while getting artifacts: {}".format(str(e)))
173+
174+
175+
class ArtifactsListCommand(common.ListCommand):
176+
kwargs = {}
177+
178+
def execute(self, **kwargs):
179+
self.kwargs = kwargs
180+
return super(ArtifactsListCommand, self).execute(**kwargs)
181+
182+
@property
183+
def request_url(self):
184+
return '/jobs/artifactsList'
185+
186+
def _get_request_params(self, kwargs):
187+
params = {'jobId': kwargs['job_id']}
188+
189+
files = kwargs.get('files')
190+
if files:
191+
params['files'] = files
192+
size = kwargs.get('size', False)
193+
if size:
194+
params['size'] = size
195+
links = kwargs.get('links', False)
196+
if links:
197+
params['links'] = links
198+
199+
return params
200+
201+
def _get_table_data(self, artifacts):
202+
columns = ['Files']
203+
if self.kwargs.get('size'):
204+
columns.append('Size (in bytes)')
205+
if self.kwargs.get('links'):
206+
columns.append('URL')
207+
208+
data = [tuple(columns)]
209+
for artifact in artifacts:
210+
row = [artifact.get('file')]
211+
if 'size' in artifact.keys():
212+
row.append(artifact['size'])
213+
if 'url' in artifact.keys():
214+
row.append(artifact['url'])
215+
data.append(tuple(row))
216+
return data

tests/functional/test_jobs.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mock
2+
import pytest
23
from click.testing import CliRunner
34

45
from paperspace.cli import cli
@@ -267,7 +268,8 @@ def test_should_send_valid_post_request_when_destroying_artifacts_with_files_spe
267268
post_patched.return_value = MockResponse(status_code=200)
268269
job_id = "some_job_id"
269270
file_names = "some_file_names"
270-
result = self.runner.invoke(cli.cli, ["jobs", "artifacts", "destroy", job_id, "--files", file_names, "--apiKey", "some_key"])
271+
result = self.runner.invoke(cli.cli, ["jobs", "artifacts", "destroy", job_id, "--files", file_names, "--apiKey",
272+
"some_key"])
271273

272274
post_patched.assert_called_with("{}/jobs/{}/artifactsDestroy".format(self.URL, job_id),
273275
files=None,
@@ -300,3 +302,41 @@ def test_should_send_send_valid_get_request_and_receive_json_response(self, get_
300302
json=None,
301303
params={"jobId": job_id})
302304
assert result.exit_code == 0
305+
306+
@mock.patch("paperspace.client.requests.get")
307+
def test_should_send_valid_get_request_with_all_parameters_for_a_list_of_artifacts(self, get_patched):
308+
get_patched.return_value = MockResponse(status_code=200)
309+
job_id = "some_job_id"
310+
result = self.runner.invoke(cli.cli,
311+
["jobs", "artifacts", "list", job_id, "--apiKey", "some_key", "--size", "--links",
312+
"--files", "foo"])
313+
314+
get_patched.assert_called_with("{}/jobs/artifactsList".format(self.URL),
315+
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
316+
json=None,
317+
params={"jobId": job_id,
318+
"size": True,
319+
"links": True,
320+
"files": "foo"})
321+
assert result.exit_code == 0
322+
323+
@mock.patch("paperspace.client.requests.get")
324+
@pytest.mark.parametrize('option,param', [("--size", "size"),
325+
("-s", "size"),
326+
("--links", "links"),
327+
("-l", "links")])
328+
def test_should_send_valid_get_request_with_valid_param_for_a_list_of_artifacts_for_both_formats_of_param(self,
329+
get_patched,
330+
option,
331+
param):
332+
get_patched.return_value = MockResponse(status_code=200)
333+
job_id = "some_job_id"
334+
result = self.runner.invoke(cli.cli,
335+
["jobs", "artifacts", "list", job_id, "--apiKey", "some_key"] + [option])
336+
337+
get_patched.assert_called_with("{}/jobs/artifactsList".format(self.URL),
338+
headers=self.EXPECTED_HEADERS_WITH_CHANGED_API_KEY,
339+
json=None,
340+
params={"jobId": job_id,
341+
param: True})
342+
assert result.exit_code == 0

0 commit comments

Comments
 (0)