upload.py (1298B)
1 #! /usr/bin/env python 2 3 import os, sys, tempfile, shutil, hashlib, tarfile 4 import cgi, cgitb 5 cgitb.enable() 6 7 PKGDIR = os.environ.get('PKGDIR', os.path.abspath('../packages')) 8 9 # Upload a package to the package directory. 10 # It is meant to contain a list of tar packages: 11 # 12 # PKGDIR/pkgname/pkgver/common.tar 13 # PKGDIR/pkgname/pkgver/arch1.tar 14 # PKGDIR/pkgname/pkgver/arch2.tar 15 # ... 16 17 form = cgi.FieldStorage() 18 def getvalue(x): 19 v = form.getvalue(x) 20 if not v: 21 print("Status: 413\ncontent-type: text/plain\n\nmissing %s\n" % x) 22 return v 23 24 pkgname = getvalue('pkgname') 25 pkgver = getvalue('pkgver') 26 pkgdata = getvalue('pkgdata') 27 # pkghash = getvalue('pkghash') # TODO provide away to verify file hashes and signatures? 28 29 up = os.path.join(PKGDIR, pkgname) 30 dest = os.path.join(up, pkgver) 31 if os.path.exists(dest): 32 print("Status: 409\ncontent-type: text/plain\n\nPackage %r already exists!\n" % dest) 33 else: 34 if not os.path.isdir(up): 35 os.makedirs(up) 36 37 tmp = tempfile.mkdtemp(dir=up) 38 try: 39 tf = os.path.join(tmp, 'some_temporary_file') 40 with open(tf, 'wb') as f: 41 f.write(pkgdata) 42 with tarfile.open(tf) as f: 43 f.extractall(tmp) 44 os.remove(tf) 45 os.rename(tmp, dest) 46 finally: 47 # cleanup 48 try: 49 shutil.rmtree(tmp) 50 except Exception: 51 pass 52 53 print('''Content-Type: text/plain\n\nok''') 54