|
1 | | -""" |
2 | | -""" |
| 1 | +"""Utilities for interacting with Github.""" |
| 2 | +from __future__ import absolute_import |
| 3 | + |
| 4 | +import os |
| 5 | + |
| 6 | +from galaxy.tools.deps.commands import which |
| 7 | + |
| 8 | +from planemo import git |
| 9 | +from planemo.io import ( |
| 10 | + communicate, |
| 11 | + IS_OS_X, |
| 12 | + untar_to, |
| 13 | +) |
3 | 14 |
|
4 | 15 | try: |
5 | 16 | import github |
|
8 | 19 | github = None |
9 | 20 | has_github_lib = False |
10 | 21 |
|
| 22 | +HUB_VERSION = "2.2.8" |
| 23 | + |
11 | 24 | NO_GITHUB_DEP_ERROR = ("Cannot use github functionality - " |
12 | 25 | "PyGithub library not available.") |
| 26 | +FAILED_TO_DOWNLOAD_HUB = "No hub executable available and it could not be installed." |
13 | 27 |
|
14 | 28 |
|
15 | 29 | def get_github_config(ctx): |
| 30 | + """Return a :class:`planemo.github_util.GithubConfig` for given configuration.""" |
| 31 | + global_github_config = _get_raw_github_config(ctx) |
| 32 | + return None if global_github_config is None else GithubConfig(global_github_config) |
| 33 | + |
| 34 | + |
| 35 | +def clone_fork_branch(ctx, target, path, **kwds): |
| 36 | + """Clone, fork, and branch a repository ahead of building a pull request.""" |
| 37 | + git.checkout( |
| 38 | + ctx, |
| 39 | + target, |
| 40 | + path, |
| 41 | + branch=kwds.get("branch", None), |
| 42 | + remote="origin", |
| 43 | + from_branch="master" |
| 44 | + ) |
| 45 | + if kwds.get("fork"): |
| 46 | + fork(ctx, path, **kwds) |
| 47 | + |
| 48 | + |
| 49 | +def fork(ctx, path, **kwds): |
| 50 | + """Fork the target repository using ``hub``.""" |
| 51 | + hub_path = ensure_hub(ctx, **kwds) |
| 52 | + hub_env = get_hub_env(ctx, path, **kwds) |
| 53 | + cmd = [hub_path, "fork"] |
| 54 | + communicate(cmd, env=hub_env) |
| 55 | + |
| 56 | + |
| 57 | +def pull_request(ctx, path, message=None, **kwds): |
| 58 | + """Create a pull request against the origin of the path using ``hub``.""" |
| 59 | + hub_path = ensure_hub(ctx, **kwds) |
| 60 | + hub_env = get_hub_env(ctx, path, **kwds) |
| 61 | + cmd = [hub_path, "pull-request"] |
| 62 | + if message is not None: |
| 63 | + cmd.extend(["-m", message]) |
| 64 | + communicate(cmd, env=hub_env) |
| 65 | + |
| 66 | + |
| 67 | +def get_hub_env(ctx, path, **kwds): |
| 68 | + """Return a environment dictionary to run hub with given user and repository target.""" |
| 69 | + env = git.git_env_for(path).copy() |
| 70 | + github_config = _get_raw_github_config(ctx) |
| 71 | + if github_config is not None: |
| 72 | + if "username" in github_config: |
| 73 | + env["GITHUB_USER"] = github_config["username"] |
| 74 | + if "password" in github_config: |
| 75 | + env["GITHUB_PASSWORD"] = github_config["password"] |
| 76 | + |
| 77 | + return env |
| 78 | + |
| 79 | + |
| 80 | +def ensure_hub(ctx, **kwds): |
| 81 | + """Ensure ``hub`` is on the system ``PATH``. |
| 82 | +
|
| 83 | + This method will ensure ``hub`` is installed if it isn't available. |
| 84 | +
|
| 85 | + For more information on ``hub`` checkout ... |
| 86 | + """ |
| 87 | + hub_path = which("hub") |
| 88 | + if not hub_path: |
| 89 | + planemo_hub_path = os.path.join(ctx.workspace, "hub") |
| 90 | + if not os.path.exists(planemo_hub_path): |
| 91 | + _try_download_hub(planemo_hub_path) |
| 92 | + |
| 93 | + if not os.path.exists(planemo_hub_path): |
| 94 | + raise Exception(FAILED_TO_DOWNLOAD_HUB) |
| 95 | + |
| 96 | + hub_path = planemo_hub_path |
| 97 | + return hub_path |
| 98 | + |
| 99 | + |
| 100 | +def _try_download_hub(planemo_hub_path): |
| 101 | + link = _hub_link() |
| 102 | + # Strip URL base and .tgz at the end. |
| 103 | + basename = link.split("/")[-1].rsplit(".", 1)[0] |
| 104 | + untar_to(link, tar_args="-zxvf - %s/bin/hub -O > '%s'" % (basename, planemo_hub_path)) |
| 105 | + communicate(["chmod", "+x", planemo_hub_path]) |
| 106 | + |
| 107 | + |
| 108 | +def _get_raw_github_config(ctx): |
| 109 | + """Return a :class:`planemo.github_util.GithubConfig` for given configuration.""" |
16 | 110 | if "github" not in ctx.global_config: |
17 | 111 | return None |
18 | | - global_github_config = ctx.global_config["github"] |
19 | | - return GithubConfig(global_github_config) |
| 112 | + return ctx.global_config["github"] |
20 | 113 |
|
21 | 114 |
|
22 | 115 | class GithubConfig(object): |
| 116 | + """Abstraction around a Github account. |
| 117 | +
|
| 118 | + Required to use ``github`` module methods that require authorization. |
| 119 | + """ |
23 | 120 |
|
24 | 121 | def __init__(self, config): |
25 | 122 | if not has_github_lib: |
26 | 123 | raise Exception(NO_GITHUB_DEP_ERROR) |
27 | 124 | self._github = github.Github(config["username"], config["password"]) |
28 | 125 |
|
29 | 126 |
|
| 127 | +def _hub_link(): |
| 128 | + if IS_OS_X: |
| 129 | + template_link = "https://github.com/github/hub/releases/download/v%s/hub-darwin-amd64-%s.tgz" |
| 130 | + else: |
| 131 | + template_link = "https://github.com/github/hub/releases/download/v%s/hub-linux-amd64-%s.tgz" |
| 132 | + return template_link % (HUB_VERSION, HUB_VERSION) |
| 133 | + |
| 134 | + |
30 | 135 | def publish_as_gist_file(ctx, path, name="index"): |
| 136 | + """Publish a gist. |
| 137 | +
|
| 138 | + More information on gists at http://gist.github.com/. |
| 139 | + """ |
31 | 140 | github_config = get_github_config(ctx) |
32 | 141 | user = github_config._github.get_user() |
33 | 142 | content = open(path, "r").read() |
34 | 143 | content_file = github.InputFileContent(content) |
35 | 144 | gist = user.create_gist(False, {name: content_file}) |
36 | 145 | return gist.files[name].raw_url |
| 146 | + |
| 147 | + |
| 148 | +__all__ = [ |
| 149 | + "clone_fork_branch", |
| 150 | + "ensure_hub", |
| 151 | + "fork", |
| 152 | + "get_github_config", |
| 153 | + "get_hub_env", |
| 154 | + "publish_as_gist_file", |
| 155 | +] |
0 commit comments