-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.py
More file actions
246 lines (205 loc) · 7.57 KB
/
app.py
File metadata and controls
246 lines (205 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python3
# Copyright(C) 2020 Fridolin Pokorny
#
# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""A reverse solver implementation for project Thoth."""
from typing import Any
from typing import Dict
from typing import List
import logging
import os
import sys
import time
import click
from thoth.analyzer import print_command_result
from thoth.common import init_logging
from thoth.common import OpenShift
from thoth.common import __version__ as __common__version__
from thoth.python import PackageVersion
from thoth.storages import GraphDatabase
from thoth.storages import __version__ as __storages__version__
from prometheus_client import Gauge, CollectorRegistry, push_to_gateway
__title__ = "thoth-revsolver"
__version__ = "0.2.13"
__component_version__ = f"{__version__}+storage.{__storages__version__}.common.{__common__version__}"
init_logging()
_LOGGER = logging.getLogger("thoth.revsolver")
_QUERY_COUNT = int(os.getenv("THOTH_REVSOLVER_QUERY_COUNT", GraphDatabase.DEFAULT_COUNT))
# metrics
THOTH_METRICS_PUSHGATEWAY_URL = os.getenv("PROMETHEUS_PUSHGATEWAY_URL")
THOTH_DEPLOYMENT_NAME = os.getenv("THOTH_DEPLOYMENT_NAME")
PROMETHEUS_REGISTRY = CollectorRegistry()
database_schema_revision_script = Gauge(
"thoth_database_schema_revision_script",
"Thoth database schema revision from script",
["component", "revision", "env"],
registry=PROMETHEUS_REGISTRY,
)
def do_solve(package_name: str, package_version: str) -> List[Dict[str, Any]]:
"""Obtain dependent packages for the given package, respect registered solvers in Thoth."""
graph = GraphDatabase()
graph.connect()
openshift = OpenShift()
result = []
for solver_name in openshift.get_solver_names():
_LOGGER.info("Obtaining dependencies for environment used by solver %r", solver_name)
solver_info = openshift.parse_python_solver_name(solver_name)
start_offset = 0
while True:
dependents = graph.get_python_package_version_dependents_all(
package_name=package_name,
os_name=solver_info["os_name"],
os_version=solver_info["os_version"],
python_version=solver_info["python_version"],
start_offset=start_offset,
count=_QUERY_COUNT,
)
for dependent in dependents:
if (
dependent["version_range"]
and dependent["version_range"] != "*"
and package_version not in PackageVersion.parse_semantic_version(dependent["version_range"])
):
_LOGGER.info(
"Package %r in version %r from %r ignored, not matching version specifier %r in environment %r",
dependent["package_name"],
dependent["package_version"],
dependent["index_url"],
package_version,
solver_name,
)
continue
_LOGGER.info(
"Found dependent %r in version %r from %r in environment %r",
dependent["package_name"],
dependent["package_version"],
dependent["index_url"],
solver_name,
)
result.append(
{
"os_name": solver_info["os_name"],
"os_version": solver_info["os_version"],
"python_version": solver_info["python_version"],
"solver_name": solver_name,
**dependent,
}
)
if len(dependents) < int(_QUERY_COUNT):
break
# Adjust for the next round.
start_offset += int(_QUERY_COUNT)
return result
def _print_version(ctx, _, value):
"""Print version and exit."""
if not value or ctx.resilient_parsing:
return
click.echo(__component_version__)
ctx.exit()
def _send_metrics():
"""Send metrics to pushgateway."""
if THOTH_DEPLOYMENT_NAME:
database_schema_revision_script.labels(
"revsolver", GraphDatabase().get_script_alembic_version_head(), THOTH_DEPLOYMENT_NAME
).inc()
else:
_LOGGER.warning("THOTH_DEPLOYMENT_NAME env variable is not set.")
if THOTH_METRICS_PUSHGATEWAY_URL and THOTH_DEPLOYMENT_NAME:
try:
_LOGGER.debug("Submitting metrics to Prometheus pushgateway %r", THOTH_METRICS_PUSHGATEWAY_URL)
push_to_gateway(
THOTH_METRICS_PUSHGATEWAY_URL,
job="revsolver",
registry=PROMETHEUS_REGISTRY,
)
except Exception as e:
_LOGGER.exception("An error occurred pushing the metrics: %s", str(e))
else:
_LOGGER.warning("PROMETHEUS_PUSHGATEWAY_URL env variable is not set.")
@click.command()
@click.pass_context
@click.option(
"-v",
"--verbose",
is_flag=True,
envvar="THOTH_REVSOLVER_VERBOSE",
help="Be verbose about what's going on.",
)
@click.option(
"--version",
is_flag=True,
is_eager=True,
callback=_print_version,
expose_value=False,
help="Print version and exit.",
)
@click.option(
"--package-name",
"-p",
type=str,
required=True,
envvar="THOTH_REVSOLVER_PACKAGE_NAME",
metavar="PKG",
help="Package name for which the reverse solver should be run.",
)
@click.option(
"--package-version",
"-r",
type=str,
metavar="VER",
required=True,
envvar="THOTH_REVSOLVER_PACKAGE_VERSION",
help="Package version for which the reverse solver should be run (version identifier).",
)
@click.option(
"--output",
"-o",
type=str,
metavar="OUTPUT",
envvar="THOTH_REVSOLVER_OUTPUT",
default="-",
show_default=True,
help="Output file or remote API to print results to, in case of URL a POST request is issued.",
)
@click.option("--no-pretty", "-P", is_flag=True, help="Do not print results nicely.")
def cli(
click_ctx: click.Context,
output: str,
package_name: str,
package_version: str,
no_pretty: bool = False,
verbose: bool = False,
) -> None:
"""Obtain dependent packages for the given package, respect registered solvers in Thoth.
The result of reverse solver is a list of dependencies that depend on the given package.
"""
if click_ctx:
click_ctx.auto_envvar_prefix = "THOTH_REVSOLVER"
if verbose:
_LOGGER.setLevel(logging.DEBUG)
_LOGGER.debug("Debug mode is on")
_LOGGER.debug("Version: %s", __component_version__)
_send_metrics()
start_time = time.monotonic()
result = do_solve(package_name=package_name, package_version=package_version)
print_command_result(
click_ctx,
result,
analyzer=__title__,
analyzer_version=__component_version__,
output=output,
duration=time.monotonic() - start_time,
pretty=not no_pretty,
)
__name__ == "__main__" and sys.exit(cli())