"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "roundup/scripts/roundup_xmlrpc_server.py" between
roundup-1.6.1.tar.gz and roundup-2.0.0.tar.gz

About: Roundup is an highly customisable issue-tracking system with command-line, web and e-mail interfaces (written in Python).

roundup_xmlrpc_server.py  (roundup-1.6.1):roundup_xmlrpc_server.py  (roundup-2.0.0)
#! /usr/bin/env python #! /usr/bin/env python
# #
# Copyright (C) 2007 Stefan Seefeld # Copyright (C) 2007 Stefan Seefeld
# All rights reserved. # All rights reserved.
# For license terms see the file COPYING.txt. # For license terms see the file COPYING.txt.
# #
# --- patch sys.path to make sure 'import roundup' finds correct version # --- patch sys.path to make sure 'import roundup' finds correct version
from __future__ import print_function
import sys import sys
import os.path as osp import os.path as osp
thisdir = osp.dirname(osp.abspath(__file__)) thisdir = osp.dirname(osp.abspath(__file__))
rootdir = osp.dirname(osp.dirname(thisdir)) rootdir = osp.dirname(osp.dirname(thisdir))
if (osp.exists(thisdir + '/__init__.py') and if (osp.exists(thisdir + '/__init__.py') and
osp.exists(rootdir + '/roundup/__init__.py')): osp.exists(rootdir + '/roundup/__init__.py')):
# the script is located inside roundup source code # the script is located inside roundup source code
sys.path.insert(0, rootdir) sys.path.insert(0, rootdir)
# --/ # --/
import base64, getopt, os, sys, socket, urllib import base64, getopt, os, sys, socket
from roundup.anypy import urllib_
from roundup.xmlrpc import translate from roundup.xmlrpc import translate
from roundup.xmlrpc import RoundupInstance from roundup.xmlrpc import RoundupInstance
import roundup.instance import roundup.instance
from roundup.instance import TrackerError from roundup.instance import TrackerError
from roundup.cgi.exceptions import Unauthorised from roundup.cgi.exceptions import Unauthorised
from SimpleXMLRPCServer import SimpleXMLRPCServer from roundup.anypy import xmlrpc_
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler SimpleXMLRPCServer = xmlrpc_.server.SimpleXMLRPCServer
SimpleXMLRPCRequestHandler = xmlrpc_.server.SimpleXMLRPCRequestHandler
class RequestHandler(SimpleXMLRPCRequestHandler): class RequestHandler(SimpleXMLRPCRequestHandler):
"""A SimpleXMLRPCRequestHandler with support for basic """A SimpleXMLRPCRequestHandler with support for basic
HTTP Authentication.""" HTTP Authentication."""
TRACKER_HOMES = {} TRACKER_HOMES = {}
TRACKERS = {} TRACKERS = {}
def is_rpc_path_valid(self): def is_rpc_path_valid(self):
path = self.path.split('/') path = self.path.split('/')
name = urllib.unquote(path[1]).lower() name = urllib_.unquote(path[1]).lower()
return name in self.TRACKER_HOMES return name in self.TRACKER_HOMES
def get_tracker(self, name): def get_tracker(self, name):
"""Return a tracker instance for given tracker name.""" """Return a tracker instance for given tracker name."""
if name in self.TRACKERS: if name in self.TRACKERS:
return self.TRACKERS[name] return self.TRACKERS[name]
if name not in self.TRACKER_HOMES: if name not in self.TRACKER_HOMES:
raise Exception('No such tracker "%s"'%name) raise Exception('No such tracker "%s"' % name)
tracker_home = self.TRACKER_HOMES[name] tracker_home = self.TRACKER_HOMES[name]
tracker = roundup.instance.open(tracker_home) tracker = roundup.instance.open(tracker_home)
self.TRACKERS[name] = tracker self.TRACKERS[name] = tracker
return tracker return tracker
def authenticate(self, tracker): def authenticate(self, tracker):
# Try to extract username and password from HTTP Authentication. # Try to extract username and password from HTTP Authentication.
username, password = None, None username, password = None, None
authorization = self.headers.get('authorization', ' ') authorization = self.headers.get('authorization', ' ')
scheme, challenge = authorization.split(' ', 1) scheme, challenge = authorization.split(' ', 1)
if scheme.lower() == 'basic': if scheme.lower() == 'basic':
decoded = base64.decodestring(challenge) decoded = base64.decodestring(challenge)
if ':' in decoded: if ':' in decoded:
username, password = decoded.split(':') username, password = decoded.split(':')
else: else:
username = decoded username = decoded
if not username: if not username:
username = 'anonymous' username = 'anonymous'
db = tracker.open('admin') db = tracker.open('admin')
try: try:
userid = db.user.lookup(username) userid = db.user.lookup(username)
except KeyError: # No such user except KeyError: # No such user
db.close() db.close()
raise Unauthorised, 'Invalid user' raise Unauthorised('Invalid user')
stored = db.user.get(userid, 'password') stored = db.user.get(userid, 'password')
if stored != password: if stored != password:
# Wrong password # Wrong password
db.close() db.close()
raise Unauthorised, 'Invalid user' raise Unauthorised('Invalid user')
db.setCurrentUser(username) db.setCurrentUser(username)
return db return db
def do_POST(self): def do_POST(self):
"""Extract username and password from authorization header.""" """Extract username and password from authorization header."""
db = None db = None
try: try:
path = self.path.split('/') path = self.path.split('/')
tracker_name = urllib.unquote(path[1]).lower() tracker_name = urllib_.unquote(path[1]).lower()
tracker = self.get_tracker(tracker_name) tracker = self.get_tracker(tracker_name)
db = self.authenticate(tracker) db = self.authenticate(tracker)
instance = RoundupInstance(db, tracker.actions, None) instance = RoundupInstance(db, tracker.actions, None)
self.server.register_instance(instance) self.server.register_instance(instance)
SimpleXMLRPCRequestHandler.do_POST(self) SimpleXMLRPCRequestHandler.do_POST(self)
except Unauthorised as message: except Unauthorised as message:
self.send_error(403, '%s (%s)'%(self.path, message)) self.send_error(403, '%s (%s)' % (self.path, message))
except: except:
if db: if db:
db.close() db.close()
exc, val, tb = sys.exc_info() exc, val, tb = sys.exc_info()
print exc, val, tb print(exc, val, tb)
raise raise
if db: if db:
db.close() db.close()
class Server(SimpleXMLRPCServer): class Server(SimpleXMLRPCServer):
def _dispatch(self, method, params): def _dispatch(self, method, params):
retn = SimpleXMLRPCServer._dispatch(self, method, params) retn = SimpleXMLRPCServer._dispatch(self, method, params)
retn = translate(retn) retn = translate(retn)
return retn return retn
def usage(): def usage():
print """Usage: %s: [options] [name=tracker home]+ print("""Usage: %s: [options] [name=tracker home]+
Options: Options:
-e, --encoding -- specify the encoding to use -e, --encoding -- specify the encoding to use
-V -- be verbose when importing -V -- be verbose when importing
-p, --port <port> -- port to listen on -p, --port <port> -- port to listen on
"""%sys.argv[0] """ % sys.argv[0])
def run(): def run():
try: try:
opts, args = getopt.getopt(sys.argv[1:], opts, args = getopt.getopt(sys.argv[1:],
'e:i:p:V', ['encoding=', 'port=']) 'e:i:p:V', ['encoding=', 'port='])
except getopt.GetoptError as e: except getopt.GetoptError:
usage() usage()
return 1 return 1
verbose = False verbose = False
port = 8000 port = 8000
encoding = None encoding = None
for opt, arg in opts: for opt, arg in opts:
if opt == '-V': if opt == '-V':
verbose = True verbose = True
skipping to change at line 153 skipping to change at line 155
elif opt in ['-e', '--encoding']: elif opt in ['-e', '--encoding']:
encoding = encoding encoding = encoding
tracker_homes = {} tracker_homes = {}
for arg in args: for arg in args:
try: try:
name, home = arg.split('=', 1) name, home = arg.split('=', 1)
# Validate the argument # Validate the argument
tracker = roundup.instance.open(home) tracker = roundup.instance.open(home)
except ValueError: except ValueError:
print 'Instances must be name=home' print('Instances must be name=home')
sys.exit(-1) sys.exit(-1)
except TrackerError: except TrackerError:
print 'Tracker home does not exist.' print('Tracker home does not exist.')
sys.exit(-1) sys.exit(-1)
tracker_homes[name] = home tracker_homes[name] = home
RequestHandler.TRACKER_HOMES=tracker_homes RequestHandler.TRACKER_HOMES = tracker_homes
if sys.version_info[0:2] < (2,5): if sys.version_info[0:2] < (2, 5):
if encoding: if encoding:
print 'encodings not supported with python < 2.5' print('encodings not supported with python < 2.5')
sys.exit(-1) sys.exit(-1)
server = Server(('', port), RequestHandler) server = Server(('', port), RequestHandler)
else: else:
server = Server(('', port), RequestHandler, server = Server(('', port), RequestHandler,
allow_none=True, encoding=encoding) allow_none=True, encoding=encoding)
# Go into the main listener loop # Go into the main listener loop
print 'Roundup XMLRPC server started on %s:%d' \ print('Roundup XMLRPC server started on %s:%d'
% (socket.gethostname(), port) % (socket.gethostname(), port))
try: try:
server.serve_forever() server.serve_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
print 'Keyboard Interrupt: exiting' print('Keyboard Interrupt: exiting')
if __name__ == '__main__': if __name__ == '__main__':
run() run()
 End of changes. 22 change blocks. 
23 lines changed or deleted 25 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)