-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_env.py
More file actions
568 lines (476 loc) · 19 KB
/
setup_env.py
File metadata and controls
568 lines (476 loc) · 19 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
"""
Environment Setup Module
======================
This module automates the setup and maintenance of a Python virtual environment with all required
dependencies for the IsochroneMapsV2 project. It provides an interactive command-line interface
with step-by-step guidance through the environment configuration process.
Setup Process:
1. Create/recreate virtual environment (.venv)
2. Check and upgrade pip to latest version
3. Setup system certificates if needed for secure connections
4. Install or create requirements.txt file using pipreqs if not present
5. Install all required dependencies from requirements.txt
6. Validate dependencies for potential conflicts
7. Check for and update outdated packages
Functions:
parse_args: Parse command line arguments with non-interactive option.
get_user_confirmation: Get y/n confirmation from user with consistent handling.
subprocess_error_handler: Handle errors from subprocess calls with informative messages.
show_progress: Show a spinner animation while a process is running.
setup_virtual_environment: Create new or update existing virtual environment.
check_and_upgrade_pip: Check pip version and upgrade if requested.
setup_system_certificates: Install system certificates if required for secure connections.
handle_requirements_file: Create or verify requirements file, generating with pipreqs if needed.
install_requirements: Install packages from requirements.txt file.
validate_dependencies: Validate installed dependencies for conflicts or issues.
parse_outdated_packages: Parse pip list --outdated output into a structured format.
update_packages: Update specified packages or all outdated packages with user selection.
check_outdated_dependencies: Check for and optionally update outdated dependencies.
main: Main function to orchestrate the environment setup process.
Usage:
Basic interactive setup:
$ python setup_env.py
Non-interactive setup with default options:
$ python setup_env.py --non-interactive
Run setup from an existing Python environment:
$ python -m setup_env
Notes:
- This script can be run repeatedly to maintain environment health
- Requires Python 3.6+ to function correctly
- Will create a virtual environment in .venv directory if not present
- Logs all operations to logs/setup.log for debugging
"""
import argparse
import logging
import os
import shutil
import subprocess
import sys
import time
from pathlib import Path
# Add the project root to the Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Configure basic logging first
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler()],
)
try:
# Try to import with the project structure
from src.utils.logging_utils import setup_logging
from src.utils.retry_util import retry
except ImportError:
# If we can't import yet (likely during initial setup), create minimal implementations
def setup_logging(log_file_name=None, **kwargs):
"""Simple logging setup for when imports aren't available."""
if log_file_name:
try:
log_dir = Path("logs")
log_dir.mkdir(exist_ok=True)
file_handler = logging.FileHandler(log_dir / log_file_name)
file_handler.setFormatter(
logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
)
logging.getLogger().addHandler(file_handler)
except Exception as e:
logging.error(f"Could not set up file logging: {e}")
def retry(func, max_attempts=3, delay=1, backoff=2, error_handler=None):
"""Simple retry function for when imports aren't available."""
attempt = 0
while attempt < max_attempts:
try:
return func()
except Exception as e:
attempt += 1
if error_handler:
error_handler(e, attempt - 1)
if attempt == max_attempts:
raise
time.sleep(delay)
delay *= backoff
# Configure logging
setup_logging(log_file_name="setup.log")
# Define constants
VENV_DIR = Path(".venv")
REQUIREMENTS_FILE = Path("requirements.txt")
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description="Set up Python environment")
parser.add_argument(
"--non-interactive",
action="store_true",
help="Run without prompts (uses defaults)",
)
return parser.parse_args()
def get_user_confirmation(prompt, default="n", args=None):
"""Get y/n confirmation from user with consistent handling.
Args:
prompt: The question to ask the user
default: Default response if in non-interactive mode
args: Command line arguments
Returns:
bool: True if confirmed, False otherwise
"""
if args and args.non_interactive:
logging.info(f"Non-interactive mode: Using default '{default}' for: {prompt}")
return default == "y"
while True:
user_input = input(f"{prompt} (y/n): ").strip().lower()
if user_input in ("y", "n"):
return user_input == "y"
print("Invalid input. Please enter 'y' or 'n'.")
def subprocess_error_handler(error, attempt):
"""Handle errors from subprocess calls."""
logging.error(f"Command failed (attempt {attempt+1}): {error}")
if isinstance(error, subprocess.CalledProcessError) and error.output:
logging.error(f"Command output: {error.output}")
def show_progress(process_name):
"""Show a simple spinner while a process is running.
Args:
process_name: Name of the running process
Returns:
function: Call this function to stop the spinner
"""
spinner = ["|", "/", "-", "\\"]
i = 0
def spin():
nonlocal i
print(f"\r{process_name} {spinner[i]} ", end="", flush=True)
i = (i + 1) % len(spinner)
def stop():
print("\r" + " " * (len(process_name) + 2), end="\r")
import threading
stop_event = threading.Event()
def spin_thread():
while not stop_event.is_set():
spin()
time.sleep(0.1)
thread = threading.Thread(target=spin_thread)
thread.daemon = True
thread.start()
return lambda: (stop_event.set(), stop())
def setup_virtual_environment(args):
"""Create or update the virtual environment.
Args:
args: Command line arguments
Returns:
Path: Path to the pip executable
"""
logging.info("Step 1: Setting up virtual environment")
pip_path = VENV_DIR / ("Scripts" if os.name == "nt" else "bin") / "pip"
try:
if VENV_DIR.exists():
logging.info("Virtual environment already exists.")
if get_user_confirmation(
"Do you want to delete the existing environment and recreate it?",
default="n",
args=args,
):
logging.info("Deleting existing virtual environment...")
shutil.rmtree(VENV_DIR)
logging.info("Creating new virtual environment...")
progress = show_progress("Creating virtual environment")
subprocess.run(
[sys.executable, "-m", "venv", str(VENV_DIR)], check=True
)
progress()
logging.info("Virtual environment created successfully.")
else:
logging.info("Using existing virtual environment.")
else:
logging.info(
"Virtual environment not found. Creating virtual environment..."
)
progress = show_progress("Creating virtual environment")
subprocess.run([sys.executable, "-m", "venv", str(VENV_DIR)], check=True)
progress()
logging.info("Virtual environment created successfully.")
except Exception as e:
logging.exception(f"Error managing virtual environment: {e}")
sys.exit(1)
return pip_path
def check_and_upgrade_pip(pip_path, args):
"""Check pip version and upgrade if requested.
Args:
pip_path: Path to pip executable
args: Command line arguments
"""
logging.info("Step 2: Checking and upgrading pip")
try:
logging.info("Checking pip version...")
result = subprocess.run(
[str(pip_path), "--version"], check=True, capture_output=True, text=True
)
logging.info(f"pip version: {result.stdout.strip()}")
if get_user_confirmation(
"Do you want to upgrade pip to the latest version?", default="y", args=args
):
progress = show_progress("Upgrading pip")
retry(
lambda: subprocess.run(
[str(pip_path), "install", "--upgrade", "pip"], check=True
),
error_handler=subprocess_error_handler,
)
progress()
logging.info("pip upgraded successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"Error checking or upgrading pip: {e}")
sys.exit(1)
def setup_system_certificates(pip_path, args):
"""Install system certificates if needed.
Args:
pip_path: Path to pip executable
args: Command line arguments
"""
logging.info("Step 3: Setting up system certificates")
if get_user_confirmation(
"Do you need to trust system certificates?", default="n", args=args
):
logging.info("Installing pip_system_certs...")
try:
progress = show_progress("Installing system certificates")
retry(
lambda: subprocess.run(
[
str(pip_path),
"install",
"--trusted-host",
"files.pythonhosted.org",
"pip_system_certs",
],
check=True,
),
error_handler=subprocess_error_handler,
)
progress()
logging.info("pip_system_certs installed successfully.")
except Exception as e:
logging.error(f"Error installing pip_system_certs: {e}")
sys.exit(1)
else:
logging.info("User chose not to trust system certificates.")
def handle_requirements_file(pip_path, args):
"""Create or verify requirements file.
Args:
pip_path: Path to pip executable
args: Command line arguments
"""
logging.info("Step 4: Handling requirements file")
if not REQUIREMENTS_FILE.exists():
logging.warning(f"Requirements file '{REQUIREMENTS_FILE}' not found.")
if get_user_confirmation(
"Do you want to create a new requirements file using pipreqs?",
default="y",
args=args,
):
logging.info("Installing pipreqs...")
progress = show_progress("Installing pipreqs")
retry(
lambda: subprocess.run(
[str(pip_path), "install", "pipreqs"], check=True
),
error_handler=subprocess_error_handler,
)
progress()
# Verify pipreqs installation
pipreqs_path = (
VENV_DIR / ("Scripts" if os.name == "nt" else "bin") / "pipreqs"
)
if not pipreqs_path.exists():
logging.error(
"pipreqs is not installed or not found in the virtual environment."
)
sys.exit(1)
logging.info("Generating requirements.txt using pipreqs...")
progress = show_progress("Generating requirements.txt")
retry(
lambda: subprocess.run(
[
str(pipreqs_path),
".",
"--ignore",
".venv",
"--force",
],
check=True,
capture_output=True,
text=True,
),
error_handler=subprocess_error_handler,
)
progress()
if not REQUIREMENTS_FILE.exists() or REQUIREMENTS_FILE.stat().st_size == 0:
logging.error("Failed to generate a valid requirements.txt file.")
sys.exit(1)
logging.info("Requirements file created successfully.")
else:
logging.info("Exiting setup - requirements file is required.")
sys.exit(1)
else:
logging.info(f"Using existing requirements file: {REQUIREMENTS_FILE}")
def install_requirements(pip_path):
"""Install packages from requirements file.
Args:
pip_path: Path to pip executable
"""
logging.info("Step 5: Installing requirements")
progress = show_progress("Installing requirements")
retry(
lambda: subprocess.run(
[str(pip_path), "install", "-r", str(REQUIREMENTS_FILE)], check=True
),
error_handler=subprocess_error_handler,
)
progress()
logging.info("Requirements installed successfully.")
def validate_dependencies(pip_path):
"""Validate installed dependencies.
Args:
pip_path: Path to pip executable
"""
logging.info("Step 6: Validating installed dependencies")
try:
progress = show_progress("Validating dependencies")
subprocess.run([str(pip_path), "check"], check=True)
progress()
logging.info("Dependency validation complete.")
except subprocess.CalledProcessError as e:
logging.error(f"Dependency validation failed: {e}")
sys.exit(1)
def parse_outdated_packages(output_text):
"""Parse pip list --outdated output into a structured format.
Args:
output_text: Output from pip list --outdated
Returns:
list: List of dictionaries containing package information
"""
lines = output_text.strip().split("\n")
if len(lines) <= 2: # Header only, no packages
return []
packages = []
# Skip header lines (Package, Version, etc.)
for line in lines[2:]:
parts = line.split()
if len(parts) >= 3:
packages.append({"name": parts[0], "current": parts[1], "latest": parts[2]})
return packages
def update_packages(pip_path, packages, all_packages=False, args=None):
"""Update specified packages or all outdated packages.
Args:
pip_path: Path to pip executable
packages: List of package dictionaries
all_packages: Whether to update all packages at once
args: Command line arguments
"""
if not packages:
return
if all_packages or (args and args.non_interactive):
logging.info("Updating all outdated packages...")
progress = show_progress("Updating all packages")
retry(
lambda: subprocess.run(
[str(pip_path), "install", "--upgrade", "-r", str(REQUIREMENTS_FILE)],
check=True,
),
error_handler=subprocess_error_handler,
)
progress()
logging.info("All dependencies updated successfully.")
else:
# Let user select packages to update
print(
"\nSelect packages to update (comma-separated numbers, 'all', or 'none'):"
)
for i, pkg in enumerate(packages):
print(f"[{i+1}] {pkg['name']}: {pkg['current']} → {pkg['latest']}")
selection = input("\nPackages to update: ").strip().lower()
if selection == "none":
logging.info("No packages selected for update.")
return
elif selection == "all":
to_update = [pkg["name"] for pkg in packages]
else:
try:
indices = [
int(i.strip()) - 1 for i in selection.split(",") if i.strip()
]
to_update = [
packages[i]["name"] for i in indices if 0 <= i < len(packages)
]
except ValueError:
logging.error("Invalid selection. No packages will be updated.")
return
if to_update:
logging.info(f"Updating selected packages: {', '.join(to_update)}")
progress = show_progress("Updating selected packages")
retry(
lambda: subprocess.run(
[str(pip_path), "install", "--upgrade"] + to_update, check=True
),
error_handler=subprocess_error_handler,
)
progress()
logging.info("Selected packages updated successfully.")
else:
logging.info("No valid packages selected for update.")
def check_outdated_dependencies(pip_path, args):
"""Check for and optionally update outdated dependencies.
Args:
pip_path: Path to pip executable
args: Command line arguments
"""
logging.info("Step 7: Checking for outdated dependencies")
try:
progress = show_progress("Checking outdated packages")
result = subprocess.run(
[str(pip_path), "list", "--outdated", "--format=columns"],
check=True,
capture_output=True,
text=True,
)
progress()
if result.stdout.strip():
logging.info("Outdated dependencies found:")
logging.info(result.stdout.strip())
outdated_packages = parse_outdated_packages(result.stdout)
if get_user_confirmation(
"Do you want to update outdated dependencies?", default="y", args=args
):
if args and args.non_interactive:
update_packages(
pip_path, outdated_packages, all_packages=True, args=args
)
else:
selective_update = get_user_confirmation(
"Would you like to select specific packages to update?",
default="n",
args=args,
)
update_packages(
pip_path,
outdated_packages,
all_packages=not selective_update,
args=args,
)
else:
logging.info("All dependencies are up-to-date.")
except subprocess.CalledProcessError as e:
logging.error(f"Error checking for outdated dependencies: {e}")
sys.exit(1)
def main():
"""Main function to orchestrate the environment setup process."""
args = parse_args()
logging.info("Starting environment setup...")
# Execute steps sequentially
pip_path = setup_virtual_environment(args)
check_and_upgrade_pip(pip_path, args)
setup_system_certificates(pip_path, args)
handle_requirements_file(pip_path, args)
install_requirements(pip_path)
validate_dependencies(pip_path)
check_outdated_dependencies(pip_path, args)
logging.info("Setup complete!")
if __name__ == "__main__":
main()