-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_environment_checker.py
283 lines (252 loc) · 11.9 KB
/
lib_environment_checker.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
"""
Module used by self_updater.py
Contains code for checking the target-project's environment.
"""
import json
import logging
import subprocess
from pathlib import Path
import dotenv
import lib_git_handler
from lib_emailer import Emailer
log = logging.getLogger(__name__)
def validate_project_path(project_path: Path) -> None:
"""
Validates that the provided project path exists.
If path is invalid:
- Sends an email to the self-updater sys-admins
- Exits the script
"""
log.info('::: validating project_path ----------')
# log.debug(f'project_path: ``{project_path}``')
if not project_path.exists():
message = f'Error: The provided project_path ``{project_path}`` does not exist. Halting self-update.'
log.exception(message)
## email project sys-admins ---------------------------------
emailer = Emailer(project_path)
email_message: str = emailer.create_setup_problem_message(message)
emailer.send_email(emailer.sys_admin_recipients, email_message)
## raise exception -----------------------------------------
raise Exception(message)
else:
log.info(f'ok / project_path, ``{project_path}``')
return
def determine_project_email_addresses(project_path: Path) -> list[list[str, str]]:
"""
Loads email addresses from the target-project's `.env` file.
Returns a list of email addresses.
Assumes the setting `ADMINS_JSON` structured like:
ADMINS_JSON='
[
[ "exampleFirstName exampleLastName", "[email protected]"],
etc...
]'
If there's an error:
- Sends an email to the self-updater sys-admins
- Exits the script
"""
log.info('::: determining email addresses ----------')
try:
settings: dict = dotenv.dotenv_values('../.env')
email_addresses_json: str = settings['ADMINS_JSON']
email_addresses: list[list[str, str]] = json.loads(email_addresses_json)
log.info(f'ok / email_addresses: {email_addresses}')
return email_addresses
except Exception as e:
message = f'Error determining email addresses: {e}'
log.exception(message)
## email project sys-admins ---------------------------------
emailer = Emailer(project_path)
email_message: str = emailer.create_setup_problem_message(message)
emailer.send_email(emailer.sys_admin_recipients, email_message)
## raise exception -----------------------------------------
raise Exception(message)
## end def determine_project_email_addresses()
def check_branch(project_path, project_email_addresses) -> None:
"""
Checks that the project is on the `main` branch.
If not, sends an email to the project sys-admins, then exits.
"""
log.info('::: checking branch ----------')
branch = fetch_branch_data(project_path)
if branch != 'main':
message = f'Error: Project is on branch ``{branch}`` instead of ``main``'
log.exception(message)
## email project sys-admins ---------------------------------
emailer = Emailer(project_path)
email_message: str = emailer.create_setup_problem_message(message)
emailer.send_email(project_email_addresses, email_message)
## raise exception -----------------------------------------
raise Exception(message)
else:
log.info(f'ok / branch, ``{branch}``')
return
def fetch_branch_data(project_path: Path) -> str:
"""
Fetches branch-data by reading the `.git/HEAD` file (avoiding calling git via subprocess due to `dubious ownership` issue).
Called by check_branch()
"""
# log.debug('starting fetch_branch_data')
git_dir = project_path / '.git'
try:
## read HEAD file to find the project branch ------------
head_file = git_dir / 'HEAD'
ref_line = head_file.read_text().strip()
if ref_line.startswith('ref:'):
project_branch = ref_line.split('/')[-1] # extract the project_branch name
else:
project_branch = 'detached'
except FileNotFoundError:
log.warning('no `.git` directory or HEAD file found.')
project_branch = 'project_branch_not_found'
except Exception:
log.exception('other problem fetching project_branch data')
project_branch = 'project_branch_not_found'
# log.debug(f'project_branch: ``{project_branch}``')
return project_branch
def check_git_status(project_path: Path, project_email_addresses: list[list[str, str]]) -> None:
"""
Checks that the project has no uncommitted changes.
If there are uncommitted changes:
- Sends an email to the project sys-admins
- Exits the script
Note: just looking for the word 'clean' because one version of git says "working tree clean"
and another says "working directory clean". TODO: consider just checking the ok boolean.
"""
log.info('::: checking git status ----------')
## check for uncommitted changes --------------------------
call_result: tuple[bool, dict] = lib_git_handler.run_git_status(project_path)
(ok, output) = call_result
if 'clean' not in output['stdout']:
message = 'Error: git-status check failed.'
log.exception(message)
## email project sys-admins ---------------------------------
emailer = Emailer(project_path)
email_message: str = emailer.create_setup_problem_message(message)
emailer.send_email(project_email_addresses, email_message)
## raise exception -----------------------------------------
raise Exception(message)
else:
log.info('ok / git status is clean')
return
def determine_python_version(project_path: Path, project_email_addresses: list[list[str, str]]) -> tuple[str, str, str]:
"""
Determines Python version from the target-project's virtual environment.
The purpose is to later run the `uv pip compile ...` command, to add the --python version
If the virtual environment or python version is invalid:
- Sends an email to the project sys-admins
- Exits the script
Of the returned info, only the resolved-python-path is currently used.
History:
- Initially I just grabbed the python version.
- But `uv pip compile ... --python version` could fail if uv didn't find that python version.
- I thought the tilde-notation would resolve this, but it didn't.
- Grabbing the actual resolved-path to the venv's python executable works.
TODO: eventually remove the unneed code.
"""
log.info('::: determining python version ----------')
## get env_python_path ------------------------------------------
env_python_path: Path = project_path.parent / 'env/bin/python3'
log.debug(f'env_python_path before resolve: ``{env_python_path}``')
if not env_python_path.exists():
message = 'Error: Virtual environment not found.'
log.exception(message)
## email project sys-admins ---------------------------------
emailer = Emailer(project_path)
email_message: str = emailer.create_setup_problem_message(message)
emailer.send_email(project_email_addresses, email_message)
## raise exception -----------------------------------------
raise Exception(message)
## get version --------------------------------------------------
python_version: str = subprocess.check_output([str(env_python_path), '--version'], text=True).strip().split()[-1]
log.debug(f'python_version: {python_version}')
## tildify version ----------------------------------------------
parts: list = python_version.split('.')
tilde_notation: str = f'~={parts[0]}.{parts[1]}.0' # converts, eg, '3.8.10' to '~=3.8.0'
log.debug(f'tilde_notation: {tilde_notation}')
## resolve env_python_path --------------------------------------
env_python_path_resolved: str = str(env_python_path.resolve()) # only this is used by the calling code
log.debug(f'env_python_path_resolved: ``{env_python_path_resolved}``')
## confirm Python 3 ---------------------------------------------
if not python_version.startswith('3.'):
message = 'Error: Invalid Python version.'
log.exception(message)
## email project-admins -------------------------------------
emailer = Emailer(project_path)
email_message: str = emailer.create_setup_problem_message(message)
emailer.send_email(project_email_addresses, email_message)
## raise exception -----------------------------------------
raise Exception(message)
log.info(f'ok / python_version, ``{python_version}``')
return (python_version, tilde_notation, env_python_path_resolved)
## end def determine_python_version()
def determine_environment_type(project_path: Path, project_email_addresses: list[list[str, str]]) -> str:
"""
Infers environment type based on the system hostname.
Returns 'local', 'staging', or 'production'.
"""
log.info('::: determining environment type ----------')
## ensure all .in files exist -----------------------------------
for filename in ['local.in', 'staging.in', 'production.in']:
full_path: Path = project_path / 'requirements' / filename
try:
assert full_path.exists()
except AssertionError:
message = f'Error: {full_path} not found'
log.exception(message)
## email project-admins ---------------------------------
emailer = Emailer(project_path)
email_message: str = emailer.create_setup_problem_message(message)
emailer.send_email(project_email_addresses, email_message)
## raise exception --------------------------------------
raise Exception(message)
## determine proper one -----------------------------------------
hostname: str = subprocess.check_output(['hostname'], text=True).strip().lower()
if hostname.startswith('d') or hostname.startswith('q'):
env_type: str = 'staging'
elif hostname.startswith('p'):
env_type: str = 'production'
else:
env_type: str = 'local'
log.info(f'ok / env_type, ``{env_type}``')
return env_type
def determine_uv_path() -> Path:
"""
Checks `which` for the `uv` command.
If that fails, gets path from this script's venv.
Used for compile and sync.
"""
log.info('::: determining uv path ----------')
try:
uv_initial_path: str = subprocess.check_output(['which', 'uv'], text=True).strip()
uv_path = Path(uv_initial_path).resolve() # to ensure an absolute-path
except subprocess.CalledProcessError:
log.debug("`which` unsuccessful; accessing this script's venv")
initial_uv_path: Path = Path(__file__).parent.parent / 'env' / 'bin' / 'uv'
uv_path = initial_uv_path.resolve()
log.info(f'ok / uv_path, ``{uv_path}``')
return uv_path
def determine_group(project_path: Path, project_email_addresses: list[list[str, str]]) -> str:
"""
Infers the group by examining existing files.
Returns the most common group.
If there's an error:
- Sends an email to the project sys-admins
- Exits the script
"""
log.info('::: determining group ----------')
try:
group_list: list[str] = subprocess.check_output(['ls', '-l', str(project_path)], text=True).splitlines()
groups = [line.split()[3] for line in group_list if len(line.split()) > 3]
most_common_group: str = max(set(groups), key=groups.count)
log.info(f'ok / most_common_group, ``{most_common_group}``')
return most_common_group
except Exception as e:
message = f'Error inferring group: {e}'
log.exception(message)
## email sys-admins -----------------------------------------
emailer = Emailer(project_path)
email_message: str = emailer.create_setup_problem_message(message)
emailer.send_email(project_email_addresses, email_message)
## raise exception -----------------------------------------
raise Exception(message)