diff options
author | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2018-10-07 20:21:31 +0200 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2018-10-07 20:21:31 +0200 |
commit | 80f050080ce11c6a0cce67c9b0281c09702b00d6 (patch) | |
tree | b486e1f6342bc757dda87df4856084f652895791 /kpov_judge/tasks/copy_rename_20_files_tail_env | |
parent | 43d7ef057b4ae752a60b947e523b3a56782bf5cd (diff) | |
parent | dbf585c2a59dbcb7f6d803d5756826b562cd7f43 (diff) |
Merge branch 'fix-tasks'
Fixes pexpect usage in most tasks. Several other cleanups and minor
refactoring.
Diffstat (limited to 'kpov_judge/tasks/copy_rename_20_files_tail_env')
-rw-r--r-- | kpov_judge/tasks/copy_rename_20_files_tail_env/solution/solution.py | 184 | ||||
-rw-r--r-- | kpov_judge/tasks/copy_rename_20_files_tail_env/task.py | 133 |
2 files changed, 137 insertions, 180 deletions
diff --git a/kpov_judge/tasks/copy_rename_20_files_tail_env/solution/solution.py b/kpov_judge/tasks/copy_rename_20_files_tail_env/solution/solution.py index 7a26b77..6036cd1 100644 --- a/kpov_judge/tasks/copy_rename_20_files_tail_env/solution/solution.py +++ b/kpov_judge/tasks/copy_rename_20_files_tail_env/solution/solution.py @@ -1,92 +1,92 @@ -# preimenuj vse datoteke tako, da zamenjaš minuse s podčrtaji
-def rename():
- import os
- for filename in os.listdir('.'):
- os.rename(filename, filename.replace('-', '_'))
-
-# Napiši čim krajši ukaz, ki vse datoteke iz /home/user/mapa/mojimenik premakne v /home/user/novi
-def mv_novi():
- import os
- for filename in os.listdir("/home/user/mapa/mojimenik/"):
- os.rename('/home/user/mapa/mojimenik/' + filename, '/home/user/novi/' + filename)
-
-# Napiši ukaz, ki s pomočjo ukaza grep v datoteko "mama.txt" izpiše vse navade (ne skrite) datoteke v trenutnem imeniku, ki vsebujejo niz "mama", v datoteko "napake.txt" pa izpiše vse morebitne napake (npr. to, da so nekateri objekti v trenutnem imeniku dejansko imeniki ali napačne simbolične povezave)
-def mama():
- import os
- import re
- import mmap
- mama = ''
- wrong = ''
- for filename in os.listdir('.'):
- try:
- f = open(filename, 'r+')
- data = mmap.mmap(f.fileno(), 0)
- if re.search('mama', data):
- mama = mama + filename + "\n"
- except ValueError:
- wrong = wrong + filename + "\n"
- open('mama.txt', 'w').write(mama)
- open('napake.txt', 'w').write(wrong)
-
-# Napiši ukaz, ki bo 5s sledil vsebini /var/log/syslog. V primeru, da se v syslogu pojavi niz "zmeda", naj program izpiše "imam ga". Poleg tega naj program med sledenjem syslog-u odšteva od 5 do 1 (vsako sekundo naj se izpiše naslednja številka.
-def checker():
- import time
- import re
- import mmap
- t = time.time() * 1000
- n = t
-
- f = open('/var/log/syslog', 'r+')
- data = mmap.mmap(f.fileno(), 0)
- me = re.findall('zmeda', data)
-
- c = len(me)
- for i in range(5, 0, -1):
- print(i)
- n += 1000
- while n > t:
- f = open('/var/log/syslog', 'r+')
- data = mmap.mmap(f.fileno(), 0)
- me = re.findall('zmeda', data)
- if len(me) > c:
- c = len(me)
- print('imam ga')
- t = time.time() * 1000
-
-# Nastavi okoljsko spremenljivko TEST, da bo imela isto vrednost kot okoljska spremenljivka USER + število okoljskih spremenljivk, ki v imenu ali vrednosti ne vsebujejo besede TEST. Primer: polz37
-def env():
- import os
- import re
- c = 0
- for i in os.environ:
- if not (re.search('TEST', i) or re.search('TEST', os.environ[i])):
- c = c + 1
- os.environ['TEST'] = os.environ['USER'] + repr(c)
-
-# S pomočjo programa cURL shrani vsebino spletne strani www.google.com v datoteko z imenom website.txt
-def googl():
- import urllib.request, urllib.error, urllib.parse
- open('website.txt', 'w').write(urllib.request.urlopen('http://www.google.com').read())
-
-# Napiši ukaz, ki bo število pojavitev značke <div> v datoteki website.txt dodal na konec te iste datoteke brez, da bi se ukaz zapisal v zgodovino ukazov "bash history"
-def div():
- import re
- import mmap
- f = open('website.txt', 'r+')
- data = mmap.mmap(f.fileno(), 0)
- me = re.findall('<[^/<>]*div[^>]*>', data)
- open('website.txt', 'a+').write(repr(len(me)))
-
-# Napiši ukaz brez uporabe programa cron, kateri 5 minut po izvedbi izpiše vsebino imenika v katerem se trenutno nahajaš
-def sleep():
- import time
- import os
- time.sleep(300)
- print(os.listdir('.'))
-
-# Napiši najkrajši ukaz, ki s pomočjo Pythona zažene preprost (integriran) HTTP strežnik kateri streže datoteke iz imenika iz katerega je bil pognan na vratih 8000
-def server():
- import http.server
- import socketserver
- httpd = socketserver.TCPServer(("", 8000), http.server.SimpleHTTPRequestHandler)
- httpd.serve_forever()
+# preimenuj vse datoteke tako, da zamenjaš minuse s podčrtaji +def rename(): + import os + for filename in os.listdir('.'): + os.rename(filename, filename.replace('-', '_')) + +# Napiši čim krajši ukaz, ki vse datoteke iz /home/user/mapa/mojimenik premakne v /home/user/novi +def mv_novi(): + import os + for filename in os.listdir("/home/user/mapa/mojimenik/"): + os.rename('/home/user/mapa/mojimenik/' + filename, '/home/user/novi/' + filename) + +# Napiši ukaz, ki s pomočjo ukaza grep v datoteko "mama.txt" izpiše vse navade (ne skrite) datoteke v trenutnem imeniku, ki vsebujejo niz "mama", v datoteko "napake.txt" pa izpiše vse morebitne napake (npr. to, da so nekateri objekti v trenutnem imeniku dejansko imeniki ali napačne simbolične povezave) +def mama(): + import os + import re + import mmap + mama = '' + wrong = '' + for filename in os.listdir('.'): + try: + f = open(filename, 'r+') + data = mmap.mmap(f.fileno(), 0) + if re.search('mama', data): + mama = mama + filename + "\n" + except ValueError: + wrong = wrong + filename + "\n" + open('mama.txt', 'w').write(mama) + open('napake.txt', 'w').write(wrong) + +# Napiši ukaz, ki bo 5s sledil vsebini /var/log/syslog. V primeru, da se v syslogu pojavi niz "zmeda", naj program izpiše "imam ga". Poleg tega naj program med sledenjem syslog-u odšteva od 5 do 1 (vsako sekundo naj se izpiše naslednja številka. +def checker(): + import time + import re + import mmap + t = time.time() * 1000 + n = t + + f = open('/var/log/syslog', 'r+') + data = mmap.mmap(f.fileno(), 0) + me = re.findall('zmeda', data) + + c = len(me) + for i in range(5, 0, -1): + print(i) + n += 1000 + while n > t: + f = open('/var/log/syslog', 'r+') + data = mmap.mmap(f.fileno(), 0) + me = re.findall('zmeda', data) + if len(me) > c: + c = len(me) + print('imam ga') + t = time.time() * 1000 + +# Nastavi okoljsko spremenljivko TEST, da bo imela isto vrednost kot okoljska spremenljivka USER + število okoljskih spremenljivk, ki v imenu ali vrednosti ne vsebujejo besede TEST. Primer: polz37 +def env(): + import os + import re + c = 0 + for i in os.environ: + if not (re.search('TEST', i) or re.search('TEST', os.environ[i])): + c = c + 1 + os.environ['TEST'] = os.environ['USER'] + repr(c) + +# S pomočjo programa cURL shrani vsebino spletne strani www.google.com v datoteko z imenom website.txt +def googl(): + import urllib.request, urllib.error, urllib.parse + open('website.txt', 'w').write(urllib.request.urlopen('http://www.google.com').read()) + +# Napiši ukaz, ki bo število pojavitev značke <div> v datoteki website.txt dodal na konec te iste datoteke brez, da bi se ukaz zapisal v zgodovino ukazov "bash history" +def div(): + import re + import mmap + f = open('website.txt', 'r+') + data = mmap.mmap(f.fileno(), 0) + me = re.findall('<[^/<>]*div[^>]*>', data) + open('website.txt', 'a+').write(repr(len(me))) + +# Napiši ukaz brez uporabe programa cron, kateri 5 minut po izvedbi izpiše vsebino imenika v katerem se trenutno nahajaš +def sleep(): + import time + import os + time.sleep(300) + print(os.listdir('.')) + +# Napiši najkrajši ukaz, ki s pomočjo Pythona zažene preprost (integriran) HTTP strežnik kateri streže datoteke iz imenika iz katerega je bil pognan na vratih 8000 +def server(): + import http.server + import socketserver + httpd = socketserver.TCPServer(("", 8000), http.server.SimpleHTTPRequestHandler) + httpd.serve_forever() 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..aa57d3e 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 @@ -1,6 +1,4 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# kpov_random_helpers should be imported by add_assignment.py +# kpov_util should be imported by add_assignment.py # TODO (polz): instructions = { @@ -116,90 +114,54 @@ 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! + + # TOD: (polz) this has to be changed! Get a move on! # - # sv: Z primozem lavricem sva skusala nekaj narediti + # sv: Z primozem lavricem sva skusala nekaj narediti # Ker gen params ni narejen, sklepam da je "Mapa" na namizju, # imena datotek pa so: 1,1-,2,2-,3,3-,4,5,6,7,8,9,.mama1,mama2,mama3,mama4,mama5,mojimenik,novi,oce1 # 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 = kpov_util.ssh_test(IP_malishell, 'student', 'vaje', ( + ('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)), + )) + 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): @@ -209,16 +171,15 @@ def gen_params(user_id, params_meta): homedir = '/home/student/' r = random.Random(user_id) params['file_creator_random_seed'] = str(r.random()) - params['file_rename_dirname'] = homedir + kpov_random_helpers.default_generators['dirname'](r) + params['file_rename_dirname'] = homedir + kpov_util.default_generators['dirname'](r) params['mv_src_dir'] = homedir + "".join([r.choice("abcdefgh") for i in range(6)]) params['mv_dst_dir'] = homedir + "".join([r.choice("ijklmnop") for i in range(6)]) params['syslog_n_zmeda'] = str(r.randint(5, 15)) - params['cowsay_string'] = kpov_random_helpers.default_generators['short_text'](r) - params['curl_fname'] = kpov_random_helpers.default_generators['filename'](r) + params['cowsay_string'] = kpov_util.default_generators['short_text'](r) + params['curl_fname'] = kpov_util.default_generators['filename'](r) 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 +193,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 +223,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 +237,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 +252,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 +269,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 +281,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']) @@ -332,11 +292,8 @@ def task_check(results, params): else: hints += ["wrong image count"] task6_ok = True - wc_cat_str = 'cat {}/lines.txt'.format(params['wc_dirname']) - if not wc_cat_str == results['wc_lines'][:len(wc_cat_str)]: - task6_ok = False try: - assert int(results['wc_lines'][len(wc_cat_str):].strip()) == int(params['wc_n_lines']) + assert int(results['wc_lines'].strip()) == int(params['wc_n_lines']) except: task6_ok = False if task6_ok: @@ -409,4 +366,4 @@ def prepare_disks(templates, task_params, global_params): d.mkdir(task_params['wc_dirname']) d.chown(1000, 1000, task_params['wc_dirname']) d.write("{}".format(os.path.join(task_params['wc_dirname'], 'count.txt')), "".join(lx)) - write_default_config(templates['simpleArbiterDhcpGW'], global_params) + #write_default_config(templates['simpleArbiterDhcpGW'], global_params) |