示例#1
0
def main():
    valid = False

    if len(sys.argv) > 2:
        settings = Settings(config_file='rb_scripts.dat')
        cookie = settings.get_cookie_file()
        server_url = settings.get_server_url()
        resource_id = sys.argv[2]

        if resource_id.isdigit():
            if sys.argv[1] == SUBMITTED_OPTION \
                or sys.argv[1] == DISCARDED_OPTION:
                valid = True
                server = ServerInterface(server_url, cookie)

                try:
                    root = RootResource(server, server_url + 'api/')
                    review_requests = root.get('review_requests')
                    review_request = \
                        ReviewRequest(review_requests.get(resource_id))

                    if sys.argv[1] == SUBMITTED_OPTION:
                        review_request.submit()
                    else:
                        review_request.discard()

                    print 'Successfully %s review request #%s' % \
                        (close_type(sys.argv[1]), resource_id)
                except urllib2.HTTPError, e:
                    print 'Close failed..  Make sure the resource exists on ' \
                          'the server and try again.'
示例#2
0
    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 SvnRepository object (with a different path).
        """
        root = RootResource(server, server.server_url + "/api")
        repo_list = root.get("repositories")

        for repository in repo_list:

            if repository.get_field("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 SvnRepository(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
示例#3
0
def main():
    valid = False

    if len(sys.argv) > 1:
        settings = Settings(config_file='rb_scripts.dat')
        cookie = settings.get_cookie_file()
        server_url = settings.get_server_url()
        resource_id = sys.argv[1]

        if resource_id.isdigit():
            valid = True
            server = ServerInterface(server_url, cookie)
            root = RootResource(server, server_url + 'api/')
            review_requests = root.get('review_requests')
            review_request = ReviewRequest(review_requests.get(resource_id))
            review_request.reopen()

    if not valid:
        print "usage: rb open <review_request_id>"
示例#4
0
def main():
    valid = False
    resource_map = {}
    resource_map[INFO] = 'info'
    resource_map[USER] = 'users'
    resource_map[ROOT] = 'self'
    resource_map[REPOSITORY] = 'repositories'
    resource_map[SESSION] = 'session'
    resource_map[GROUP] = 'groups'
    resource_map[REVIEW_REQUEST] = 'review_requests'

    if len(sys.argv) > 1:
        settings = Settings(config_file='rb_scripts.dat')
        cookie = settings.get_cookie_file()
        server_url = settings.get_server_url()

        if re.match('-', sys.argv[1]):
            valid = True
            resource_name = re.split('-', sys.argv[1])[1]
            server = ServerInterface(server_url, cookie)
            root = RootResource(server, server_url + 'api/')
            resource_list = root.get(resource_map[resource_name])

            if len(sys.argv) > 2 and sys.argv[2]:
                resource_id = sys.argv[2]
                resource = resource_list.get(resource_id)
                print resource
            else:
                print resource_list

    if not valid:
        print "usage: rb info -resource_name [resource_id]"
        print ""
        print "resource_names:"
        for n in RESOURCE_NAMES:
            print "     %s" % n
示例#5
0
def main():
    valid = True

    if len(sys.argv) > 0:
        settings = Settings(config_file='rb_scripts.dat')
        cookie = settings.get_cookie_file()
        server_url = settings.get_server_url()
        client = get_client(server_url)
        server_repo = client.get_info()
        diff, parent_diff = client.diff(None)
        diff_file = open('diff', 'w')
        diff_file.write(diff)
        diff_file.close()
        diff_data = {
            'filename': 'diff',
            'content': diff
        }
        parent_diff_data = None

        if parent_diff:
            parent_diff_data = {}
            parent_diff_file = open('parent_diff', 'w')
            parent_diff_file.write(parent_diff)
            parent_diff_file.close()
            parent_diff_path['filename'] = 'parent_diff'
            parent_diff_path['content'] = parent_diff

        server = ServerInterface(server_url, cookie)
        root = RootResource(server, server_url + 'api/')
        repositories = RepositoryList(root.get('repositories'))
        repo_id = repositories.get_repository_id(server_repo.path)

        if repo_id:
            try:
                review_requests = root.get('review_requests')
                review_request = review_requests.create()
                review_request.update_field('submit_as',
                                            settings.get_setting('user_name'))
                review_request.update_field('repository', str(repo_id))
                review_request.save()
                diffs = review_request.get_or_create('diffs')
                resource_diff = diffs.create()
                resource_diff.update_file('path', diff_data)

                if server_repo.base_path:
                    resource_diff.update_field('basedir',
                                               server_repo.base_path)

                if parent_diff_data:
                    diff.update_file('parent_diff_path', parent_diff_data)

                resource_diff.save()
                review_request_draft = ReviewRequestDraft(
                    review_request.get_or_create('draft'))
                review_request_draft.publish()
                print "Review request created at: %s\n" % review_request.url
                print "To view the review request in a browser go to: " \
                    "%sr/%s/" % (server_url, review_request.get_field('id'))
            except Error, e:
                raise e
        else:
            print "The repository could not be found on the server."
示例#6
0
def diff(args):
    valid = False
    resource_map = {GET: 'diffs', VIEW: 'diffs'}
    settings = Settings()

    if len(args) > 0:  # command given
        cwd = os.getcwd()
        cookie_file = settings.get_cookie_file()
        cookie = os.path.join(cwd, cookie_file)
        server_url = settings.get_server_url()
        command = args[0]

        if command[0] == '-':
            command = command[1:]

        if RESOURCE_NAMES.count(command) == 1:
            valid = True

            if command == MAKE:
                """builds a local diff

                builds a diff of the local repository. call is:
                rb -diff <diff_file> <args>

                    diff_file: optional, the name of the file to write to
                    args: arguments required for build a diff. Unneaded for
                          most clients, but some (e.g. Perforce require it)
                """
                client = get_client(server_url)

                if client == None:
                    print 'could not find the source control manager'
                    exit()

                #determine the file to save to
                diff_file = None
                if len(args) > 1:
                    diff_file = args[1]
                else:
                    diff_file = settings.get_setting('client_diff')

                diff_args = None

                #diff_args is only used for certain clients (e.g. Perforce)
                if len(args) > 2:
                    diff_args = args[2]

                diff = client.diff(diff_args)

                #write the diff
                file = open(diff_file, 'w')

                if file < 0:
                    print 'could not open the file ' + diff_file + \
                            ' to write the diff.'

                diff = diff[0].split('\n')
                for line in diff:
                    file.write(line + '\n')

                file.close()

            elif command == VIEW or command == GET:
                """
                Viewing info about a diff and requesting the physical diff
                is almost completely the same operation, both are stored at
                the same location on the server. The only difference in the
                request is that to get the diff file, a specific mime-type
                is listed in the Accept header of the get request
                (currently text/x-patch, it is defined in config.dat).
                """
                resource_name = resource_map[command]

                #establish a connection to the server
                server = ServerInterface(server_url, cookie)
                root = RootResource(server, server_url \
                                + settings.get_api_uri())

                #find the review
                if len(args) > 1 and args[1].isdigit():
                    id = args[1]
                    review_requests = root.get('review_requests')
                    
                    try:
                        request = ReviewRequest(review_requests.get(id))
                    except HTTPError:
                        print 'Unknown review request id: ' + id
                        exit()

                    diffs = ResourceList(server, \
                                    request.get_link(resource_name))

                    """find out which diff is required

                    find the required diff. If no preference is given, will
                    default to diff 1. If diff_id of 'all' is requested,
                    the operation (VIEW or GET) will be run on each diff,
                    one at a time. In this case, diff files will be
                    prefixed with <diff_num>_.
                    """
                    diff_id = '1' if len(args) < 3 \
                                  else str(args[2])
                    if diff_id.isdigit():
                        #single diff
                        diff = DiffResource(diffs.get(diff_id))

                        if command == VIEW:
                            #VIEW will display all fields unless
                            #a specific field is requested
                            if len(args) > 3 and args[3] != 'all':
                                print diff.get_field(args[3])
                            else:
                                keys = diff.get_fields()

                                for key in keys:
                                    print str(key) + ': ' \
                                        + str(diff.get_field(key))
                        else:  # GET
                            if len(args) > 3:
                                diff_file = args[3]
                            else:
                                diff_file = \
                                    settings.get_setting('server_diff')

                            sd = diff.get_file( \
                                    settings.get_setting('diff_mime'))

                            file = open(diff_file, 'w')

                            if file < 0:
                                print 'could not open "' + diff_file \
                                                + '" for writing.'
                                exit()

                            file.write(sd)
                            file.close()
                    elif diff_id == 'all':
                        #deal with each dif, one at a time
                        ids = diffs.get_fields()
                        for diff_id in ids:
                            diff = DiffResource(diffs.get(diff_id))
                            print 'diff ' + str(diff_id) + ':'

                            if command == VIEW:
                                #VIEW will display all fields
                                #unless a specific field is requested
                                if len(args) > 3 and \
                                            args[3] != 'all':
                                    print diff.get_field(args[3])
                                else:
                                    keys = diff.get_fields()

                                    for key in keys:
                                        print '\t' + str(key) + ': ' \
                                            + str(diff.get_field(key))
                            else:  # GET
                                if len(args) > 3:
                                    diff_file = args[3]
                                else:
                                    diff_file = \
                                        settings.get_setting('server_diff')
                                diff_file = str(diff_id) + '_' + diff_file

                                sd = diff.get_file( \
                                    settings.get_setting('diff_mime'))

                                file = open(diff_file, 'w')

                                if file < 0:
                                    print 'could not open "' + diff_file \
                                                    + '" for writing.'
                                    exit()
                    else:
                        print diff_id + ' is not a valid diff ID'
                else:
                    if len(args) > 1:
                        print args[1] + ' is not a valid resource ID'
                    else:
                        print command + ' needs a valid resource ID'

        else:
            print 'Invalid command: ' + command

    if not valid:
        print 'usage rb diff -resource_name [resource_id]\nresource_names:'
        for name in RESOURCE_NAMES:
            print '     ' + name
示例#7
0
def main():
    valid = False

    if len(sys.argv) > 2:
        settings = Settings(config_file='rb_scripts.dat')
        cookie = settings.get_cookie_file()
        server_url = settings.get_server_url()
        resource_id = sys.argv[2]

        if resource_id.isdigit():
            split = re.split(':', sys.argv[1])
            file_type = split[0]
            file_name = split[1]

            if file_type and file_name:
                if file_type == SCREENSHOT_OPTION \
                    or file_type == DIFF_OPTION:
                    valid = True
                    server = ServerInterface(server_url, cookie)
                    root = RootResource(server, server_url + 'api/')
                    review_requests = root.get('review_requests')
                    review_request = review_requests.get(resource_id)
                    m = re.match(SCREENSHOT_OPTION, file_type)

                    try:
                        if m:
                            # Screenshot
                            ss_file = open('%s' % file_name, 'r')
                            ss_data = {
                                'filename': os.path.split(file_name)[1],
                                'content': ss_file.read()
                            }

                            sss = review_request.get_or_create('screenshots')
                            ss = sss.create()
                            ss.update_file('path', ss_data)

                            if len(sys.argv) > 3:
                                i = 3

                                while i < len(sys.argv):
                                    split = re.split(':', sys.argv[i])
                                    ss.update_field(split[0], split[1])
                                    i = i + 1

                            ss.save()
                        else:
                            # Diff
                            diff_file = open(file_name, 'r')
                            diff_data = {
                                'filename': file_name,
                                'content': diff_file.read()
                            }
                            diff_file.close()
                            diffs = review_request.get_or_create('diffs')
                            resource_diff = diffs.create()
                            resource_diff.update_file('path', diff_data)
                            resource_diff.save()

                        review_request_draft = \
                            ReviewRequestDraft(
                            review_request.get_or_create('draft'))
                        review_request_draft.publish()
                        print "Request complete.  See: %s" % review_request.url
                    except urllib2.HTTPError, e:
                        if e.code == 500:
                            print "There was an internal server error."
                            print "Please try your request again."
                        else:
                            print "The request failed."
                            print e.read()
                            raise e