RBTools-0.3.4/0000755000175000017500000000000011640247307013652 5ustar chipx86chipx8600000000000000RBTools-0.3.4/rbtools/0000755000175000017500000000000011640247307015336 5ustar chipx86chipx8600000000000000RBTools-0.3.4/rbtools/postreview.py0000755000175000017500000044455211640247306020137 0ustar chipx86chipx8600000000000000#!/usr/bin/env python import base64 import cookielib import getpass import marshal import mimetools import os import re import socket import stat import subprocess import sys import tempfile import urllib import urllib2 from datetime import datetime from optparse import OptionParser from pkg_resources import parse_version from tempfile import mkstemp from urlparse import urljoin, urlparse try: from hashlib import md5 except ImportError: # Support Python versions before 2.5. from md5 import md5 try: # Specifically import json_loads, to work around some issues with # installations containing incompatible modules named "json". from json import loads as json_loads except ImportError: from simplejson import loads as json_loads # This specific import is necessary to handle the paths for # cygwin enabled machines. if (sys.platform.startswith('win') or sys.platform.startswith('cygwin')): import ntpath as cpath else: import posixpath as cpath from rbtools import get_package_version, get_version_string ### # Default configuration -- user-settable variables follow. ### # The following settings usually aren't needed, but if your Review # Board crew has specific preferences and doesn't want to express # them with command line switches, set them here and you're done. # In particular, setting the REVIEWBOARD_URL variable will allow # you to make it easy for people to submit reviews regardless of # their SCM setup. # # Note that in order for this script to work with a reviewboard site # that uses local paths to access a repository, the 'Mirror path' # in the repository setup page must be set to the remote URL of the # repository. # # Reviewboard URL. # # Set this if you wish to hard-code a default server to always use. # It's generally recommended to set this using your SCM repository # (for those that support it -- currently only SVN, Git, and Perforce). # # For example, on SVN: # $ svn propset reviewboard:url http://reviewboard.example.com . # # Or with Git: # $ git config reviewboard.url http://reviewboard.example.com # # On Perforce servers version 2008.1 and above: # $ p4 counter reviewboard.url http://reviewboard.example.com # # Older Perforce servers only allow numerical counters, so embedding # the url in the counter name is also supported: # $ p4 counter reviewboard.url.http:\|\|reviewboard.example.com 1 # # Note that slashes are not allowed in Perforce counter names, so replace them # with pipe characters (they are a safe substitute as they are not used # unencoded in URLs). You may need to escape them when issuing the p4 counter # command as above. # # If this is not possible or desired, setting the value here will let # you get started quickly. # # For all other repositories, a .reviewboardrc file present at the top of # the checkout will also work. For example: # # $ cat .reviewboardrc # REVIEWBOARD_URL = "http://reviewboard.example.com" # REVIEWBOARD_URL = None # Default submission arguments. These are all optional; run this # script with --help for descriptions of each argument. TARGET_GROUPS = None TARGET_PEOPLE = None SUBMIT_AS = None PUBLISH = False OPEN_BROWSER = False # Debugging. For development... DEBUG = False ### # End user-settable variables. ### user_config = None tempfiles = [] options = None configs = [] ADD_REPOSITORY_DOCS_URL = \ 'http://www.reviewboard.org/docs/manual/dev/admin/management/repositories/' GNU_DIFF_WIN32_URL = 'http://gnuwin32.sourceforge.net/packages/diffutils.htm' class APIError(Exception): def __init__(self, http_status, error_code, rsp=None, *args, **kwargs): Exception.__init__(self, *args, **kwargs) self.http_status = http_status self.error_code = error_code self.rsp = rsp def __str__(self): code_str = "HTTP %d" % self.http_status if self.error_code: code_str += ', API Error %d' % self.error_code if self.rsp and 'err' in self.rsp: return '%s (%s)' % (self.rsp['err']['msg'], code_str) else: return code_str class HTTPRequest(urllib2.Request): def __init__(self, url, body='', headers={}, method="PUT"): urllib2.Request.__init__(self, url, body, headers) self.method = method def get_method(self): return self.method class RepositoryInfo: """ A representation of a source code repository. """ def __init__(self, path=None, base_path=None, supports_changesets=False, supports_parent_diffs=False): self.path = path self.base_path = base_path self.supports_changesets = supports_changesets self.supports_parent_diffs = supports_parent_diffs debug("repository info: %s" % self) def __str__(self): return "Path: %s, Base path: %s, Supports changesets: %s" % \ (self.path, self.base_path, self.supports_changesets) def set_base_path(self, base_path): if not base_path.startswith('/'): base_path = '/' + base_path debug("changing repository info base_path from %s to %s" % \ (self.base_path, base_path)) self.base_path = base_path def find_server_repository_info(self, server): """ Try to find the repository from the list of repositories on the server. For Subversion, this could be a repository with a different URL. For all other clients, this is a noop. """ return self class SvnRepositoryInfo(RepositoryInfo): """ A representation of a SVN source code repository. This version knows how to find a matching repository on the server even if the URLs differ. """ def __init__(self, path, base_path, uuid, supports_parent_diffs=False): RepositoryInfo.__init__(self, path, base_path, supports_parent_diffs=supports_parent_diffs) self.uuid = uuid def find_server_repository_info(self, server): """ The point of this function is to find a repository on the server that matches self, even if the paths aren't the same. (For example, if self uses an 'http' path, but the server uses a 'file' path for the same repository.) It does this by comparing repository UUIDs. If the repositories use the same path, you'll get back self, otherwise you'll get a different SvnRepositoryInfo object (with a different path). """ repositories = server.get_repositories() for repository in repositories: if repository['tool'] != 'Subversion': continue info = self._get_repository_info(server, repository) if not info or self.uuid != info['uuid']: continue repos_base_path = info['url'][len(info['root_url']):] relpath = self._get_relative_path(self.base_path, repos_base_path) if relpath: return SvnRepositoryInfo(info['url'], relpath, self.uuid) # We didn't find a matching repository on the server. We'll just return # self and hope for the best. return self def _get_repository_info(self, server, repository): try: return server.get_repository_info(repository['id']) except APIError, e: # If the server couldn't fetch the repository info, it will return # code 210. Ignore those. # Other more serious errors should still be raised, though. if e.error_code == 210: return None raise e def _get_relative_path(self, path, root): pathdirs = self._split_on_slash(path) rootdirs = self._split_on_slash(root) # root is empty, so anything relative to that is itself if len(rootdirs) == 0: return path # If one of the directories doesn't match, then path is not relative # to root. if rootdirs != pathdirs[:len(rootdirs)]: return None # All the directories matched, so the relative path is whatever # directories are left over. The base_path can't be empty, though, so # if the paths are the same, return '/' if len(pathdirs) == len(rootdirs): return '/' else: return '/' + '/'.join(pathdirs[len(rootdirs):]) def _split_on_slash(self, path): # Split on slashes, but ignore multiple slashes and throw away any # trailing slashes. split = re.split('/*', path) if split[-1] == '': split = split[0:-1] return split class ClearCaseRepositoryInfo(RepositoryInfo): """ A representation of a ClearCase source code repository. This version knows how to find a matching repository on the server even if the URLs differ. """ def __init__(self, path, base_path, vobstag, supports_parent_diffs=False): RepositoryInfo.__init__(self, path, base_path, supports_parent_diffs=supports_parent_diffs) self.vobstag = vobstag def find_server_repository_info(self, server): """ The point of this function is to find a repository on the server that matches self, even if the paths aren't the same. (For example, if self uses an 'http' path, but the server uses a 'file' path for the same repository.) It does this by comparing VOB's name. If the repositories use the same path, you'll get back self, otherwise you'll get a different ClearCaseRepositoryInfo object (with a different path). """ # Find VOB's family uuid based on VOB's tag uuid = self._get_vobs_uuid(self.vobstag) debug("Repositorie's %s uuid is %r" % (self.vobstag, uuid)) repositories = server.get_repositories() for repository in repositories: if repository['tool'] != 'ClearCase': continue info = self._get_repository_info(server, repository) if not info or uuid != info['uuid']: continue debug('Matching repository uuid:%s with path:%s' %(uuid, info['repopath'])) return ClearCaseRepositoryInfo(info['repopath'], info['repopath'], uuid) # We didn't found uuid but if version is >= 1.5.3 # we can try to use VOB's name hoping it is better # than current VOB's path. if server.rb_version >= '1.5.3': self.path = cpath.split(self.vobstag)[1] # We didn't find a matching repository on the server. # We'll just return self and hope for the best. return self def _get_vobs_uuid(self, vobstag): """Return family uuid of VOB.""" property_lines = execute(["cleartool", "lsvob", "-long", vobstag], split_lines=True) for line in property_lines: if line.startswith('Vob family uuid:'): return line.split(' ')[-1].rstrip() def _get_repository_info(self, server, repository): try: return server.get_repository_info(repository['id']) except APIError, e: # If the server couldn't fetch the repository info, it will return # code 210. Ignore those. # Other more serious errors should still be raised, though. if e.error_code == 210: return None raise e class PresetHTTPAuthHandler(urllib2.BaseHandler): """urllib2 handler that conditionally presets the use of HTTP Basic Auth. This is used when specifying --username= on the command line. It will force an HTTP_AUTHORIZATION header with the user info, asking the user for any missing info beforehand. It will then try this header for that first request. It will only do this once. """ handler_order = 480 # After Basic auth def __init__(self, url, password_mgr): self.url = url self.password_mgr = password_mgr self.used = False def reset(self): self.password_mgr.rb_user = options.http_username self.password_mgr.rb_pass = options.http_password self.used = False def http_request(self, request): if options.username and not self.used: # Note that we call password_mgr.find_user_password to get the # username and password we're working with. This allows us to # prompt if, say, --username was specified but --password was not. username, password = \ self.password_mgr.find_user_password('Web API', self.url) raw = '%s:%s' % (username, password) request.add_header( urllib2.HTTPBasicAuthHandler.auth_header, 'Basic %s' % base64.b64encode(raw).strip()) self.used = True return request https_request = http_request class ReviewBoardHTTPErrorProcessor(urllib2.HTTPErrorProcessor): """Processes HTTP error codes. Python 2.6 gets HTTP error code processing right, but 2.4 and 2.5 only accepts HTTP 200 and 206 as success codes. This handler ensures that anything in the 200 range is a success. """ def http_response(self, request, response): if not (200 <= response.code < 300): response = self.parent.error('http', request, response, response.code, response.msg, response.info()) return response https_response = http_response class ReviewBoardHTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler): """Custom Basic Auth handler that doesn't retry excessively. urllib2's HTTPBasicAuthHandler retries over and over, which is useless. This subclass only retries once to make sure we've attempted with a valid username and password. It will then fail so we can use tempt_fate's retry handler. """ def __init__(self, *args, **kwargs): urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs) self._retried = False self._lasturl = "" def retry_http_basic_auth(self, *args, **kwargs): if self._lasturl != args[0]: self._retried = False self._lasturl = args[0] if not self._retried: self._retried = True self.retried = 0 response = urllib2.HTTPBasicAuthHandler.retry_http_basic_auth( self, *args, **kwargs) if response.code != 401: self._retried = False return response else: return None class ReviewBoardHTTPPasswordMgr(urllib2.HTTPPasswordMgr): """ Adds HTTP authentication support for URLs. Python 2.4's password manager has a bug in http authentication when the target server uses a non-standard port. This works around that bug on Python 2.4 installs. This also allows post-review to prompt for passwords in a consistent way. See: http://bugs.python.org/issue974757 """ def __init__(self, reviewboard_url, rb_user=None, rb_pass=None): self.passwd = {} self.rb_url = reviewboard_url self.rb_user = rb_user self.rb_pass = rb_pass def find_user_password(self, realm, uri): if uri.startswith(self.rb_url): if self.rb_user is None or self.rb_pass is None: if options.diff_filename == '-': die('HTTP authentication is required, but cannot be ' 'used with --diff-filename=-') print "==> HTTP Authentication Required" print 'Enter authorization information for "%s" at %s' % \ (realm, urlparse(uri)[1]) if not self.rb_user: self.rb_user = raw_input('Username: ') if not self.rb_pass: self.rb_pass = getpass.getpass('Password: ') return self.rb_user, self.rb_pass else: # If this is an auth request for some other domain (since HTTP # handlers are global), fall back to standard password management. return urllib2.HTTPPasswordMgr.find_user_password(self, realm, uri) class ReviewBoardServer(object): """ An instance of a Review Board server. """ def __init__(self, url, info, cookie_file): self.url = url if self.url[-1] != '/': self.url += '/' self._info = info self._server_info = None self.root_resource = None self.deprecated_api = False self.cookie_file = cookie_file self.cookie_jar = cookielib.MozillaCookieJar(self.cookie_file) if self.cookie_file: try: self.cookie_jar.load(self.cookie_file, ignore_expires=True) except IOError: pass # Set up the HTTP libraries to support all of the features we need. cookie_handler = urllib2.HTTPCookieProcessor(self.cookie_jar) password_mgr = ReviewBoardHTTPPasswordMgr(self.url, options.username, options.password) basic_auth_handler = ReviewBoardHTTPBasicAuthHandler(password_mgr) digest_auth_handler = urllib2.HTTPDigestAuthHandler(password_mgr) self.preset_auth_handler = PresetHTTPAuthHandler(self.url, password_mgr) http_error_processor = ReviewBoardHTTPErrorProcessor() opener = urllib2.build_opener(cookie_handler, basic_auth_handler, digest_auth_handler, self.preset_auth_handler, http_error_processor) opener.addheaders = [('User-agent', 'RBTools/' + get_package_version())] urllib2.install_opener(opener) def check_api_version(self): """Checks the API version on the server to determine which to use.""" try: root_resource = self.api_get('api/') rsp = self.api_get(root_resource['links']['info']['href']) self.rb_version = rsp['info']['product']['package_version'] if parse_version(self.rb_version) >= parse_version('1.5.2'): self.deprecated_api = False self.root_resource = root_resource debug('Using the new web API') return True except APIError, e: if e.http_status not in (401, 404): # We shouldn't reach this. If there's a permission denied # from lack of logging in, then the basic auth handler # should have hit it. # # However in some versions it wants you to be logged in # and returns a 401 from the application after you've # done your http basic auth die("Unable to access the root /api/ URL on the server.") return False # This is an older Review Board server with the old API. self.deprecated_api = True debug('Using the deprecated Review Board 1.0 web API') return True def login(self, force=False): """ Logs in to a Review Board server, prompting the user for login information if needed. """ if (options.diff_filename == '-' and not options.username and not options.submit_as and not options.password): die('Authentication information needs to be provided on ' 'the command line when using --diff-filename=-') if self.deprecated_api: print "==> Review Board Login Required" print "Enter username and password for Review Board at %s" % \ self.url if options.username: username = options.username elif options.submit_as: username = options.submit_as elif not force and self.has_valid_cookie(): # We delay the check for a valid cookie until after looking # at args, so that it doesn't override the command line. return else: username = raw_input('Username: ') if not options.password: password = getpass.getpass('Password: ') else: password = options.password debug('Logging in with username "%s"' % username) try: self.api_post('api/json/accounts/login/', { 'username': username, 'password': password, }) except APIError, e: die("Unable to log in: %s" % e) debug("Logged in.") elif force: self.preset_auth_handler.reset() def has_valid_cookie(self): """ Load the user's cookie file and see if they have a valid 'rbsessionid' cookie for the current Review Board server. Returns true if so and false otherwise. """ try: parsed_url = urlparse(self.url) host = parsed_url[1] path = parsed_url[2] or '/' # Cookie files don't store port numbers, unfortunately, so # get rid of the port number if it's present. host = host.split(":")[0] # Cookie files also append .local to bare hostnames if '.' not in host: host += '.local' debug("Looking for '%s %s' cookie in %s" % \ (host, path, self.cookie_file)) try: cookie = self.cookie_jar._cookies[host][path]['rbsessionid'] if not cookie.is_expired(): debug("Loaded valid cookie -- no login required") return True debug("Cookie file loaded, but cookie has expired") except KeyError: debug("Cookie file loaded, but no cookie for this server") except IOError, error: debug("Couldn't load cookie file: %s" % error) return False def get_configured_repository(self): for config in configs: if 'REPOSITORY' in config: return config['REPOSITORY'] return None def new_review_request(self, changenum, submit_as=None): """ Creates a review request on a Review Board server, updating an existing one if the changeset number already exists. If submit_as is provided, the specified user name will be recorded as the submitter of the review request (given that the logged in user has the appropriate permissions). """ # If repository_path is a list, find a name in the list that's # registered on the server. if isinstance(self.info.path, list): repositories = self.get_repositories() debug("Repositories on Server: %s" % repositories) debug("Server Aliases: %s" % self.info.path) for repository in repositories: if repository['path'] in self.info.path: self.info.path = repository['path'] break if isinstance(self.info.path, list): sys.stderr.write('\n') sys.stderr.write('There was an error creating this review ' 'request.\n') sys.stderr.write('\n') sys.stderr.write('There was no matching repository path' 'found on the server.\n') sys.stderr.write('List of configured repositories:\n') for repository in repositories: sys.stderr.write('\t%s\n' % repository['path']) sys.stderr.write('Unknown repository paths found:\n') for foundpath in self.info.path: sys.stderr.write('\t%s\n' % foundpath) sys.stderr.write('Ask the administrator to add one of ' 'these repositories\n') sys.stderr.write('to the Review Board server.\n') sys.stderr.write('For information on adding repositories, ' 'please read\n') sys.stderr.write(ADD_REPOSITORY_DOCS_URL + '\n') die() repository = options.repository_url \ or self.get_configured_repository() \ or self.info.path try: debug("Attempting to create review request on %s for %s" % (repository, changenum)) data = {} if changenum: data['changenum'] = changenum if submit_as: debug("Submitting the review request as %s" % submit_as) data['submit_as'] = submit_as if self.deprecated_api: data['repository_path'] = repository rsp = self.api_post('api/json/reviewrequests/new/', data) else: data['repository'] = repository links = self.root_resource['links'] assert 'review_requests' in links review_request_href = links['review_requests']['href'] rsp = self.api_post(review_request_href, data) except APIError, e: if e.error_code == 204: # Change number in use rsp = e.rsp if options.diff_only: # In this case, fall through and return to tempt_fate. debug("Review request already exists.") else: debug("Review request already exists. Updating it...") self.update_review_request_from_changenum( changenum, rsp['review_request']) elif e.error_code == 206: # Invalid repository sys.stderr.write('\n') sys.stderr.write('There was an error creating this review ' 'request.\n') sys.stderr.write('\n') sys.stderr.write('The repository path "%s" is not in the\n' % self.info.path) sys.stderr.write('list of known repositories on the server.\n') sys.stderr.write('\n') sys.stderr.write('Ask the administrator to add this ' 'repository to the Review Board server.\n') sys.stderr.write('For information on adding repositories, ' 'please read\n') sys.stderr.write(ADD_REPOSITORY_DOCS_URL + '\n') die() else: raise e else: debug("Review request created") return rsp['review_request'] def update_review_request_from_changenum(self, changenum, review_request): if self.deprecated_api: self.api_post( 'api/json/reviewrequests/%s/update_from_changenum/' % review_request['id']) else: self.api_put(review_request['links']['self']['href'], { 'changenum': review_request['changenum'], }) def set_review_request_field(self, review_request, field, value): """ Sets a field in a review request to the specified value. """ rid = review_request['id'] debug("Attempting to set field '%s' to '%s' for review request '%s'" % (field, value, rid)) if self.deprecated_api: self.api_post('api/json/reviewrequests/%s/draft/set/' % rid, { field: value, }) else: self.api_put(review_request['links']['draft']['href'], { field: value, }) def get_review_request(self, rid): """ Returns the review request with the specified ID. """ if self.deprecated_api: url = 'api/json/reviewrequests/%s/' % rid else: url = '%s%s/' % ( self.root_resource['links']['review_requests']['href'], rid) rsp = self.api_get(url) return rsp['review_request'] def get_repositories(self): """ Returns the list of repositories on this server. """ if self.deprecated_api: rsp = self.api_get('api/json/repositories/') repositories = rsp['repositories'] else: rsp = self.api_get( self.root_resource['links']['repositories']['href']) repositories = rsp['repositories'] while 'next' in rsp['links']: rsp = self.api_get(rsp['links']['next']['href']) repositories.extend(rsp['repositories']) return repositories def get_repository_info(self, rid): """ Returns detailed information about a specific repository. """ if self.deprecated_api: url = 'api/json/repositories/%s/info/' % rid else: rsp = self.api_get( '%s%s/' % (self.root_resource['links']['repositories']['href'], rid)) url = rsp['repository']['links']['info']['href'] rsp = self.api_get(url) return rsp['info'] def save_draft(self, review_request): """ Saves a draft of a review request. """ if self.deprecated_api: self.api_post('api/json/reviewrequests/%s/draft/save/' % \ review_request['id']) else: self.api_put(review_request['links']['draft']['href'], { 'public': 1, }) debug("Review request draft saved") def upload_diff(self, review_request, diff_content, parent_diff_content): """ Uploads a diff to a Review Board server. """ debug("Uploading diff, size: %d" % len(diff_content)) if parent_diff_content: debug("Uploading parent diff, size: %d" % len(parent_diff_content)) fields = {} files = {} if self.info.base_path: fields['basedir'] = self.info.base_path files['path'] = { 'filename': 'diff', 'content': diff_content } if parent_diff_content: files['parent_diff_path'] = { 'filename': 'parent_diff', 'content': parent_diff_content } if self.deprecated_api: self.api_post('api/json/reviewrequests/%s/diff/new/' % review_request['id'], fields, files) else: self.api_post(review_request['links']['diffs']['href'], fields, files) def reopen(self, review_request): """ Reopen discarded review request. """ debug("Reopening") if self.deprecated_api: self.api_post('api/json/reviewrequests/%s/reopen/' % review_request['id']) else: self.api_put(review_request['links']['self']['href'], { 'status': 'pending', }) def publish(self, review_request): """ Publishes a review request. """ debug("Publishing") if self.deprecated_api: self.api_post('api/json/reviewrequests/%s/publish/' % review_request['id']) else: self.api_put(review_request['links']['draft']['href'], { 'public': 1, }) def _get_server_info(self): if not self._server_info: self._server_info = self._info.find_server_repository_info(self) return self._server_info info = property(_get_server_info) def process_json(self, data): """ Loads in a JSON file and returns the data if successful. On failure, APIError is raised. """ rsp = json_loads(data) if rsp['stat'] == 'fail': # With the new API, we should get something other than HTTP # 200 for errors, in which case we wouldn't get this far. assert self.deprecated_api self.process_error(200, data) return rsp def process_error(self, http_status, data): """Processes an error, raising an APIError with the information.""" try: rsp = json_loads(data) assert rsp['stat'] == 'fail' debug("Got API Error %d (HTTP code %d): %s" % (rsp['err']['code'], http_status, rsp['err']['msg'])) debug("Error data: %r" % rsp) raise APIError(http_status, rsp['err']['code'], rsp, rsp['err']['msg']) except ValueError: debug("Got HTTP error: %s: %s" % (http_status, data)) raise APIError(http_status, None, None, data) def http_get(self, path): """ Performs an HTTP GET on the specified path, storing any cookies that were set. """ debug('HTTP GETting %s' % path) url = self._make_url(path) rsp = urllib2.urlopen(url).read() try: self.cookie_jar.save(self.cookie_file) except IOError, e: debug('Failed to write cookie file: %s' % e) return rsp def _make_url(self, path): """Given a path on the server returns a full http:// style url""" if path.startswith('http'): # This is already a full path. return path app = urlparse(self.url)[2] if path[0] == '/': url = urljoin(self.url, app[:-1] + path) else: url = urljoin(self.url, app + path) if not url.startswith('http'): url = 'http://%s' % url return url def api_get(self, path): """ Performs an API call using HTTP GET at the specified path. """ try: return self.process_json(self.http_get(path)) except urllib2.HTTPError, e: self.process_error(e.code, e.read()) def http_post(self, path, fields, files=None): """ Performs an HTTP POST on the specified path, storing any cookies that were set. """ if fields: debug_fields = fields.copy() else: debug_fields = {} if 'password' in debug_fields: debug_fields["password"] = "**************" url = self._make_url(path) debug('HTTP POSTing to %s: %s' % (url, debug_fields)) content_type, body = self._encode_multipart_formdata(fields, files) headers = { 'Content-Type': content_type, 'Content-Length': str(len(body)) } try: r = urllib2.Request(str(url), body, headers) data = urllib2.urlopen(r).read() try: self.cookie_jar.save(self.cookie_file) except IOError, e: debug('Failed to write cookie file: %s' % e) return data except urllib2.HTTPError, e: # Re-raise so callers can interpret it. raise e except urllib2.URLError, e: try: debug(e.read()) except AttributeError: pass die("Unable to access %s. The host path may be invalid\n%s" % \ (url, e)) def http_put(self, path, fields): """ Performs an HTTP PUT on the specified path, storing any cookies that were set. """ url = self._make_url(path) debug('HTTP PUTting to %s: %s' % (url, fields)) content_type, body = self._encode_multipart_formdata(fields, None) headers = { 'Content-Type': content_type, 'Content-Length': str(len(body)) } try: r = HTTPRequest(url, body, headers, method='PUT') data = urllib2.urlopen(r).read() self.cookie_jar.save(self.cookie_file) return data except urllib2.HTTPError, e: # Re-raise so callers can interpret it. raise e except urllib2.URLError, e: try: debug(e.read()) except AttributeError: pass die("Unable to access %s. The host path may be invalid\n%s" % \ (url, e)) def http_delete(self, path): """ Performs an HTTP DELETE on the specified path, storing any cookies that were set. """ url = self._make_url(path) debug('HTTP DELETing %s' % url) try: r = HTTPRequest(url, method='DELETE') data = urllib2.urlopen(r).read() self.cookie_jar.save(self.cookie_file) return data except urllib2.HTTPError, e: # Re-raise so callers can interpret it. raise e except urllib2.URLError, e: try: debug(e.read()) except AttributeError: pass die("Unable to access %s. The host path may be invalid\n%s" % \ (url, e)) def api_post(self, path, fields=None, files=None): """ Performs an API call using HTTP POST at the specified path. """ try: return self.process_json(self.http_post(path, fields, files)) except urllib2.HTTPError, e: self.process_error(e.code, e.read()) def api_put(self, path, fields=None): """ Performs an API call using HTTP PUT at the specified path. """ try: return self.process_json(self.http_put(path, fields)) except urllib2.HTTPError, e: self.process_error(e.code, e.read()) def api_delete(self, path): """ Performs an API call using HTTP DELETE at the specified path. """ try: return self.process_json(self.http_delete(path)) except urllib2.HTTPError, e: self.process_error(e.code, e.read()) def _encode_multipart_formdata(self, fields, files): """ Encodes data for use in an HTTP POST. """ BOUNDARY = mimetools.choose_boundary() content = "" fields = fields or {} files = files or {} for key in fields: content += "--" + BOUNDARY + "\r\n" content += "Content-Disposition: form-data; name=\"%s\"\r\n" % key content += "\r\n" content += str(fields[key]) + "\r\n" for key in files: filename = files[key]['filename'] value = files[key]['content'] content += "--" + BOUNDARY + "\r\n" content += "Content-Disposition: form-data; name=\"%s\"; " % key content += "filename=\"%s\"\r\n" % filename content += "\r\n" content += value + "\r\n" content += "--" + BOUNDARY + "--\r\n" content += "\r\n" content_type = "multipart/form-data; boundary=%s" % BOUNDARY return content_type, content class SCMClient(object): """ A base representation of an SCM tool for fetching repository information and generating diffs. """ def get_repository_info(self): return None def check_options(self): pass def scan_for_server(self, repository_info): """ Scans the current directory on up to find a .reviewboard file containing the server path. """ server_url = None if user_config: server_url = self._get_server_from_config(user_config, repository_info) if not server_url: for config in configs: server_url = self._get_server_from_config(config, repository_info) if server_url: break return server_url def diff(self, args): """ Returns the generated diff and optional parent diff for this repository. The returned tuple is (diff_string, parent_diff_string) """ return (None, None) def diff_between_revisions(self, revision_range, args, repository_info): """ Returns the generated diff between revisions in the repository. """ return (None, None) def _get_server_from_config(self, config, repository_info): if 'REVIEWBOARD_URL' in config: return config['REVIEWBOARD_URL'] elif 'TREES' in config: trees = config['TREES'] if not isinstance(trees, dict): die("Warning: 'TREES' in config file is not a dict!") # If repository_info is a list, check if any one entry is in trees. path = None if isinstance(repository_info.path, list): for path in repository_info.path: if path in trees: break else: path = None elif repository_info.path in trees: path = repository_info.path if path and 'REVIEWBOARD_URL' in trees[path]: return trees[path]['REVIEWBOARD_URL'] return None class CVSClient(SCMClient): """ A wrapper around the cvs tool that fetches repository information and generates compatible diffs. """ def get_repository_info(self): if not check_install("cvs"): return None cvsroot_path = os.path.join("CVS", "Root") if not os.path.exists(cvsroot_path): return None fp = open(cvsroot_path, "r") repository_path = fp.read().strip() fp.close() i = repository_path.find("@") if i != -1: repository_path = repository_path[i + 1:] i = repository_path.rfind(":") if i != -1: host = repository_path[:i] try: canon = socket.getfqdn(host) repository_path = repository_path.replace('%s:' % host, '%s:' % canon) except socket.error, msg: debug("failed to get fqdn for %s, msg=%s" % (host, msg)) return RepositoryInfo(path=repository_path) def diff(self, files): """ Performs a diff across all modified files in a CVS repository. CVS repositories do not support branches of branches in a way that makes parent diffs possible, so we never return a parent diff (the second value in the tuple). """ return (self.do_diff(files), None) def diff_between_revisions(self, revision_range, args, repository_info): """ Performs a diff between 2 revisions of a CVS repository. """ revs = [] for rev in revision_range.split(":"): revs += ["-r", rev] return (self.do_diff(revs + args), None) def do_diff(self, params): """ Performs the actual diff operation through cvs diff, handling fake errors generated by CVS. """ # Diff returns "1" if differences were found. return execute(["cvs", "diff", "-uN"] + params, extra_ignore_errors=(1,)) class ClearCaseClient(SCMClient): """ A wrapper around the clearcase tool that fetches repository information and generates compatible diffs. This client assumes that cygwin is installed on windows. """ viewtype = None def get_repository_info(self): """Returns information on the Clear Case repository. This will first check if the cleartool command is installed and in the path, and post-review was run from inside of the view. """ if not check_install('cleartool help'): return None viewname = execute(["cleartool", "pwv", "-short"]).strip() if viewname.startswith('** NONE'): return None # Now that we know it's ClearCase, make sure we have GNU diff installed, # and error out if we don't. check_gnu_diff() property_lines = execute(["cleartool", "lsview", "-full", "-properties", "-cview"], split_lines=True) for line in property_lines: properties = line.split(' ') if properties[0] == 'Properties:': # Determine the view type and check if it's supported. # # Specifically check if webview was listed in properties # because webview types also list the 'snapshot' # entry in properties. if 'webview' in properties: die("Webviews are not supported. You can use post-review" " only in dynamic or snapshot view.") if 'dynamic' in properties: self.viewtype = 'dynamic' else: self.viewtype = 'snapshot' break # Find current VOB's tag vobstag = execute(["cleartool", "describe", "-short", "vob:."], ignore_errors=True).strip() if "Error: " in vobstag: die("To generate diff run post-review inside vob.") # From current working directory cut path to VOB. # VOB's tag contain backslash character before VOB's name. # I hope that first character of VOB's tag like '\new_proj' # won't be treat as new line character but two separate: # backslash and letter 'n' cwd = os.getcwd() base_path = cwd[:cwd.find(vobstag) + len(vobstag)] return ClearCaseRepositoryInfo(path=base_path, base_path=base_path, vobstag=vobstag, supports_parent_diffs=False) def check_options(self): if ((options.revision_range or options.tracking) and self.viewtype != "dynamic"): die("To generate diff using parent branch or by passing revision " "ranges, you must use a dynamic view.") def _determine_version(self, version_path): """Determine numeric version of revision. CHECKEDOUT is marked as infinity to be treated always as highest possible version of file. CHECKEDOUT, in ClearCase, is something like HEAD. """ branch, number = cpath.split(version_path) if number == 'CHECKEDOUT': return float('inf') return int(number) def _construct_extended_path(self, path, version): """Combine extended_path from path and version. CHECKEDOUT must be removed becasue this one version doesn't exists in MVFS (ClearCase dynamic view file system). Only way to get content of checked out file is to use filename only.""" if not version or version.endswith('CHECKEDOUT'): return path return "%s@@%s" % (path, version) def _sanitize_branch_changeset(self, changeset): """Return changeset containing non-binary, branched file versions. Changeset contain only first and last version of file made on branch. """ changelist = {} for path, previous, current in changeset: version_number = self._determine_version(current) if path not in changelist: changelist[path] = { 'highest': version_number, 'current': current, 'previous': previous } if version_number == 0: # Previous version of 0 version on branch is base changelist[path]['previous'] = previous elif version_number > changelist[path]['highest']: changelist[path]['highest'] = version_number changelist[path]['current'] = current # Convert to list changeranges = [] for path, version in changelist.iteritems(): changeranges.append( (self._construct_extended_path(path, version['previous']), self._construct_extended_path(path, version['current'])) ) return changeranges def _sanitize_checkedout_changeset(self, changeset): """Return changeset containing non-binary, checkdout file versions.""" changeranges = [] for path, previous, current in changeset: version_number = self._determine_version(current) changeranges.append( (self._construct_extended_path(path, previous), self._construct_extended_path(path, current)) ) return changeranges def _directory_content(self, path): """Return directory content ready for saving to tempfile.""" return ''.join([ '%s\n' % s for s in sorted(os.listdir(path)) ]) def _construct_changeset(self, output): return [ info.split('\t') for info in output.strip().split('\n') ] def get_checkedout_changeset(self): """Return information about the checked out changeset. This function returns: kind of element, path to file, previews and current file version. """ changeset = [] # We ignore return code 1 in order to # omit files that Clear Case can't read. output = execute([ "cleartool", "lscheckout", "-all", "-cview", "-me", "-fmt", r"%En\t%PVn\t%Vn\n"], extra_ignore_errors=(1,), with_errors=False) if output: changeset = self._construct_changeset(output) return self._sanitize_checkedout_changeset(changeset) def get_branch_changeset(self, branch): """Returns information about the versions changed on a branch. This takes into account the changes on the branch owned by the current user in all vobs of the current view. """ changeset = [] # We ignore return code 1 in order to # omit files that Clear Case can't read. if sys.platform.startswith('win'): CLEARCASE_XPN = '%CLEARCASE_XPN%' else: CLEARCASE_XPN = '$CLEARCASE_XPN' output = execute([ "cleartool", "find", "-all", "-version", "brtype(%s)" % branch, "-exec", 'cleartool descr -fmt ' \ r'"%En\t%PVn\t%Vn\n" ' \ + CLEARCASE_XPN], extra_ignore_errors=(1,), with_errors=False) if output: changeset = self._construct_changeset(output) return self._sanitize_branch_changeset(changeset) def diff(self, files): """Performs a diff of the specified file and its previous version.""" if options.tracking: changeset = self.get_branch_changeset(options.tracking) else: changeset = self.get_checkedout_changeset() return self.do_diff(changeset) def diff_between_revisions(self, revision_range, args, repository_info): """Performs a diff between passed revisions or branch.""" # Convert revision range to list of: # (previous version, current version) tuples revision_range = revision_range.split(';') changeset = zip(revision_range[0::2], revision_range[1::2]) return (self.do_diff(changeset)[0], None) def diff_files(self, old_file, new_file): """Return unified diff for file. Most effective and reliable way is use gnu diff. """ diff_cmd = ["diff", "-uN", old_file, new_file] dl = execute(diff_cmd, extra_ignore_errors=(1,2), translate_newlines=False) # If the input file has ^M characters at end of line, lets ignore them. dl = dl.replace('\r\r\n', '\r\n') dl = dl.splitlines(True) # Special handling for the output of the diff tool on binary files: # diff outputs "Files a and b differ" # and the code below expects the output to start with # "Binary files " if (len(dl) == 1 and dl[0].startswith('Files %s and %s differ' % (old_file, new_file))): dl = ['Binary files %s and %s differ\n' % (old_file, new_file)] # We need oids of files to translate them to paths on reviewboard repository old_oid = execute(["cleartool", "describe", "-fmt", "%On", old_file]) new_oid = execute(["cleartool", "describe", "-fmt", "%On", new_file]) if dl == [] or dl[0].startswith("Binary files "): if dl == []: dl = ["File %s in your changeset is unmodified\n" % new_file] dl.insert(0, "==== %s %s ====\n" % (old_oid, new_oid)) dl.append('\n') else: dl.insert(2, "==== %s %s ====\n" % (old_oid, new_oid)) return dl def diff_directories(self, old_dir, new_dir): """Return uniffied diff between two directories content. Function save two version's content of directory to temp files and treate them as casual diff between two files. """ old_content = self._directory_content(old_dir) new_content = self._directory_content(new_dir) old_tmp = make_tempfile(content=old_content) new_tmp = make_tempfile(content=new_content) diff_cmd = ["diff", "-uN", old_tmp, new_tmp] dl = execute(diff_cmd, extra_ignore_errors=(1,2), translate_newlines=False, split_lines=True) # Replacing temporary filenames to # real directory names and add ids if dl: dl[0] = dl[0].replace(old_tmp, old_dir) dl[1] = dl[1].replace(new_tmp, new_dir) old_oid = execute(["cleartool", "describe", "-fmt", "%On", old_dir]) new_oid = execute(["cleartool", "describe", "-fmt", "%On", new_dir]) dl.insert(2, "==== %s %s ====\n" % (old_oid, new_oid)) return dl def do_diff(self, changeset): """Generates a unified diff for all files in the changeset.""" diff = [] for old_file, new_file in changeset: dl = [] if cpath.isdir(new_file): dl = self.diff_directories(old_file, new_file) elif cpath.exists(new_file): dl = self.diff_files(old_file, new_file) else: debug("File %s does not exist or access is denied." % new_file) continue if dl: diff.append(''.join(dl)) return (''.join(diff), None) class SVNClient(SCMClient): # Match the diff control lines generated by 'svn diff'. DIFF_ORIG_FILE_LINE_RE = re.compile(r'^---\s+.*\s+\(.*\)') DIFF_NEW_FILE_LINE_RE = re.compile(r'^\+\+\+\s+.*\s+\(.*\)') """ A wrapper around the svn Subversion tool that fetches repository information and generates compatible diffs. """ def get_repository_info(self): if not check_install('svn help'): return None # Get the SVN repository path (either via a working copy or # a supplied URI) svn_info_params = ["svn", "info"] if options.repository_url: svn_info_params.append(options.repository_url) data = execute(svn_info_params, ignore_errors=True) m = re.search(r'^Repository Root: (.+)$', data, re.M) if not m: return None path = m.group(1) m = re.search(r'^URL: (.+)$', data, re.M) if not m: return None base_path = m.group(1)[len(path):] or "/" m = re.search(r'^Repository UUID: (.+)$', data, re.M) if not m: return None # Now that we know it's SVN, make sure we have GNU diff installed, # and error out if we don't. check_gnu_diff() return SvnRepositoryInfo(path, base_path, m.group(1)) def check_options(self): if (options.repository_url and not options.revision_range and not options.diff_filename): sys.stderr.write("The --repository-url option requires either the " "--revision-range option or the --diff-filename " "option.\n") sys.exit(1) def scan_for_server(self, repository_info): # Scan first for dot files, since it's faster and will cover the # user's $HOME/.reviewboardrc server_url = super(SVNClient, self).scan_for_server(repository_info) if server_url: return server_url return self.scan_for_server_property(repository_info) def scan_for_server_property(self, repository_info): def get_url_prop(path): url = execute(["svn", "propget", "reviewboard:url", path]).strip() return url or None for path in walk_parents(os.getcwd()): if not os.path.exists(os.path.join(path, ".svn")): break prop = get_url_prop(path) if prop: return prop return get_url_prop(repository_info.path) def diff(self, files): """ Performs a diff across all modified files in a Subversion repository. SVN repositories do not support branches of branches in a way that makes parent diffs possible, so we never return a parent diff (the second value in the tuple). """ return (self.do_diff(["svn", "diff", "--diff-cmd=diff"] + files), None) def diff_changelist(self, changelist): """ Performs a diff for a local changelist. """ return (self.do_diff(["svn", "diff", "--changelist", changelist]), None) def diff_between_revisions(self, revision_range, args, repository_info): """ Performs a diff between 2 revisions of a Subversion repository. """ if options.repository_url: revisions = revision_range.split(':') if len(revisions) < 1: return None elif len(revisions) == 1: revisions.append('HEAD') # if a new path was supplied at the command line, set it files = [] if len(args) == 1: repository_info.set_base_path(args[0]) elif len(args) > 1: files = args url = repository_info.path + repository_info.base_path new_url = url + '@' + revisions[1] # When the source revision is zero, assume the user wants to # upload a diff containing all the files in ``base_path`` as new # files. If the base path within the repository is added to both # the old and new URLs, the ``svn diff`` command will error out # since the base_path didn't exist at revision zero. To avoid # that error, use the repository's root URL as the source for # the diff. if revisions[0] == "0": url = repository_info.path old_url = url + '@' + revisions[0] return (self.do_diff(["svn", "diff", "--diff-cmd=diff", old_url, new_url] + files, repository_info), None) # Otherwise, perform the revision range diff using a working copy else: return (self.do_diff(["svn", "diff", "--diff-cmd=diff", "-r", revision_range], repository_info), None) def do_diff(self, cmd, repository_info=None): """ Performs the actual diff operation, handling renames and converting paths to absolute. """ diff = execute(cmd, split_lines=True) diff = self.handle_renames(diff) diff = self.convert_to_absolute_paths(diff, repository_info) return ''.join(diff) def handle_renames(self, diff_content): """ The output of svn diff is incorrect when the file in question came into being via svn mv/cp. Although the patch for these files are relative to its parent, the diff header doesn't reflect this. This function fixes the relevant section headers of the patch to portray this relationship. """ # svn diff against a repository URL on two revisions appears to # handle moved files properly, so only adjust the diff file names # if they were created using a working copy. if options.repository_url: return diff_content result = [] from_line = "" for line in diff_content: if self.DIFF_ORIG_FILE_LINE_RE.match(line): from_line = line continue # This is where we decide how mangle the previous '--- ' if self.DIFF_NEW_FILE_LINE_RE.match(line): to_file, _ = self.parse_filename_header(line[4:]) info = self.svn_info(to_file) if info.has_key("Copied From URL"): url = info["Copied From URL"] root = info["Repository Root"] from_file = urllib.unquote(url[len(root):]) result.append(from_line.replace(to_file, from_file)) else: result.append(from_line) #as is, no copy performed # We only mangle '---' lines. All others get added straight to # the output. result.append(line) return result def convert_to_absolute_paths(self, diff_content, repository_info): """ Converts relative paths in a diff output to absolute paths. This handles paths that have been svn switched to other parts of the repository. """ result = [] for line in diff_content: front = None if (self.DIFF_NEW_FILE_LINE_RE.match(line) or self.DIFF_ORIG_FILE_LINE_RE.match(line) or line.startswith('Index: ')): front, line = line.split(" ", 1) if front: if line.startswith('/'): #already absolute line = front + " " + line else: # filename and rest of line (usually the revision # component) file, rest = self.parse_filename_header(line) # If working with a diff generated outside of a working # copy, then file paths are already absolute, so just # add initial slash. if options.repository_url: path = urllib.unquote( "%s/%s" % (repository_info.base_path, file)) else: info = self.svn_info(file) url = info["URL"] root = info["Repository Root"] path = urllib.unquote(url[len(root):]) line = front + " " + path + rest result.append(line) return result def svn_info(self, path): """Return a dict which is the result of 'svn info' at a given path.""" svninfo = {} for info in execute(["svn", "info", path], split_lines=True): parts = info.strip().split(": ", 1) if len(parts) == 2: key, value = parts svninfo[key] = value return svninfo # Adapted from server code parser.py def parse_filename_header(self, s): parts = None if "\t" in s: # There's a \t separating the filename and info. This is the # best case scenario, since it allows for filenames with spaces # without much work. The info can also contain tabs after the # initial one; ignore those when splitting the string. parts = s.split("\t", 1) # There's spaces being used to separate the filename and info. # This is technically wrong, so all we can do is assume that # 1) the filename won't have multiple consecutive spaces, and # 2) there's at least 2 spaces separating the filename and info. if " " in s: parts = re.split(r" +", s) if parts: parts[1] = '\t' + parts[1] return parts # strip off ending newline, and return it as the second component return [s.split('\n')[0], '\n'] class PerforceClient(SCMClient): """ A wrapper around the p4 Perforce tool that fetches repository information and generates compatible diffs. """ def get_repository_info(self): if not check_install('p4 help'): return None data = execute(["p4", "info"], ignore_errors=True) m = re.search(r'^Server address: (.+)$', data, re.M) if not m: return None repository_path = m.group(1).strip() try: hostname, port = repository_path.split(":") info = socket.gethostbyaddr(hostname) # If aliases exist for hostname, create a list of alias:port # strings for repository_path. if info[1]: servers = [info[0]] + info[1] repository_path = ["%s:%s" % (server, port) for server in servers] else: repository_path = "%s:%s" % (info[0], port) except (socket.gaierror, socket.herror): pass m = re.search(r'^Server version: [^ ]*/([0-9]+)\.([0-9]+)/[0-9]+ .*$', data, re.M) self.p4d_version = int(m.group(1)), int(m.group(2)) return RepositoryInfo(path=repository_path, supports_changesets=True) def scan_for_server(self, repository_info): # Scan first for dot files, since it's faster and will cover the # user's $HOME/.reviewboardrc server_url = \ super(PerforceClient, self).scan_for_server(repository_info) if server_url: return server_url return self.scan_for_server_counter(repository_info) def scan_for_server_counter(self, repository_info): """ Checks the Perforce counters to see if the Review Board server's url is specified. Since Perforce only started supporting non-numeric counter values in server version 2008.1, we support both a normal counter 'reviewboard.url' with a string value and embedding the url in a counter name like 'reviewboard.url.http:||reviewboard.example.com'. Note that forward slashes aren't allowed in counter names, so pipe ('|') characters should be used. These should be safe because they should not be used unencoded in urls. """ counters_text = execute(["p4", "counters"]) # Try for a "reviewboard.url" counter first. m = re.search(r'^reviewboard.url = (\S+)', counters_text, re.M) if m: return m.group(1) # Next try for a counter of the form: # reviewboard_url.http:||reviewboard.example.com m2 = re.search(r'^reviewboard.url\.(\S+)', counters_text, re.M) if m2: return m2.group(1).replace('|', '/') return None def get_changenum(self, args): if len(args) == 0: return "default" elif len(args) == 1: if args[0] == "default": return "default" try: return str(int(args[0])) except ValueError: # (if it isn't a number, it can't be a cln) return None # there are multiple args (not a cln) else: return None def diff(self, args): """ Goes through the hard work of generating a diff on Perforce in order to take into account adds/deletes and to provide the necessary revision information. """ # set the P4 enviroment: if options.p4_client: os.environ['P4CLIENT'] = options.p4_client if options.p4_port: os.environ['P4PORT'] = options.p4_port if options.p4_passwd: os.environ['P4PASSWD'] = options.p4_passwd changenum = self.get_changenum(args) if changenum is None: return self._path_diff(args) else: return self._changenum_diff(changenum) def _path_diff(self, args): """ Process a path-style diff. See _changenum_diff for the alternate version that handles specific change numbers. Multiple paths may be specified in `args`. The path styles supported are: //path/to/file Upload file as a "new" file. //path/to/dir/... Upload all files as "new" files. //path/to/file[@#]rev Upload file from that rev as a "new" file. //path/to/file[@#]rev,[@#]rev Upload a diff between revs. //path/to/dir/...[@#]rev,[@#]rev Upload a diff of all files between revs in that directory. """ r_revision_range = re.compile(r'^(?P//[^@#]+)' + r'(?P[#@][^,]+)?' + r'(?P,[#@][^,]+)?$') empty_filename = make_tempfile() tmp_diff_from_filename = make_tempfile() tmp_diff_to_filename = make_tempfile() diff_lines = [] for path in args: m = r_revision_range.match(path) if not m: die('Path %r does not match a valid Perforce path.' % (path,)) revision1 = m.group('revision1') revision2 = m.group('revision2') first_rev_path = m.group('path') if revision1: first_rev_path += revision1 records = self._run_p4(['files', first_rev_path]) # Make a map for convenience. files = {} # Records are: # 'rev': '1' # 'func': '...' # 'time': '1214418871' # 'action': 'edit' # 'type': 'ktext' # 'depotFile': '...' # 'change': '123456' for record in records: if record['action'] not in ('delete', 'move/delete'): if revision2: files[record['depotFile']] = [record, None] else: files[record['depotFile']] = [None, record] if revision2: # [1:] to skip the comma. second_rev_path = m.group('path') + revision2[1:] records = self._run_p4(['files', second_rev_path]) for record in records: if record['action'] not in ('delete', 'move/delete'): try: m = files[record['depotFile']] m[1] = record except KeyError: files[record['depotFile']] = [None, record] old_file = new_file = empty_filename changetype_short = None for depot_path, (first_record, second_record) in files.items(): old_file = new_file = empty_filename if first_record is None: self._write_file(depot_path + '#' + second_record['rev'], tmp_diff_to_filename) new_file = tmp_diff_to_filename changetype_short = 'A' base_revision = 0 elif second_record is None: self._write_file(depot_path + '#' + first_record['rev'], tmp_diff_from_filename) old_file = tmp_diff_from_filename changetype_short = 'D' base_revision = int(first_record['rev']) elif first_record['rev'] == second_record['rev']: # We when we know the revisions are the same, we don't need # to do any diffing. This speeds up large revision-range # diffs quite a bit. continue else: self._write_file(depot_path + '#' + first_record['rev'], tmp_diff_from_filename) self._write_file(depot_path + '#' + second_record['rev'], tmp_diff_to_filename) new_file = tmp_diff_to_filename old_file = tmp_diff_from_filename changetype_short = 'M' base_revision = int(first_record['rev']) dl = self._do_diff(old_file, new_file, depot_path, base_revision, changetype_short, ignore_unmodified=True) diff_lines += dl os.unlink(empty_filename) os.unlink(tmp_diff_from_filename) os.unlink(tmp_diff_to_filename) return (''.join(diff_lines), None) def _run_p4(self, command): """Execute a perforce command using the python marshal API. - command: A list of strings of the command to execute. The return type depends on the command being run. """ command = ['p4', '-G'] + command p = subprocess.Popen(command, stdout=subprocess.PIPE) result = [] has_error = False while 1: try: data = marshal.load(p.stdout) except EOFError: break else: result.append(data) if data.get('code', None) == 'error': has_error = True rc = p.wait() if rc or has_error: for record in result: if 'data' in record: print record['data'] die('Failed to execute command: %s\n' % (command,)) return result """ Return a "sanitized" change number for submission to the Review Board server. For default changelists, this is always None. Otherwise, use the changelist number for submitted changelists, or if the p4d is 2002.2 or newer. This is because p4d < 2002.2 does not return enough information about pending changelists in 'p4 describe' for Review Board to make use of them (specifically, the list of files is missing). This would result in the diffs being rejected. """ def sanitize_changenum(self, changenum): if changenum == "default": return None else: v = self.p4d_version if v[0] < 2002 or (v[0] == "2002" and v[1] < 2): describeCmd = ["p4"] if options.p4_passwd: describeCmd.append("-P") describeCmd.append(options.p4_passwd) describeCmd = describeCmd + ["describe", "-s", changenum] description = execute(describeCmd, split_lines=True) if '*pending*' in description[0]: return None return changenum def _changenum_diff(self, changenum): """ Process a diff for a particular change number. This handles both pending and submitted changelists. See _path_diff for the alternate version that does diffs of depot paths. """ # TODO: It might be a good idea to enhance PerforceDiffParser to # understand that newFile could include a revision tag for post-submit # reviewing. cl_is_pending = False debug("Generating diff for changenum %s" % changenum) description = [] if changenum == "default": cl_is_pending = True else: describeCmd = ["p4"] if options.p4_passwd: describeCmd.append("-P") describeCmd.append(options.p4_passwd) describeCmd = describeCmd + ["describe", "-s", changenum] description = execute(describeCmd, split_lines=True) if re.search("no such changelist", description[0]): die("CLN %s does not exist." % changenum) # Some P4 wrappers are addding an extra line before the description if '*pending*' in description[0] or '*pending*' in description[1]: cl_is_pending = True v = self.p4d_version if cl_is_pending and (v[0] < 2002 or (v[0] == "2002" and v[1] < 2) or changenum == "default"): # Pre-2002.2 doesn't give file list in pending changelists, # or we don't have a description for a default changeset, # so we have to get it a different way. info = execute(["p4", "opened", "-c", str(changenum)], split_lines=True) if len(info) == 1 and info[0].startswith("File(s) not opened on this client."): die("Couldn't find any affected files for this change.") for line in info: data = line.split(" ") description.append("... %s %s" % (data[0], data[2])) else: # Get the file list for line_num, line in enumerate(description): if 'Affected files ...' in line: break else: # Got to the end of all the description lines and didn't find # what we were looking for. die("Couldn't find any affected files for this change.") description = description[line_num+2:] diff_lines = [] empty_filename = make_tempfile() tmp_diff_from_filename = make_tempfile() tmp_diff_to_filename = make_tempfile() for line in description: line = line.strip() if not line: continue m = re.search(r'\.\.\. ([^#]+)#(\d+) ' r'(add|edit|delete|integrate|branch|move/add' r'|move/delete)', line) if not m: die("Unsupported line from p4 opened: %s" % line) depot_path = m.group(1) base_revision = int(m.group(2)) if not cl_is_pending: # If the changelist is pending our base revision is the one that's # currently in the depot. If we're not pending the base revision is # actually the revision prior to this one base_revision -= 1 changetype = m.group(3) debug('Processing %s of %s' % (changetype, depot_path)) old_file = new_file = empty_filename old_depot_path = new_depot_path = None changetype_short = None if changetype in ['edit', 'integrate']: # A big assumption new_revision = base_revision + 1 # We have an old file, get p4 to take this old version from the # depot and put it into a plain old temp file for us old_depot_path = "%s#%s" % (depot_path, base_revision) self._write_file(old_depot_path, tmp_diff_from_filename) old_file = tmp_diff_from_filename # Also print out the new file into a tmpfile if cl_is_pending: new_file = self._depot_to_local(depot_path) else: new_depot_path = "%s#%s" %(depot_path, new_revision) self._write_file(new_depot_path, tmp_diff_to_filename) new_file = tmp_diff_to_filename changetype_short = "M" elif changetype in ['add', 'branch', 'move/add']: # We have a new file, get p4 to put this new file into a pretty # temp file for us. No old file to worry about here. if cl_is_pending: new_file = self._depot_to_local(depot_path) else: self._write_file(depot_path, tmp_diff_to_filename) new_file = tmp_diff_to_filename changetype_short = "A" elif changetype in ['delete', 'move/delete']: # We've deleted a file, get p4 to put the deleted file into a temp # file for us. The new file remains the empty file. old_depot_path = "%s#%s" % (depot_path, base_revision) self._write_file(old_depot_path, tmp_diff_from_filename) old_file = tmp_diff_from_filename changetype_short = "D" else: die("Unknown change type '%s' for %s" % (changetype, depot_path)) dl = self._do_diff(old_file, new_file, depot_path, base_revision, changetype_short) diff_lines += dl os.unlink(empty_filename) os.unlink(tmp_diff_from_filename) os.unlink(tmp_diff_to_filename) return (''.join(diff_lines), None) def _do_diff(self, old_file, new_file, depot_path, base_revision, changetype_short, ignore_unmodified=False): """ Do the work of producing a diff for Perforce. old_file - The absolute path to the "old" file. new_file - The absolute path to the "new" file. depot_path - The depot path in Perforce for this file. base_revision - The base perforce revision number of the old file as an integer. changetype_short - The change type as a single character string. ignore_unmodified - If True, will return an empty list if the file is not changed. Returns a list of strings of diff lines. """ if hasattr(os, 'uname') and os.uname()[0] == 'SunOS': diff_cmd = ["gdiff", "-urNp", old_file, new_file] else: diff_cmd = ["diff", "-urNp", old_file, new_file] # Diff returns "1" if differences were found. dl = execute(diff_cmd, extra_ignore_errors=(1,2), translate_newlines=False) # If the input file has ^M characters at end of line, lets ignore them. dl = dl.replace('\r\r\n', '\r\n') dl = dl.splitlines(True) cwd = os.getcwd() if depot_path.startswith(cwd): local_path = depot_path[len(cwd) + 1:] else: local_path = depot_path # Special handling for the output of the diff tool on binary files: # diff outputs "Files a and b differ" # and the code below expects the output to start with # "Binary files " if len(dl) == 1 and \ dl[0].startswith('Files %s and %s differ' % (old_file, new_file)): dl = ['Binary files %s and %s differ\n' % (old_file, new_file)] if dl == [] or dl[0].startswith("Binary files "): if dl == []: if ignore_unmodified: return [] else: print "Warning: %s in your changeset is unmodified" % \ local_path dl.insert(0, "==== %s#%s ==%s== %s ====\n" % \ (depot_path, base_revision, changetype_short, local_path)) dl.append('\n') elif len(dl) > 1: m = re.search(r'(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)', dl[1]) if m: timestamp = m.group(1) else: # Thu Sep 3 11:24:48 2007 m = re.search(r'(\w+)\s+(\w+)\s+(\d+)\s+(\d\d:\d\d:\d\d)\s+(\d\d\d\d)', dl[1]) if not m: die("Unable to parse diff header: %s" % dl[1]) month_map = { "Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04", "May": "05", "Jun": "06", "Jul": "07", "Aug": "08", "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12", } month = month_map[m.group(2)] day = m.group(3) timestamp = m.group(4) year = m.group(5) timestamp = "%s-%s-%s %s" % (year, month, day, timestamp) dl[0] = "--- %s\t%s#%s\n" % (local_path, depot_path, base_revision) dl[1] = "+++ %s\t%s\n" % (local_path, timestamp) # Not everybody has files that end in a newline (ugh). This ensures # that the resulting diff file isn't broken. if dl[-1][-1] != '\n': dl.append('\n') else: die("ERROR, no valid diffs: %s" % dl[0]) return dl def _write_file(self, depot_path, tmpfile): """ Grabs a file from Perforce and writes it to a temp file. p4 print sets the file readonly and that causes a later call to unlink fail. So we make the file read/write. """ debug('Writing "%s" to "%s"' % (depot_path, tmpfile)) execute(["p4", "print", "-o", tmpfile, "-q", depot_path]) os.chmod(tmpfile, stat.S_IREAD | stat.S_IWRITE) def _depot_to_local(self, depot_path): """ Given a path in the depot return the path on the local filesystem to the same file. If there are multiple results, take only the last result from the where command. """ where_output = self._run_p4(['where', depot_path]) try: return where_output[-1]['path'] except: # XXX: This breaks on filenames with spaces. return where_output[-1]['data'].split(' ')[2].strip() class MercurialClient(SCMClient): """ A wrapper around the hg Mercurial tool that fetches repository information and generates compatible diffs. """ def __init__(self): self.hgrc = {} self._type = 'hg' self._hg_root = '' self._remote_path = () self._hg_env = { 'HGRCPATH': os.devnull, 'HGPLAIN': '1', } # `self._remote_path_candidates` is an ordered set of hgrc # paths that are checked if `parent_branch` option is not given # explicitly. The first candidate found to exist will be used, # falling back to `default` (the last member.) self._remote_path_candidates = ['reviewboard', 'origin', 'parent', 'default'] def get_repository_info(self): if not check_install('hg --help'): return None self._load_hgrc() if not self.hg_root: # hg aborted => no mercurial repository here. return None svn_info = execute(["hg", "svn", "info"], ignore_errors=True) if (not svn_info.startswith('abort:') and not svn_info.startswith("hg: unknown command") and not svn_info.lower().startswith('not a child of')): return self._calculate_hgsubversion_repository_info(svn_info) self._type = 'hg' path = self.hg_root base_path = '/' if self.hgrc: self._calculate_remote_path() if self._remote_path: path = self._remote_path[1] base_path = '' return RepositoryInfo(path=path, base_path=base_path, supports_parent_diffs=True) def _calculate_remote_path(self): for candidate in self._remote_path_candidates: rc_key = 'paths.%s' % candidate if (not self._remote_path and self.hgrc.get(rc_key)): self._remote_path = (candidate, self.hgrc.get(rc_key)) debug('Using candidate path %r: %r' % self._remote_path) return def _calculate_hgsubversion_repository_info(self, svn_info): self._type = 'svn' m = re.search(r'^Repository Root: (.+)$', svn_info, re.M) if not m: return None path = m.group(1) m2 = re.match(r'^(svn\+ssh|http|https|svn)://([-a-zA-Z0-9.]*@)(.*)$', path) if m2: path = '%s://%s' % (m2.group(1), m2.group(3)) m = re.search(r'^URL: (.+)$', svn_info, re.M) if not m: return None base_path = m.group(1)[len(path):] or "/" return RepositoryInfo(path=path, base_path=base_path, supports_parent_diffs=True) @property def hg_root(self): if not self._hg_root: root = execute(['hg', 'root'], env=self._hg_env, ignore_errors=True) if not root.startswith('abort:'): self._hg_root = root.strip() else: return None return self._hg_root def _load_hgrc(self): for line in execute(['hg', 'showconfig'], split_lines=True): key, value = line.split('=', 1) self.hgrc[key] = value.strip() def extract_summary(self, revision): """ Extracts the first line from the description of the given changeset. """ return execute(['hg', 'log', '-r%s' % revision, '--template', r'{desc|firstline}\n'], env=self._hg_env) def extract_description(self, rev1, rev2): """ Extracts all descriptions in the given revision range and concatenates them, most recent ones going first. """ numrevs = len(execute([ 'hg', 'log', '-r%s:%s' % (rev2, rev1), '--follow', '--template', r'{rev}\n'], env=self._hg_env ).strip().split('\n')) return execute(['hg', 'log', '-r%s:%s' % (rev2, rev1), '--follow', '--template', r'{desc}\n\n', '--limit', str(numrevs - 1)], env=self._hg_env).strip() def diff(self, files): """ Performs a diff across all modified files in a Mercurial repository. """ files = files or [] if self._type == 'svn': return self._get_hgsubversion_diff(files) else: return self._get_outgoing_diff(files) def _get_hgsubversion_diff(self, files): parent = execute(['hg', 'parent', '--svn', '--template', '{node}\n']).strip() if options.parent_branch: parent = options.parent_branch if options.guess_summary and not options.summary: options.summary = self.extract_summary(".") if options.guess_description and not options.description: options.description = self.extract_description(parent, ".") return (execute(["hg", "diff", "--svn", '-r%s:.' % parent]), None) def _get_outgoing_diff(self, files): """ When working with a clone of a Mercurial remote, we need to find out what the outgoing revisions are for a given branch. It would be nice if we could just do `hg outgoing --patch `, but there are a couple of problems with this. For one, the server-side diff parser isn't yet equipped to filter out diff headers such as "comparing with..." and "changeset: :". Another problem is that the output of `outgoing` potentially includes changesets across multiple branches. In order to provide the most accurate comparison between one's local clone and a given remote -- something akin to git's diff command syntax `git diff ..` -- we have to do the following: - get the name of the current branch - get a list of outgoing changesets, specifying a custom format - filter outgoing changesets by the current branch name - get the "top" and "bottom" outgoing changesets - use these changesets as arguments to `hg diff -r -r ` Future modifications may need to be made to account for odd cases like having multiple diverged branches which share partial history -- or we can just punish developers for doing such nonsense :) """ files = files or [] remote = self._remote_path[0] if not remote and options.parent_branch: remote = options.parent_branch current_branch = execute(['hg', 'branch'], env=self._hg_env).strip() outgoing_changesets = \ self._get_outgoing_changesets(current_branch, remote) top_rev, bottom_rev = \ self._get_top_and_bottom_outgoing_revs(outgoing_changesets) if options.guess_summary and not options.summary: options.summary = self.extract_summary(top_rev).rstrip("\n") if options.guess_description and not options.description: options.description = self.extract_description(bottom_rev, top_rev) full_command = ['hg', 'diff', '-r', str(bottom_rev), '-r', str(top_rev)] + files return (execute(full_command, env=self._hg_env), None) def _get_outgoing_changesets(self, current_branch, remote): """ Given the current branch name and a remote path, return a list of outgoing changeset numbers. """ outgoing_changesets = [] raw_outgoing = execute(['hg', '-q', 'outgoing', '--template', 'b:{branches}\nr:{rev}\n\n', remote], env=self._hg_env) for pair in raw_outgoing.split('\n\n'): if not pair.strip(): continue branch, rev = pair.strip().split('\n') branch_name = branch[len('b:'):].strip() branch_name = branch_name or 'default' revno = rev[len('r:'):] if branch_name == current_branch and revno.isdigit(): debug('Found outgoing changeset %s for branch %r' % (revno, branch_name)) outgoing_changesets.append(int(revno)) return outgoing_changesets def _get_top_and_bottom_outgoing_revs(self, outgoing_changesets): # This is a classmethod rather than a func mostly just to keep the # module namespace clean. Pylint told me to do it. top_rev = max(outgoing_changesets) bottom_rev = min(outgoing_changesets) parents = execute(["hg", "log", "-r", str(bottom_rev), "--template", "{parents}"], env=self._hg_env) parents = parents.rstrip("\n").split(":") if len(parents) > 1: bottom_rev = parents[0] else: bottom_rev = bottom_rev - 1 bottom_rev = max(0, bottom_rev) return top_rev, bottom_rev def diff_between_revisions(self, revision_range, args, repository_info): """ Performs a diff between 2 revisions of a Mercurial repository. """ if self._type != 'hg': raise NotImplementedError if ':' in revision_range: r1, r2 = revision_range.split(':') else: # If only 1 revision is given, we find the first parent and use # that as the second revision. # # We could also use "hg diff -c r1", but then we couldn't reuse the # code for extracting descriptions. r2 = revision_range r1 = execute(["hg", "parents", "-r", r2, "--template", "{rev}\n"]).split()[0] if options.guess_summary and not options.summary: options.summary = self.extract_summary(r2) if options.guess_description and not options.description: options.description = self.extract_description(r1, r2) return (execute(["hg", "diff", "-r", r1, "-r", r2], env=self._hg_env), None) def scan_for_server(self, repository_info): # Scan first for dot files, since it's faster and will cover the # user's $HOME/.reviewboardrc server_url = \ super(MercurialClient, self).scan_for_server(repository_info) if not server_url and self.hgrc.get('reviewboard.url'): server_url = self.hgrc.get('reviewboard.url').strip() if not server_url and self._type == "svn": # Try using the reviewboard:url property on the SVN repo, if it # exists. prop = SVNClient().scan_for_server_property(repository_info) if prop: return prop return server_url class GitClient(SCMClient): """ A wrapper around git that fetches repository information and generates compatible diffs. This will attempt to generate a diff suitable for the remote repository, whether git, SVN or Perforce. """ def __init__(self): SCMClient.__init__(self) # Store the 'correct' way to invoke git, just plain old 'git' by default self.git = 'git' def _strip_heads_prefix(self, ref): """ Strips prefix from ref name, if possible """ return re.sub(r'^refs/heads/', '', ref) def get_repository_info(self): if not check_install('git --help'): # CreateProcess (launched via subprocess, used by check_install) # does not automatically append .cmd for things it finds in PATH. # If we're on Windows, and this works, save it for further use. if sys.platform.startswith('win') and check_install('git.cmd --help'): self.git = 'git.cmd' else: return None git_dir = execute([self.git, "rev-parse", "--git-dir"], ignore_errors=True).rstrip("\n") if git_dir.startswith("fatal:") or not os.path.isdir(git_dir): return None self.bare = execute([self.git, "config", "core.bare"]).strip() == 'true' # post-review in directories other than the top level of # of a work-tree would result in broken diffs on the server if not self.bare: os.chdir(os.path.dirname(os.path.abspath(git_dir))) self.head_ref = execute([self.git, 'symbolic-ref', '-q', 'HEAD']).strip() # We know we have something we can work with. Let's find out # what it is. We'll try SVN first, but only if there's a .git/svn # directory. Otherwise, it may attempt to create one and scan # revisions, which can be slow. git_svn_dir = os.path.join(git_dir, 'svn') if os.path.isdir(git_svn_dir) and len(os.listdir(git_svn_dir)) > 0: data = execute([self.git, "svn", "info"], ignore_errors=True) m = re.search(r'^Repository Root: (.+)$', data, re.M) if m: path = m.group(1) m = re.search(r'^URL: (.+)$', data, re.M) if m: base_path = m.group(1)[len(path):] or "/" m = re.search(r'^Repository UUID: (.+)$', data, re.M) if m: uuid = m.group(1) self.type = "svn" # Get SVN tracking branch if options.parent_branch: self.upstream_branch = options.parent_branch else: data = execute([self.git, "svn", "rebase", "-n"], ignore_errors=True) m = re.search(r'^Remote Branch:\s*(.+)$', data, re.M) if m: self.upstream_branch = m.group(1) else: sys.stderr.write('Failed to determine SVN tracking ' 'branch. Defaulting to "master"\n') self.upstream_branch = 'master' return SvnRepositoryInfo(path=path, base_path=base_path, uuid=uuid, supports_parent_diffs=True) else: # Versions of git-svn before 1.5.4 don't (appear to) support # 'git svn info'. If we fail because of an older git install, # here, figure out what version of git is installed and give # the user a hint about what to do next. version = execute([self.git, "svn", "--version"], ignore_errors=True) version_parts = re.search('version (\d+)\.(\d+)\.(\d+)', version) svn_remote = execute([self.git, "config", "--get", "svn-remote.svn.url"], ignore_errors=True) if (version_parts and not self.is_valid_version((int(version_parts.group(1)), int(version_parts.group(2)), int(version_parts.group(3))), (1, 5, 4)) and svn_remote): die("Your installation of git-svn must be upgraded to " "version 1.5.4 or later") # Okay, maybe Perforce. # TODO # Nope, it's git then. # Check for a tracking branch and determine merge-base short_head = self._strip_heads_prefix(self.head_ref) merge = execute([self.git, 'config', '--get', 'branch.%s.merge' % short_head], ignore_errors=True).strip() remote = execute([self.git, 'config', '--get', 'branch.%s.remote' % short_head], ignore_errors=True).strip() merge = self._strip_heads_prefix(merge) self.upstream_branch = '' if remote and remote != '.' and merge: self.upstream_branch = '%s/%s' % (remote, merge) url = None if options.repository_url: url = options.repository_url else: self.upstream_branch, origin_url = \ self.get_origin(self.upstream_branch, True) if not origin_url or origin_url.startswith("fatal:"): self.upstream_branch, origin_url = self.get_origin() url = origin_url.rstrip('/') # Central bare repositories don't have origin URLs. # We return git_dir instead and hope for the best. url = origin_url.rstrip('/') if not url: url = os.path.abspath(git_dir) # There is no remote, so skip this part of upstream_branch. self.upstream_branch = self.upstream_branch.split('/')[-1] if url: self.type = "git" return RepositoryInfo(path=url, base_path='', supports_parent_diffs=True) return None def get_origin(self, default_upstream_branch=None, ignore_errors=False): """Get upstream remote origin from options or parameters. Returns a tuple: (upstream_branch, remote_url) """ upstream_branch = options.tracking or default_upstream_branch or \ 'origin/master' upstream_remote = upstream_branch.split('/')[0] origin_url = execute([self.git, "config", "--get", "remote.%s.url" % upstream_remote], ignore_errors=True).rstrip("\n") return (upstream_branch, origin_url) def is_valid_version(self, actual, expected): """ Takes two tuples, both in the form: (major_version, minor_version, micro_version) Returns true if the actual version is greater than or equal to the expected version, and false otherwise. """ return (actual[0] > expected[0]) or \ (actual[0] == expected[0] and actual[1] > expected[1]) or \ (actual[0] == expected[0] and actual[1] == expected[1] and \ actual[2] >= expected[2]) def scan_for_server(self, repository_info): # Scan first for dot files, since it's faster and will cover the # user's $HOME/.reviewboardrc server_url = super(GitClient, self).scan_for_server(repository_info) if server_url: return server_url # TODO: Maybe support a server per remote later? Is that useful? url = execute([self.git, "config", "--get", "reviewboard.url"], ignore_errors=True).strip() if url: return url if self.type == "svn": # Try using the reviewboard:url property on the SVN repo, if it # exists. prop = SVNClient().scan_for_server_property(repository_info) if prop: return prop return None def diff(self, args): """ Performs a diff across all modified files in the branch, taking into account a parent branch. """ parent_branch = options.parent_branch self.merge_base = execute([self.git, "merge-base", self.upstream_branch, self.head_ref]).strip() if parent_branch: diff_lines = self.make_diff(parent_branch) parent_diff_lines = self.make_diff(self.merge_base, parent_branch) else: diff_lines = self.make_diff(self.merge_base, self.head_ref) parent_diff_lines = None if options.guess_summary and not options.summary: s = execute([self.git, "log", "--pretty=format:%s", "HEAD^.."], ignore_errors=True) options.summary = s.replace('\n', ' ').strip() if options.guess_description and not options.description: options.description = execute( [self.git, "log", "--pretty=format:%s%n%n%b", (parent_branch or self.merge_base) + ".."], ignore_errors=True).strip() return (diff_lines, parent_diff_lines) def make_diff(self, ancestor, commit=""): """ Performs a diff on a particular branch range. """ if commit: rev_range = "%s..%s" % (ancestor, commit) else: rev_range = ancestor if self.type == "svn": diff_lines = execute([self.git, "diff", "--no-color", "--no-prefix", "--no-ext-diff", "-r", "-u", rev_range], split_lines=True) return self.make_svn_diff(ancestor, diff_lines) elif self.type == "git": return execute([self.git, "diff", "--no-color", "--full-index", "--no-ext-diff", rev_range]) return None def make_svn_diff(self, parent_branch, diff_lines): """ Formats the output of git diff such that it's in a form that svn diff would generate. This is needed so the SVNTool in Review Board can properly parse this diff. """ rev = execute([self.git, "svn", "find-rev", parent_branch]).strip() if not rev: return None diff_data = "" filename = "" newfile = False for line in diff_lines: if line.startswith("diff "): # Grab the filename and then filter this out. # This will be in the format of: # # diff --git a/path/to/file b/path/to/file info = line.split(" ") diff_data += "Index: %s\n" % info[2] diff_data += "=" * 67 diff_data += "\n" elif line.startswith("index "): # Filter this out. pass elif line.strip() == "--- /dev/null": # New file newfile = True elif line.startswith("--- "): newfile = False diff_data += "--- %s\t(revision %s)\n" % \ (line[4:].strip(), rev) elif line.startswith("+++ "): filename = line[4:].strip() if newfile: diff_data += "--- %s\t(revision 0)\n" % filename diff_data += "+++ %s\t(revision 0)\n" % filename else: # We already printed the "--- " line. diff_data += "+++ %s\t(working copy)\n" % filename elif line.startswith("new file mode"): # Filter this out. pass elif line.startswith("Binary files "): # Add the following so that we know binary files were added/changed diff_data += "Cannot display: file marked as a binary type.\n" diff_data += "svn:mime-type = application/octet-stream\n" else: diff_data += line return diff_data def diff_between_revisions(self, revision_range, args, repository_info): """Perform a diff between two arbitrary revisions""" # Make a parent diff to the first of the revisions so that we # never end up with broken patches: self.merge_base = execute([self.git, "merge-base", self.upstream_branch, self.head_ref]).strip() if ":" not in revision_range: # only one revision is specified # Check if parent contains the first revision and make a # parent diff if not: pdiff_required = execute([self.git, "branch", "-r", "--contains", revision_range]) parent_diff_lines = None if not pdiff_required: parent_diff_lines = self.make_diff(self.merge_base, revision_range) if options.guess_summary and not options.summary: s = execute([self.git, "log", "--pretty=format:%s", revision_range + ".."], ignore_errors=True) options.summary = s.replace('\n', ' ').strip() if options.guess_description and not options.description: options.description = execute( [self.git, "log", "--pretty=format:%s%n%n%b", revision_range + ".."], ignore_errors=True).strip() return (self.make_diff(revision_range), parent_diff_lines) else: r1, r2 = revision_range.split(":") # Check if parent contains the first revision and make a # parent diff if not: pdiff_required = execute([self.git, "branch", "-r", "--contains", r1]) parent_diff_lines = None if not pdiff_required: parent_diff_lines = self.make_diff(self.merge_base, r1) if options.guess_summary and not options.summary: s = execute([self.git, "log", "--pretty=format:%s", "%s..%s" % (r1, r2)], ignore_errors=True) options.summary = s.replace('\n', ' ').strip() if options.guess_description and not options.description: options.description = execute( [self.git, "log", "--pretty=format:%s%n%n%b", "%s..%s" % (r1, r2)], ignore_errors=True).strip() return (self.make_diff(r1, r2), parent_diff_lines) class PlasticClient(SCMClient): """ A wrapper around the cm Plastic tool that fetches repository information and generates compatible diffs """ def get_repository_info(self): if not check_install('cm version'): return None # Get the repository that the current directory is from. If there # is more than one repository mounted in the current directory, # bail out for now (in future, should probably enter a review # request per each repository.) split = execute(["cm", "ls", "--format={8}"], split_lines=True, ignore_errors=True) m = re.search(r'^rep:(.+)$', split[0], re.M) if not m: return None # Make sure the repository list contains only one unique entry if len(split) != split.count(split[0]): # Not unique! die('Directory contains more than one mounted repository') path = m.group(1) # Get the workspace directory, so we can strip it from the diff output self.workspacedir = execute(["cm", "gwp", ".", "--format={1}"], split_lines=False, ignore_errors=True).strip() debug("Workspace is %s" % self.workspacedir) return RepositoryInfo(path, supports_changesets=True, supports_parent_diffs=False) def get_changenum(self, args): """ Extract the integer value from a changeset ID (cs:1234) """ if len(args) == 1 and args[0].startswith("cs:"): try: return str(int(args[0][3:])) except ValueError: pass return None def sanitize_changenum(self, changenum): """ Return a "sanitized" change number. Currently a no-op """ return changenum def diff(self, args): """ Performs a diff across all modified files in a Plastic workspace Parent diffs are not supported (the second value in the tuple). """ changenum = self.get_changenum(args) if changenum is None: return self.branch_diff(args), None else: return self.changenum_diff(changenum), None def diff_between_revisions(self, revision_range, args, repository_info): """ Performs a diff between 2 revisions of a Plastic repository. Assume revision_range is a branch specification (br:/main/task001) and hand over to branch_diff """ return (self.branch_diff(revision_range), None) def changenum_diff(self, changenum): debug("changenum_diff: %s" % (changenum)) files = execute(["cm", "log", "cs:" + changenum, "--csFormat={items}", "--itemFormat={shortstatus} {path} " "rev:revid:{revid} rev:revid:{parentrevid} " "src:{srccmpath} rev:revid:{srcdirrevid} " "dst:{dstcmpath} rev:revid:{dstdirrevid}{newline}"], split_lines = True) debug("got files: %s" % (files)) # Diff generation based on perforce client diff_lines = [] empty_filename = make_tempfile() tmp_diff_from_filename = make_tempfile() tmp_diff_to_filename = make_tempfile() for f in files: f = f.strip() if not f: continue m = re.search(r'(?P[ACIMR]) (?P.*) ' r'(?Prev:revid:[-\d]+) ' r'(?Prev:revid:[-\d]+) ' r'src:(?P.*) ' r'(?Prev:revid:[-\d]+) ' r'dst:(?P.*) ' r'(?Prev:revid:[-\d]+)$', f) if not m: die("Could not parse 'cm log' response: %s" % f) changetype = m.group("type") filename = m.group("file") if changetype == "M": # Handle moved files as a delete followed by an add. # Clunky, but at least it works oldfilename = m.group("srcpath") oldspec = m.group("srcrevspec") newfilename = m.group("dstpath") newspec = m.group("dstrevspec") self.write_file(oldfilename, oldspec, tmp_diff_from_filename) dl = self.diff_files(tmp_diff_from_filename, empty_filename, oldfilename, "rev:revid:-1", oldspec, changetype) diff_lines += dl self.write_file(newfilename, newspec, tmp_diff_to_filename) dl = self.diff_files(empty_filename, tmp_diff_to_filename, newfilename, newspec, "rev:revid:-1", changetype) diff_lines += dl else: newrevspec = m.group("revspec") parentrevspec = m.group("parentrevspec") debug("Type %s File %s Old %s New %s" % (changetype, filename, parentrevspec, newrevspec)) old_file = new_file = empty_filename if (changetype in ['A'] or (changetype in ['C', 'I'] and parentrevspec == "rev:revid:-1")): # File was Added, or a Change or Merge (type I) and there # is no parent revision self.write_file(filename, newrevspec, tmp_diff_to_filename) new_file = tmp_diff_to_filename elif changetype in ['C', 'I']: # File was Changed or Merged (type I) self.write_file(filename, parentrevspec, tmp_diff_from_filename) old_file = tmp_diff_from_filename self.write_file(filename, newrevspec, tmp_diff_to_filename) new_file = tmp_diff_to_filename elif changetype in ['R']: # File was Removed self.write_file(filename, parentrevspec, tmp_diff_from_filename) old_file = tmp_diff_from_filename else: die("Don't know how to handle change type '%s' for %s" % (changetype, filename)) dl = self.diff_files(old_file, new_file, filename, newrevspec, parentrevspec, changetype) diff_lines += dl os.unlink(empty_filename) os.unlink(tmp_diff_from_filename) os.unlink(tmp_diff_to_filename) return ''.join(diff_lines) def branch_diff(self, args): debug("branch diff: %s" % (args)) if len(args) > 0: branch = args[0] else: branch = args if not branch.startswith("br:"): return None if not options.branch: options.branch = branch files = execute(["cm", "fbc", branch, "--format={3} {4}"], split_lines = True) debug("got files: %s" % (files)) diff_lines = [] empty_filename = make_tempfile() tmp_diff_from_filename = make_tempfile() tmp_diff_to_filename = make_tempfile() for f in files: f = f.strip() if not f: continue m = re.search(r'^(?P.*)#(?P\d+) (?P.*)$', f) if not m: die("Could not parse 'cm fbc' response: %s" % f) filename = m.group("file") branch = m.group("branch") revno = m.group("revno") # Get the base revision with a cm find basefiles = execute(["cm", "find", "revs", "where", "item='" + filename + "'", "and", "branch='" + branch + "'", "and", "revno=" + revno, "--format={item} rev:revid:{id} " "rev:revid:{parent}", "--nototal"], split_lines = True) # We only care about the first line m = re.search(r'^(?P.*) ' r'(?Prev:revid:[-\d]+) ' r'(?Prev:revid:[-\d]+)$', basefiles[0]) basefilename = m.group("filename") newrevspec = m.group("revspec") parentrevspec = m.group("parentrevspec") # Cope with adds/removes changetype = "C" if parentrevspec == "rev:revid:-1": changetype = "A" elif newrevspec == "rev:revid:-1": changetype = "R" debug("Type %s File %s Old %s New %s" % (changetype, basefilename, parentrevspec, newrevspec)) old_file = new_file = empty_filename if changetype == "A": # File Added self.write_file(basefilename, newrevspec, tmp_diff_to_filename) new_file = tmp_diff_to_filename elif changetype == "R": # File Removed self.write_file(basefilename, parentrevspec, tmp_diff_from_filename) old_file = tmp_diff_from_filename else: self.write_file(basefilename, parentrevspec, tmp_diff_from_filename) old_file = tmp_diff_from_filename self.write_file(basefilename, newrevspec, tmp_diff_to_filename) new_file = tmp_diff_to_filename dl = self.diff_files(old_file, new_file, basefilename, newrevspec, parentrevspec, changetype) diff_lines += dl os.unlink(empty_filename) os.unlink(tmp_diff_from_filename) os.unlink(tmp_diff_to_filename) return ''.join(diff_lines) def diff_files(self, old_file, new_file, filename, newrevspec, parentrevspec, changetype, ignore_unmodified=False): """ Do the work of producing a diff for Plastic (based on the Perforce one) old_file - The absolute path to the "old" file. new_file - The absolute path to the "new" file. filename - The file in the Plastic workspace newrevspec - The revid spec of the changed file parentrevspecspec - The revision spec of the "old" file changetype - The change type as a single character string ignore_unmodified - If true, will return an empty list if the file is not changed. Returns a list of strings of diff lines. """ if filename.startswith(self.workspacedir): filename = filename[len(self.workspacedir):] diff_cmd = ["diff", "-urN", old_file, new_file] # Diff returns "1" if differences were found. dl = execute(diff_cmd, extra_ignore_errors=(1,2), translate_newlines = False) # If the input file has ^M characters at end of line, lets ignore them. dl = dl.replace('\r\r\n', '\r\n') dl = dl.splitlines(True) # Special handling for the output of the diff tool on binary files: # diff outputs "Files a and b differ" # and the code below expects the output to start with # "Binary files " if (len(dl) == 1 and dl[0].startswith('Files %s and %s differ' % (old_file, new_file))): dl = ['Binary files %s and %s differ\n' % (old_file, new_file)] if dl == [] or dl[0].startswith("Binary files "): if dl == []: if ignore_unmodified: return [] else: print "Warning: %s in your changeset is unmodified" % \ filename dl.insert(0, "==== %s (%s) ==%s==\n" % (filename, newrevspec, changetype)) dl.append('\n') else: dl[0] = "--- %s\t%s\n" % (filename, parentrevspec) dl[1] = "+++ %s\t%s\n" % (filename, newrevspec) # Not everybody has files that end in a newline. This ensures # that the resulting diff file isn't broken. if dl[-1][-1] != '\n': dl.append('\n') return dl def write_file(self, filename, filespec, tmpfile): """ Grabs a file from Plastic and writes it to a temp file """ debug("Writing '%s' (rev %s) to '%s'" % (filename, filespec, tmpfile)) execute(["cm", "cat", filespec, "--file=" + tmpfile]) SCMCLIENTS = ( SVNClient(), CVSClient(), GitClient(), MercurialClient(), PerforceClient(), ClearCaseClient(), PlasticClient(), ) def debug(s): """ Prints debugging information if post-review was run with --debug """ if DEBUG or options and options.debug: print ">>> %s" % s def make_tempfile(content=None): """ Creates a temporary file and returns the path. The path is stored in an array for later cleanup. """ fd, tmpfile = mkstemp() if content: os.write(fd, content) os.close(fd) tempfiles.append(tmpfile) return tmpfile def check_install(command): """ Try executing an external command and return a boolean indicating whether that command is installed or not. The 'command' argument should be something that executes quickly, without hitting the network (for instance, 'svn help' or 'git --version'). """ try: subprocess.Popen(command.split(' '), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return True except OSError: return False def check_gnu_diff(): """Checks if GNU diff is installed, and informs the user if it's not.""" has_gnu_diff = False try: result = execute(['diff', '--version'], ignore_errors=True) has_gnu_diff = 'GNU diffutils' in result except OSError: pass if not has_gnu_diff: sys.stderr.write('\n') sys.stderr.write('GNU diff is required for Subversion ' 'repositories. Make sure it is installed\n') sys.stderr.write('and in the path.\n') sys.stderr.write('\n') if os.name == 'nt': sys.stderr.write('On Windows, you can install this from:\n') sys.stderr.write(GNU_DIFF_WIN32_URL) sys.stderr.write('\n') die() def execute(command, env=None, split_lines=False, ignore_errors=False, extra_ignore_errors=(), translate_newlines=True, with_errors=True): """ Utility function to execute a command and return the output. """ if isinstance(command, list): debug(subprocess.list2cmdline(command)) else: debug(command) if env: env.update(os.environ) else: env = os.environ.copy() env['LC_ALL'] = 'en_US.UTF-8' env['LANGUAGE'] = 'en_US.UTF-8' if with_errors: errors_output = subprocess.STDOUT else: errors_output = subprocess.PIPE if sys.platform.startswith('win'): p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=errors_output, shell=False, universal_newlines=translate_newlines, env=env) else: p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=errors_output, shell=False, close_fds=True, universal_newlines=translate_newlines, env=env) if split_lines: data = p.stdout.readlines() else: data = p.stdout.read() rc = p.wait() if rc and not ignore_errors and rc not in extra_ignore_errors: die('Failed to execute command: %s\n%s' % (command, data)) return data def die(msg=None): """ Cleanly exits the program with an error message. Erases all remaining temporary files. """ for tmpfile in tempfiles: try: os.unlink(tmpfile) except: pass if msg: print msg sys.exit(1) def walk_parents(path): """ Walks up the tree to the root directory. """ while os.path.splitdrive(path)[1] != os.sep: yield path path = os.path.dirname(path) def load_config_files(homepath): """Loads data from .reviewboardrc files""" def _load_config(path): config = { 'TREES': {}, } filename = os.path.join(path, '.reviewboardrc') if os.path.exists(filename): try: execfile(filename, config) except SyntaxError, e: die('Syntax error in config file: %s\n' 'Line %i offset %i\n' % (filename, e.lineno, e.offset)) return config return None for path in walk_parents(os.getcwd()): config = _load_config(path) if config: configs.append(config) globals()['user_config'] = _load_config(homepath) def tempt_fate(server, tool, changenum, diff_content=None, parent_diff_content=None, submit_as=None, retries=3): """ Attempts to create a review request on a Review Board server and upload a diff. On success, the review request path is displayed. """ try: if options.rid: review_request = server.get_review_request(options.rid) else: review_request = server.new_review_request(changenum, submit_as) if options.target_groups: server.set_review_request_field(review_request, 'target_groups', options.target_groups) if options.target_people: server.set_review_request_field(review_request, 'target_people', options.target_people) if options.summary: server.set_review_request_field(review_request, 'summary', options.summary) if options.branch: server.set_review_request_field(review_request, 'branch', options.branch) if options.bugs_closed: # append to existing list options.bugs_closed = options.bugs_closed.strip(", ") bug_set = set(re.split("[, ]+", options.bugs_closed)) | \ set(review_request['bugs_closed']) options.bugs_closed = ",".join(bug_set) server.set_review_request_field(review_request, 'bugs_closed', options.bugs_closed) if options.description: server.set_review_request_field(review_request, 'description', options.description) if options.testing_done: server.set_review_request_field(review_request, 'testing_done', options.testing_done) if options.change_description: server.set_review_request_field(review_request, 'changedescription', options.change_description) except APIError, e: if e.error_code == 103: # Not logged in retries = retries - 1 # We had an odd issue where the server ended up a couple of # years in the future. Login succeeds but the cookie date was # "odd" so use of the cookie appeared to fail and eventually # ended up at max recursion depth :-(. Check for a maximum # number of retries. if retries >= 0: server.login(force=True) tempt_fate(server, tool, changenum, diff_content, parent_diff_content, submit_as, retries=retries) return if options.rid: die("Error getting review request %s: %s" % (options.rid, e)) else: die("Error creating review request: %s" % e) if not server.info.supports_changesets or not options.change_only: try: server.upload_diff(review_request, diff_content, parent_diff_content) except APIError, e: sys.stderr.write('\n') sys.stderr.write('Error uploading diff\n') sys.stderr.write('\n') if e.error_code == 105: sys.stderr.write('The generated diff file was empty. This ' 'usually means no files were\n') sys.stderr.write('modified in this change.\n') sys.stderr.write('\n') sys.stderr.write('Try running with --output-diff and --debug ' 'for more information.\n') sys.stderr.write('\n') die("Your review request still exists, but the diff is not " + "attached.") if options.reopen: server.reopen(review_request) if options.publish: server.publish(review_request) request_url = 'r/' + str(review_request['id']) + '/' review_url = urljoin(server.url, request_url) if not review_url.startswith('http'): review_url = 'http://%s' % review_url print "Review request #%s posted." % (review_request['id'],) print print review_url return review_url def parse_options(args): parser = OptionParser(usage="%prog [-pond] [-r review_id] [changenum]", version="RBTools " + get_version_string()) parser.add_option("-p", "--publish", dest="publish", action="store_true", default=PUBLISH, help="publish the review request immediately after " "submitting") parser.add_option("-r", "--review-request-id", dest="rid", metavar="ID", default=None, help="existing review request ID to update") parser.add_option("-o", "--open", dest="open_browser", action="store_true", default=OPEN_BROWSER, help="open a web browser to the review request page") parser.add_option("-n", "--output-diff", dest="output_diff_only", action="store_true", default=False, help="outputs a diff to the console and exits. " "Does not post") parser.add_option("--server", dest="server", default=REVIEWBOARD_URL, metavar="SERVER", help="specify a different Review Board server " "to use") parser.add_option("--diff-only", dest="diff_only", action="store_true", default=False, help="uploads a new diff, but does not update " "info from changelist") parser.add_option("--reopen", dest="reopen", action="store_true", default=False, help="reopen discarded review request " "after update") parser.add_option("--target-groups", dest="target_groups", default=TARGET_GROUPS, help="names of the groups who will perform " "the review") parser.add_option("--target-people", dest="target_people", default=TARGET_PEOPLE, help="names of the people who will perform " "the review") parser.add_option("--summary", dest="summary", default=None, help="summary of the review ") parser.add_option("--description", dest="description", default=None, help="description of the review ") parser.add_option("--description-file", dest="description_file", default=None, help="text file containing a description of the review") parser.add_option("--guess-summary", dest="guess_summary", action="store_true", default=False, help="guess summary from the latest commit (git/" "hg/hgsubversion only)") parser.add_option("--guess-description", dest="guess_description", action="store_true", default=False, help="guess description based on commits on this branch " "(git/hg/hgsubversion only)") parser.add_option("--testing-done", dest="testing_done", default=None, help="details of testing done ") parser.add_option("--testing-done-file", dest="testing_file", default=None, help="text file containing details of testing done ") parser.add_option("--branch", dest="branch", default=None, help="affected branch ") parser.add_option("--bugs-closed", dest="bugs_closed", default=None, help="list of bugs closed ") parser.add_option("--change-description", default=None, help="description of what changed in this revision of " "the review request when updating an existing request") parser.add_option("--revision-range", dest="revision_range", default=None, help="generate the diff for review based on given " "revision range") parser.add_option("--submit-as", dest="submit_as", default=SUBMIT_AS, metavar="USERNAME", help="user name to be recorded as the author of the " "review request, instead of the logged in user") parser.add_option("--username", dest="username", default=None, metavar="USERNAME", help="user name to be supplied to the reviewboard server") parser.add_option("--password", dest="password", default=None, metavar="PASSWORD", help="password to be supplied to the reviewboard server") parser.add_option("--change-only", dest="change_only", action="store_true", default=False, help="updates info from changelist, but does " "not upload a new diff (only available if your " "repository supports changesets)") parser.add_option("--parent", dest="parent_branch", default=None, metavar="PARENT_BRANCH", help="the parent branch this diff should be against " "(only available if your repository supports " "parent diffs)") parser.add_option("--tracking-branch", dest="tracking", default=None, metavar="TRACKING", help="Tracking branch from which your branch is derived " "(git only, defaults to origin/master)") parser.add_option("--p4-client", dest="p4_client", default=None, help="the Perforce client name that the review is in") parser.add_option("--p4-port", dest="p4_port", default=None, help="the Perforce servers IP address that the review is on") parser.add_option("--p4-passwd", dest="p4_passwd", default=None, help="the Perforce password or ticket of the user in the P4USER environment variable") parser.add_option('--svn-changelist', dest='svn_changelist', default=None, help='generate the diff for review based on a local SVN ' 'changelist') parser.add_option("--repository-url", dest="repository_url", default=None, help="the url for a repository for creating a diff " "outside of a working copy (currently only " "supported by Subversion with --revision-range or " "--diff-filename and ClearCase with relative " "paths outside the view). For git, this specifies" "the origin url of the current repository, " "overriding the origin url supplied by the git client.") parser.add_option("-d", "--debug", action="store_true", dest="debug", default=DEBUG, help="display debug output") parser.add_option("--diff-filename", dest="diff_filename", default=None, help='upload an existing diff file, instead of ' 'generating a new diff') parser.add_option('--http-username', dest='http_username', default=None, metavar='USERNAME', help='username for HTTP Basic authentication') parser.add_option('--http-password', dest='http_password', default=None, metavar='PASSWORD', help='password for HTTP Basic authentication') (globals()["options"], args) = parser.parse_args(args) if options.description and options.description_file: sys.stderr.write("The --description and --description-file options " "are mutually exclusive.\n") sys.exit(1) if options.description_file: if os.path.exists(options.description_file): fp = open(options.description_file, "r") options.description = fp.read() fp.close() else: sys.stderr.write("The description file %s does not exist.\n" % options.description_file) sys.exit(1) if options.testing_done and options.testing_file: sys.stderr.write("The --testing-done and --testing-done-file options " "are mutually exclusive.\n") sys.exit(1) if options.testing_file: if os.path.exists(options.testing_file): fp = open(options.testing_file, "r") options.testing_done = fp.read() fp.close() else: sys.stderr.write("The testing file %s does not exist.\n" % options.testing_file) sys.exit(1) if options.reopen and not options.rid: sys.stderr.write("The --reopen option requires " "--review-request-id option.\n") sys.exit(1) if options.change_description and not options.rid: sys.stderr.write("--change-description may only be used " "when updating an existing review-request\n") sys.exit(1) return args def determine_client(): repository_info = None tool = None # Try to find the SCM Client we're going to be working with. for tool in SCMCLIENTS: repository_info = tool.get_repository_info() if repository_info: break if not repository_info: if options.repository_url: print "No supported repository could be access at the supplied url." else: print "The current directory does not contain a checkout from a" print "supported source code repository." sys.exit(1) # Verify that options specific to an SCM Client have not been mis-used. if options.change_only and not repository_info.supports_changesets: sys.stderr.write("The --change-only option is not valid for the " "current SCM client.\n") sys.exit(1) if options.parent_branch and not repository_info.supports_parent_diffs: sys.stderr.write("The --parent option is not valid for the " "current SCM client.\n") sys.exit(1) if ((options.p4_client or options.p4_port) and \ not isinstance(tool, PerforceClient)): sys.stderr.write("The --p4-client and --p4-port options are not valid " "for the current SCM client.\n") sys.exit(1) return (repository_info, tool) def main(): origcwd = os.path.abspath(os.getcwd()) if 'APPDATA' in os.environ: homepath = os.environ['APPDATA'] elif 'HOME' in os.environ: homepath = os.environ["HOME"] else: homepath = '' # Load the config and cookie files cookie_file = os.path.join(homepath, ".post-review-cookies.txt") load_config_files(homepath) args = parse_options(sys.argv[1:]) debug('RBTools %s' % get_version_string()) debug('Home = %s' % homepath) repository_info, tool = determine_client() # Verify that options specific to an SCM Client have not been mis-used. tool.check_options() # Try to find a valid Review Board server to use. if options.server: server_url = options.server else: server_url = tool.scan_for_server(repository_info) if not server_url: print "Unable to find a Review Board server for this source code tree." sys.exit(1) server = ReviewBoardServer(server_url, repository_info, cookie_file) # Handle the case where /api/ requires authorization (RBCommons). if not server.check_api_version(): die("Unable to log in with the supplied username and password.") if repository_info.supports_changesets: changenum = tool.get_changenum(args) else: changenum = None if options.revision_range: diff, parent_diff = tool.diff_between_revisions(options.revision_range, args, repository_info) elif options.svn_changelist: diff, parent_diff = tool.diff_changelist(options.svn_changelist) elif options.diff_filename: parent_diff = None if options.diff_filename == '-': diff = sys.stdin.read() else: try: fp = open(os.path.join(origcwd, options.diff_filename), 'r') diff = fp.read() fp.close() except IOError, e: die("Unable to open diff filename: %s" % e) else: diff, parent_diff = tool.diff(args) if len(diff) == 0: die("There don't seem to be any diffs!") if (isinstance(tool, PerforceClient) or isinstance(tool, PlasticClient)) and changenum is not None: changenum = tool.sanitize_changenum(changenum) # NOTE: In Review Board 1.5.2 through 1.5.3.1, the changenum support # is broken, so we have to force the deprecated API. if (parse_version(server.rb_version) >= parse_version('1.5.2') and parse_version(server.rb_version) <= parse_version('1.5.3.1')): debug('Using changenums on Review Board %s, which is broken. ' 'Falling back to the deprecated 1.0 API' % server.rb_version) server.deprecated_api = True if options.output_diff_only: # The comma here isn't a typo, but rather suppresses the extra newline print diff, sys.exit(0) # Let's begin. server.login() review_url = tempt_fate(server, tool, changenum, diff_content=diff, parent_diff_content=parent_diff, submit_as=options.submit_as) # Load the review up in the browser if requested to: if options.open_browser: try: import webbrowser if 'open_new_tab' in dir(webbrowser): # open_new_tab is only in python 2.5+ webbrowser.open_new_tab(review_url) elif 'open_new' in dir(webbrowser): webbrowser.open_new(review_url) else: os.system( 'start %s' % review_url ) except: print 'Error opening review URL: %s' % review_url if __name__ == "__main__": main() RBTools-0.3.4/rbtools/__init__.py0000644000175000017500000000411111640247306017443 0ustar chipx86chipx8600000000000000# # __init__.py -- Basic version and package information # # Copyright (c) 2007-2009 Christian Hammond # Copyright (c) 2007-2009 David Trowbridge # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # # The version of RBTools # # This is in the format of: # # (Major, Minor, Micro, alpha/beta/rc/final, Release Number, Released) # VERSION = (0, 3, 4, 'final', 0, True) def get_version_string(): version = '%s.%s' % (VERSION[0], VERSION[1]) if VERSION[2]: version += ".%s" % VERSION[2] if VERSION[3] != 'final': if VERSION[3] == 'rc': version += ' RC%s' % VERSION[4] else: version += ' %s %s' % (VERSION[3], VERSION[4]) if not is_release(): version += " (dev)" return version def get_package_version(): version = '%s.%s' % (VERSION[0], VERSION[1]) if VERSION[2]: version += ".%s" % VERSION[2] if VERSION[3] != 'final': version += '%s%s' % (VERSION[3], VERSION[4]) return version def is_release(): return VERSION[5] __version_info__ = VERSION[:-1] __version__ = get_package_version() RBTools-0.3.4/rbtools/tests.py0000644000175000017500000011723311640247306017060 0ustar chipx86chipx8600000000000000import os import re import shutil import sys import tempfile import time import unittest import urllib2 from random import randint from textwrap import dedent try: from cStringIO import StringIO except ImportError: from StringIO import StringIO try: import json except ImportError: import simplejson as json import nose from rbtools.postreview import execute, load_config_files from rbtools.postreview import APIError, GitClient, MercurialClient, \ RepositoryInfo, ReviewBoardServer, \ SvnRepositoryInfo import rbtools.postreview TEMPDIR_SUFFIX = '__' + __name__.replace('.', '_') def is_exe_in_path(name): """Checks whether an executable is in the user's search path. This expects a name without any system-specific executable extension. It will append the proper extension as necessary. For example, use "myapp" and not "myapp.exe". This will return True if the app is in the path, or False otherwise. Taken from djblets.util.filesystem to avoid an extra dependency """ if sys.platform == 'win32' and not name.endswith('.exe'): name += ".exe" for dir in os.environ['PATH'].split(os.pathsep): if os.path.exists(os.path.join(dir, name)): return True return False def _get_tmpdir(): return tempfile.mkdtemp(TEMPDIR_SUFFIX) class MockHttpUnitTest(unittest.TestCase): deprecated_api = False def setUp(self): # Save the old http_get and http_post rbtools.postreview.options = OptionsStub() self.saved_http_get = ReviewBoardServer.http_get self.saved_http_post = ReviewBoardServer.http_post self.server = ReviewBoardServer('http://localhost:8080/', RepositoryInfo(), None) ReviewBoardServer.http_get = self._http_method ReviewBoardServer.http_post = self._http_method self.server.deprecated_api = self.deprecated_api self.http_response = {} def tearDown(self): ReviewBoardServer.http_get = self.saved_http_get ReviewBoardServer.http_post = self.saved_http_post def _http_method(self, path, *args, **kwargs): if isinstance(self.http_response, dict): http_response = self.http_response[path] else: http_response = self.http_response if isinstance(http_response, Exception): raise http_response else: return http_response class OptionsStub(object): def __init__(self): self.debug = True self.guess_summary = False self.guess_description = False self.tracking = None self.username = None self.password = None self.repository_url = None class GitClientTests(unittest.TestCase): TESTSERVER = "http://127.0.0.1:8080" def _gitcmd(self, command, env=None, split_lines=False, ignore_errors=False, extra_ignore_errors=(), translate_newlines=True, git_dir=None): if git_dir: full_command = ['git', '--git-dir=%s/.git' % git_dir] else: full_command = ['git'] full_command.extend(command) return execute(full_command, env, split_lines, ignore_errors, extra_ignore_errors, translate_newlines) def _git_add_file_commit(self, file, data, msg): """Add a file to a git repository with the content of data and commit with msg. """ foo = open(file, 'w') foo.write(data) foo.close() self._gitcmd(['add', file]) self._gitcmd(['commit', '-m', msg]) def setUp(self): if not is_exe_in_path('git'): raise nose.SkipTest('git not found in path') self.orig_dir = os.getcwd() self.git_dir = _get_tmpdir() os.chdir(self.git_dir) self._gitcmd(['init'], git_dir=self.git_dir) foo = open(os.path.join(self.git_dir, 'foo.txt'), 'w') foo.write(FOO) foo.close() self._gitcmd(['add', 'foo.txt']) self._gitcmd(['commit', '-m', 'initial commit']) self.clone_dir = _get_tmpdir() os.rmdir(self.clone_dir) self._gitcmd(['clone', self.git_dir, self.clone_dir]) self.client = GitClient() os.chdir(self.orig_dir) rbtools.postreview.user_config = {} rbtools.postreview.configs = [] rbtools.postreview.options = OptionsStub() rbtools.postreview.options.parent_branch = None def tearDown(self): os.chdir(self.orig_dir) shutil.rmtree(self.git_dir) shutil.rmtree(self.clone_dir) def test_get_repository_info_simple(self): """Test GitClient get_repository_info, simple case""" os.chdir(self.clone_dir) ri = self.client.get_repository_info() self.assert_(isinstance(ri, RepositoryInfo)) self.assertEqual(ri.base_path, '') self.assertEqual(ri.path.rstrip("/.git"), self.git_dir) self.assertTrue(ri.supports_parent_diffs) self.assertFalse(ri.supports_changesets) def test_scan_for_server_simple(self): """Test GitClient scan_for_server, simple case""" os.chdir(self.clone_dir) ri = self.client.get_repository_info() server = self.client.scan_for_server(ri) self.assert_(server is None) def test_scan_for_server_reviewboardrc(self): "Test GitClient scan_for_server, .reviewboardrc case""" os.chdir(self.clone_dir) rc = open(os.path.join(self.clone_dir, '.reviewboardrc'), 'w') rc.write('REVIEWBOARD_URL = "%s"' % self.TESTSERVER) rc.close() rbtools.postreview.user_config = load_config_files(self.clone_dir) ri = self.client.get_repository_info() server = self.client.scan_for_server(ri) self.assertEqual(server, self.TESTSERVER) def test_scan_for_server_property(self): """Test GitClient scan_for_server using repo property""" os.chdir(self.clone_dir) self._gitcmd(['config', 'reviewboard.url', self.TESTSERVER]) ri = self.client.get_repository_info() self.assertEqual(self.client.scan_for_server(ri), self.TESTSERVER) def test_diff_simple(self): """Test GitClient simple diff case""" diff = "diff --git a/foo.txt b/foo.txt\n" \ "index 634b3e8ff85bada6f928841a9f2c505560840b3a..5e98e9540e1b741b5be24fcb33c40c1c8069c1fb 100644\n" \ "--- a/foo.txt\n" \ "+++ b/foo.txt\n" \ "@@ -6,7 +6,4 @@ multa quoque et bello passus, dum conderet urbem,\n" \ " inferretque deos Latio, genus unde Latinum,\n" \ " Albanique patres, atque altae moenia Romae.\n" \ " Musa, mihi causas memora, quo numine laeso,\n" \ "-quidve dolens, regina deum tot volvere casus\n" \ "-insignem pietate virum, tot adire labores\n" \ "-impulerit. Tantaene animis caelestibus irae?\n" \ " \n" os.chdir(self.clone_dir) self.client.get_repository_info() self._git_add_file_commit('foo.txt', FOO1, 'delete and modify stuff') self.assertEqual(self.client.diff(None), (diff, None)) def test_diff_simple_multiple(self): """Test GitClient simple diff with multiple commits case""" diff = "diff --git a/foo.txt b/foo.txt\n" \ "index 634b3e8ff85bada6f928841a9f2c505560840b3a..63036ed3fcafe870d567a14dd5884f4fed70126c 100644\n" \ "--- a/foo.txt\n" \ "+++ b/foo.txt\n" \ "@@ -1,12 +1,11 @@\n" \ " ARMA virumque cano, Troiae qui primus ab oris\n" \ "+ARMA virumque cano, Troiae qui primus ab oris\n" \ " Italiam, fato profugus, Laviniaque venit\n" \ " litora, multum ille et terris iactatus et alto\n" \ " vi superum saevae memorem Iunonis ob iram;\n" \ "-multa quoque et bello passus, dum conderet urbem,\n" \ "+dum conderet urbem,\n" \ " inferretque deos Latio, genus unde Latinum,\n" \ " Albanique patres, atque altae moenia Romae.\n" \ "+Albanique patres, atque altae moenia Romae.\n" \ " Musa, mihi causas memora, quo numine laeso,\n" \ "-quidve dolens, regina deum tot volvere casus\n" \ "-insignem pietate virum, tot adire labores\n" \ "-impulerit. Tantaene animis caelestibus irae?\n" \ " \n" os.chdir(self.clone_dir) self.client.get_repository_info() self._git_add_file_commit('foo.txt', FOO1, 'commit 1') self._git_add_file_commit('foo.txt', FOO2, 'commit 1') self._git_add_file_commit('foo.txt', FOO3, 'commit 1') self.assertEqual(self.client.diff(None), (diff, None)) def test_diff_branch_diverge(self): """Test GitClient diff with divergent branches""" diff1 = "diff --git a/foo.txt b/foo.txt\n" \ "index 634b3e8ff85bada6f928841a9f2c505560840b3a..e619c1387f5feb91f0ca83194650bfe4f6c2e347 100644\n" \ "--- a/foo.txt\n" \ "+++ b/foo.txt\n" \ "@@ -1,4 +1,6 @@\n" \ " ARMA virumque cano, Troiae qui primus ab oris\n" \ "+ARMA virumque cano, Troiae qui primus ab oris\n" \ "+ARMA virumque cano, Troiae qui primus ab oris\n" \ " Italiam, fato profugus, Laviniaque venit\n" \ " litora, multum ille et terris iactatus et alto\n" \ " vi superum saevae memorem Iunonis ob iram;\n" \ "@@ -6,7 +8,4 @@ multa quoque et bello passus, dum conderet urbem,\n" \ " inferretque deos Latio, genus unde Latinum,\n" \ " Albanique patres, atque altae moenia Romae.\n" \ " Musa, mihi causas memora, quo numine laeso,\n" \ "-quidve dolens, regina deum tot volvere casus\n" \ "-insignem pietate virum, tot adire labores\n" \ "-impulerit. Tantaene animis caelestibus irae?\n" \ " \n" diff2 = "diff --git a/foo.txt b/foo.txt\n" \ "index 634b3e8ff85bada6f928841a9f2c505560840b3a..5e98e9540e1b741b5be24fcb33c40c1c8069c1fb 100644\n" \ "--- a/foo.txt\n" \ "+++ b/foo.txt\n" \ "@@ -6,7 +6,4 @@ multa quoque et bello passus, dum conderet urbem,\n" \ " inferretque deos Latio, genus unde Latinum,\n" \ " Albanique patres, atque altae moenia Romae.\n" \ " Musa, mihi causas memora, quo numine laeso,\n" \ "-quidve dolens, regina deum tot volvere casus\n" \ "-insignem pietate virum, tot adire labores\n" \ "-impulerit. Tantaene animis caelestibus irae?\n" \ " \n" os.chdir(self.clone_dir) self._git_add_file_commit('foo.txt', FOO1, 'commit 1') self._gitcmd(['checkout', '-b', 'mybranch', '--track', 'origin/master']) self._git_add_file_commit('foo.txt', FOO2, 'commit 2') self.client.get_repository_info() self.assertEqual(self.client.diff(None), (diff1, None)) self._gitcmd(['checkout', 'master']) self.client.get_repository_info() self.assertEqual(self.client.diff(None), (diff2, None)) def test_diff_tracking_no_origin(self): """Test GitClient diff with a tracking branch, but no origin remote""" diff = "diff --git a/foo.txt b/foo.txt\n" \ "index 634b3e8ff85bada6f928841a9f2c505560840b3a..5e98e9540e1b741b5be24fcb33c40c1c8069c1fb 100644\n" \ "--- a/foo.txt\n" \ "+++ b/foo.txt\n" \ "@@ -6,7 +6,4 @@ multa quoque et bello passus, dum conderet urbem,\n" \ " inferretque deos Latio, genus unde Latinum,\n" \ " Albanique patres, atque altae moenia Romae.\n" \ " Musa, mihi causas memora, quo numine laeso,\n" \ "-quidve dolens, regina deum tot volvere casus\n" \ "-insignem pietate virum, tot adire labores\n" \ "-impulerit. Tantaene animis caelestibus irae?\n" \ " \n" os.chdir(self.clone_dir) self._gitcmd(['remote', 'add', 'quux', self.git_dir]) self._gitcmd(['fetch', 'quux']) self._gitcmd(['checkout', '-b', 'mybranch', '--track', 'quux/master']) self._git_add_file_commit('foo.txt', FOO1, 'delete and modify stuff') self.client.get_repository_info() self.assertEqual(self.client.diff(None), (diff, None)) def test_diff_local_tracking(self): """Test GitClient diff with a local tracking branch""" diff = "diff --git a/foo.txt b/foo.txt\n" \ "index 634b3e8ff85bada6f928841a9f2c505560840b3a..e619c1387f5feb91f0ca83194650bfe4f6c2e347 100644\n" \ "--- a/foo.txt\n" \ "+++ b/foo.txt\n" \ "@@ -1,4 +1,6 @@\n" \ " ARMA virumque cano, Troiae qui primus ab oris\n" \ "+ARMA virumque cano, Troiae qui primus ab oris\n" \ "+ARMA virumque cano, Troiae qui primus ab oris\n" \ " Italiam, fato profugus, Laviniaque venit\n" \ " litora, multum ille et terris iactatus et alto\n" \ " vi superum saevae memorem Iunonis ob iram;\n" \ "@@ -6,7 +8,4 @@ multa quoque et bello passus, dum conderet urbem,\n" \ " inferretque deos Latio, genus unde Latinum,\n" \ " Albanique patres, atque altae moenia Romae.\n" \ " Musa, mihi causas memora, quo numine laeso,\n" \ "-quidve dolens, regina deum tot volvere casus\n" \ "-insignem pietate virum, tot adire labores\n" \ "-impulerit. Tantaene animis caelestibus irae?\n" \ " \n" os.chdir(self.clone_dir) self._git_add_file_commit('foo.txt', FOO1, 'commit 1') self._gitcmd(['checkout', '-b', 'mybranch', '--track', 'master']) self._git_add_file_commit('foo.txt', FOO2, 'commit 2') self.client.get_repository_info() self.assertEqual(self.client.diff(None), (diff, None)) def test_diff_tracking_override(self): """Test GitClient diff with option override for tracking branch""" diff = "diff --git a/foo.txt b/foo.txt\n" \ "index 634b3e8ff85bada6f928841a9f2c505560840b3a..5e98e9540e1b741b5be24fcb33c40c1c8069c1fb 100644\n" \ "--- a/foo.txt\n" \ "+++ b/foo.txt\n" \ "@@ -6,7 +6,4 @@ multa quoque et bello passus, dum conderet urbem,\n" \ " inferretque deos Latio, genus unde Latinum,\n" \ " Albanique patres, atque altae moenia Romae.\n" \ " Musa, mihi causas memora, quo numine laeso,\n" \ "-quidve dolens, regina deum tot volvere casus\n" \ "-insignem pietate virum, tot adire labores\n" \ "-impulerit. Tantaene animis caelestibus irae?\n" \ " \n" os.chdir(self.clone_dir) rbtools.postreview.options.tracking = 'origin/master' self._gitcmd(['remote', 'add', 'bad', self.git_dir]) self._gitcmd(['fetch', 'bad']) self._gitcmd(['checkout', '-b', 'mybranch', '--track', 'bad/master']) self._git_add_file_commit('foo.txt', FOO1, 'commit 1') self.client.get_repository_info() self.assertEqual(self.client.diff(None), (diff, None)) def test_diff_slash_tracking(self): """Test GitClient diff with tracking branch that has slash in its name""" diff = "diff --git a/foo.txt b/foo.txt\n" \ "index 5e98e9540e1b741b5be24fcb33c40c1c8069c1fb..e619c1387f5feb91f0ca83194650bfe4f6c2e347 100644\n" \ "--- a/foo.txt\n" \ "+++ b/foo.txt\n" \ "@@ -1,4 +1,6 @@\n" \ " ARMA virumque cano, Troiae qui primus ab oris\n" \ "+ARMA virumque cano, Troiae qui primus ab oris\n" \ "+ARMA virumque cano, Troiae qui primus ab oris\n" \ " Italiam, fato profugus, Laviniaque venit\n" \ " litora, multum ille et terris iactatus et alto\n" \ " vi superum saevae memorem Iunonis ob iram;\n" os.chdir(self.git_dir) self._gitcmd(['checkout', '-b', 'not-master']) self._git_add_file_commit('foo.txt', FOO1, 'commit 1') os.chdir(self.clone_dir) self._gitcmd(['fetch', 'origin']) self._gitcmd(['checkout', '-b', 'my/branch', '--track', 'origin/not-master']) self._git_add_file_commit('foo.txt', FOO2, 'commit 2') self.client.get_repository_info() self.assertEqual(self.client.diff(None), (diff, None)) class MercurialTestBase(unittest.TestCase): def setUp(self): self._hg_env = {} def _hgcmd(self, command, split_lines=False, ignore_errors=False, extra_ignore_errors=(), translate_newlines=True, hg_dir=None): if hg_dir: full_command = ['hg', '--cwd', hg_dir] else: full_command = ['hg'] # We're *not* doing `env = env or {}` here because # we want the caller to be able to *enable* reading # of user and system-level hgrc configuration. env = self._hg_env.copy() if not env: env = { 'HGRCPATH': os.devnull, 'HGPLAIN': '1', } full_command.extend(command) return execute(full_command, env, split_lines, ignore_errors, extra_ignore_errors, translate_newlines) def _hg_add_file_commit(self, filename, data, msg): outfile = open(filename, 'w') outfile.write(data) outfile.close() self._hgcmd(['add', filename]) self._hgcmd(['commit', '-m', msg]) class MercurialClientTests(MercurialTestBase): TESTSERVER = 'http://127.0.0.1:8080' CLONE_HGRC = dedent(""" [paths] default = %(hg_dir)s cloned = %(clone_dir)s [reviewboard] url = %(test_server)s [diff] git = true """).rstrip() def setUp(self): MercurialTestBase.setUp(self) if not is_exe_in_path('hg'): raise nose.SkipTest('hg not found in path') self.orig_dir = os.getcwd() self.hg_dir = _get_tmpdir() os.chdir(self.hg_dir) self._hgcmd(['init'], hg_dir=self.hg_dir) foo = open(os.path.join(self.hg_dir, 'foo.txt'), 'w') foo.write(FOO) foo.close() self._hgcmd(['add', 'foo.txt']) self._hgcmd(['commit', '-m', 'initial commit']) self.clone_dir = _get_tmpdir() os.rmdir(self.clone_dir) self._hgcmd(['clone', self.hg_dir, self.clone_dir]) os.chdir(self.clone_dir) self.client = MercurialClient() clone_hgrc = open(self.clone_hgrc_path, 'wb') clone_hgrc.write(self.CLONE_HGRC % { 'hg_dir': self.hg_dir, 'clone_dir': self.clone_dir, 'test_server': self.TESTSERVER, }) clone_hgrc.close() self.client.get_repository_info() rbtools.postreview.user_config = {} rbtools.postreview.options = OptionsStub() rbtools.postreview.options.parent_branch = None os.chdir(self.clone_dir) @property def clone_hgrc_path(self): return os.path.join(self.clone_dir, '.hg', 'hgrc') @property def hgrc_path(self): return os.path.join(self.hg_dir, '.hg', 'hgrc') def tearDown(self): os.chdir(self.orig_dir) shutil.rmtree(self.hg_dir) shutil.rmtree(self.clone_dir) def testGetRepositoryInfoSimple(self): """Test MercurialClient get_repository_info, simple case""" ri = self.client.get_repository_info() self.assertTrue(isinstance(ri, RepositoryInfo)) self.assertEqual('', ri.base_path) hgpath = ri.path if os.path.basename(hgpath) == '.hg': hgpath = os.path.dirname(hgpath) self.assertEqual(self.hg_dir, hgpath) self.assertTrue(ri.supports_parent_diffs) self.assertFalse(ri.supports_changesets) def testScanForServerSimple(self): """Test MercurialClient scan_for_server, simple case""" os.rename(self.clone_hgrc_path, os.path.join(self.clone_dir, '._disabled_hgrc')) self.client.hgrc = {} self.client._load_hgrc() ri = self.client.get_repository_info() server = self.client.scan_for_server(ri) self.assertTrue(server is None) def testScanForServerWhenPresentInHgrc(self): """Test MercurialClient scan_for_server when present in hgrc""" ri = self.client.get_repository_info() server = self.client.scan_for_server(ri) self.assertEqual(self.TESTSERVER, server) def testScanForServerReviewboardrc(self): """Test MercurialClient scan_for_server when in .reviewboardrc""" rc = open(os.path.join(self.clone_dir, '.reviewboardrc'), 'w') rc.write('REVIEWBOARD_URL = "%s"' % self.TESTSERVER) rc.close() ri = self.client.get_repository_info() server = self.client.scan_for_server(ri) self.assertEqual(self.TESTSERVER, server) def testDiffSimple(self): """Test MercurialClient diff, simple case""" self.client.get_repository_info() self._hg_add_file_commit('foo.txt', FOO1, 'delete and modify stuff') diff_result = self.client.diff(None) self.assertEqual((EXPECTED_HG_DIFF_0, None), diff_result) def testDiffSimpleMultiple(self): """Test MercurialClient diff with multiple commits""" self.client.get_repository_info() self._hg_add_file_commit('foo.txt', FOO1, 'commit 1') self._hg_add_file_commit('foo.txt', FOO2, 'commit 2') self._hg_add_file_commit('foo.txt', FOO3, 'commit 3') diff_result = self.client.diff(None) self.assertEqual((EXPECTED_HG_DIFF_1, None), diff_result) def testDiffBranchDiverge(self): """Test MercurialClient diff with diverged branch""" self._hg_add_file_commit('foo.txt', FOO1, 'commit 1') self._hgcmd(['branch', 'diverged']) self._hg_add_file_commit('foo.txt', FOO2, 'commit 2') self.client.get_repository_info() self.assertEqual((EXPECTED_HG_DIFF_2, None), self.client.diff(None)) self._hgcmd(['update', '-C', 'default']) self.client.get_repository_info() self.assertEqual((EXPECTED_HG_DIFF_3, None), self.client.diff(None)) class MercurialSubversionClientTests(MercurialTestBase): TESTSERVER = "http://127.0.0.1:8080" def __init__(self, *args, **kwargs): self._tmpbase = '' self.clone_dir = '' self.svn_repo = '' self.svn_checkout = '' self.client = None self._svnserve_pid = 0 self._max_svnserve_pid_tries = 12 self._svnserve_port = os.environ.get('SVNSERVE_PORT') self._required_exes = ('svnadmin', 'svnserve', 'svn') MercurialTestBase.__init__(self, *args, **kwargs) def setUp(self): MercurialTestBase.setUp(self) self._hg_env = {'FOO': 'BAR'} for exe in self._required_exes: if not is_exe_in_path(exe): raise nose.SkipTest('missing svn stuff! giving up!') if not self._has_hgsubversion(): raise nose.SkipTest('unable to use `hgsubversion` extension! ' 'giving up!') if not self._tmpbase: self._tmpbase = _get_tmpdir() self._create_svn_repo() self._fire_up_svnserve() self._fill_in_svn_repo() try: self._get_testing_clone() except (OSError, IOError): msg = 'could not clone from svn repo! skipping...' raise nose.SkipTest(msg), None, sys.exc_info()[2] self._spin_up_client() self._stub_in_config_and_options() os.chdir(self.clone_dir) def _has_hgsubversion(self): output = self._hgcmd(['svn', '--help'], ignore_errors=True, extra_ignore_errors=(255)) return not re.search("unknown command ['\"]svn['\"]", output, re.I) def tearDown(self): shutil.rmtree(self.clone_dir) os.kill(self._svnserve_pid, 9) if self._tmpbase: shutil.rmtree(self._tmpbase) def _svn_add_file_commit(self, filename, data, msg): outfile = open(filename, 'w') outfile.write(data) outfile.close() execute(['svn', 'add', filename]) execute(['svn', 'commit', '-m', msg]) def _create_svn_repo(self): self.svn_repo = os.path.join(self._tmpbase, 'svnrepo') execute(['svnadmin', 'create', self.svn_repo]) def _fire_up_svnserve(self): if not self._svnserve_port: self._svnserve_port = str(randint(30000, 40000)) pid_file = os.path.join(self._tmpbase, 'svnserve.pid') execute(['svnserve', '--pid-file', pid_file, '-d', '--listen-port', self._svnserve_port, '-r', self._tmpbase]) for i in range(0, self._max_svnserve_pid_tries): try: self._svnserve_pid = int(open(pid_file).read().strip()) return except (IOError, OSError): time.sleep(0.25) # This will re-raise the last exception, which will be either # IOError or OSError if the above fails and this branch is reached raise def _fill_in_svn_repo(self): self.svn_checkout = os.path.join(self._tmpbase, 'checkout.svn') execute(['svn', 'checkout', 'file://%s' % self.svn_repo, self.svn_checkout]) os.chdir(self.svn_checkout) for subtree in ('trunk', 'branches', 'tags'): execute(['svn', 'mkdir', subtree]) execute(['svn', 'commit', '-m', 'filling in T/b/t']) os.chdir(os.path.join(self.svn_checkout, 'trunk')) for i, data in enumerate([FOO, FOO1, FOO2]): self._svn_add_file_commit('foo.txt', data, 'foo commit %s' % i) def _get_testing_clone(self): self.clone_dir = os.path.join(self._tmpbase, 'checkout.hg') self._hgcmd([ 'clone', 'svn://127.0.0.1:%s/svnrepo' % self._svnserve_port, self.clone_dir, ]) def _spin_up_client(self): os.chdir(self.clone_dir) self.client = MercurialClient() def _stub_in_config_and_options(self): rbtools.postreview.user_config = {} rbtools.postreview.options = OptionsStub() rbtools.postreview.options.parent_branch = None def testGetRepositoryInfoSimple(self): """Test MercurialClient (+svn) get_repository_info, simple case""" ri = self.client.get_repository_info() self.assertEqual('svn', self.client._type) self.assertEqual('/trunk', ri.base_path) self.assertEqual('svn://127.0.0.1:%s/svnrepo' % self._svnserve_port, ri.path) def testScanForServerSimple(self): """Test MercurialClient (+svn) scan_for_server, simple case""" ri = self.client.get_repository_info() server = self.client.scan_for_server(ri) self.assertTrue(server is None) def testScanForServerReviewboardrc(self): """Test MercurialClient (+svn) scan_for_server in .reviewboardrc""" rc_filename = os.path.join(self.clone_dir, '.reviewboardrc') rc = open(rc_filename, 'w') rc.write('REVIEWBOARD_URL = "%s"' % self.TESTSERVER) rc.close() ri = self.client.get_repository_info() server = self.client.scan_for_server(ri) self.assertEqual(self.TESTSERVER, server) def testScanForServerProperty(self): """Test MercurialClient (+svn) scan_for_server in svn property""" os.chdir(self.svn_checkout) execute(['svn', 'update']) execute(['svn', 'propset', 'reviewboard:url', self.TESTSERVER, self.svn_checkout]) execute(['svn', 'commit', '-m', 'adding reviewboard:url property']) os.chdir(self.clone_dir) self._hgcmd(['pull']) self._hgcmd(['update', '-C']) ri = self.client.get_repository_info() self.assertEqual(self.TESTSERVER, self.client.scan_for_server(ri)) def testDiffSimple(self): """Test MercurialClient (+svn) diff, simple case""" self.client.get_repository_info() self._hg_add_file_commit('foo.txt', FOO4, 'edit 4') self.assertEqual(EXPECTED_HG_SVN_DIFF_0, self.client.diff(None)[0]) def testDiffSimpleMultiple(self): """Test MercurialClient (+svn) diff with multiple commits""" self.client.get_repository_info() self._hg_add_file_commit('foo.txt', FOO4, 'edit 4') self._hg_add_file_commit('foo.txt', FOO5, 'edit 5') self._hg_add_file_commit('foo.txt', FOO6, 'edit 6') self.assertEqual(EXPECTED_HG_SVN_DIFF_1, self.client.diff(None)[0]) class SVNClientTests(unittest.TestCase): def test_relative_paths(self): """Testing SvnRepositoryInfo._get_relative_path""" info = SvnRepositoryInfo('http://svn.example.com/svn/', '/', '') self.assertEqual(info._get_relative_path('/foo', '/bar'), None) self.assertEqual(info._get_relative_path('/', '/trunk/myproject'), None) self.assertEqual(info._get_relative_path('/trunk/myproject', '/'), '/trunk/myproject') self.assertEqual( info._get_relative_path('/trunk/myproject', ''), '/trunk/myproject') self.assertEqual( info._get_relative_path('/trunk/myproject', '/trunk'), '/myproject') self.assertEqual( info._get_relative_path('/trunk/myproject', '/trunk/myproject'), '/') class ApiTests(MockHttpUnitTest): def setUp(self): super(ApiTests, self).setUp() self.http_response = { 'api/': json.dumps({ 'stat': 'ok', 'links': { 'info': { 'href': 'api/info/', 'method': 'GET', }, }, }), } def test_check_api_version_1_5_2_higher(self): """Testing checking the API version compatibility (RB >= 1.5.2)""" self.http_response.update(self._build_info_resource('1.5.2')) self.server.check_api_version() self.assertFalse(self.server.deprecated_api) self.http_response.update(self._build_info_resource('1.5.3alpha0')) self.server.check_api_version() self.assertFalse(self.server.deprecated_api) def test_check_api_version_1_5_1_lower(self): """Testing checking the API version compatibility (RB < 1.5.2)""" self.http_response.update(self._build_info_resource('1.5.1')) self.server.check_api_version() self.assertTrue(self.server.deprecated_api) def test_check_api_version_old_api(self): """Testing checking the API version compatibility (RB < 1.5.0)""" self.http_response = { 'api/': APIError(404, 0), } self.server.check_api_version() self.assertTrue(self.server.deprecated_api) def _build_info_resource(self, package_version): return { 'api/info/': json.dumps({ 'stat': 'ok', 'info': { 'product': { 'package_version': package_version, }, }, }), } class DeprecatedApiTests(MockHttpUnitTest): deprecated_api = True SAMPLE_ERROR_STR = json.dumps({ 'stat': 'fail', 'err': { 'code': 100, 'msg': 'This is a test failure', } }) def test_parse_get_error_http_200(self): self.http_response = self.SAMPLE_ERROR_STR try: self.server.api_get('/foo/') # Shouldn't be reached self._assert(False) except APIError, e: self.assertEqual(e.http_status, 200) self.assertEqual(e.error_code, 100) self.assertEqual(e.rsp['stat'], 'fail') self.assertEqual(str(e), 'This is a test failure (HTTP 200, API Error 100)') def test_parse_post_error_http_200(self): self.http_response = self.SAMPLE_ERROR_STR try: self.server.api_post('/foo/') # Shouldn't be reached self._assert(False) except APIError, e: self.assertEqual(e.http_status, 200) self.assertEqual(e.error_code, 100) self.assertEqual(e.rsp['stat'], 'fail') self.assertEqual(str(e), 'This is a test failure (HTTP 200, API Error 100)') def test_parse_get_error_http_400(self): self.http_response = self._make_http_error('/foo/', 400, self.SAMPLE_ERROR_STR) try: self.server.api_get('/foo/') # Shouldn't be reached self._assert(False) except APIError, e: self.assertEqual(e.http_status, 400) self.assertEqual(e.error_code, 100) self.assertEqual(e.rsp['stat'], 'fail') self.assertEqual(str(e), 'This is a test failure (HTTP 400, API Error 100)') def test_parse_post_error_http_400(self): self.http_response = self._make_http_error('/foo/', 400, self.SAMPLE_ERROR_STR) try: self.server.api_post('/foo/') # Shouldn't be reached self._assert(False) except APIError, e: self.assertEqual(e.http_status, 400) self.assertEqual(e.error_code, 100) self.assertEqual(e.rsp['stat'], 'fail') self.assertEqual(str(e), 'This is a test failure (HTTP 400, API Error 100)') def _make_http_error(self, url, code, body): return urllib2.HTTPError(url, code, body, {}, StringIO(body)) FOO = """\ ARMA virumque cano, Troiae qui primus ab oris Italiam, fato profugus, Laviniaque venit litora, multum ille et terris iactatus et alto vi superum saevae memorem Iunonis ob iram; multa quoque et bello passus, dum conderet urbem, inferretque deos Latio, genus unde Latinum, Albanique patres, atque altae moenia Romae. Musa, mihi causas memora, quo numine laeso, quidve dolens, regina deum tot volvere casus insignem pietate virum, tot adire labores impulerit. Tantaene animis caelestibus irae? """ FOO1 = """\ ARMA virumque cano, Troiae qui primus ab oris Italiam, fato profugus, Laviniaque venit litora, multum ille et terris iactatus et alto vi superum saevae memorem Iunonis ob iram; multa quoque et bello passus, dum conderet urbem, inferretque deos Latio, genus unde Latinum, Albanique patres, atque altae moenia Romae. Musa, mihi causas memora, quo numine laeso, """ FOO2 = """\ ARMA virumque cano, Troiae qui primus ab oris ARMA virumque cano, Troiae qui primus ab oris ARMA virumque cano, Troiae qui primus ab oris Italiam, fato profugus, Laviniaque venit litora, multum ille et terris iactatus et alto vi superum saevae memorem Iunonis ob iram; multa quoque et bello passus, dum conderet urbem, inferretque deos Latio, genus unde Latinum, Albanique patres, atque altae moenia Romae. Musa, mihi causas memora, quo numine laeso, """ FOO3 = """\ ARMA virumque cano, Troiae qui primus ab oris ARMA virumque cano, Troiae qui primus ab oris Italiam, fato profugus, Laviniaque venit litora, multum ille et terris iactatus et alto vi superum saevae memorem Iunonis ob iram; dum conderet urbem, inferretque deos Latio, genus unde Latinum, Albanique patres, atque altae moenia Romae. Albanique patres, atque altae moenia Romae. Musa, mihi causas memora, quo numine laeso, """ FOO4 = """\ Italiam, fato profugus, Laviniaque venit litora, multum ille et terris iactatus et alto vi superum saevae memorem Iunonis ob iram; dum conderet urbem, inferretque deos Latio, genus unde Latinum, Albanique patres, atque altae moenia Romae. Musa, mihi causas memora, quo numine laeso, """ FOO5 = """\ litora, multum ille et terris iactatus et alto Italiam, fato profugus, Laviniaque venit vi superum saevae memorem Iunonis ob iram; dum conderet urbem, Albanique patres, atque altae moenia Romae. Albanique patres, atque altae moenia Romae. Musa, mihi causas memora, quo numine laeso, inferretque deos Latio, genus unde Latinum, ARMA virumque cano, Troiae qui primus ab oris ARMA virumque cano, Troiae qui primus ab oris """ FOO6 = """\ ARMA virumque cano, Troiae qui primus ab oris ARMA virumque cano, Troiae qui primus ab oris Italiam, fato profugus, Laviniaque venit litora, multum ille et terris iactatus et alto vi superum saevae memorem Iunonis ob iram; dum conderet urbem, inferretque deos Latio, genus unde Latinum, Albanique patres, atque altae moenia Romae. Albanique patres, atque altae moenia Romae. Musa, mihi causas memora, quo numine laeso, """ EXPECTED_HG_DIFF_0 = """\ diff --git a/foo.txt b/foo.txt --- a/foo.txt +++ b/foo.txt @@ -6,7 +6,4 @@ inferretque deos Latio, genus unde Latinum, Albanique patres, atque altae moenia Romae. Musa, mihi causas memora, quo numine laeso, -quidve dolens, regina deum tot volvere casus -insignem pietate virum, tot adire labores -impulerit. Tantaene animis caelestibus irae? """ EXPECTED_HG_DIFF_1 = """\ diff --git a/foo.txt b/foo.txt --- a/foo.txt +++ b/foo.txt @@ -1,12 +1,11 @@ +ARMA virumque cano, Troiae qui primus ab oris ARMA virumque cano, Troiae qui primus ab oris Italiam, fato profugus, Laviniaque venit litora, multum ille et terris iactatus et alto vi superum saevae memorem Iunonis ob iram; -multa quoque et bello passus, dum conderet urbem, +dum conderet urbem, inferretque deos Latio, genus unde Latinum, Albanique patres, atque altae moenia Romae. +Albanique patres, atque altae moenia Romae. Musa, mihi causas memora, quo numine laeso, -quidve dolens, regina deum tot volvere casus -insignem pietate virum, tot adire labores -impulerit. Tantaene animis caelestibus irae? """ EXPECTED_HG_DIFF_2 = """\ diff --git a/foo.txt b/foo.txt --- a/foo.txt +++ b/foo.txt @@ -1,3 +1,5 @@ +ARMA virumque cano, Troiae qui primus ab oris +ARMA virumque cano, Troiae qui primus ab oris ARMA virumque cano, Troiae qui primus ab oris Italiam, fato profugus, Laviniaque venit litora, multum ille et terris iactatus et alto """ EXPECTED_HG_DIFF_3 = """\ diff --git a/foo.txt b/foo.txt --- a/foo.txt +++ b/foo.txt @@ -6,7 +6,4 @@ inferretque deos Latio, genus unde Latinum, Albanique patres, atque altae moenia Romae. Musa, mihi causas memora, quo numine laeso, -quidve dolens, regina deum tot volvere casus -insignem pietate virum, tot adire labores -impulerit. Tantaene animis caelestibus irae? """ EXPECTED_HG_SVN_DIFF_0 = """\ Index: foo.txt =================================================================== --- foo.txt\t(revision 4) +++ foo.txt\t(working copy) @@ -1,4 +1,1 @@ -ARMA virumque cano, Troiae qui primus ab oris -ARMA virumque cano, Troiae qui primus ab oris -ARMA virumque cano, Troiae qui primus ab oris Italiam, fato profugus, Laviniaque venit @@ -6,3 +3,8 @@ vi superum saevae memorem Iunonis ob iram; -multa quoque et bello passus, dum conderet urbem, +dum conderet urbem, + + + + + inferretque deos Latio, genus unde Latinum, """ EXPECTED_HG_SVN_DIFF_1 = """\ Index: foo.txt =================================================================== --- foo.txt\t(revision 4) +++ foo.txt\t(working copy) @@ -1,2 +1,1 @@ -ARMA virumque cano, Troiae qui primus ab oris ARMA virumque cano, Troiae qui primus ab oris @@ -6,6 +5,6 @@ vi superum saevae memorem Iunonis ob iram; -multa quoque et bello passus, dum conderet urbem, -inferretque deos Latio, genus unde Latinum, -Albanique patres, atque altae moenia Romae. -Musa, mihi causas memora, quo numine laeso, +dum conderet urbem, inferretque deos Latio, genus +unde Latinum, Albanique patres, atque altae +moenia Romae. Albanique patres, atque altae +moenia Romae. Musa, mihi causas memora, quo numine laeso, """ RBTools-0.3.4/RBTools.egg-info/0000755000175000017500000000000011640247307016670 5ustar chipx86chipx8600000000000000RBTools-0.3.4/RBTools.egg-info/dependency_links.txt0000644000175000017500000000006711640247307022752 0ustar chipx86chipx8600000000000000http://downloads.reviewboard.org/releases/RBTools/0.3/ RBTools-0.3.4/RBTools.egg-info/top_level.txt0000644000175000017500000000001011640247307021411 0ustar chipx86chipx8600000000000000rbtools RBTools-0.3.4/RBTools.egg-info/PKG-INFO0000644000175000017500000000116611640247307017771 0ustar chipx86chipx8600000000000000Metadata-Version: 1.0 Name: RBTools Version: 0.3.4 Summary: Command line tools for use with Review Board Home-page: http://www.reviewboard.org/ Author: Christian Hammond Author-email: chipx86@chipx86.com License: MIT Download-URL: http://downloads.reviewboard.org/releases/RBTools/0.3/ Description: UNKNOWN Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: Environment :: Console Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Topic :: Software Development RBTools-0.3.4/RBTools.egg-info/SOURCES.txt0000644000175000017500000000055411640247307020560 0ustar chipx86chipx8600000000000000AUTHORS COPYING INSTALL MANIFEST.in NEWS README ez_setup.py setup.cfg setup.py RBTools.egg-info/PKG-INFO RBTools.egg-info/SOURCES.txt RBTools.egg-info/dependency_links.txt RBTools.egg-info/entry_points.txt RBTools.egg-info/top_level.txt contrib/P4Tool.txt contrib/README.P4Tool contrib/internal/release.py rbtools/__init__.py rbtools/postreview.py rbtools/tests.pyRBTools-0.3.4/RBTools.egg-info/entry_points.txt0000644000175000017500000000007111640247307022164 0ustar chipx86chipx8600000000000000[console_scripts] post-review = rbtools.postreview:main RBTools-0.3.4/contrib/0000755000175000017500000000000011640247307015312 5ustar chipx86chipx8600000000000000RBTools-0.3.4/contrib/README.P4Tool0000644000175000017500000000113111640247306017305 0ustar chipx86chipx8600000000000000About P4Tool ------------ P4Tool.txt is an extension to P4win that adds support for invoking post-review from the UI. Installation ------------ 1) Make a copy of P4Tool.txt and modify it for your setup. Specifically, you'll need to replace "" with the path to the post-review script on your system. If using a compiled post-review.exe, place the path to this file and remove "python" before the file path. 2) Import P4Tool.txt into P4win. Usage ----- To post a change for review, right-click on the change and select "post-review". RBTools-0.3.4/contrib/P4Tool.txt0000644000175000017500000000020511640247306017170 0ustar chipx86chipx8600000000000000P4Win Tools for Review Board >>post-review python "" %C --p4-client $c --p4-port $p 1 0 1 0 0 1 0 0 RBTools-0.3.4/contrib/internal/0000755000175000017500000000000011640247307017126 5ustar chipx86chipx8600000000000000RBTools-0.3.4/contrib/internal/release.py0000755000175000017500000001277611640247306021137 0ustar chipx86chipx8600000000000000#!/usr/bin/env python # # Performs a release of Review Board. This can only be run by the core # developers with release permissions. # import hashlib import mimetools import os import shutil import subprocess import sys import tempfile import urllib2 sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..")) from rbtools import __version__, __version_info__, is_release PY_VERSIONS = ["2.4", "2.5", "2.6", "2.7"] LATEST_PY_VERSION = PY_VERSIONS[-1] PACKAGE_NAME = 'RBTools' RELEASES_URL = \ 'reviewboard.org:/var/www/downloads.reviewboard.org/' \ 'htdocs/releases/%s/%s.%s/' % (PACKAGE_NAME, __version_info__[0], __version_info__[1]) RBWEBSITE_API_URL = 'http://www.reviewboard.org/api/' RELEASES_API_URL = '%sproducts/rbtools/releases/' % RBWEBSITE_API_URL built_files = [] def load_config(): filename = os.path.join(os.path.expanduser('~'), '.rbwebsiterc') if not os.path.exists(filename): sys.stderr.write("A .rbwebsiterc file must exist in the form of:\n") sys.stderr.write("\n") sys.stderr.write("USERNAME = ''\n") sys.stderr.write("PASSWORD = ''\n") sys.exit(1) user_config = {} try: execfile(filename, user_config) except SyntaxError, e: sys.stderr.write('Syntax error in config file: %s\n' 'Line %i offset %i\n' % (filename, e.lineno, e.offset)) sys.exit(1) auth_handler = urllib2.HTTPBasicAuthHandler() auth_handler.add_password(realm='Web API', uri=RBWEBSITE_API_URL, user=user_config['USERNAME'], passwd=user_config['PASSWORD']) opener = urllib2.build_opener(auth_handler) urllib2.install_opener(opener) def execute(cmdline): if isinstance(cmdline, list): print ">>> %s" % subprocess.list2cmdline(cmdline) else: print ">>> %s" % cmdline p = subprocess.Popen(cmdline, shell=True, stdout=subprocess.PIPE) s = '' for data in p.stdout.readlines(): s += data sys.stdout.write(data) rc = p.wait() if rc != 0: print "!!! Error invoking command." sys.exit(1) return s def run_setup(target, pyver = LATEST_PY_VERSION): execute("python%s ./setup.py release %s" % (pyver, target)) def clone_git_tree(git_dir): new_git_dir = tempfile.mkdtemp(prefix='rbtools-release.') os.chdir(new_git_dir) execute('git clone %s .' % git_dir) return new_git_dir def build_targets(): for pyver in PY_VERSIONS: run_setup("bdist_egg", pyver) built_files.append("dist/%s-%s-py%s.egg" % (PACKAGE_NAME, __version__, pyver)) run_setup("sdist") built_files.append("dist/%s-%s.tar.gz" % (PACKAGE_NAME, __version__)) def build_checksums(): sha_filename = 'dist/%s-%s.sha256sum' % (PACKAGE_NAME, __version__) out_f = open(sha_filename, 'w') for filename in built_files: m = hashlib.sha256() in_f = open(filename, 'r') m.update(in_f.read()) in_f.close() out_f.write('%s %s\n' % (m.hexdigest(), os.path.basename(filename))) out_f.close() built_files.append(sha_filename) def upload_files(): execute("scp %s %s" % (" ".join(built_files), RELEASES_URL)) def tag_release(): execute("git tag release-%s" % __version__) def register_release(): if __version_info__[4] == 'final': run_setup("register") scm_revision = execute(['git rev-parse', 'release-%s' % __version__]) data = { 'major_version': __version_info__[0], 'minor_version': __version_info__[1], 'micro_version': __version_info__[2], 'release_type': __version_info__[3], 'release_num': __version_info__[4], 'scm_revision': scm_revision, } boundary = mimetools.choose_boundary() content = '' for key, value in data.iteritems(): content += '--%s\r\n' % boundary content += 'Content-Disposition: form-data; name="%s"\r\n' % key content += '\r\n' content += str(value) + '\r\n' content += '--%s--\r\n' % boundary content += '\r\n' headers = { 'Content-Type': 'multipart/form-data; boundary=%s' % boundary, 'Content-Length': str(len(content)), } print 'Posting release to reviewboard.org' try: f = urllib2.urlopen(urllib2.Request(url=RELEASES_API_URL, data=content, headers=headers)) f.read() except urllib2.HTTPError, e: print "Error uploading. Got HTTP code %d:" % e.code print e.read() except urllib2.URLError, e: try: print "Error uploading. Got URL error:" % e.code print e.read() except AttributeError: pass def main(): if not os.path.exists("setup.py"): sys.stderr.write("This must be run from the root of the " "Djblets tree.\n") sys.exit(1) load_config() if not is_release(): sys.stderr.write('This has not been marked as a release in ' 'rbtools/__init__.py\n') sys.exit(1) cur_dir = os.getcwd() git_dir = clone_git_tree(cur_dir) build_targets() build_checksums() upload_files() os.chdir(cur_dir) shutil.rmtree(git_dir) tag_release() register_release() if __name__ == "__main__": main() RBTools-0.3.4/MANIFEST.in0000644000175000017500000000021511640247306015405 0ustar chipx86chipx8600000000000000recursive-include contrib *.py *.txt README* include ez_setup.py include AUTHORS include COPYING include INSTALL include NEWS include README RBTools-0.3.4/PKG-INFO0000644000175000017500000000116611640247307014753 0ustar chipx86chipx8600000000000000Metadata-Version: 1.0 Name: RBTools Version: 0.3.4 Summary: Command line tools for use with Review Board Home-page: http://www.reviewboard.org/ Author: Christian Hammond Author-email: chipx86@chipx86.com License: MIT Download-URL: http://downloads.reviewboard.org/releases/RBTools/0.3/ Description: UNKNOWN Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: Environment :: Console Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Topic :: Software Development RBTools-0.3.4/INSTALL0000644000175000017500000000033311640247306014701 0ustar chipx86chipx8600000000000000Installation ============ To install rbtools, simply run the following as root: $ python setup.py install Or to automatically download and install the latest version, you can run: $ easy_install -U RBTools RBTools-0.3.4/COPYING0000644000175000017500000000212511640247306014704 0ustar chipx86chipx8600000000000000Copyright (c) 2007-2010 Christian Hammond Copyright (c) 2007-2010 David Trowbridge Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. RBTools-0.3.4/ez_setup.py0000644000175000017500000002400011640247306016055 0ustar chipx86chipx8600000000000000#!python """Bootstrap setuptools installation If you want to use setuptools in your package's setup.py, just include this file in the same directory with it, and add this to the top of your setup.py:: from ez_setup import use_setuptools use_setuptools() If you want to require a specific version of setuptools, set a download mirror, or use an alternate download directory, you can do so by supplying the appropriate options to ``use_setuptools()``. This file can also be run as a script to install or upgrade setuptools. """ import sys DEFAULT_VERSION = "0.6c11" DEFAULT_URL = "http://pypi.python.org/packages/%s/s/setuptools/" % sys.version[:3] md5_data = { 'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca', 'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb', 'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b', 'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a', 'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618', 'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac', 'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5', 'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4', 'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c', 'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b', 'setuptools-0.6c10-py2.3.egg': 'ce1e2ab5d3a0256456d9fc13800a7090', 'setuptools-0.6c10-py2.4.egg': '57d6d9d6e9b80772c59a53a8433a5dd4', 'setuptools-0.6c10-py2.5.egg': 'de46ac8b1c97c895572e5e8596aeb8c7', 'setuptools-0.6c10-py2.6.egg': '58ea40aef06da02ce641495523a0b7f5', 'setuptools-0.6c11-py2.3.egg': '2baeac6e13d414a9d28e7ba5b5a596de', 'setuptools-0.6c11-py2.4.egg': 'bd639f9b0eac4c42497034dec2ec0c2b', 'setuptools-0.6c11-py2.5.egg': '64c94f3bf7a72a13ec83e0b24f2749b2', 'setuptools-0.6c11-py2.6.egg': 'bfa92100bd772d5a213eedd356d64086', 'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27', 'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277', 'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa', 'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e', 'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e', 'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f', 'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2', 'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc', 'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167', 'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64', 'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d', 'setuptools-0.6c6-py2.3.egg': '35686b78116a668847237b69d549ec20', 'setuptools-0.6c6-py2.4.egg': '3c56af57be3225019260a644430065ab', 'setuptools-0.6c6-py2.5.egg': 'b2f8a7520709a5b34f80946de5f02f53', 'setuptools-0.6c7-py2.3.egg': '209fdf9adc3a615e5115b725658e13e2', 'setuptools-0.6c7-py2.4.egg': '5a8f954807d46a0fb67cf1f26c55a82e', 'setuptools-0.6c7-py2.5.egg': '45d2ad28f9750e7434111fde831e8372', 'setuptools-0.6c8-py2.3.egg': '50759d29b349db8cfd807ba8303f1902', 'setuptools-0.6c8-py2.4.egg': 'cba38d74f7d483c06e9daa6070cce6de', 'setuptools-0.6c8-py2.5.egg': '1721747ee329dc150590a58b3e1ac95b', 'setuptools-0.6c9-py2.3.egg': 'a83c4020414807b496e4cfbe08507c03', 'setuptools-0.6c9-py2.4.egg': '260a2be2e5388d66bdaee06abec6342a', 'setuptools-0.6c9-py2.5.egg': 'fe67c3e5a17b12c0e7c541b7ea43a8e6', 'setuptools-0.6c9-py2.6.egg': 'ca37b1ff16fa2ede6e19383e7b59245a', } import sys, os try: from hashlib import md5 except ImportError: from md5 import md5 def _validate_md5(egg_name, data): if egg_name in md5_data: digest = md5(data).hexdigest() if digest != md5_data[egg_name]: print >>sys.stderr, ( "md5 validation of %s failed! (Possible download problem?)" % egg_name ) sys.exit(2) return data def use_setuptools( version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, download_delay=15 ): """Automatically find/download setuptools and make it available on sys.path `version` should be a valid setuptools version number that is available as an egg for download under the `download_base` URL (which should end with a '/'). `to_dir` is the directory where setuptools will be downloaded, if it is not already available. If `download_delay` is specified, it should be the number of seconds that will be paused before initiating a download, should one be required. If an older version of setuptools is installed, this routine will print a message to ``sys.stderr`` and raise SystemExit in an attempt to abort the calling script. """ was_imported = 'pkg_resources' in sys.modules or 'setuptools' in sys.modules def do_download(): egg = download_setuptools(version, download_base, to_dir, download_delay) sys.path.insert(0, egg) import setuptools; setuptools.bootstrap_install_from = egg try: import pkg_resources except ImportError: return do_download() try: pkg_resources.require("setuptools>="+version); return except pkg_resources.VersionConflict, e: if was_imported: print >>sys.stderr, ( "The required version of setuptools (>=%s) is not available, and\n" "can't be installed while this script is running. Please install\n" " a more recent version first, using 'easy_install -U setuptools'." "\n\n(Currently using %r)" ) % (version, e.args[0]) sys.exit(2) except pkg_resources.DistributionNotFound: pass del pkg_resources, sys.modules['pkg_resources'] # reload ok return do_download() def download_setuptools( version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, delay = 15 ): """Download setuptools from a specified location and return its filename `version` should be a valid setuptools version number that is available as an egg for download under the `download_base` URL (which should end with a '/'). `to_dir` is the directory where the egg will be downloaded. `delay` is the number of seconds to pause before an actual download attempt. """ import urllib2, shutil egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3]) url = download_base + egg_name saveto = os.path.join(to_dir, egg_name) src = dst = None if not os.path.exists(saveto): # Avoid repeated downloads try: from distutils import log if delay: log.warn(""" --------------------------------------------------------------------------- This script requires setuptools version %s to run (even to display help). I will attempt to download it for you (from %s), but you may need to enable firewall access for this script first. I will start the download in %d seconds. (Note: if this machine does not have network access, please obtain the file %s and place it in this directory before rerunning this script.) ---------------------------------------------------------------------------""", version, download_base, delay, url ); from time import sleep; sleep(delay) log.warn("Downloading %s", url) src = urllib2.urlopen(url) # Read/write all in one block, so we don't create a corrupt file # if the download is interrupted. data = _validate_md5(egg_name, src.read()) dst = open(saveto,"wb"); dst.write(data) finally: if src: src.close() if dst: dst.close() return os.path.realpath(saveto) def main(argv, version=DEFAULT_VERSION): """Install or upgrade setuptools and EasyInstall""" try: import setuptools except ImportError: egg = None try: egg = download_setuptools(version, delay=0) sys.path.insert(0,egg) from setuptools.command.easy_install import main return main(list(argv)+[egg]) # we're done here finally: if egg and os.path.exists(egg): os.unlink(egg) else: if setuptools.__version__ == '0.0.1': print >>sys.stderr, ( "You have an obsolete version of setuptools installed. Please\n" "remove it from your system entirely before rerunning this script." ) sys.exit(2) req = "setuptools>="+version import pkg_resources try: pkg_resources.require(req) except pkg_resources.VersionConflict: try: from setuptools.command.easy_install import main except ImportError: from easy_install import main main(list(argv)+[download_setuptools(delay=0)]) sys.exit(0) # try to force an exit else: if argv: from setuptools.command.easy_install import main main(argv) else: print "Setuptools version",version,"or greater has been installed." print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)' def update_md5(filenames): """Update our built-in md5 registry""" import re for name in filenames: base = os.path.basename(name) f = open(name,'rb') md5_data[base] = md5(f.read()).hexdigest() f.close() data = [" %r: %r,\n" % it for it in md5_data.items()] data.sort() repl = "".join(data) import inspect srcfile = inspect.getsourcefile(sys.modules[__name__]) f = open(srcfile, 'rb'); src = f.read(); f.close() match = re.search("\nmd5_data = {\n([^}]+)}", src) if not match: print >>sys.stderr, "Internal error!" sys.exit(2) src = src[:match.start(1)] + repl + src[match.end(1):] f = open(srcfile,'w') f.write(src) f.close() if __name__=='__main__': if len(sys.argv)>2 and sys.argv[1]=='--md5update': update_md5(sys.argv[2:]) else: main(sys.argv[1:]) RBTools-0.3.4/AUTHORS0000644000175000017500000000140211640247306014716 0ustar chipx86chipx8600000000000000Lead Developers: * Christian Hammond * David Trowbridge Contributors: * Andrew Stitcher * Anthony Cruz * Ben Hollis * Bryan Halter * Chris Clark * Dan Savilonis * Dana Lacoste * Daniel Cestari * Daniel LaMotte * David Gardner * Dick Porter * Eric Huss * Flavio Castelli * Gyula Faller * Holden Karau * Ian Monroe * Jan Koprowski * Jason Felice * Jeremy Bettis * Laurent Nicolas * Lepton Wu * Luke Lu * Luke Robison * Matthew Woehlke * Mike Crute * Nathan Dimmock * Nathan Heijermans * Noah Kantrowitz * Paul Scott * Peter Ward * Petr Novák * Raghu Kaippully * Ravi Kondamuru * Ryan Oblak * Ryan Shelley * Severin Gehwolf * Stacey Sheldon * Stefan Ring * Steven Ihde * Steven Russell * Thilo-Alexander Ginkel * Tom Saeger RBTools-0.3.4/setup.cfg0000644000175000017500000000046411640247307015477 0ustar chipx86chipx8600000000000000[egg_info] tag_build = tag_svn_revision = 0 tag_date = 0 [aliases] snapshot = egg_info -Dr nightly = egg_info -dR alpha2 = egg_info -DRb alpha2 alpha1 = egg_info -DRb alpha1 beta2 = egg_info -DRb beta2 beta1 = egg_info -DRb beta1 rc1 = egg_info -DRb rc1 rc2 = egg_info -DRb rc2 release = egg_info -DRb '' RBTools-0.3.4/setup.py0000755000175000017500000000446511640247306015377 0ustar chipx86chipx8600000000000000#!/usr/bin/env python # # setup.py -- Installation for rbtools. # # Copyright (C) 2009 Christian Hammond # Copyright (C) 2009 David Trowbridge # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. from ez_setup import use_setuptools use_setuptools() from setuptools import setup, find_packages from setuptools.command.test import test from rbtools import get_package_version, is_release, VERSION PACKAGE_NAME = 'RBTools' if is_release(): download_url = "http://downloads.reviewboard.org/releases/%s/%s.%s/" % \ (PACKAGE_NAME, VERSION[0], VERSION[1]) else: download_url = "http://downloads.reviewboard.org/nightlies/" install_requires = [] try: import json except ImportError: install_requires.append('simplejson') setup(name=PACKAGE_NAME, version=get_package_version(), license="MIT", description="Command line tools for use with Review Board", entry_points = { 'console_scripts': [ 'post-review = rbtools.postreview:main', ], }, install_requires=install_requires, dependency_links = [ download_url, ], packages=find_packages(), include_package_data=True, maintainer="Christian Hammond", maintainer_email="chipx86@chipx86.com", url="http://www.reviewboard.org/", download_url=download_url, classifiers=[ "Development Status :: 4 - Beta", "Environment :: Console", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Software Development", ] ) RBTools-0.3.4/README0000644000175000017500000000065311640247306014535 0ustar chipx86chipx8600000000000000About rbtools ============= rbtools is a collection of console utility scripts for use with Review Board. This consists of the following officially supported tools: * post-review - Create and update review requests based on changes in a local tree. There are also some user-contributed scripts and application plugins in the contrib directory. See the associated README files for more information. RBTools-0.3.4/NEWS0000644000175000017500000000033311640247306014347 0ustar chipx86chipx8600000000000000Release Notes ============= Release notes for RBTools can be found in the reviewboard tree under docs/releasenotes/rbtools/. These can also be read online at http://www.reviewboard.org/docs/releasenotes/dev/rbtools/.