Created
January 6, 2015 20:50
-
-
Save layus/546ba1381d32fe8ea30a to your computer and use it in GitHub Desktop.
INGInious-related script that also extract the filename of an archive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # This Python file uses the following encoding: utf-8 | |
| import re | |
| import sys | |
| import json | |
| import base64 | |
| # Retrieves the input data from INGInious and open input file | |
| try: | |
| archive = {} | |
| with open('/.__input/__inputdata.json', 'r') as file: | |
| archive = json.loads(file.read().strip('\0').strip())['input']['archive']; | |
| if not isinstance(archive, dict): | |
| sys.stderr.write("Internal error") | |
| sys.exit(2) | |
| if "filename" not in archive or "value" not in archive: | |
| sys.stderr.write("Internal error") | |
| sys.exit(2) | |
| # ensure filename matches | |
| match = re.search(r'^(\d{6,8})-(\d{6,8}).(tgz|zip)$', archive['filename']) | |
| if not match: | |
| sys.stderr.write("Le nom de fichier doit être NOMA1-NOMA2.tgz ou NOMA1-NOMA2.zip.") | |
| sys.exit(2) | |
| noma1, noma2, ext = match.groups() | |
| noma1 = noma1.ljust(8,"0") | |
| noma2 = noma2.ljust(8,"0") | |
| data = {"noma1":noma1, "noma2":noma2, "ext":ext} | |
| with open('/tmp/data.json', 'w') as outfile: | |
| json.dump(data, outfile) | |
| name = '{0}-{1}.{2}'.format(noma1, noma2, ext) | |
| with open(name, 'w') as f: | |
| # Do not use print as it does not support binary | |
| f.write(base64.b64decode(archive["value"])) | |
| print(name) | |
| except IOError, e: | |
| sys.stderr.write("Internal file error") | |
| sys.exit(2) | |
| except ValueError, e: | |
| sys.stderr.write("Input is not compatible") | |
| sys.exit(2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment