def __init__(self, username, password, host, port=80, secure=False, rpc_path='/login/xmlrpc'): """ Initialize the XML-RPC interface to a Trac instance. Arguments: - `username`: the user to authenticate as - `password`: a valid password - `host`: the host name serving the Trac instance - `port`: defaults to 80 - `secure`: whether https (SSL) is used - `rpc_path`: the path to the XML-RPC interface of the Trac interface """ self._username = username self._password = password self._host = host self._port = port self._rpc_path = rpc_path self._secure = secure self._editor = self._find_editor() self.trac = Trac(self._username, self._password, self._host, self._port, self._secure, self._rpc_path) # set up shell options cmd.Cmd.__init__(self) self.prompt = "trac->> " self.ruler = '-' self.intro = "Welcome to TracShell!\nType `help` for a list of commands"
import logging from pprint import pformat import json import subprocess import re import git from trac import Trac logging.basicConfig(level=logging.INFO) log = logging.getLogger('startreview') # connect to Trac instance TRAC_CONF = json.load(open(os.path.expanduser('~/.trac'))) trac = Trac(**TRAC_CONF) # parse out ticket IDs and commit IDs commits = git.log_to_commits(sys.stdin, trac.match_ticket_url) # iterate over tickets mentioned in commits for commit in commits: for ticket in commit['tickets']: # get ticket attributes attrs = trac.get_ticket_attrs(ticket) log.info("Processing ticket %s: %s" % (ticket, attrs['summary'])) log.debug("Ticket " + str(ticket) + " attributes: " + pformat(attrs)) # prepare changes in Trac trac_changes = {'id': ticket, 'comment': '', 'attributes': {}} reviewer = attrs.get('reviewer', '')
#!/usr/bin/env python from trac import Trac trac = Trac() print trac.trac.system.listMethods()
class TracShell(cmd.Cmd): """ TracShell is a shell interface to a Trac instance. It uses and XML-RPC interface to Trac provided by: http://trac-hacks.org/wiki/XmlRpcPlugin#DownloadandSource """ def __init__(self, username, password, host, port=80, secure=False, rpc_path='/login/xmlrpc'): """ Initialize the XML-RPC interface to a Trac instance. Arguments: - `username`: the user to authenticate as - `password`: a valid password - `host`: the host name serving the Trac instance - `port`: defaults to 80 - `secure`: whether https (SSL) is used - `rpc_path`: the path to the XML-RPC interface of the Trac interface """ self._username = username self._password = password self._host = host self._port = port self._rpc_path = rpc_path self._secure = secure self._editor = self._find_editor() self.trac = Trac(self._username, self._password, self._host, self._port, self._secure, self._rpc_path) # set up shell options cmd.Cmd.__init__(self) self.prompt = "trac->> " self.ruler = '-' self.intro = "Welcome to TracShell!\nType `help` for a list of commands" def _find_editor(self): """ Try to find the users' editor by testing the $EDITOR environment variable, warn the user if one isn't found and return None. """ try: return os.environ['EDITOR'] except KeyError: print "Warning: No editor found, see `help editors`" return None def do_query(self, query): """ Query for tickets in Trac Arguments: - `query`: A Trac query string (see `help queries` for more info) """ if not(query.strip()): print "No query specified." return tickets = self.trac.query_tickets(query) if tickets: for ticket in tickets: (id, date, mod, data) = ticket print "%5s: [%s] %s" % (id, data['status'].center(8), data['summary']) else: print "Query returned no results" do_q = do_query def do_view(self, ticket_id): """ View a specific ticket in trac Arguments: - `ticket_id`: An integer id of the ticket to view """ try: ticket = self.trac.get_ticket(int(ticket_id)) except ValueError: print "Invalid ticket nr specified." return if ticket: (id, created, modified, data) = ticket data['created'] = created data['last_modified'] = modified print "Details for Ticket: %s\n" % id for k, v in data.iteritems(): print "%15s: %s" % (k, v) else: print "Ticket %s not found" % ticket_id do_v = do_view def do_changelog(self, ticket_id): """ View the changes to a ticket Arguments: - `ticket_id`: An integer id of the ticket to view """ try: changes = self.trac.get_ticket_changelog(int(ticket_id)) except ValueError: print "Invalid ticket id specified." return print "Changelog for Ticket %s:\n" % ticket_id if changes: for change in changes: (time, author, field, old, new, pflag) = change print "%s by %s:" % (time, author) print "Changed '%s' from '%s' to '%s'\n" % (field, old, new) do_log = do_changelog def do_create(self, param_str): """ Create and submit a new ticket to Trac instance trac->> create `title` `desc` `type` ... This feature works but is still under development. Please report any bugs you find. Arguments: - `summary`: Title of the ticket """ # would like to launch a blank template tmp file # and parse the returned file try: fname = tempfile.mktemp() fh = open(fname, "w") template_lines = ["summary=%s\n" % param_str, "reporter=\n", "description=\n", "type=\n", "priority=\n", "component=\n", "milestone=\n", "version=\n", "keywords=\n"] fh.writelines(template_lines) fh.close() subprocess.call(self._editor.split() + [fname]) try: data = self.parse_ticket_file(open(fname)) except ValueError: print "Something went wrong or the file was formatted" print "wrong. Please try submitting the ticket again" print "or file a bug report with the TracShell devs." return False try: id = self.trac.create_ticket(data.pop("summary"), data.pop("description"), data) except Exception, e: print "A problem has occurred communicating with Trac." print "Error: %s" % e print "Please file a bug report with the TracShell devs." return False if id: print "Created ticket %s: %s" % (id, param_str) except Exception, e: print e print "Try `help create` for more info" pass