def push(ctx, repo_path, to, branch, force=False): env = git_env_for(repo_path) cmd = ["git", "push"] if force: cmd += ["--force"] cmd += [to, branch] io.communicate(cmd, env=env)
def clone_fork_branch(ctx, target, path, remote_name=DEFAULT_REMOTE_NAME, **kwds): """Clone, fork, and branch a repository ahead of building a pull request.""" git.checkout(ctx, target, path, branch=kwds.get("branch", None), remote="origin", from_branch="master") if kwds.get("fork"): try: fork(ctx, path, remote_name=remote_name, **kwds) except Exception: pass if 'GITHUB_USER' in os.environ: # On CI systems fork doesn't add a local remote under circumstances I don't quite understand, # but that's probably linked to https://github.com/cli/cli/issues/2722 cmd = [ 'git', 'remote', 'add', remote_name, f"https://github.com/{os.environ['GITHUB_USER']}/{os.path.basename(target)}" ] try: communicate(cmd, cwd=path) except RuntimeError: # Can add the remote only once pass return remote_name
def fork(ctx, path, remote_name=DEFAULT_REMOTE_NAME, **kwds): """Fork the target repository using ``gh``.""" gh_path = ensure_gh(ctx, **kwds) gh_env = get_gh_env(ctx, path, **kwds) cmd = [gh_path, "repo", "fork", '--remote=true', '--remote-name', remote_name] communicate(cmd, cwd=path, env=gh_env) return remote_name
def pull_request(ctx, path, message=None, **kwds): """Create a pull request against the origin of the path using ``hub``.""" hub_path = ensure_hub(ctx, **kwds) hub_env = get_hub_env(ctx, path, **kwds) cmd = [hub_path, "pull-request"] if message is not None: cmd.extend(["-m", message]) communicate(cmd, env=hub_env)
def push(ctx, repo_path, to=None, branch=None, force=False): env = git_env_for(repo_path) cmd = ["git", "push"] if force: cmd += ["--force"] if to and branch: cmd += ['-u', to, branch] io.communicate(cmd, env=env, cwd=repo_path)
def _try_download_hub(planemo_hub_path): link = _hub_link() # Strip URL base and .tgz at the end. basename = link.split("/")[-1].rsplit(".", 1)[0] untar_to(link, tar_args="-zxvf - %s/bin/hub -O > '%s'" % (basename, planemo_hub_path)) communicate(["chmod", "+x", planemo_hub_path])
def create_repository(ctx, owner, repo, dest, dry_run, **kwds): gh_path = ensure_gh(ctx, **kwds) gh_env = get_gh_env(ctx, dry_run=dry_run, **kwds) cmd = [gh_path, 'repo', 'create', '-y', '--public', "{owner}/{repo}".format(owner=owner, repo=repo)] if dry_run: "Would run command '{}'".format(" ".join(cmd)) git.init(ctx, dest) return dest communicate(cmd, env=gh_env, cwd=dest) return os.path.join(dest, repo)
def pull_request(ctx, path, message=None, **kwds): """Create a pull request against the origin of the path using ``gh``.""" gh_path = ensure_gh(ctx, **kwds) gh_env = get_gh_env(ctx, path, **kwds) cmd = [gh_path, "pr", "create"] if message is None: cmd.append('--fill') else: lines = message.splitlines() cmd.extend(['--title', lines[0]]) if len(lines) > 1: cmd.extend(["--body", "\n".join(lines[1:])]) communicate(cmd, env=gh_env)
def clone(*args, **kwds): """Clone a git repository. See :func:`command_clone` for description of arguments. """ command = command_clone(*args, **kwds) return io.communicate(command)
def _communicate(self, command_builder): stdout, _ = communicate( command_builder.command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) return stdout
def ls_remote(ctx, remote_repo): """Return a dictionary with refs as key and commits as value.""" commits_and_refs = io.communicate( ["git", "ls-remote", remote_repo], stdout=subprocess.PIPE, )[0] return dict(line.split()[::-1] for line in commits_and_refs.decode('utf-8').splitlines())
def log_contents(self): logs_command = docker_util.logs_command( **self.docker_target_kwds ) output, _ = communicate( logs_command ) return output
def diff(ctx, directory, range): """Produce a list of diff-ed files for commit range.""" cmd_template = "cd '%s' && git diff --name-only '%s' --" cmd = cmd_template % (directory, range) stdout, _ = io.communicate(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return [l.strip() for l in text_type(stdout).splitlines() if l]
def diff(ctx, directory, range): """Produce a list of diff-ed files for commit range.""" cmd_template = "cd '%s' && git diff --name-only '%s' --" cmd = cmd_template % (directory, range) stdout, _ = io.communicate( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) return [l.strip() for l in text_type(stdout).splitlines() if l]
def diff(ctx, directory, range): """Produce a list of diff-ed files for commit range.""" cmd_template = "cd '%s' && git diff --name-only '%s' --" cmd = cmd_template % (directory, range) stdout, _ = io.communicate(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) return [line.strip() for line in unicodify(stdout).splitlines() if line]
def log_contents(self): logs_command = docker_util.logs_command( self.server_name, **self.docker_target_kwds ) output, _ = communicate( logs_command ) return output
def checkout(ctx, remote_repo, local_path, branch=None, remote="origin", from_branch="master"): """Checkout a new branch from a remote repository.""" env = git_env_for(local_path) if not os.path.exists(local_path): io.communicate(command_clone(ctx, remote_repo, local_path)) else: io.communicate(["git", "fetch", remote], env=env) if branch: io.communicate(["git", "checkout", "%s/%s" % (remote, from_branch), "-b", branch], env=env) else: io.communicate(["git", "merge", "--ff-only", "%s/%s" % (remote, from_branch)], env=env)
def rev(ctx, directory): """Raw revision for git directory specified. Throws ``RuntimeError`` if not a git directory. """ cmd_template = "cd '%s' && git rev-parse HEAD" cmd = cmd_template % directory stdout, _ = io.communicate(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return unicodify(stdout).strip()
def test_io_capture(): """Test :func:`planemo.io.conditionally_captured_io`.""" with io.conditionally_captured_io(True, tee=False) as capture: io.warn("Problem...") assert_equal(capture[0]["data"], "Problem...") with io.conditionally_captured_io(True, tee=False) as capture: io.shell("echo 'Problem...'") assert_equal(capture[0]["data"], "echo 'Problem...'") assert_equal(capture[1]["data"], "Problem...") with io.conditionally_captured_io(True, tee=False) as capture: io.communicate("echo 'Problem...'") assert_equal(capture[0]["data"], "echo 'Problem...'") assert_equal(capture[1]["data"], "Problem...") with io.conditionally_captured_io(False, tee=False) as capture: io.communicate("echo 'Test...'") assert capture is None
def rev(ctx, directory): """ Raw revision for git directory specified. Throws ``RuntimeError`` if not a git directory. """ cmd_template = "cd '%s' && git rev-parse HEAD" cmd = cmd_template % directory stdout, _ = io.communicate( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) return stdout.strip()
def create_release(ctx, from_dir, target_dir, owner, repo, version, dry_run, notes="", **kwds): assert_new_version(ctx, version, owner=owner, repo=repo) target_repository_path = get_or_create_repository(ctx, owner=owner, repo=repo, dry_run=dry_run) add_dir_contents_to_repo(ctx, from_dir, target_dir, target_repository_path, version=version, dry_run=dry_run, notes=notes) gh_path = ensure_gh(ctx, **kwds) gh_env = get_gh_env(ctx, dry_run=dry_run, **kwds) cmd = [ gh_path, 'release', '-R', "{}/{}".format(owner, repo), 'create', "v{version}".format(version=version), '--title', str(version), ] cmd.extend(['--notes', notes or changelog_in_repo(target_repository_path)]) if not dry_run: communicate(cmd, env=gh_env) else: ctx.log("Would run command '{}'".format(" ".join(cmd)))
def add(ctx, repo_path, file_path): env = git_env_for(repo_path) io.communicate("cd '%s' && git add '%s'" % (repo_path, os.path.abspath(file_path)), env=env)
def branch(ctx, repo_path, branch, from_branch=None): env = git_env_for(repo_path) cmd = ["git", "checkout", "-b", branch] if from_branch is not None: cmd.append(from_branch) io.communicate(cmd, env=env)
def commit(ctx, repo_path, message=""): env = git_env_for(repo_path) io.communicate(["git", "commit", "-m", message], env=env)
def add(ctx, repo_path, file_path): env = git_env_for(repo_path) io.communicate( ["git", "add", os.path.relpath(file_path, repo_path)], env=env, cwd=repo_path)
def _try_download_hub(planemo_hub_path): link = _hub_link() # Strip URL base and .tgz at the end. basename = link.split("/")[-1].rsplit(".", 1)[0] untar_to(link, tar_args="-Ozxvf - %s/bin/hub > '%s'" % (basename, planemo_hub_path)) communicate(["chmod", "+x", planemo_hub_path])
def fork(ctx, path, **kwds): """Fork the target repository using ``hub``.""" hub_path = ensure_hub(ctx, **kwds) hub_env = get_hub_env(ctx, path, **kwds) cmd = [hub_path, "fork"] communicate(cmd, env=hub_env)
def init(ctx, repo_path): env = git_env_for(repo_path) io.communicate(["git", "init"], env=env)
def fork(ctx, path, **kwds): """Fork the target repository using ``hub``.""" gh_path = ensure_gh(ctx, **kwds) gh_env = get_gh_env(ctx, path, **kwds) cmd = [gh_path, "repo", "fork"] communicate(cmd, env=gh_env)