upload.py 1.3 KB

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