diff options
Diffstat (limited to 'kpov_judge/tasks/copy_rename_20_files_tail_env')
-rw-r--r-- | kpov_judge/tasks/copy_rename_20_files_tail_env/task.py | 129 |
1 files changed, 53 insertions, 76 deletions
diff --git a/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py b/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py index 7e3c3c8..d817083 100644 --- a/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py +++ b/kpov_judge/tasks/copy_rename_20_files_tail_env/task.py @@ -116,9 +116,37 @@ params_meta = { } def task(IP_malishell, file_rename_dirname, mv_src_dir, mv_dst_dir, cowsay_string, curl_fname, wc_dirname): - from pexpect import pxssh + import collections import os - # TODO: (polz) this has to be changed! Get a move on! + from pexpect import pxssh + + commands = [ + ('preimenuj', '/bin/ls -a1 {}'.format(file_rename_dirname)), + ('pre_mv_src', '/bin/ls -a1 {}'.format(mv_src_dir)), + ('pre_mv_dst', '/bin/ls -a1 {}'.format(mv_dst_dir)), + ('mv_ls_size', '/bin/ls -l ~/mv_ukaz'), + (None, '. ~/mv_ukaz'), + ('post_mv_src', '/bin/ls -a1 {}'.format(mv_src_dir)), + ('post_mv_dst', '/bin/ls -a1 {}'.format(mv_dst_dir)), + + # mama_ukaz + (None, 'cd /home/student/grep_test_dir'), + (None, '. ~/mama_ukaz'), + ('grep_napake', 'cat ~/napake.txt'), + ('grep_mama', 'cat ~/mama.txt'), + + # sledenje syslog + (None, 'date'), + ('syslog_start_time', '/usr/local/bin/syslog_spammer &'), + ('syslog_result', '/home/student/syslog_ukaz.sh'), + ('syslog_end_time', 'date'), + + ('cowsay', 'cowsay "{}"'.format(cowsay_string)), + ('wc_origfile', 'cat {}/count.txt'.format(wc_dirname)), + ('wc_lines', 'cat {}/lines.txt'.format(wc_dirname)), + ] + + # TOD: (polz) this has to be changed! Get a move on! # # sv: Z primozem lavricem sva skusala nekaj narediti # Ker gen params ni narejen, sklepam da je "Mapa" na namizju, @@ -126,80 +154,31 @@ def task(IP_malishell, file_rename_dirname, mv_src_dir, mv_dst_dir, cowsay_strin # v mojimenik se nahaja mojimenikfile # mama2 vsebuje "mama" #Stirje subt-aski dodani.By Mihec. - results = dict() - conn = pxssh.pxssh() - conn.login(IP_malishell, 'student', 'vaje') - # preimenuj datoteke - conn.sendline('/bin/ls -a1 {}'.format(file_rename_dirname)) - conn.prompt() - results['preimenuj'] = conn.before - # premakni datoteke s cim krajsim ukazom - conn.sendline('/bin/ls -a1 {}'.format(mv_src_dir)) - conn.prompt() - results['pre_mv_src'] = conn.before - conn.sendline('/bin/ls -a1 {}'.format(mv_dst_dir)) - conn.prompt() - results['pre_mv_dst'] = conn.before - conn.sendline('/bin/ls -l ~/mv_ukaz') - conn.prompt() - results['mv_ls_size'] = conn.before - conn.sendline('. ~/mv_ukaz') - conn.prompt() - conn.sendline('/bin/ls -a1 {}'.format(mv_src_dir)) - conn.prompt() - results['post_mv_src'] = conn.before - conn.sendline('/bin/ls -a1 {}'.format(mv_dst_dir)) - conn.prompt() - results['post_mv_dst'] = conn.before - # mama_ukaz - # - conn.sendline('cd /home/student/grep_test_dir') - conn.prompt() - conn.sendline('. ~/mama_ukaz') - conn.prompt() - conn.sendline('cat ~/napake.txt') - conn.prompt() - results['grep_napake'] = conn.before - conn.sendline('cat ~/mama.txt') - conn.prompt() - results['grep_mama'] = conn.before - # sledenje syslog - conn.sendline('date') - conn.prompt() - conn.sendline('/usr/local/bin/syslog_spammer &') - conn.prompt() - results['syslog_start_time'] = conn.before - conn.sendline('/home/student/syslog_ukaz.sh') - conn.prompt() - results['syslog_result'] = conn.before - conn.sendline('date') - conn.prompt() - results['syslog_end_time'] = conn.before - conn.sendline('cowsay "{}"'.format(cowsay_string)) - conn.prompt() - results['cowsay'] = conn.before + results = collections.defaultdict(str) + try: + s = pxssh.pxssh(encoding='utf-8') + s.login(IP_malishell, 'student', 'vaje') + results['ssh'] = True + for test, command in commands: + s.sendline(command) + s.prompt() + if test: + results[test] = s.before + s.logout() + except Exception as ex: + results['ssh'] = str(ex) + try: results['curl_env'] = os.environ['images'] except: results['curl_env'] = '' - conn.sendline('cat {}/count.txt'.format(wc_dirname)) - conn.prompt() - results['wc_origfile'] = conn.before - conn.sendline('cat {}/lines.txt'.format(wc_dirname)) - conn.prompt() - results['wc_lines'] = conn.before - conn.logout() - #results['curl'] = subprocess.check_output(["cat","/home/student/Desktop/website.txt"]) + #results['curl'] = subprocess.check_output(["cat","/home/student/Desktop/website.txt"]) #results['chkimages'] = subprocess.check_output(["curl www.24ur.com >> dlg.txt && cat dlg.txt | grep -c ","images"]) - #results['count'] = subprocess.check_output(["wc","/home/student/Desktop/count.txt"]) - #results['lines'] = subprocess.check_output(["cat","/home/student/Desktop/lines.txt"]) - #results['cowsay'] = subprocess.check_output(["dpkg --get-selections | grep","cowsay"]) - #results['phttp'] = subprocess.check_output(["lsof -i ",":8080"]) return results def gen_params(user_id, params_meta): @@ -218,7 +197,6 @@ def gen_params(user_id, params_meta): params['curl_n_images'] = str(r.randint(30,100)) params['wc_dirname'] = homedir + "".join([r.choice("rstuvxz") for i in range(8)]) params['wc_n_lines'] = str(r.randint(200, 600)) - #params['phttp'] = "8080" #params['images'] = subprocess.check_output(["echo","$images"]) return params @@ -232,22 +210,20 @@ def task_check(results, params): for i in range(20): fnames.append("".join([ r.choice("_abcdefghijk") for j in range(8)])) #TO FINISH SCORING WE REQUIRE DICT KEYS AND FUNCTIONS gen_params AND task TO BE FINISHED - #POINTS FOR EACH TASK MAY BE ADJUSTED IN THE FUTURE - #TASK 1 - # print( results['preimenuj']) + + # preimenuj 1 lines = results['preimenuj'].split("\n") task1_ok = len(lines) == len(fnames) + 4 task1_ok = task1_ok and lines[0].find(params['file_rename_dirname']) >= 0 task1_ok = task1_ok and lines[1].strip() == "." task1_ok = task1_ok and lines[2].strip() == ".." - # print task1_ok for fname in fnames: - # print task1_ok, fname, results['preimenuj'].find(fname) task1_ok = task1_ok and results['preimenuj'].find(fname) > 0 if task1_ok: score += 2 else: hints += ["wrong dash rename"] + # premakni datoteke s cim krajsim ukazom fnames = [] try: @@ -264,6 +240,8 @@ def task_check(results, params): score += 1 else: hints += ["wrong mv_command size or owner"] + + # rename files pre_src = set([i.strip() for i in results['pre_mv_src'].split('\n')[3:-1]]) pre_dst = set([i.strip() for i in results['pre_mv_dst'].split('\n')[3:-1]]) task2_ok = task2_ok and pre_src == set(fnames) @@ -276,8 +254,8 @@ def task_check(results, params): score += 1 else: hints += ["wrong rename files"] + # mama_ukaz - # task3_ok = True mama_fnames = [] for i in range(20): @@ -291,8 +269,6 @@ def task_check(results, params): linknames = [] for i in range(20): linknames.append("".join([ r.choice("123456789abcdefghijk") for j in range(8)])) - #print( results['grep_mama']) - #print( results['grep_napake']) for i in dirnames + linknames + papa_fnames: if (i not in papa_fnames) and results['grep_napake'].find(i) < 0: task3_ok = False @@ -310,6 +286,7 @@ def task_check(results, params): score += 2 else: hints += ["mama cries"] + # sledenje syslog #print( results['syslog_start_time']) #print( results['syslog_result']) @@ -321,7 +298,7 @@ def task_check(results, params): score += 1 else: hints += ["wrong syslog count"] - # + task5_ok = True try: assert int(results['curl_env'].strip()) == int(params['curl_n_images']) |