2006-04-20 16:26:50 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright (C) 2006 Peter Poeml. 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
|
|
|
|
|
2006-08-07 12:08:54 +02:00
|
|
|
BLOCKSIZE=4096
|
2006-04-20 16:26:50 +02:00
|
|
|
|
|
|
|
def delfile(url, file, username, password):
|
|
|
|
|
2006-07-19 17:15:57 +02:00
|
|
|
auth_string = base64.encodestring('%s:%s' % (username, password)).strip()
|
2006-04-20 16:26:50 +02:00
|
|
|
|
|
|
|
u = urlparse.urlparse(url)
|
|
|
|
host = u[1]
|
|
|
|
path = u[2]
|
|
|
|
|
|
|
|
conn = httplib.HTTP(host)
|
|
|
|
conn.putrequest('DELETE', '%s' % path)
|
|
|
|
conn.putheader('Host', host)
|
|
|
|
conn.putheader('Authorization', 'Basic %s' % auth_string)
|
|
|
|
conn.endheaders()
|
|
|
|
|
|
|
|
|
|
|
|
reply, msg, headers = conn.getreply()
|
|
|
|
|
2006-08-07 12:08:54 +02:00
|
|
|
if reply != 200:
|
2006-04-20 16:26:50 +02:00
|
|
|
print 'error deleting %s' % file
|
|
|
|
print 'upload-DELETE reply=', reply, ' msg=', msg, 'headers=', headers
|
|
|
|
|
|
|
|
|
2006-08-07 12:08:54 +02:00
|
|
|
def putfile(url, username, password, file=None, strbuf=None):
|
|
|
|
|
|
|
|
if file == None and strbuf == None:
|
|
|
|
print >>sys.stderr, 'putfile requires either a filename or a string buffer'
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if strbuf:
|
|
|
|
size = len(strbuf)
|
|
|
|
else:
|
|
|
|
size = os.stat(file)[6]
|
2006-04-20 16:26:50 +02:00
|
|
|
|
2006-07-19 17:15:57 +02:00
|
|
|
auth_string = base64.encodestring('%s:%s' % (username, password)).strip()
|
2006-04-20 16:26:50 +02:00
|
|
|
|
|
|
|
u = urlparse.urlparse(url)
|
|
|
|
host = u[1]
|
|
|
|
path = u[2]
|
|
|
|
|
|
|
|
conn = httplib.HTTP(host)
|
|
|
|
conn.putrequest('PUT', '%s' % path)
|
|
|
|
conn.putheader('Host', host)
|
|
|
|
conn.putheader('Content-Type', 'text/plain')
|
|
|
|
conn.putheader('Content-Length', str(size))
|
|
|
|
conn.putheader('Authorization', 'Basic %s' % auth_string)
|
|
|
|
conn.endheaders()
|
|
|
|
|
2006-08-07 12:08:54 +02:00
|
|
|
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 '.',
|
|
|
|
pass
|
|
|
|
|
|
|
|
if not buf: break
|
2006-09-26 11:02:00 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
conn.send(buf)
|
|
|
|
except:
|
|
|
|
print
|
|
|
|
print 'ERROR uploading %s' % file
|
|
|
|
print
|
|
|
|
os._exit(1)
|
|
|
|
|
2006-08-07 12:08:54 +02:00
|
|
|
fp.close()
|
2006-04-20 16:26:50 +02:00
|
|
|
|
|
|
|
reply, msg, headers = conn.getreply()
|
|
|
|
|
2006-08-07 12:08:54 +02:00
|
|
|
if reply != 200:
|
2006-04-20 16:26:50 +02:00
|
|
|
print 'error uploading %s' % file
|
|
|
|
print 'upload-PUT reply=', reply, ' msg=', msg, 'headers=', headers
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
import sys
|
|
|
|
|
|
|
|
username = 'yourusername'
|
|
|
|
password = 'yourpassword'
|
|
|
|
file = sys.argv[1]
|
|
|
|
url = 'http://api.opensuse.org/source/exim/exim/%s' % os.path.basename(file)
|
|
|
|
|
|
|
|
putfile(url, file, username, password)
|
|
|
|
|
|
|
|
delfile(url, file, username, password)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|