mirror of
https://github.com/release-engineering/dist-git.git
synced 2025-02-23 23:12:55 +00:00
make upload.cgi python2/3 compatible, fix formatting
This commit is contained in:
parent
d558c4054b
commit
eb9ab02d02
2 changed files with 33 additions and 28 deletions
|
@ -25,7 +25,6 @@ Requires: dist-git-selinux
|
||||||
Requires: git
|
Requires: git
|
||||||
Requires: git-daemon
|
Requires: git-daemon
|
||||||
Requires: mod_ssl
|
Requires: mod_ssl
|
||||||
Requires: fedmsg
|
|
||||||
Requires: crudini
|
Requires: crudini
|
||||||
Requires: moreutils
|
Requires: moreutils
|
||||||
Requires(pre): shadow-utils
|
Requires(pre): shadow-utils
|
||||||
|
@ -34,10 +33,12 @@ Requires(pre): shadow-utils
|
||||||
Requires: python-requests
|
Requires: python-requests
|
||||||
Requires: python-configparser
|
Requires: python-configparser
|
||||||
Requires: python-grokmirror
|
Requires: python-grokmirror
|
||||||
|
Requires: fedmsg
|
||||||
%else
|
%else
|
||||||
Requires: python2-requests
|
Requires: python3-requests
|
||||||
Requires: python2-configparser
|
Requires: python3-configparser
|
||||||
Recommends: python3-grokmirror
|
Recommends: python3-grokmirror
|
||||||
|
Requires: python3-fedmsg
|
||||||
%endif
|
%endif
|
||||||
|
|
||||||
%description
|
%description
|
||||||
|
@ -126,6 +127,10 @@ install -d %{buildroot}%{installdir}/web
|
||||||
|
|
||||||
cp -a scripts/httpd/upload.cgi %{buildroot}%{installdir}/web/
|
cp -a scripts/httpd/upload.cgi %{buildroot}%{installdir}/web/
|
||||||
|
|
||||||
|
%if 0%{?rhel} && 0%{?rhel} < 8
|
||||||
|
sed -i '1 s|#.*|#!/usr/bin/python2|' %{buildroot}%{installdir}/web/upload.cgi
|
||||||
|
%endif
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# SELinux
|
# SELinux
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python3
|
||||||
#
|
#
|
||||||
# CGI script to handle file updates for the rpms git repository. There
|
# CGI script to handle file updates for the rpms git repository. There
|
||||||
# is nothing really complex here other than tedious checking of our
|
# is nothing really complex here other than tedious checking of our
|
||||||
|
@ -37,18 +37,19 @@ def send_error(text, status='500 Internal Server Error'):
|
||||||
text (str): The error message to send the client
|
text (str): The error message to send the client
|
||||||
status (str, optional): The HTTP status code to return to the client.
|
status (str, optional): The HTTP status code to return to the client.
|
||||||
"""
|
"""
|
||||||
print 'Status: %s' % status
|
print('Status: %s' % status)
|
||||||
print 'Content-type: text/plain'
|
print('Content-type: text/plain\n')
|
||||||
print
|
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:
|
||||||
send_error('Required field "%s" is not present.' % var,
|
send_error('Required field "%s" is not present.' % var,
|
||||||
status='400 Bad Request')
|
status='400 Bad Request')
|
||||||
|
|
||||||
if isinstance(ret, list):
|
if isinstance(ret, list):
|
||||||
send_error('Multiple values given for "%s". Aborting.' % var,
|
send_error('Multiple values given for "%s". Aborting.' % var,
|
||||||
status='400 Bad Request')
|
status='400 Bad Request')
|
||||||
|
@ -57,11 +58,13 @@ def check_form(form, var):
|
||||||
|
|
||||||
def check_group(username):
|
def check_group(username):
|
||||||
authenticated = False
|
authenticated = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if username in grp.getgrnam(PACKAGER_GROUP)[3]:
|
if username in grp.getgrnam(PACKAGER_GROUP)[3]:
|
||||||
authenticated = True
|
authenticated = True
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return authenticated
|
return authenticated
|
||||||
|
|
||||||
|
|
||||||
|
@ -70,7 +73,6 @@ def hardlink(src, dest, username):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.link(src, dest)
|
os.link(src, dest)
|
||||||
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if e.errno != errno.EEXIST:
|
if e.errno != errno.EEXIST:
|
||||||
send_error(str(e))
|
send_error(str(e))
|
||||||
|
@ -82,11 +84,10 @@ def hardlink(src, dest, username):
|
||||||
sys.stderr.write("[username=%s] ln %s %s\n" % (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=0o2755):
|
||||||
try:
|
try:
|
||||||
os.makedirs(dir_, mode=mode)
|
os.makedirs(dir_, mode=mode)
|
||||||
sys.stderr.write('[username=%s] mkdir %s\n' % (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:
|
||||||
send_error(str(e))
|
send_error(str(e))
|
||||||
|
@ -107,23 +108,22 @@ def main():
|
||||||
config = ConfigParser()
|
config = ConfigParser()
|
||||||
config.read('/etc/dist-git/dist-git.conf')
|
config.read('/etc/dist-git/dist-git.conf')
|
||||||
|
|
||||||
os.umask(002)
|
os.umask(0o002)
|
||||||
|
|
||||||
username = os.environ.get('SSL_CLIENT_S_DN_CN', None)
|
username = os.environ.get('SSL_CLIENT_S_DN_CN', None)
|
||||||
gssname = os.environ.get('GSS_NAME', os.environ.get('REMOTE_USER', None))
|
gssname = os.environ.get('GSS_NAME', os.environ.get('REMOTE_USER', None))
|
||||||
if gssname and '@' in gssname and not username:
|
if gssname and '@' in gssname and not username:
|
||||||
username = gssname.partition('@')[0]
|
username = gssname.partition('@')[0]
|
||||||
|
|
||||||
if not config.getboolean('upload', 'disable_group_check', fallback=False) and\
|
if not config.getboolean('upload', 'disable_group_check', fallback=False) and\
|
||||||
not check_group(username):
|
not check_group(username):
|
||||||
send_error('You must connect with a valid certificate and be in the '
|
send_error('You must connect with a valid certificate and be in the '
|
||||||
'%s group to upload.' % PACKAGER_GROUP,
|
'%s group to upload.' % PACKAGER_GROUP,
|
||||||
status='403 Forbidden')
|
status='403 Forbidden')
|
||||||
|
|
||||||
print 'Content-Type: text/plain'
|
|
||||||
print
|
|
||||||
|
|
||||||
assert os.environ['REQUEST_URI'].split('/')[1] == 'repo'
|
assert os.environ['REQUEST_URI'].split('/')[1] == 'repo'
|
||||||
|
|
||||||
|
print('Content-Type: text/plain\n')
|
||||||
form = cgi.FieldStorage()
|
form = cgi.FieldStorage()
|
||||||
name = check_form(form, 'name').strip('/')
|
name = check_form(form, 'name').strip('/')
|
||||||
|
|
||||||
|
@ -147,7 +147,7 @@ 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 'filename' in form:
|
if 'filename' in form:
|
||||||
action = 'check'
|
action = 'check'
|
||||||
filename = check_form(form, 'filename')
|
filename = check_form(form, 'filename')
|
||||||
|
@ -201,20 +201,21 @@ def main():
|
||||||
|
|
||||||
if os.path.exists(dest_file):
|
if os.path.exists(dest_file):
|
||||||
if action == 'check':
|
if action == 'check':
|
||||||
print 'Available'
|
print('Available')
|
||||||
else:
|
else:
|
||||||
upload_file.file.close()
|
upload_file.file.close()
|
||||||
dest_file_stat = os.stat(dest_file)
|
dest_file_stat = os.stat(dest_file)
|
||||||
print 'File %s already exists' % filename
|
print('File %s already exists' % filename)
|
||||||
print 'File: %s Size: %d' % (dest_file, dest_file_stat.st_size)
|
print('File: %s Size: %d' % (dest_file, dest_file_stat.st_size))
|
||||||
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif action == 'check':
|
elif action == 'check':
|
||||||
if os.path.exists(old_path):
|
if os.path.exists(old_path):
|
||||||
# The file had been uploaded at the old path
|
# The file had been uploaded at the old path
|
||||||
hardlink(old_path, dest_file, username)
|
hardlink(old_path, dest_file, username)
|
||||||
print 'Available'
|
print('Available')
|
||||||
else:
|
else:
|
||||||
print 'Missing'
|
print('Missing')
|
||||||
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif hash_type == "md5" and config.getboolean('upload', 'nomd5', fallback=True):
|
elif hash_type == "md5" and config.getboolean('upload', 'nomd5', fallback=True):
|
||||||
|
@ -227,7 +228,7 @@ def main():
|
||||||
# grab a temporary filename and dump our file in there
|
# grab a temporary filename and dump our file in there
|
||||||
tempfile.tempdir = module_dir
|
tempfile.tempdir = module_dir
|
||||||
tmpfile = tempfile.mkstemp(checksum)[1]
|
tmpfile = tempfile.mkstemp(checksum)[1]
|
||||||
tmpfd = open(tmpfile, 'w')
|
tmpfd = open(tmpfile, 'wb')
|
||||||
|
|
||||||
# now read the whole file in
|
# now read the whole file in
|
||||||
m = getattr(hashlib, hash_type)()
|
m = getattr(hashlib, hash_type)()
|
||||||
|
@ -252,7 +253,7 @@ def main():
|
||||||
# wow, even the checksum matches. make sure full path is valid now
|
# wow, even the checksum matches. make sure full path is valid now
|
||||||
makedirs(hash_dir, username)
|
makedirs(hash_dir, username)
|
||||||
os.rename(tmpfile, dest_file)
|
os.rename(tmpfile, dest_file)
|
||||||
os.chmod(dest_file, 0644)
|
os.chmod(dest_file, 0o644)
|
||||||
|
|
||||||
# set mtime of the uploaded file if provided
|
# set mtime of the uploaded file if provided
|
||||||
if 'mtime' in form:
|
if 'mtime' in form:
|
||||||
|
@ -268,10 +269,10 @@ def main():
|
||||||
sys.stderr.write('[username=%s] Stored %s (%d bytes)' % (username,
|
sys.stderr.write('[username=%s] Stored %s (%d bytes)' % (username,
|
||||||
dest_file,
|
dest_file,
|
||||||
filesize))
|
filesize))
|
||||||
print 'File %s size %d %s %s stored OK' % (filename, filesize,
|
print('File %s size %d %s %s stored OK' % (filename, filesize,
|
||||||
hash_type.upper(), checksum)
|
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 used to look for
|
||||||
if hash_type == "md5" and config.getboolean('upload', 'old_paths', fallback=True):
|
if hash_type == "md5" and config.getboolean('upload', 'old_paths', fallback=True):
|
||||||
hardlink(dest_file, old_path, username)
|
hardlink(dest_file, old_path, username)
|
||||||
|
|
||||||
|
@ -289,13 +290,12 @@ def main():
|
||||||
path=msgpath)
|
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))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
main()
|
main()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
import traceback
|
import traceback
|
||||||
sys.stderr.write('%s\n' % traceback.format_exc())
|
sys.stderr.write('%s\n' % traceback.format_exc())
|
||||||
|
|
Loading…
Add table
Reference in a new issue