-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathupdate.py
More file actions
143 lines (111 loc) · 5.36 KB
/
Copy pathupdate.py
File metadata and controls
143 lines (111 loc) · 5.36 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
import click
import os
import sys
import re
from .conf import config
from . import cli, echo_error, echo_info, utils
@cli.command(context_settings={"ignore_unknown_options": True})
@click.argument("action", type=click.Choice(["requirements"]))
@click.argument("profile", default='default')
@click.argument("additional_args", nargs=-1)
def update(action, profile, additional_args):
"""
Update project-specific files and dependencies.
This command allows you to update project-specific files and dependencies for a specified project configuration.
Currently, it supports the 'requirements' action, which is used to update the requirements.
The `update` command performs the specified 'action' to update project-specific files or dependencies. It ensures
that the specified project configuration exists.
Note:
- Ensure that the specified project configuration ('name') is valid and defined in your project's configuration.
- Additional arguments can be used to customize the update process if supported by the action.
"""
if action == "requirements":
create_req(True, profile)
def create_req(yes, profile, confirm_value=True):
"""
Load project's pipenv and build a requirements.txt.
This function generates a requirements.txt file based on the project's pipenv environment. It offers the option
to confirm the regeneration of the requirements.txt file.
:param confirm_value: bool, default: True
If True, confirms the regeneration of requirements.txt.
Note:
- The function handles requirements.txt generation and checking.
- It prompts for confirmation to regenerate the requirements.txt file.
- If errors are detected, it provides an option to continue or exit the script.
:return: None
"""
conf = config.get_profile(profile)
dist_folder = conf["distribution_folder"]
if yes or click.confirm( text=f"Would you like to regenerate {dist_folder}/requirements.txt ?",
default=confirm_value):
ret = os.system(f"pipfile2req --hashes > {dist_folder}/requirements.txt 2>/dev/null")
if ret != 0:
ret = os.system(f"uv export --no-dev > {dist_folder}/requirements.txt")
if ret != 0:
sys.exit(1)
file_object = open(f"{dist_folder}/requirements.txt", 'r')
generated_requirements = file_object.read()
for line in generated_requirements.splitlines():
if "]==" in line:
# we got a dependency with extras
generated_requirements += re.sub(r"\[.*?\]", "", line) + "\n"
file_object.close()
file_obj = open(f"{dist_folder}/requirements.txt", 'w')
file_obj.write(generated_requirements)
file_obj.close()
echo_info("requirements.txt successfully generated")
# DEPRECATED: This enitre check is only required prior viur-core 3.6.13
try:
if check_req(f"{dist_folder}/requirements.txt"):
if not click.confirm(f"There are some depencency errors, are you sure you want to continue?"):
sys.exit(0)
except ModuleNotFoundError:
pass
def check_req(projects_requirements_path):
"""
Check project's requirements against core requirements.
This check is only possible prior viur-core 3.6.13, the function is deprecated.
This function checks the project's requirements to validate package versions and hashes.
It identifies and reports errors if there are discrepancies.
:param projects_requirements_path: str
The path to the project's requirements.txt file.
:return: list
A list of error messages, if any, indicating package version and hash discrepancies.
"""
import site
from pip._internal.req import parse_requirements
from pip._internal.network.session import PipSession
sp = site.getsitepackages()[0]
core_requirements = None
for req in (
os.path.join(sp, "viur", "core", "requirements.txt"),
os.path.join(sp, "viur", "requirements.txt")
):
if os.path.exists(req):
core_requirements = req
break
errors = []
if core_requirements:
core_requirements_obj = utils.requirements_to_dict(parse_requirements(core_requirements, session=PipSession()))
projects_requirements_obj = utils.requirements_to_dict(
parse_requirements(projects_requirements_path, session=PipSession())
)
for package, options in core_requirements_obj.items():
if package not in projects_requirements_obj:
errors.append(f"missing package: {package} with version {options['version']}")
continue
elif options["version"] != projects_requirements_obj[package]["version"]:
errors.append(
f"version mismatch: expected {options['version']} "
f"got {projects_requirements_obj[package]['version']}: {package}"
)
continue
else:
# package exists, test hash
project_hashes = projects_requirements_obj[package]["hashes"]["sha256"]
core_hashes = options["hashes"]["sha256"]
if not set(core_hashes).issubset(set(project_hashes)):
errors.append(f"package hash mismatch: {package}")
for error in errors:
echo_error(error)
return errors