Skip to content

Commit

Permalink
Add return value for modified filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
player1537 committed Nov 17, 2016
1 parent 238da6c commit 4f529b0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
26 changes: 23 additions & 3 deletions django_remote_submission/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ def submit_job_to_server(job_pk, password, username=None, client=None,
)

sftp = client.open_sftp()
path = os.path.join(job.remote_directory, job.remote_filename)
sftp.putfo(six.StringIO(job.program), path)
sftp.chdir(job.remote_directory)
sftp.putfo(six.StringIO(job.program), job.remote_filename)

job.status = Job.STATUS.submitted
job.save()

stdin, stdout, stderr = client.exec_command(
command='python -u {}'.format(path),
command='cd {} && python -u {}'.format(
job.remote_directory,
job.remote_filename,
),
bufsize=1,
)

Expand Down Expand Up @@ -98,3 +101,20 @@ def submit_job_to_server(job_pk, password, username=None, client=None,
job.status = Job.STATUS.failure

job.save()

file_attrs = sftp.listdir_attr()
file_map = { attr.filename: attr for attr in file_attrs }
script_attr = file_map[job.remote_filename]
script_mtime = script_attr.st_mtime

modified = []
for attr in file_attrs:
if attr is script_attr:
continue

if attr.st_mtime < script_mtime:
continue

modified.append(attr.filename)

return modified
38 changes: 38 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,41 @@ def test_program_log_none(self):

job = Job.objects.get(pk=job.pk)
self.assertEqual(job.status, Job.STATUS.success)


def test_program_modified_files(self):
user = get_user_model().objects.get_or_create(
username=self.remote_user,
)[0]

server = Server.objects.create(
title='1-server-title',
hostname=self.server_hostname,
port=self.server_port,
)

program = '''
import time
for i in range(5):
with open('{}.txt'.format(i), 'w') as f:
f.write('foo')
time.sleep(0.1)
'''

job = Job.objects.create(
title='1-job-title',
program=textwrap.dedent(program),
remote_directory=self.remote_directory,
remote_filename=self.remote_filename,
owner=user,
server=server,
)

modified = submit_job_to_server(job.pk, self.remote_password)

self.assertEqual(len(modified), 5)
expected = ['0.txt', '1.txt', '2.txt', '3.txt', '4.txt']
self.assertEqual(modified, expected)

job = Job.objects.get(pk=job.pk)
self.assertEqual(job.status, Job.STATUS.success)

0 comments on commit 4f529b0

Please sign in to comment.