def checkout(self, module, rootdir, newdir=None, branch=None, rev=None): if newdir is not None: srcdir = os.path.join(rootdir, newdir) else: srcdir = rootdir if not os.path.exists(srcdir): os.makedirs(srcdir) if os.path.exists(module): tarball_path = module else: # Download module to rootdir filename = os.path.basename(module).split('?')[0] tarball_path = os.path.join(srcdir, filename) cmd = get_download_command(module, tarball_path, '/dev/stdout') if cmd is None: return command = Command(cmd, srcdir) self._run_command(command, CHECKOUT) if not os.path.exists(tarball_path): return # Unpack the tarball fe = create_file_extractor(tarball_path) fe.extract(srcdir)
def diff(self, uri, branch=None, revs=None, files=None): self._check_srcdir(uri) cmd = ['cvs', '-z3', '-q', '-d', self.uri, 'diff', '-uN'] if revs is not None: for rev in revs: cmd.extend(['-r', rev]) if os.path.isfile(uri): cwd = os.path.dirname(uri) else: cwd = uri if files is not None: for file in files: cmd.append(file) else: cmd.append('.') command = Command(cmd, cwd) # If cvs is successful, it returns a successful status; if there is an error, # it prints an error message and returns a failure status. One # exception to this is the cvs diff command. It will return a successful status # if it found no differences, or a failure status if there were differences or if # there was an error. Because this behavior provides no good way to detect errors, # in the future it is possible that cvs diff will be changed to behave like # the other cvs commands. try: self._run_command(command, DIFF) except RepositoryCommandError, e: if e.returncode != 0 and not e.error: pass else: raise e
def get_info(uri, user=None, passwd=None): if os.path.isdir(uri): path = uri uri = '.' else: path = '.' if (user is not None) and (passwd is not None): cmd = ['svn', 'info', uri, '--username', user, '--password', passwd] else: cmd = ['svn', 'info', uri] command = Command(cmd, path, env={'LC_ALL': 'C'}) out = run_command_sync(command) retval = {} for line in out.splitlines(): if ':' not in line: continue key, value = line.split(':', 1) retval[key.lower().strip()] = value.strip() if retval == {} or (uri in retval and retval[uri] == '(Not a valid URL)'): return None return retval
def checkout(self, module, rootdir, newdir=None, branch=None, rev=None): # branch doesn't make sense here module == branch if newdir is not None: srcdir = os.path.join(rootdir, newdir) elif newdir == '.': srcdir = rootdir else: srcdir = os.path.join(rootdir, module) if os.path.exists(srcdir): try: self.update(srcdir, rev) return except RepositoryInvalidWorkingCopy: # If srcdir is not a valid working copy, # continue with the checkout pass cmd = ['bzr', 'branch', self.uri] if newdir is not None: cmd.append(newdir) else: cmd.append(module) command = Command(cmd, rootdir) self._run_command(command, CHECKOUT)
def diff(self, uri, branch=None, revs=None, files=None): self._check_uri(uri) if os.path.isfile(uri): cwd = self.__get_root_dir(uri) files = [uri[len(cwd):].strip("/")] elif os.path.isdir(uri): cwd = uri else: cwd = os.getcwd() cmd = ['git', 'diff'] if revs is not None: if len(revs) == 1: cmd.append(revs[0]) elif len(revs) > 1: cmd.append("%s..%s" % (revs[0], revs[1])) cmd.append("--") if files is not None: cmd.extend(files) command = Command(cmd, cwd, env={'PAGER': ''}) self._run_command(command, DIFF)
def show(self, uri, rev=None): self._check_uri(uri) if os.path.isfile(uri): cwd = self.__get_root_dir(uri) target = uri[len(cwd):].strip("/") elif os.path.isdir(uri): cwd = uri target = None else: cwd = os.getcwd() target = None cmd = ['git', 'show', '--pretty=format:'] if rev is not None: cmd.append(rev) cmd.append("--") if target is not None: cmd.append(target) command = Command(cmd, cwd, env={'PAGER': ''}) self._run_command(command, DIFF)
def blame(self, uri, rev=None, files=None, mc=False): # In cvs rev already includes the branch info # so no need for a branch parameter self._check_srcdir(uri) cmd = ['cvs', '-z3', '-q', '-d', self.uri, 'annotate'] if rev is not None: cmd.extend(['-r', rev]) if os.path.isfile(uri): directory = os.path.dirname(uri) target = os.path.basename(uri) else: directory = uri target = '.' if files is not None: for file in files: cmd.append(file) else: cmd.append(target) command = Command(cmd, directory) self._run_command(command, BLAME)
def ls(self, uri, rev=None): self._check_uri(uri) target = None if os.path.isfile(uri): cwd = os.path.dirname(uri) target = os.path.basename(uri) elif os.path.isdir(uri): cwd = uri else: cwd = os.getcwd() if rev is None: try: get_config(uri, 'remote.origin.url') rev = 'origin/master' except CommandError: rev = 'HEAD' cmd = ['git', 'ls-tree', '--name-only', '--full-name', '-r', rev] if target is not None: cmd.append(target) command = Command(cmd, cwd, env={'PAGER': ''}) self._run_command(command, LS)
def log(self, uri, rev=None, files=None): repo_uri = get_auth_info(uri)['uri'] self._check_uri(repo_uri) if os.path.isfile(repo_uri): cwd = os.path.dirname(repo_uri) target = '.' elif os.path.isdir(repo_uri): cwd = repo_uri target = '.' else: cwd = os.getcwd() target = repo_uri cmd = ['svn', '-v', 'log'] cmd = self._get_command_auth(cmd) if rev is not None: cmd.extend(['-r', rev]) if files is not None: if target != '.': cmd.append(target) for file in files: cmd.append(file) else: cmd.append(target) command = Command(cmd, cwd, env={'LC_ALL': 'C'}) self._run_command(command, LOG)
def blame(self, uri, rev=None, files=None, mc=False): self._check_uri(uri) if os.path.isfile(uri): cwd = os.path.dirname(uri) files = [os.path.basename(uri)] elif os.path.isdir(uri): cwd = uri else: cwd = os.getcwd() cmd = ['git', 'blame', '--root', '-l', '-t', '-f'] if mc: cmd.extend(['-M', '-C']) if rev is not None: cmd.append(rev) else: try: get_config(uri, 'remote.origin.url') cmd.append('origin/master') except CommandError: pass cmd.append('--') # Git doesn't support multiple files # we take just the first one cmd.append(files and files[0] or uri) command = Command(cmd, cwd, env={'PAGER': ''}) self._run_command(command, BLAME)
def get_repository_from_path(path): if os.path.isfile(path): path = os.path.dirname(path) pattern = re.compile("^[ \t]*(checkout of)?(parent)? branch:(.*)$") uri = None try: cmd = ['bzr', 'info'] command = Command(cmd, path, env={'LC_ALL': 'C'}) out = command.run_sync() for line in out.splitlines(): match = pattern.match(line) if not match: continue uri = match.group(3).strip() break except CommandError: raise RepositoryInvalidWorkingCopy( '"%s" does not appear to be a Bzr working copy' % path) if uri is None: raise RepositoryInvalidWorkingCopy( '"%s" does not appear to be a Bzr working copy' % path) return 'bzr', uri
def log(self, uri, rev=None, files=None, gitref=None): self._check_uri(uri) if os.path.isfile(uri): cwd = os.path.dirname(uri) files = [os.path.basename(uri)] elif os.path.isdir(uri): cwd = uri else: cwd = os.getcwd() cmd = [ 'git', 'log', '--topo-order', '--pretty=fuller', '--parents', '--name-status', '-M', '-C', '-c' ] # Git < 1.6.4 -> --decorate # Git = 1.6.4 -> broken # Git > 1.6.4 -> --decorate=full try: major, minor, micro = self._get_git_version() except ValueError: major, minor, micro, extra = self._get_git_version() if major <= 1 and minor < 6: cmd.append('--decorate') elif major <= 1 and minor == 6 and micro <= 4: cmd.append('--decorate') else: cmd.append('--decorate=full') if gitref: cmd.append(gitref) else: try: get_config(uri, 'remote.origin.url') if major <= 1 and minor < 8: cmd.append('origin') else: cmd.append('--remotes=origin') except CommandError: pass cmd.append('--all') if rev is not None: cmd.append(rev) if files: cmd.append('--') for file in files: cmd.append(file) elif cwd != uri: cmd.append(uri) command = Command(cmd, cwd, env={'PAGER': ''}) try: self._run_command(command, LOG) except RepositoryCommandError: pass
def get_config(path, option=None): if os.path.isfile(path): path = os.path.dirname(path) cmd = ['git', 'config'] if option is not None: cmd.extend(['--get', option]) else: cmd.extend(['-l']) command = Command(cmd, path, env={'PAGER': ''}) out = command.run_sync() if option is not None: return out.strip('\n\t ') retval = {} for line in out.splitlines(): if '=' not in line: continue key, value = line.split('=', 1) retval[key.lower().strip()] = value.strip('\n\t ') if retval == {}: return None return retval
def ls(self, uri, rev=None): self._check_uri(uri) if os.path.isfile(uri): cwd = os.path.dirname(uri) target = os.path.basename(uri) elif os.path.isdir(uri): cwd = uri target = '.' else: cwd = os.getcwd() target = uri cmd = ['svn', '-R', 'ls'] if rev is not None: if target == '.': cmd.extend(['-r', rev]) else: target += "@%s" % (rev) cmd.append(target) command = Command(cmd, cwd, env={'LC_ALL': 'C'}) self._run_command(command, LS)
def diff(self, uri, branch=None, revs=None, files=None): self._check_uri(uri) if os.path.isfile(uri): cwd = os.path.dirname(uri) target = '.' elif os.path.isdir(uri): cwd = uri target = '.' else: target = uri cwd = os.getcwd() cmd = ['svn', 'diff'] if revs is not None: if len(revs) == 1: cmd.extend(['-r', revs[0]]) elif len(revs) > 1: cmd.extend(['-r', revs[0] + ':' + revs[1]]) if files is not None: for file in files: if target == '.': cmd.append(file) else: cmd.append(os.path.join(target, file)) else: cmd.append(target) command = Command(cmd, cwd, env={'LC_ALL': 'C'}) self._run_command(command, DIFF)
def log(self, uri, rev=None, files=None, branch=None): self._check_uri(uri) if os.path.isfile(uri): cwd = os.path.dirname(uri) target = '.' elif os.path.isdir(uri): cwd = uri target = '.' else: cwd = os.getcwd() target = uri cmd = ['svn', '-v', 'log'] if rev is not None: cmd.extend(['-r', rev]) if files is not None: if target != '.': cmd.append(target) for file in files: cmd.append(file) else: cmd.append(target) command = Command(cmd, cwd, env={'LC_ALL': 'C'}) self._run_command(command, LOG)
def update(self, uri, rev=None, force=False): if not force: self._check_uri(uri) cmd = ['svn', 'update'] if rev is not None: cmd.extend(['-r', rev]) cmd.append(uri) command = Command(cmd, env={'LC_ALL': 'C'}) self._run_command(command, UPDATE)
def rlog(self, module, rev=None, files=None): cmd = ['cvs', '-z3', '-q', '-d', self.uri, 'rlog'] if rev is not None: cmd.extend(['-r', rev]) if files is not None: for file in files: cmd.append(os.path.join(module, file)) else: cmd.append(module) command = Command(cmd) self._run_command(command, LOG)
def update(self, uri, rev=None): self._check_uri(uri) #TODO: revision cmd = ['bzr', 'pull'] if os.path.isfile(uri): directory = os.path.dirname(uri) else: directory = uri command = Command(cmd, directory) self._run_command(command, UPDATE)
def get_last_revision(self, uri): self._check_uri(uri) cmd = ['bzr', 'revno'] command = Command(cmd, uri) try: out = command.run_sync() except: return None if out == "": return None return out.strip('\n\t ')
def update(self, uri, rev=None, force=False): repo_uri = get_auth_info(uri)['uri'] if not force: self._check_uri(repo_uri) cmd = ['svn', 'update'] cmd = self._get_command_auth(cmd) if rev is not None: cmd.extend(['-r', rev]) cmd.append(repo_uri) command = Command(cmd, env={'LC_ALL': 'C'}) self._run_command(command, UPDATE)
def get_last_revision(self, uri): self._check_uri(uri) cmd = ['git', 'rev-list', 'HEAD^..HEAD'] command = Command(cmd, uri, env={'PAGER': ''}) try: out = command.run_sync() except: return None if out == "": return None return out.strip('\n\t ')
def _checkout_branch(self, path, branch): self._check_uri(path) current, branches = self._get_branches(path) if branch in branches: if branches.index(branch) == current: return cmd = ['git', 'checkout', branch] else: cmd = ['git', 'checkout', '-b', branch, 'origin/%s' % (branch)] command = Command(cmd, path) command.run()
def update(self, uri, rev=None): self._check_uri(uri) branch = rev if branch is not None: self._checkout_branch(uri, branch) cmd = ['git', 'pull'] if os.path.isfile(uri): directory = os.path.dirname(uri) else: directory = uri command = Command(cmd, directory) self._run_command(command, UPDATE)
def list_files(uri): if os.path.isdir(uri): path = uri uri = '.' else: path = '.' cmd = ['svn', 'ls', uri] command = Command(cmd, path, env={'LC_ALL': 'C'}) out = run_command_sync(command) retval = [] for line in out.splitlines(): retval.append(line.strip()) return retval
def checkout(self, uri, rootdir, newdir=None, branch=None, rev=None, dateStamp=None): '''Checkout a module or path from repository @param uri: Module or path to check out. When using as a path it should be relative to the module being the module name the root. modulename/path/to/file ''' # TODO: In CVS branch and rev are incompatible, we should # raise an exception if both parameters are provided and # use them, it doesn't matter which, when only one is provided. if newdir is not None: srcdir = os.path.join(rootdir, newdir) elif newdir == '.' or uri == '.': srcdir = rootdir else: srcdir = os.path.join(rootdir, uri) if os.path.exists(srcdir): try: self.update(srcdir, rev, dateStamp) return except RepositoryInvalidWorkingCopy: # If srcdir is not a valid working copy, # continue with the checkout pass cmd = ['cvs', '-z3', '-q', '-d', self.uri, 'checkout', '-P'] if rev is not None: cmd.extend(['-r', rev]) if newdir is not None: cmd.extend(['-d', newdir]) if dateStamp is not None: cmd.extend(['-D', dateStamp]) cmd.append(uri) command = Command(cmd, rootdir) self._run_command(command, CHECKOUT)
def update(self, uri, rev=None): self._check_srcdir(uri) cmd = ['cvs', '-z3', '-q', '-d', self.uri, 'update', '-P', '-d'] if rev is not None: cmd.extend(['-r', rev]) if os.path.isfile(uri): directory = os.path.dirname(uri) cmd.append(os.path.basename(uri)) else: directory = uri cmd.append('.') command = Command(cmd, directory) self._run_command(command, UPDATE)
def size(self, uri, rev=None): self._check_uri(uri) cmd = ['git', 'cat-file', '-s'] cwd = self.__get_root_dir(uri) target = uri[len(cwd):].strip("/") if rev is not None: target = "%s:%s" % (rev, target) else: target = "HEAD:%s" % (target) cmd.append(target) command = Command(cmd, cwd, env={'PAGER': ''}) self._run_command(command, SIZE)
def checkout(self, module, rootdir, newdir=None, branch=None, rev=None): if newdir is not None: srcdir = os.path.join(rootdir, newdir) elif newdir == '.': srcdir = rootdir else: if module == '.': srcdir = os.path.join(rootdir, os.path.basename(self.uri.rstrip('/'))) else: srcdir = os.path.join(rootdir, module) if os.path.exists(srcdir): try: self.update(srcdir, rev) return except RepositoryInvalidWorkingCopy: # If srcdir is not a valid working copy, # continue with the checkout pass # module == '.' is a special case to download the whole repository if module == '.': uri = self.uri else: uri = os.path.join(self.uri, module) cmd = ['git', 'clone', uri] if newdir is not None: cmd.append(newdir) elif module == '.': cmd.append(os.path.basename(uri.rstrip('/'))) else: cmd.append(module) def ignore_progress_stderr(*args): return True command = Command(cmd, rootdir, error_handler_func=ignore_progress_stderr) self._run_command(command, CHECKOUT) if branch is not None: self._checkout_branch(srcdir, branch)
def _get_git_version(self): if self.git_version is not None: return self.git_version cmd = ['git', '--version'] command = Command(cmd) out = command.run_sync() # it could looks like: # git version 1.7.10.4 // 1.8.4.rc3 // 1.7.12.4 (Apple Git-37) version = out.replace("git version ", "") try: self.git_version = tuple([int(i) for i in version.split('.')]) except ValueError: self.git_version = tuple([int(i) for i in version.split('.')[0:3]]) return self.git_version