Make dist-git-upload.cgi PEP8 compliant

Signed-off-by: Till Maas <opensource@till.name>
This commit is contained in:
Till Maas 2016-02-23 19:55:27 +01:00 committed by Kevin Fenzi
parent f1eff0070a
commit 25a4bd15ec

View file

@ -6,17 +6,17 @@
# #
# License: GPL # License: GPL
import cgi
import errno
import grp
import hashlib
import os import os
import sys import sys
import cgi
import tempfile import tempfile
import grp
import errno
import fedmsg import fedmsg
import fedmsg.config import fedmsg.config
import hashlib
# Reading buffer size # Reading buffer size
BUFFER_SIZE = 4096 BUFFER_SIZE = 4096
@ -30,6 +30,7 @@ CACHE_DIR = '/srv/cache/lookaside/pkgs'
# Fedora Packager Group # Fedora Packager Group
PACKAGER_GROUP = 'packager' PACKAGER_GROUP = 'packager'
def send_error(text, status='500 Internal Server Error'): def send_error(text, status='500 Internal Server Error'):
"""Send an error back to the client """Send an error back to the client
@ -46,6 +47,7 @@ def send_error(text, status='500 Internal Server Error'):
print text print text
sys.exit(0) sys.exit(0)
def check_form(form, var): def check_form(form, var):
ret = form.getvalue(var, None) ret = form.getvalue(var, None)
if ret is None: if ret is None:
@ -56,6 +58,7 @@ def check_form(form, var):
status='400 Bad Request') status='400 Bad Request')
return ret return ret
def check_auth(username): def check_auth(username):
authenticated = False authenticated = False
try: try:
@ -80,13 +83,13 @@ def hardlink(src, dest, username):
os.unlink(dest) os.unlink(dest)
os.link(src, dest) os.link(src, dest)
print >> sys.stderr, "[username=%s] ln %s %s" % (username, src, dest) sys.stderr.write("[username=%s] ln %s %s\n" % (username, src, dest))
def makedirs(dir, username, mode=02755): def makedirs(dir_, username, mode=02755):
try: try:
os.makedirs(dir, mode=mode) os.makedirs(dir_, mode=mode)
print >> sys.stderr, '[username=%s] mkdir %s' % (username, dir) sys.stderr.write('[username=%s] mkdir %s\n' % (username, dir_))
except OSError as e: except OSError as e:
if e.errno != errno.EEXIST: if e.errno != errno.EEXIST:
@ -98,7 +101,8 @@ def main():
username = os.environ.get('SSL_CLIENT_S_DN_CN', None) username = os.environ.get('SSL_CLIENT_S_DN_CN', None)
if not check_auth(username): if not check_auth(username):
send_error('You must connect with a valid certificate and be in the %s group to upload.' % PACKAGER_GROUP, send_error('You must connect with a valid certificate and be in the '
'%s group to upload.' % PACKAGER_GROUP,
status='403 Forbidden') status='403 Forbidden')
print 'Content-Type: text/plain' print 'Content-Type: text/plain'
@ -110,11 +114,11 @@ def main():
name = check_form(form, 'name') name = check_form(form, 'name')
# Search for the file hash, start with stronger hash functions # Search for the file hash, start with stronger hash functions
if form.has_key('sha512sum'): if 'sha512sum' in form:
checksum = check_form(form, 'sha512sum') checksum = check_form(form, 'sha512sum')
hash_type = "sha512" hash_type = "sha512"
elif form.has_key('md5sum'): elif 'md5sum' in form:
# Fallback on md5, as it's what we currently use # Fallback on md5, as it's what we currently use
checksum = check_form(form, 'md5sum') checksum = check_form(form, 'md5sum')
hash_type = "md5" hash_type = "md5"
@ -130,14 +134,17 @@ def main():
# Is this a submission or a test? # Is this a submission or a test?
# in a test, we don't get a file, just a filename. # in a test, we don't get a file, just a filename.
# In a submission, we don;t get a filename, just the file. # In a submission, we don;t get a filename, just the file.
if form.has_key('filename'): if 'filename' in form:
action = 'check' action = 'check'
filename = check_form(form, 'filename') filename = check_form(form, 'filename')
filename = os.path.basename(filename) filename = os.path.basename(filename)
print >> sys.stderr, '[username=%s] Checking file status: NAME=%s FILENAME=%s %sSUM=%s' % (username, name, filename, hash_type.upper(), checksum) sys.stderr.write('[username=%s] Checking file status: NAME=%s '
'FILENAME=%s %sSUM=%s\n' % (username, name, filename,
hash_type.upper(),
checksum))
else: else:
action = 'upload' action = 'upload'
if form.has_key('file'): if 'file' in form:
upload_file = form['file'] upload_file = form['file']
if not upload_file.file: if not upload_file.file:
send_error('No file given for upload. Aborting.', send_error('No file given for upload. Aborting.',
@ -146,16 +153,20 @@ def main():
else: else:
send_error('Required field "file" is not present.', send_error('Required field "file" is not present.',
status='400 Bad Request') status='400 Bad Request')
print >> sys.stderr, '[username=%s] Processing upload request: NAME=%s FILENAME=%s %sSUM=%s' % (username, name, filename, hash_type.upper(), checksum)
sys.stderr.write('[username=%s] Processing upload request: '
'NAME=%s FILENAME=%s %sSUM=%s\n' % (
username, name, filename, hash_type.upper(),
checksum))
module_dir = os.path.join(CACHE_DIR, name) module_dir = os.path.join(CACHE_DIR, name)
hash_dir = os.path.join(module_dir, filename, hash_type, checksum) hash_dir = os.path.join(module_dir, filename, hash_type, checksum)
msgpath = os.path.join(name, filename, hash_type, checksum, filename) msgpath = os.path.join(name, filename, hash_type, checksum, filename)
# first test if the module really exists # first test if the module really exists
git_dir = os.path.join(GITREPO, '%s.git' % name) git_dir = os.path.join(GITREPO, '%s.git' % name)
if not os.path.isdir(git_dir): if not os.path.isdir(git_dir):
print >> sys.stderr, '[username=%s] Unknown module: %s' % (username, name) sys.stderr.write('[username=%s] Unknown module: %s' % (username, name))
send_error('Module "%s" does not exist!' % name, send_error('Module "%s" does not exist!' % name,
status='404 Not Found') status='404 Not Found')
@ -207,7 +218,8 @@ def main():
check_checksum = m.hexdigest() check_checksum = m.hexdigest()
if checksum != check_checksum: if checksum != check_checksum:
os.unlink(tmpfile) os.unlink(tmpfile)
send_error("%s check failed. Received %s instead of %s." % (hash_type.upper(), check_checksum, checksum), send_error("%s check failed. Received %s instead of %s." %
(hash_type.upper(), check_checksum, checksum),
status='400 Bad Request') status='400 Bad Request')
# wow, even the checksum matches. make sure full path is valid now # wow, even the checksum matches. make sure full path is valid now
@ -215,8 +227,11 @@ def main():
os.rename(tmpfile, dest_file) os.rename(tmpfile, dest_file)
os.chmod(dest_file, 0644) os.chmod(dest_file, 0644)
print >> sys.stderr, '[username=%s] Stored %s (%d bytes)' % (username, dest_file, filesize) sys.stderr.write('[username=%s] Stored %s (%d bytes)' % (username,
print 'File %s size %d %s %s stored OK' % (filename, filesize, hash_type.upper(), checksum) dest_file,
filesize))
print 'File %s size %d %s %s stored OK' % (filename, filesize,
hash_type.upper(), checksum)
# Add the file to the old path, where fedpkg is currently looking for it # Add the file to the old path, where fedpkg is currently looking for it
if hash_type == "md5": if hash_type == "md5":
@ -230,8 +245,9 @@ def main():
fedmsg.init(name="relay_inbound", cert_prefix="lookaside", **config) fedmsg.init(name="relay_inbound", cert_prefix="lookaside", **config)
topic = "lookaside.new" topic = "lookaside.new"
msg = dict(name=name, md5sum=checksum, filename=filename.split('/')[-1], msg = dict(name=name, md5sum=checksum,
agent=username, path=msgpath) filename=filename.split('/')[-1], agent=username,
path=msgpath)
fedmsg.publish(modname="git", topic=topic, msg=msg) fedmsg.publish(modname="git", topic=topic, msg=msg)
except Exception as e: except Exception as e:
print "Error with fedmsg", str(e) print "Error with fedmsg", str(e)
@ -242,5 +258,5 @@ if __name__ == '__main__':
except Exception as e: except Exception as e:
import traceback import traceback
print >> sys.stderr, '%s' % traceback.format_exc() sys.stderr.write('%s\n' % traceback.format_exc())
send_error(str(e)) send_error(str(e))