Debian Codegraph @

tasks/deps/tasks.py

raw symbols SCIP
1 # -*- coding: ascii -*-
2 #
3 # Copyright 2018 - 2025
4 # Andr\xe9 Malo or his licensors, as applicable
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 """
18 Dependency Management Tasks
19 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
20
21 """
22 import json as _json
23 import os as _os
24 import sys as _sys
25
26 import invoke as _invoke
27
28 from .. import pypi as _pypi
29 from .._inv import tasks as _tasks
30 from .._inv import util as _util
31
32 # pylint: disable = import-outside-toplevel
33
34 NAMESPACE = "deps"
35
36
37 def _default_config(ctx):
38 """
39 Set default config
40
41 Returns:
42 adict: ctx.deps after defaults being applied
43 """
44 if getattr(_default_config, "applied", None):
45 return ctx.deps
46
47 ctx.deps = _util.dictmerge(
48 _util.adict(
49 toplevel=[
50 "development.txt",
51 "tests/requirements.txt",
52 ],
53 upgrade=[
54 "checkout-requirements.txt",
55 ],
56 keep_as_is=[
57 "compat-requirements.txt",
58 ],
59 no_compat=["setuptools", "pip", "build"],
60 no_upgrade=[],
61 no_latest=[],
62 boilerplate=["setuptools", "pip", "build"],
63 compat=True,
64 latest=_util.adict(
65 file="development.txt",
66 pattern=r"(?=^-e\s+\.)",
67 prefix="# Latest dependencies",
68 suffix="",
69 ),
70 ),
71 ctx.get("deps", {}),
72 )
73 _default_config.applied = True
74 return ctx.deps
75
76
77 @_invoke.task()
78 def old(ctx):
79 """List outdated python packages"""
80 with ctx.shell.root_dir():
81 ctx.run(ctx.c("pip list -i %s -o", _pypi.index_url(ctx)), echo=True)
82
83
84 @_invoke.task()
85 def package(ctx, upgrade=False):
86 """
87 Update python dependencies, excluding development (``-e .``)
88
89 Parameters:
90 upgrade (bool):
91 Run pip install with ``-U`` flag?
92 """
93 cmd = ["pip", "install", "-i", _pypi.index_url(ctx)]
94 if upgrade:
95 cmd += ["-U"]
96 cmd += ["-e", "."]
97
98 with ctx.shell.root_dir():
99 ctx.run(ctx.c(cmd), echo=True)
100
101
102 @_invoke.task(default=True)
103 def dev(ctx, upgrade=False):
104 """
105 Update python dependencies, including development (``-r development.txt``)
106
107 Parameters:
108 upgrade (bool):
109 Run pip install with ``-U`` flag?
110 """
111 cmd = ["pip", "install", "-i", _pypi.index_url(ctx)]
112 if upgrade:
113 cmd += ["-U"]
114 cmd += ["-r", "development.txt"]
115
116 with ctx.shell.root_dir():
117 ctx.run(ctx.c(cmd), echo=True)
118
119
120 @_invoke.task()
121 def reset(ctx, upgrade=False):
122 """
123 Reset your virtual env
124
125 This command uninstalls everything except editable installs and reinstalls
126 from scratch (``-r development.txt``)
127
128 Parameters:
129 upgrade (bool):
130 Run pip install with ``-U`` flag?
131 """
132 cmd = [ctx.which("bash"), "-il", "%s/reset.sh"]
133 if upgrade:
134 cmd += ["-u"]
135 cmd += ["."]
136 with ctx.shell.root_dir():
137 ctx.run(
138 ctx.c(cmd, ctx.shell.native(_os.path.dirname(__file__))),
139 pty=True,
140 )
141
142
143 @_invoke.task()
144 def inspect(ctx, deps=False, verbose=False, debug=False):
145 """
146 Inspect current dependencies and print to stdout
147
148 Parameters:
149 deps (bool):
150 run ``inv deps`` before?
151
152 verbose (bool):
153 Verbose mode? Default: true
154
155 debug (bool):
156 Debug mode? Default: false
157 """
158 if deps:
159 _tasks.execute(ctx, "deps.dev")
160
161 from . import _inspect
162
163 _sys.stdout.write(
164 _json.dumps(
165 _inspect.inspect_dependencies(
166 ctx,
167 _default_config(ctx),
168 verbose=verbose,
169 debug=debug,
170 ),
171 indent=4,
172 default=str,
173 )
174 + "\n"
175 )
176
177
178 @_invoke.task()
179 def check(ctx, upgrade=False, deps=False, verbose=True, debug=False):
180 """
181 Suggest dependency changes (basically dry run for `inv deps.patch`)
182
183 Parameters:
184 deps (bool):
185 run ``inv deps`` before?
186
187 upgrade (bool):
188 Consider upgrades? They might be incompatible with your code.
189 Default: false
190
191 verbose (bool):
192 Verbose mode? Default: true
193
194 debug (bool):
195 Debug mode? Default: false
196 """
197 if deps:
198 _tasks.execute(ctx, "deps.dev")
199
200 from . import _suggest
201
202 result = _suggest.suggest_updates(
203 ctx,
204 _default_config(ctx),
205 upgrade=upgrade,
206 verbose=verbose,
207 debug=debug,
208 )
209
210 result["replace"] = {
211 file.name: dict(type=file.type, **info)
212 for file, info in result["replace"].items()
213 }
214 result["latest"] = result["latest"] and result["latest"].as_patch_info()
215 _sys.stdout.write(
216 _json.dumps(
217 result,
218 indent=4,
219 default=str,
220 )
221 + "\n"
222 )
223
224
225 @_invoke.task()
226 def patch(ctx, deps=False, upgrade=False, verbose=True, debug=False):
227 """
228 Find dependency updates and patch the files
229
230 Parameters:
231 deps (bool):
232 run ``inv.deps`` before?
233
234 upgrade (bool):
235 Consider upgrades? They might be incompatible with your code.
236 Default: false
237
238 verbose (bool):
239 Verbose mode? Default: true
240
241 debug (bool):
242 Debug mode? Default: false
243 """
244 if deps:
245 _tasks.execute(ctx, "deps.dev")
246
247 from . import _patch
248
249 _sys.stdout.write(
250 _json.dumps(
251 _patch.patch_updates(
252 ctx,
253 _default_config(ctx),
254 upgrade=upgrade,
255 verbose=verbose,
256 debug=debug,
257 ),
258 indent=4,
259 default=str,
260 )
261 + "\n"
262 )