comparison env/lib/python3.7/site-packages/planemo/github_util.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 """Utilities for interacting with Github."""
2 from __future__ import absolute_import
3
4 import os
5
6 from galaxy.tool_util.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 )
14
15 try:
16 import github
17 has_github_lib = True
18 except ImportError:
19 github = None
20 has_github_lib = False
21
22 HUB_VERSION = "2.2.8"
23
24 NO_GITHUB_DEP_ERROR = ("Cannot use github functionality - "
25 "PyGithub library not available.")
26 FAILED_TO_DOWNLOAD_HUB = "No hub executable available and it could not be installed."
27
28
29 def get_github_config(ctx, allow_anonymous=False):
30 """Return a :class:`planemo.github_util.GithubConfig` for given configuration."""
31 global_github_config = _get_raw_github_config(ctx)
32 return GithubConfig(global_github_config, allow_anonymous=allow_anonymous)
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 try:
47 fork(ctx, path, **kwds)
48 except Exception:
49 pass
50
51
52 def fork(ctx, path, **kwds):
53 """Fork the target repository using ``hub``."""
54 hub_path = ensure_hub(ctx, **kwds)
55 hub_env = get_hub_env(ctx, path, **kwds)
56 cmd = [hub_path, "fork"]
57 communicate(cmd, env=hub_env)
58
59
60 def pull_request(ctx, path, message=None, **kwds):
61 """Create a pull request against the origin of the path using ``hub``."""
62 hub_path = ensure_hub(ctx, **kwds)
63 hub_env = get_hub_env(ctx, path, **kwds)
64 cmd = [hub_path, "pull-request"]
65 if message is not None:
66 cmd.extend(["-m", message])
67 communicate(cmd, env=hub_env)
68
69
70 def get_hub_env(ctx, path, **kwds):
71 """Return a environment dictionary to run hub with given user and repository target."""
72 env = git.git_env_for(path).copy()
73 github_config = _get_raw_github_config(ctx)
74 if github_config is not None:
75 if "username" in github_config:
76 env["GITHUB_USER"] = github_config["username"]
77 if "password" in github_config:
78 env["GITHUB_PASSWORD"] = github_config["password"]
79
80 return env
81
82
83 def ensure_hub(ctx, **kwds):
84 """Ensure ``hub`` is on the system ``PATH``.
85
86 This method will ensure ``hub`` is installed if it isn't available.
87
88 For more information on ``hub`` checkout ...
89 """
90 hub_path = which("hub")
91 if not hub_path:
92 planemo_hub_path = os.path.join(ctx.workspace, "hub")
93 if not os.path.exists(planemo_hub_path):
94 _try_download_hub(planemo_hub_path)
95
96 if not os.path.exists(planemo_hub_path):
97 raise Exception(FAILED_TO_DOWNLOAD_HUB)
98
99 hub_path = planemo_hub_path
100 return hub_path
101
102
103 def _try_download_hub(planemo_hub_path):
104 link = _hub_link()
105 # Strip URL base and .tgz at the end.
106 basename = link.split("/")[-1].rsplit(".", 1)[0]
107 untar_to(link, tar_args=['-zxvf', '-', "%s/bin/hub" % basename], path=planemo_hub_path)
108 communicate(["chmod", "+x", planemo_hub_path])
109
110
111 def _get_raw_github_config(ctx):
112 """Return a :class:`planemo.github_util.GithubConfig` for given configuration."""
113 if "github" not in ctx.global_config:
114 if "GITHUB_USER" in os.environ and "GITHUB_PASSWORD" in os.environ:
115 return {
116 "username": os.environ["GITHUB_USER"],
117 "password": os.environ["GITHUB_PASSWORD"],
118 }
119 if "github" not in ctx.global_config:
120 raise Exception("github account not found in planemo config and GITHUB_USER / GITHUB_PASSWORD environment variables unset")
121 return ctx.global_config["github"]
122
123
124 class GithubConfig(object):
125 """Abstraction around a Github account.
126
127 Required to use ``github`` module methods that require authorization.
128 """
129
130 def __init__(self, config, allow_anonymous=False):
131 if not has_github_lib:
132 raise Exception(NO_GITHUB_DEP_ERROR)
133 if "username" not in config or "password" not in config:
134 if not allow_anonymous:
135 raise Exception("github authentication unavailable")
136 github_object = github.Github()
137 else:
138 github_object = github.Github(config["username"], config["password"])
139 self._github = github_object
140
141
142 def _hub_link():
143 if IS_OS_X:
144 template_link = "https://github.com/github/hub/releases/download/v%s/hub-darwin-amd64-%s.tgz"
145 else:
146 template_link = "https://github.com/github/hub/releases/download/v%s/hub-linux-amd64-%s.tgz"
147 return template_link % (HUB_VERSION, HUB_VERSION)
148
149
150 def publish_as_gist_file(ctx, path, name="index"):
151 """Publish a gist.
152
153 More information on gists at http://gist.github.com/.
154 """
155 github_config = get_github_config(ctx, allow_anonymous=False)
156 user = github_config._github.get_user()
157 with open(path, "r") as fh:
158 content = fh.read()
159 content_file = github.InputFileContent(content)
160 gist = user.create_gist(False, {name: content_file})
161 return gist.files[name].raw_url
162
163
164 def get_repository_object(ctx, name):
165 github_object = get_github_config(ctx, allow_anonymous=True)
166 return github_object._github.get_repo(name)
167
168
169 __all__ = (
170 "clone_fork_branch",
171 "ensure_hub",
172 "fork",
173 "get_github_config",
174 "get_hub_env",
175 "publish_as_gist_file",
176 )