1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-02-25 19:52:12 +01:00
github.com_openSUSE_osc/osc/othermethods.py
Dr. Peter Poeml baafea955b - add 'edituser' command for editing the metadata of a user account. It tries
to create a user if it doesn't exist yet. A new command 'usermeta' replaces
  'id' respectively 'userid'.
- add preliminary support for doing uploads with a subsequent "commit" request
  (not switched on now, since I want to do more testing on this, and there is
  no way to pass on a commit message yet anyway.)
- add runtime check for build.rpm version, so the rpm package dependencies is
  no longer required
- fix typo introduced last time in the editmeta, deletepac, deleteprj commands
- rewrite the PUT and DELETE request methods to 
  - transparently handle HTTPS 
  - handle path parameters (for commits)
  - send user agent
- rename get_user_id() function to get_user_meta()
- change copyright notice from Peter Poeml to Peter Poeml / Novell Inc.
- fix testsuite
- add CREDITS file
2006-10-12 13:22:56 +00:00

97 lines
2.7 KiB
Python
Executable File

#!/usr/bin/python
# Copyright (C) 2006 Peter Poeml / Novell Inc. All rights reserved.
# This program is free software; it may be used, copied, modified
# and distributed under the terms of the GNU General Public Licence,
# either version 2, or (at your option) any later version.
"""
This file is provided because urllib2 doesn't have support for the DELETE and
PUT methods.
"""
import httplib
import base64
import os
import urlparse
from osc.core import __version__
BLOCKSIZE=4096
def request(method, url, username, password, file=None, strbuf=None):
"""call with method = (PUT|DELETE)"""
if method == 'PUT':
if file == None and strbuf == None:
print >>sys.stderr, 'putting a file requires either a filename or a string buffer'
sys.exit(1)
if strbuf:
size = len(strbuf)
else:
size = os.path.getsize(file)
scheme, host, path, params, query, fragment = urlparse.urlparse(url)
if query:
path += '?' + query
if scheme == 'https':
conn = httplib.HTTPS(host)
elif scheme == 'http':
conn = httplib.HTTP(host)
else:
sys.exit('unknown scheme %s' % scheme)
# Headers
conn.putrequest(method, '%s' % path)
conn.putheader('Host', host)
conn.putheader('User-agent', 'osc/%s' % __version__)
auth_string = base64.encodestring('%s:%s' % (username, password)).strip()
conn.putheader('Authorization', 'Basic %s' % auth_string)
if method == 'PUT':
conn.putheader('Content-Type', 'text/plain')
conn.putheader('Content-Length', str(size))
conn.endheaders()
# Body
if method == 'PUT':
if strbuf:
conn.send(strbuf)
else:
fp = open(file, 'rb')
#n = 0
while 1:
buf = fp.read(BLOCKSIZE)
#n+=1
#if n % 10 == 0:
# print 'upload-sending blocknum=', n
# print '.',
if not buf: break
try:
conn.send(buf)
except:
sys.exit('ERROR uploading %s' % file)
fp.close()
reply, msg, headers = conn.getreply()
if reply != 200:
print >>sys.stderr, 'Error: can\'t %s \'%s\'' % (method, url)
print >>sys.stderr, 'reply:', reply
print >>sys.stderr, '\nDebugging output follows.\nurl:\n%s\nheaders:\n%s\nresponse:\n%s' % (url, headers, msg)
def delfile(url, file, username, password):
return request('DELETE', url, username, password, file=file)
def putfile(url, username, password, file=None, strbuf=None):
return request('PUT', url, username, password, file=file, strbuf=strbuf)