From e33085889f26c76587fdab2ad600ed336dc028b5 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sat, 6 Oct 2018 23:35:00 +0200 Subject: Improve ssh error reporting in some tasks --- kpov_judge/tasks/copy_rename_100_files/task.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'kpov_judge/tasks/copy_rename_100_files') diff --git a/kpov_judge/tasks/copy_rename_100_files/task.py b/kpov_judge/tasks/copy_rename_100_files/task.py index 9ad4d66..c658d27 100644 --- a/kpov_judge/tasks/copy_rename_100_files/task.py +++ b/kpov_judge/tasks/copy_rename_100_files/task.py @@ -73,16 +73,19 @@ params_meta = { } def task(host, folder): + import collections from pexpect import pxssh - - # ideally, this would be done using a SFTP client instead of pxssh - target = pxssh.pxssh() - target.login(host, 'test', 'test') - results = { - 'files': target.run('ls -1').split('\n'), # XXX: file may have newlines - 'contents': target.run("cat *"), # XXX: may include other files in $HOME - } - target.logout() + results = collections.defaultdict(str) + try: + # ideally, this would be done using a SFTP client instead of pxssh + s = pxssh.pxssh(encoding='utf-8') + s.login(host, 'test', 'test') + results['ssh'] = True + results['files'] = s.run('ls -1').split('\n'), # XXX: file may have newlines + results['contents'] = s.run('cat *'), # XXX: may include other files in $HOME + s.logout() + except Exception as ex: + results['ssh'] = str(ex) return results def gen_params(user_id, params_meta): -- cgit v1.2.1 From b5b35faea7f4205f353f57178ddc795b7dce5043 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:02:26 +0200 Subject: Refactor SSH testing code out of individual tasks Catch SSH errors and report failures as hints. Also some cleanups and 2to3 fixes. --- kpov_judge/tasks/copy_rename_100_files/task.py | 29 ++++++++++---------------- 1 file changed, 11 insertions(+), 18 deletions(-) (limited to 'kpov_judge/tasks/copy_rename_100_files') diff --git a/kpov_judge/tasks/copy_rename_100_files/task.py b/kpov_judge/tasks/copy_rename_100_files/task.py index c658d27..f7fb3f1 100644 --- a/kpov_judge/tasks/copy_rename_100_files/task.py +++ b/kpov_judge/tasks/copy_rename_100_files/task.py @@ -73,20 +73,10 @@ params_meta = { } def task(host, folder): - import collections - from pexpect import pxssh - results = collections.defaultdict(str) - try: - # ideally, this would be done using a SFTP client instead of pxssh - s = pxssh.pxssh(encoding='utf-8') - s.login(host, 'test', 'test') - results['ssh'] = True - results['files'] = s.run('ls -1').split('\n'), # XXX: file may have newlines - results['contents'] = s.run('cat *'), # XXX: may include other files in $HOME - s.logout() - except Exception as ex: - results['ssh'] = str(ex) - return results + return kpov_util.ssh_test(host, 'test', 'test', ( + ('files', 'ls -1'), # XXX: file may have newlines + ('contents', 'cat *'), # XXX: may include other files in $HOME + )) def gen_params(user_id, params_meta): pass @@ -96,10 +86,13 @@ def task_check(results, params): score = 0 hints = [] + if results['ssh'] is not True: + hints += ['ssh failed: ' + results['ssh']] + matched = 0 files = os.listdir(params['folder']) for fn in files: - if fn in results['files']: + if fn in results['files'].splitlines(): matched += 1 if matched > 0: score = 1 @@ -113,12 +106,12 @@ def task_check(results, params): score += 3 else: hints += ["wrong number of files"] - rl = results['contents'].split('\n') + rl = results['contents'].splitlines() rl.sort() tl = [] for fn in files: - with open(fn) as f: - tl += f.read().upper().split('\n') + with open(os.path.join(params['folder'], fn)) as f: + tl += f.read().upper().splitlines() tl.sort() if rl == tl: score += 4 -- cgit v1.2.1 From 3091fc2877600f7926ed6a596c316183499125f8 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:11:49 +0200 Subject: Remove coding: lines Python 3 source is utf-8 by default. --- kpov_judge/tasks/copy_rename_100_files/task.py | 1 - 1 file changed, 1 deletion(-) (limited to 'kpov_judge/tasks/copy_rename_100_files') diff --git a/kpov_judge/tasks/copy_rename_100_files/task.py b/kpov_judge/tasks/copy_rename_100_files/task.py index f7fb3f1..92c8570 100644 --- a/kpov_judge/tasks/copy_rename_100_files/task.py +++ b/kpov_judge/tasks/copy_rename_100_files/task.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # # TODO: # - check if everything is filled in (computers, params, preparation) -- cgit v1.2.1 From dbf585c2a59dbcb7f6d803d5756826b562cd7f43 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 7 Oct 2018 19:38:40 +0200 Subject: Fix or remove hashbangs --- kpov_judge/tasks/copy_rename_100_files/task.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'kpov_judge/tasks/copy_rename_100_files') diff --git a/kpov_judge/tasks/copy_rename_100_files/task.py b/kpov_judge/tasks/copy_rename_100_files/task.py index 92c8570..051f9f7 100644 --- a/kpov_judge/tasks/copy_rename_100_files/task.py +++ b/kpov_judge/tasks/copy_rename_100_files/task.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python -# # TODO: # - check if everything is filled in (computers, params, preparation) # - improve scoring -- cgit v1.2.1