-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
368 lines (284 loc) · 9.8 KB
/
__init__.py
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python3
#
# __init__.py
"""
Create virtual environments using ``pyproject.toml`` metadata.
"""
#
# Copyright © 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
#
# stdlib
import os
import pathlib
import shutil
from typing import Dict, List, Optional, Union
# 3rd party
import click
import virtualenv # type: ignore
from domdf_python_tools.paths import PathPlus, traverse_to_file
from domdf_python_tools.typing import PathLike
from domdf_python_tools.words import word_join
from packaging.requirements import Requirement
from shippinglabel import read_pyvenv
from virtualenv.run import session_via_cli # type: ignore
from virtualenv.run.session import Session # type: ignore
from virtualenv.seed.wheels import pip_wheel_env_run # type: ignore
# this package
from pyproject_devenv.config import ConfigDict, load_toml
__all__ = ["mkdevenv", "BaseInstallError", "InstallFromFileError", "InstallError"]
__author__: str = "Dominic Davis-Foster"
__copyright__: str = "2020-2021 Dominic Davis-Foster"
__license__: str = "MIT License"
__version__: str = "0.1.1"
__email__: str = "dominic@davis-foster.co.uk"
virtualenv_version = tuple(map(int, virtualenv.__version__.split('.')))
if virtualenv_version >= (20, 4):
_pip_wheel_env_run = pip_wheel_env_run
def pip_wheel_env_run(search_dirs, app_data):
return _pip_wheel_env_run(search_dirs, app_data, os.environ)
class BaseInstallError(RuntimeError):
"""
Base :exc:`Exception` to indicate an error occurred when installing packages.
"""
class InstallFromFileError(BaseInstallError):
"""
:exc:`Exception` to indicate an error occurred when installing packages from a requirements file.
:param filename: The file listing the packages to install.
"""
def __init__(self, filename: PathLike):
if not isinstance(filename, pathlib.Path):
filename = PathPlus(filename)
self.filename: str = filename.as_posix()
"""
The file listing the packages to install.
.. latex:clearpage::
"""
super().__init__(f"Could not install from {self.filename!r}")
class InstallError(BaseInstallError):
r"""
:exc:`Exception` to indicate an error occurred when installing packages.
:param \*requirements: The requirements being installed.
"""
def __init__(self, *requirements: Union[str, Requirement]):
#: The requirements being installed.
self.requirements: List[str] = list(map(str, requirements))
requirements_string = word_join(self.requirements, use_repr=True)
super().__init__(f"Could not install the given requirements: {requirements_string}")
class _Devenv:
"""
Create a "devenv".
.. TODO:: Maybe make this public at some point?
:param project_dir: The root of the project to create the devenv for.
:param venv_dir: The directory to create the devenv in, relative to ``repo_dir``.
:param verbosity: The verbosity of the function. ``0`` = quiet, ``2`` = very verbose.
:param upgrade: Whether to upgrade all specified packages to the newest available version.
"""
def __init__(
self,
project_dir: PathLike,
venv_dir: PathLike = "venv",
*,
verbosity: int = 1,
upgrade: bool = False,
):
self.project_dir: PathPlus = self.determine_project_dir(project_dir)
self.config: ConfigDict = self.load_config()
self.venv_dir = self.project_dir / venv_dir
self.verbosity: int = int(verbosity)
self.upgrade: bool = upgrade
# TODO: config option
self.extras_to_install = sorted(self.config["optional_dependencies"])
@staticmethod
def determine_project_dir(project_dir: PathLike) -> PathPlus:
"""
Determine the project base directory.
Subclasses may override this method to customise the behaviour.
:param project_dir:
"""
return traverse_to_file(PathPlus(project_dir), "pyproject.toml")
def load_config(self) -> ConfigDict:
"""
Load the configuration.
Subclasses may override this method to customise the behaviour.
"""
return load_toml(self.project_dir / "pyproject.toml")
def create(self) -> int:
args = [
str(self.venv_dir),
"--prompt",
f"{self.config['name']}",
"--seeder",
"pip",
"--download",
]
if self.verbosity:
args.append("--verbose")
if self.verbosity >= 2:
args.append("--verbose")
of_session = session_via_cli(args)
if not of_session.seeder.enabled: # pragma: no cover
return 1
with of_session:
of_session.run()
self.install_project_requirements(of_session)
self.install_extra_requirements(of_session)
# TODO: config option for tests dir
if (self.project_dir / "tests" / "requirements.txt").is_file():
self.install_test_requirements(of_session)
self.install_build_requirements(of_session)
if self.verbosity:
click.echo()
self.update_pyvenv()
return 0
def install_project_requirements(self, of_session):
"""
Install the project's requirements/dependencies.
:param of_session:
"""
if self.config["dependencies"]:
self.report_installing("project requirements")
self.install_requirements(
of_session,
*self.config["dependencies"],
)
def install_extra_requirements(self, of_session):
"""
Install the project's extra-requirements/optional-dependencies.
:param of_session:
"""
for extra in self.extras_to_install:
self.report_installing(f"extra {extra!r}")
self.install_requirements(
of_session,
*self.config["optional_dependencies"][extra],
)
def install_test_requirements(self, of_session):
"""
Install the project's test requirements.
:param of_session:
"""
self.report_installing("test requirements")
self.install_requirements(
of_session,
requirements_file=self.project_dir / "tests" / "requirements.txt",
)
def install_build_requirements(self, of_session):
"""
Install the project's build requirements.
:param of_session:
"""
if self.config["build_dependencies"] is not None:
self.report_installing("build requirements")
self.install_requirements(
of_session,
*self.config["build_dependencies"],
)
def report_installing(self, what: str):
"""
Report that a category of requirements is being installed.
:param what: The type/category of requirements to report the installation of,
e.g. "library requirements".
"""
if self.verbosity:
click.echo()
click.echo(f" Installing {what.strip()} ".center(shutil.get_terminal_size().columns, '='))
# @overload
# def install_requirements(
# self,
# session: Session,
# *requirements: Union[str, Requirement],
# requirements_file: None = ...
# ): ...
#
# @overload
# def install_requirements(
# self,
# session: Session,
# requirements_file: PathLike,
# ): ...
def install_requirements(
self,
session: Session,
*requirements: Union[str, Requirement],
requirements_file: Optional[PathLike] = None,
):
r"""
Install requirements into a virtualenv.
:param session:
:param \*requirements: The requirements to install.
:param requirements_file: The file to install the requirements from, with ``pip install -r <filename>``.
``*requirements`` and ``requirements_file`` are mutually exclusive.
"""
if requirements and requirements_file:
raise TypeError("'*requirements' and 'requirements_file' are mutually exclusive.")
cmd = [
session.creator.exe,
"-m",
"pip",
"install",
"--disable-pip-version-check",
]
if requirements_file:
cmd.append("-r")
cmd.append(str(requirements_file))
else:
cmd.extend(map(str, requirements))
if self.verbosity < 1:
cmd.append("--quiet")
elif self.verbosity > 1:
cmd.append("--verbose")
if self.upgrade:
cmd.append("--upgrade")
try:
session.seeder._execute(
[str(x) for x in cmd],
pip_wheel_env_run(session.seeder.extra_search_dir, session.seeder.app_data),
)
except RuntimeError: # pragma: no cover
if requirements_file:
raise InstallFromFileError(requirements_file)
else:
raise InstallError(*requirements)
def update_pyvenv(self) -> None:
"""
Read and update the ``pyvenv.cfg`` file of the virtualenv.
"""
pyvenv_config: Dict[str, str] = read_pyvenv(self.venv_dir)
pyvenv_config["pyproject-devenv"] = __version__
with (self.venv_dir / "pyvenv.cfg").open('w') as fp:
for key, value in pyvenv_config.items():
value = f" = " + str(value).replace('\n', '\n\t')
fp.write(f"{key}{value}\n")
def mkdevenv(
project_dir: PathLike,
venv_dir: PathLike = "venv",
*,
verbosity: int = 1,
upgrade: bool = False,
) -> int:
"""
Create a "devenv".
:param project_dir: The root of the project to create the devenv for.
:param venv_dir: The directory to create the devenv in, relative to ``repo_dir``.
:param verbosity: The verbosity of the function. ``0`` = quiet, ``2`` = very verbose.
:param upgrade: Whether to upgrade all specified packages to the newest available version.
"""
return _Devenv(project_dir, venv_dir, verbosity=verbosity, upgrade=upgrade).create()