1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: UTF-8 -*- |
3
|
|
|
|
4
|
|
|
# Isomer - The distributed application framework |
5
|
|
|
# ============================================== |
6
|
|
|
# Copyright (C) 2011-2020 Heiko 'riot' Weinen <[email protected]> and others. |
7
|
|
|
# |
8
|
|
|
# This program is free software: you can redistribute it and/or modify |
9
|
|
|
# it under the terms of the GNU Affero General Public License as published by |
10
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
# (at your option) any later version. |
12
|
|
|
# |
13
|
|
|
# This program is distributed in the hope that it will be useful, |
14
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
# GNU Affero General Public License for more details. |
17
|
|
|
# |
18
|
|
|
# You should have received a copy of the GNU Affero General Public License |
19
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
|
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
Module: Misc |
24
|
|
|
============ |
25
|
|
|
|
26
|
|
|
Miscellaneous functionality for the management tool. |
27
|
|
|
|
28
|
|
|
""" |
29
|
|
|
import json |
30
|
|
|
import os |
31
|
|
|
import click |
32
|
|
|
import git |
33
|
|
|
import requests |
34
|
|
|
|
35
|
|
|
from isomer.error import abort |
36
|
|
|
from isomer.logger import error, debug |
37
|
|
|
from isomer.misc import sorted_alphanumerical |
38
|
|
|
|
39
|
|
|
from isomer.tool import log, run_process |
40
|
|
|
from isomer.tool.cli import cli |
41
|
|
|
from isomer.tool.defaults import source_api_url, pypi_api_url |
42
|
|
|
from isomer.version import version_info |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
@cli.command(short_help="Show running isomer tool version") |
46
|
|
|
def version(): |
47
|
|
|
"""Log the version information""" |
48
|
|
|
|
49
|
|
|
log("Tool version info:", version_info, os.path.dirname(__file__)) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def get_github_releases(): |
53
|
|
|
"""Get release data from Github.""" |
54
|
|
|
|
55
|
|
|
log("Getting the source api url") |
56
|
|
|
request = requests.get(source_api_url) |
57
|
|
|
|
58
|
|
|
if not request.ok: |
59
|
|
|
log("No repository data from github api!", lvl=error) |
60
|
|
|
return {} |
61
|
|
|
|
62
|
|
|
repo_item = json.loads(request.text or request.content) |
63
|
|
|
log("Isomer repository: ", repo_item, pretty=True, lvl=debug) |
64
|
|
|
tags_url = repo_item["tags_url"].split("{")[0] |
65
|
|
|
log("Releases url:", tags_url, lvl=debug) |
66
|
|
|
|
67
|
|
|
request = requests.get(tags_url) |
68
|
|
|
if not request.ok: |
69
|
|
|
log("No tag data from github api!", lvl=error) |
70
|
|
|
return {} |
71
|
|
|
|
72
|
|
|
release_items = json.loads(request.text or request.content) |
73
|
|
|
log("Isomer raw releases:", release_items, pretty=True, lvl=debug) |
74
|
|
|
releases = {} |
75
|
|
|
for item in release_items: |
76
|
|
|
releases[item["name"]] = item["tarball_url"] |
77
|
|
|
|
78
|
|
|
return releases |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
def get_pypi_releases(): |
82
|
|
|
"""Get release data from pypi.""" |
83
|
|
|
|
84
|
|
|
log("Getting the source api url") |
85
|
|
|
request = requests.get(pypi_api_url) |
86
|
|
|
|
87
|
|
|
if not request.ok: |
88
|
|
|
log("No data from pypi api!", lvl=error) |
89
|
|
|
return {} |
90
|
|
|
|
91
|
|
|
release_items = json.loads(request.text or request.content) |
92
|
|
|
log("Isomer raw releases:", release_items, pretty=True, lvl=debug) |
93
|
|
|
releases = {} |
94
|
|
|
for version_tag, items in release_items["releases"].items(): |
95
|
|
|
if len(items) == 0: |
96
|
|
|
continue |
97
|
|
|
for item in items: |
98
|
|
|
if item["python_version"] != "source": |
99
|
|
|
continue |
100
|
|
|
releases[version_tag] = item["url"] |
101
|
|
|
|
102
|
|
|
return releases |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
def get_git_releases(repository_path, fetch=False): |
106
|
|
|
"""Get release data from a git repository. Optionally, fetch from upstream first""" |
107
|
|
|
|
108
|
|
|
log("Getting git tags from", repository_path) |
109
|
|
|
|
110
|
|
|
releases = {} |
111
|
|
|
|
112
|
|
|
repo = git.Repo(repository_path) |
113
|
|
|
|
114
|
|
|
if fetch is True: |
115
|
|
|
log("Fetching origin tags") |
116
|
|
|
success, result = run_process( |
117
|
|
|
repository_path, |
118
|
|
|
["git", "fetch", "--tags", "-v", "origin"], |
119
|
|
|
) |
120
|
|
|
if not success: |
121
|
|
|
log(result, lvl=error) |
122
|
|
|
|
123
|
|
|
#origin = repo.remotes["origin"] |
124
|
|
|
#log("Origin:", list(origin.urls), pretty=True) |
125
|
|
|
#origin.fetch(tags=True) |
126
|
|
|
|
127
|
|
|
tags = repo.tags |
128
|
|
|
|
129
|
|
|
log("Raw tags:", tags, pretty=True, lvl=debug) |
130
|
|
|
|
131
|
|
|
for item in tags: |
132
|
|
|
releases[str(item)] = item |
|
|
|
|
133
|
|
|
|
134
|
|
|
return releases |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
@cli.command(short_help="Check software sources for installable Versions") |
138
|
|
|
@click.option( |
139
|
|
|
"--source", "-s", default=None, |
140
|
|
|
type=click.Choice(["link", "copy", "git", "github", "pypi"]), |
141
|
|
|
help="Override instance source (link, copy, git, github)" |
142
|
|
|
) |
143
|
|
|
@click.option("--url", "-u", default="", type=click.Path()) |
144
|
|
|
@click.option("--fetch", "-f", default=False, is_flag=True, |
145
|
|
|
help="Fetch the newest updates on a git repository") |
146
|
|
|
@click.pass_context |
147
|
|
|
def versions(ctx, source, url, fetch): |
148
|
|
|
"""Check instance sources for installable versions""" |
149
|
|
|
|
150
|
|
|
releases = _get_versions(ctx, source, url, fetch) |
151
|
|
|
|
152
|
|
|
releases_keys = sorted_alphanumerical(releases.keys()) |
153
|
|
|
|
154
|
|
|
log("Available Isomer releases:", releases_keys, pretty=True) |
155
|
|
|
log("Latest:", releases_keys[-1]) |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
def _get_versions(ctx, source, url, fetch): |
159
|
|
|
|
160
|
|
|
instance_configuration = ctx.obj["instance_configuration"] |
161
|
|
|
|
162
|
|
|
source = source if source is not None else instance_configuration["source"] |
163
|
|
|
|
164
|
|
|
releases = {} |
165
|
|
|
|
166
|
|
|
if source == "github": |
167
|
|
|
releases = get_github_releases() |
168
|
|
|
elif source == "pypi": |
169
|
|
|
releases = get_pypi_releases() |
170
|
|
|
elif source == "git": |
171
|
|
|
if url is not "": |
172
|
|
|
repo = url |
173
|
|
|
else: |
174
|
|
|
repo = instance_configuration["url"] |
175
|
|
|
releases = get_git_releases(repo, fetch) |
176
|
|
|
else: |
177
|
|
|
log("Other methods to acquire versions than github are currently WiP") |
178
|
|
|
abort(60001) |
179
|
|
|
|
180
|
|
|
return releases |
181
|
|
|
|