# TODO: # - check if everything is filled in (computers, params, preparation) # - improve scoring # - test # - switch to a real SSH/SFTP client to properly handle filenames instructions = { 'si':u""" Ustvari dva navidezna računalnika - simpleArbiter in Student. Poskrbi, da bosta oba na istem omrežju in da bo imel Student naslov {student_IP}. Obenem mora biti simpleArbiter povezan na Internet. Računajte, da se na studentGUI ob zagonu zažene program {net_prog_name}, ki vam spreminja nastavitve mrežne kartice. V domačem imeniku uporabnika student obstaja program {P_c} v programskem jeziku C. Prevedite ga v program z imenom {P_executable}. Izvorna koda je namenoma pokvarjena tako, da so vanjo vrinjeni nepotrebni znaki. Pred prevajanjem jo morate popraviti. Napišite skripto ali program {P_script} v domačem imeniku uporabnika student, ki: - požene {P_c} z argumentom {arg_c} in mu na standardni vhod pripelje vrednost spremenljivke {env_c} - vse, kar {P_c} izpiše na stderr, spravi v datoteko {out_stderr_c}. - vse vrstice, ki jih {P_c} izpiše na stdout in vsebujejo zaporedje znakov "ma", zapiše v {out_stdout_c}. Lastnik vseh ustvarjenih datotek mora biti uporabnik test. Gesla uporabnika test ne smete spreminjati. """, 'en': u''' ''', } computers = { 'SimpleArbiter': { 'disks': [ { 'name': 'simpleArbiter', }, ], 'network_interfaces': [ {'network': 'net1'}, {'network': 'net2'}, ], 'flavor': 'm1.tiny', 'config_drive': True, }, 'Student': { 'disks': [ {'name': 'student-mock_entrance'} ], 'flavor': 'm1.tiny', 'network_interfaces': [{'network': 'net1'}], 'config_drive': True, } } networks = { 'net1': { 'public': True, }, 'net2': { 'public': False, }, } params_meta = { 'student_IP': { 'descriptions': { 'si': 'IP naslov SimpleStudent', 'en': 'IP address of SimpleStudent', }, 'w': False, 'public': True, 'type': 'IP', 'generated': True, }, 'net_prog_name': { 'descriptions': { 'si': 'Ime programa, ki ponastalvlja naslov', 'en': 'The name of the program resetting the network' }, 'w': False, 'public': True, 'type': 'filename', 'generated': True, }, 'P_c': { 'descriptions': { 'si': 'Datoteka s programom v C', 'en': 'Filename of the program in C', }, 'w': False, 'public': True, 'type': 'filename', 'generated': True, }, 'P_executable': { 'description': { 'si': 'Ime prevedenega programa v C', 'en': 'Filename of the compiled C program' }, 'w': False, 'public': True, 'type': 'filename', 'generated': True, }, 'arg_c': { 'descriptions': { 'si': 'Vrednost argumenta', 'en': 'Argument value', }, 'w': False, 'public': True, 'type': 'short_text', 'generated': True, }, 'env_c': { 'descriptions': { 'si': 'Ime okoljske spremenljivke', 'en': 'The name of the environment environment', }, 'w': False, 'public': True, 'type': 'short_text', 'generated': True, }, 'out_stderr_c': { 'descriptions': { 'si': 'Datoteka z napakami', 'en': 'File to store errors', }, 'w': True, 'public': True, 'type': 'filename', 'generated': True, }, 'P_script': { 'descriptions': { 'si': 'Ime skripte', 'en': 'Filename of the script', }, 'w': False, 'public': True, 'type': 'filename', 'generated': True, }, 'out_stdout_c': { 'descriptions': { 'si': 'Datoteka z izhodom', 'en': 'File to store the output', }, 'w': False, 'public': True, 'type': 'filename', 'generated': True, }, 'param_gen_seed': { 'descriptions': { 'si': 'Nakljucno seme', 'en': 'Random seed', }, 'w': False, 'public': True, 'type': None, 'generated': True, }, 'c_destroy_gen_seed': { 'descriptions': { 'si': 'Nakljucno seme za kvarjenje kode v C', 'en': 'Random seed for destroying the C code', }, 'w': False, 'public': False, 'type': None, 'generated': True, } } def task(student_IP, net_prog_name, P_c, P_executable, arg_c, env_c, out_stderr_c, out_stdout_c, P_script, param_gen_seed): import pxssh import random # ideally, this would be done using a SFTP client instead of pxssh target = pxssh.pxssh() # target.login(student_IP, 'test', 'test') r = random.Random(param_gen_seed) env_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in xrange(11)]) arg_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in xrange(13)]) stdin_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in xrange(17)]) conn.sendline('export {}={}; {}'.format(env_c, env_val, P_script)) conn.prompt() results['script_run'] = conn.before() conn.sendline('cat {}', out_stderr_c) conn.prompt() results['script_stderr'] = conn.before() conn.sendline('cat {}', out_stdout_c) conn.prompt() results['script_stdout'] = conn.before() conn.sendline('echo "{}" | {} "{}" 2> /dev/null'.format(stdin_val, P_c, arg_val)) conn.prompt() results['prog_stdout'] = conn.before() conn.sendline('echo "{}" | {} "{}" > /dev/null'.format(stdin_val, P_c, arg_val)) conn.prompt() results['prog_stderr'] = conn.before() target.logout() return results def gen_params(user_id, params_meta): import random r = random.Random(user_id) params = kpov_random_helpers.default_gen(user_id, params_meta) homedir = '/home/student' params['env_c'] = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ') for i in xrange(5)]) params['P_c'] = "".join([r.choice('abcdefghijklmnoprst') for i in xrange(5)]) + ".c" params['param_gen_seed'] = r.randint(0, 2**24) params['c_destroy_gen_seed'] = r.randint(0, 2**24) for k in []: params[k] = homedir + params[k] return params def task_check(results, params): import os score = 0 hints = [] r = random.Random(param_gen_seed) env_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in xrange(11)]) arg_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in xrange(13)]) stdin_val = "".join([r.choice('ABCDEFGHIJKLMNPRSTUVZ012345') for i in xrange(17)]) matched = 0 files = os.listdir(params['folder']) for fn in files: if fn in results['files']: matched += 1 if matched > 0: score = 1 else: hints += ["no files"] if matched > len(files)/2: score += 2 else: hints += ["less than half the files"] if (matched == len(files)): score += 3 else: hints += ["wrong number of files"] rl = results['contents'].split('\n') rl.sort() tl = [] for fn in files: with open(fn) as f: tl += f.read().upper().split('\n') tl.sort() if rl == tl: score += 4 else: hints += ["wrong files"] return hints, score def prepare_disks(templates, params): import random c_source = """ """ pass