This repository was archived by the owner on Jan 5, 2026. It is now read-only.
forked from shreyasmene06/pyvm-updater
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_version.py
More file actions
652 lines (524 loc) · 23.5 KB
/
python_version.py
File metadata and controls
652 lines (524 loc) · 23.5 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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#!/usr/bin/env python3
"""
Python Version Manager - CLI Tool
Checks and installs Python to the latest version across Windows, Linux, and macOS
IMPORTANT: This tool installs Python but does NOT modify your system's default Python.
Your existing Python installation remains unchanged to avoid breaking system tools.
Requirements:
pip install requests beautifulsoup4 packaging click
Note: Dependencies are automatically installed via setup.py during CLI installation.
"""
import platform
import sys
import os
import subprocess
import tempfile
import shutil
import hashlib
import re
import time
from pathlib import Path
from typing import Optional, Tuple
try:
import requests
from bs4 import BeautifulSoup
from packaging import version as pkg_version
import click
except ImportError as e:
print("ERROR: Missing required packages.")
print("Please install them using:")
print(" pip install requests beautifulsoup4 packaging click")
print("\nOr install this tool via:")
print(" pip install -e .")
print(f"\nDetails: {e}")
sys.exit(1)
# Constants
MAX_RETRIES = 3
RETRY_DELAY = 2 # seconds
DOWNLOAD_TIMEOUT = 120 # seconds
REQUEST_TIMEOUT = 15 # seconds
def get_os_info():
"""Detect the operating system and architecture"""
os_name = platform.system().lower()
machine = platform.machine().lower()
# Normalize architecture names
if machine in ['amd64', 'x86_64']:
arch = 'amd64'
elif machine in ['arm64', 'aarch64']:
arch = 'arm64'
else:
arch = 'x86'
return os_name, arch
def is_admin():
"""Check if script is running with admin/sudo privileges"""
try:
if platform.system().lower() == 'windows':
import ctypes
# Type hint fix: windll is only available on Windows
return ctypes.windll.shell32.IsUserAnAdmin() != 0 # type: ignore[attr-defined]
else:
# os.geteuid() only exists on Unix-like systems
return hasattr(os, 'geteuid') and os.geteuid() == 0
except Exception:
return False
def validate_version_string(version_str: str) -> bool:
"""Validate that version string matches expected format (e.g., 3.11.5)"""
if not version_str:
return False
# Match format: digit.digit[.digit[...]]
pattern = r'^\d+\.\d+(\.\d+)*$'
return bool(re.match(pattern, version_str))
def get_latest_python_info_with_retry() -> Tuple[Optional[str], Optional[str]]:
"""Fetch the latest Python version with retry logic"""
for attempt in range(MAX_RETRIES):
try:
result = get_latest_python_info()
if result[0]: # If we got a version
return result
if attempt < MAX_RETRIES - 1:
time.sleep(RETRY_DELAY * (attempt + 1)) # Exponential backoff
except Exception as e:
if attempt < MAX_RETRIES - 1:
print(f"Attempt {attempt + 1} failed, retrying...")
time.sleep(RETRY_DELAY * (attempt + 1))
else:
print(f"All retry attempts failed: {e}")
return None, None
def get_latest_python_info() -> Tuple[Optional[str], Optional[str]]:
"""Fetch the latest Python version and download URLs"""
URL = "https://www.python.org/downloads/"
try:
response = requests.get(URL, timeout=REQUEST_TIMEOUT)
response.raise_for_status()
# Specify parser explicitly for consistency
soup = BeautifulSoup(response.text, 'html.parser')
# Get version from download button
download_button = soup.find('a', class_='button')
if not download_button:
print("Error: Could not find download button on Python.org")
return None, None
latest_ver_string = download_button.get_text(strip=True)
latest_ver = latest_ver_string.split()[-1]
# Validate version string
if not validate_version_string(latest_ver):
print(f"Error: Invalid version format retrieved: {latest_ver}")
return None, None
# Get download URL for specific OS
download_url_raw = download_button.get('href')
download_url: Optional[str] = None
if download_url_raw and isinstance(download_url_raw, str):
if not download_url_raw.startswith('http'):
download_url = f"https://www.python.org{download_url_raw}"
else:
download_url = download_url_raw
return latest_ver, download_url
except requests.Timeout:
print("Error: Request to python.org timed out. Check your internet connection.")
return None, None
except requests.RequestException as e:
print(f"Error: Network request failed: {e}")
return None, None
except Exception as e:
print(f"Error: Unexpected error while fetching Python info: {e}")
return None, None
def download_file(url: str, destination: str) -> bool:
"""Download a file with progress indication and integrity checking"""
try:
# Validate URL
if not url.startswith(('https://', 'http://')):
print(f"Error: Invalid URL scheme: {url}")
return False
response = requests.get(url, stream=True, timeout=DOWNLOAD_TIMEOUT)
response.raise_for_status()
total_size = int(response.headers.get('content-length', 0))
block_size = 8192
downloaded = 0
with open(destination, 'wb') as f:
for chunk in response.iter_content(chunk_size=block_size):
if chunk:
f.write(chunk)
downloaded += len(chunk)
if total_size:
percent = (downloaded / total_size) * 100
print(f"\rDownloading: {percent:.1f}% ({downloaded}/{total_size} bytes)", end='', flush=True)
else:
# No content-length header
print(f"\rDownloading: {downloaded} bytes", end='', flush=True)
print() # New line after progress
# Verify file was downloaded
if not os.path.exists(destination):
print("Error: Downloaded file not found")
return False
file_size = os.path.getsize(destination)
if total_size and file_size != total_size:
print(f"Warning: Downloaded file size ({file_size}) doesn't match expected size ({total_size})")
return True
except requests.Timeout:
print("\nError: Download timed out. Please check your internet connection.")
return False
except requests.RequestException as e:
print(f"\nError: Download failed: {e}")
return False
except IOError as e:
print(f"\nError: Could not write to file {destination}: {e}")
return False
except Exception as e:
print(f"\nError: Unexpected error during download: {e}")
return False
def update_python_windows(version_str: str) -> bool:
"""Update Python on Windows"""
print("\n🪟 Windows detected - Downloading Python installer...")
# Validate version string
if not validate_version_string(version_str):
print(f"Error: Invalid version string: {version_str}")
return False
# Construct Windows installer URL - safely
try:
parts = version_str.split('.')
if len(parts) < 3:
print(f"Error: Version string must have major.minor.patch format: {version_str}")
return False
major, minor, patch = parts[0], parts[1], parts[2]
except (ValueError, IndexError) as e:
print(f"Error parsing version string '{version_str}': {e}")
return False
arch = 'amd64' if platform.machine().lower() in ['amd64', 'x86_64'] else 'win32'
installer_url = f"https://www.python.org/ftp/python/{version_str}/python-{version_str}-{arch}.exe"
temp_dir = tempfile.gettempdir()
installer_path = os.path.join(temp_dir, f"python-{version_str}-installer.exe")
print(f"Downloading from: {installer_url}")
if not download_file(installer_url, installer_path):
return False
print("\n⚠️ Starting installer...")
print("Please follow the installer prompts.")
print("Recommendation: Check 'Add Python to PATH'")
try:
# Run installer (interactive mode) - using list instead of shell
result = subprocess.run([installer_path], check=False)
if result.returncode != 0:
print(f"Warning: Installer exited with code {result.returncode}")
return True
except FileNotFoundError:
print(f"Error: Installer not found at {installer_path}")
return False
except PermissionError:
print("Error: Permission denied. Try running as Administrator.")
return False
except Exception as e:
print(f"Error running installer: {e}")
return False
finally:
# Cleanup - with better error handling
try:
if os.path.exists(installer_path):
os.remove(installer_path)
print(f"Cleaned up temporary installer file")
except PermissionError:
print(f"Warning: Could not delete temporary file {installer_path} (permission denied)")
except OSError as e:
print(f"Warning: Could not delete temporary file {installer_path}: {e}")
def update_python_linux(version_str: str) -> bool:
"""Install Python on Linux using package manager (does NOT modify system defaults)"""
print("\n🐧 Linux detected")
# Validate version string
if not validate_version_string(version_str):
print(f"Error: Invalid version string: {version_str}")
return False
# Extract major.minor version (e.g., "3.11" from "3.11.5")
try:
parts = version_str.split('.')
if len(parts) < 2:
print(f"Error: Invalid version format: {version_str}")
return False
major_minor = f"{parts[0]}.{parts[1]}"
except (ValueError, IndexError) as e:
print(f"Error parsing version: {e}")
return False
# Detect package manager
if shutil.which('apt'):
print("Using apt package manager...")
print("\n⚠️ This requires sudo privileges to install Python.")
print("⚠️ This will add the deadsnakes PPA (third-party repository).")
print("\n⚠️ IMPORTANT: This will NOT modify your system's default Python.")
print(" Your existing Python will remain unchanged.")
# Use safer subprocess approach - no shell=True
commands = [
["sudo", "apt", "update"],
["sudo", "apt", "install", "-y", "software-properties-common"],
["sudo", "add-apt-repository", "-y", "ppa:deadsnakes/ppa"],
["sudo", "apt", "update"],
["sudo", "apt", "install", "-y", f"python{major_minor}"],
["sudo", "apt", "install", "-y", f"python{major_minor}-venv", f"python{major_minor}-distutils"]
]
for cmd in commands:
print(f"Running: {' '.join(cmd)}")
try:
result = subprocess.run(cmd, check=False, capture_output=False)
if result.returncode != 0:
print(f"⚠️ Command failed with exit code {result.returncode}: {' '.join(cmd)}")
print("Continuing anyway...")
except FileNotFoundError:
print(f"Error: Command not found: {cmd[0]}")
return False
except Exception as e:
print(f"Error running command: {e}")
return False
# Verify installation
python_path = f"/usr/bin/python{major_minor}"
if not os.path.exists(python_path):
print(f"⚠️ Warning: {python_path} not found after installation")
return False
print(f"\n✅ Python {major_minor} installed successfully at {python_path}")
print(f"\n💡 Your system Python remains unchanged. Use 'python{major_minor}' to access the new version.")
return True
elif shutil.which('yum') or shutil.which('dnf'):
pkg_mgr = 'dnf' if shutil.which('dnf') else 'yum'
print(f"Using {pkg_mgr} package manager...")
print("\n⚠️ This requires sudo privileges.")
print(f"\nPlease run manually:")
print(f" sudo {pkg_mgr} install python3")
print(f"\nNote: Specific version {version_str} may not be available via {pkg_mgr}")
print("Consider using pyenv for version-specific installations.")
return False
else:
print("No supported package manager found (apt, yum, or dnf).")
print("\n📦 Recommended: Install pyenv for easy Python version management")
print("Visit: https://github.com/pyenv/pyenv#installation")
print("\nPyenv installation (quick):")
print(" curl https://pyenv.run | bash")
print(f" pyenv install {version_str}")
return False
def update_python_macos(version_str: str) -> bool:
"""Update Python on macOS using Homebrew or official installer"""
print("\n🍎 macOS detected")
# Validate version string
if not validate_version_string(version_str):
print(f"Error: Invalid version string: {version_str}")
return False
if shutil.which('brew'):
print("Using Homebrew...")
print("Attempting to update Python via Homebrew...")
try:
# Update Homebrew
print("Updating Homebrew...")
result = subprocess.run(["brew", "update"], check=False, capture_output=True, text=True)
if result.returncode != 0:
print(f"Warning: brew update failed: {result.stderr}")
# Upgrade Python
print("Upgrading Python...")
result = subprocess.run(["brew", "upgrade", "python3"], check=False, capture_output=True, text=True)
if result.returncode != 0:
print(f"Note: {result.stderr}")
print("Python may already be up-to-date or not installed via Homebrew")
return True
except FileNotFoundError:
print("Error: Homebrew command not found")
return False
except Exception as e:
print(f"Error running Homebrew: {e}")
return False
else:
print("Homebrew not found.")
print("\n📥 Option 1: Install via official installer")
# Fix URL construction - proper format is "3-11-5" not "3115"
url_version = version_str.replace('.', '-')
print(f" https://www.python.org/downloads/release/python-{url_version}/")
print("\n📦 Option 2: Install Homebrew first")
print(" /bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"")
print(" Then run: brew install python")
return False
def check_python_version(silent: bool = False) -> Tuple[str, Optional[str], bool]:
"""
Check local Python version against the latest stable version from python.org
Returns: (local_version, latest_version, needs_update)
"""
local_ver = platform.python_version()
if not silent:
print(f"Checking Python version... (Current: {local_ver})")
# Use retry logic
latest_ver, _ = get_latest_python_info_with_retry()
if not latest_ver:
if not silent:
print("Error: Could not fetch latest version information.")
print("Please check your internet connection and try again.")
return local_ver, None, False
try:
# Validate latest version
if not validate_version_string(latest_ver):
if not silent:
print(f"Error: Invalid version format from server: {latest_ver}")
return local_ver, None, False
# Use 'version.parse' to create comparable version objects
local_version_obj = pkg_version.parse(local_ver)
latest_version_obj = pkg_version.parse(latest_ver)
needs_update = local_version_obj < latest_version_obj
if not silent:
# Display Results
print("\n" + "=" * 40)
print(" Python Version Check Report")
print("=" * 40)
print(f"Your version: {local_ver}")
print(f"Latest version: {latest_ver}")
print("=" * 40)
if not needs_update:
print("✓ You are up-to-date!")
else:
print(f"⚠ A new version ({latest_ver}) is available!")
return local_ver, latest_ver, needs_update
except Exception as e:
if not silent:
print(f"Error comparing versions: {e}")
print("This might be due to an unexpected version format.")
return local_ver, latest_ver, False
def show_python_usage_instructions(version_str: str, os_name: str):
"""
Show user how to use the newly installed Python version.
Does NOT modify system defaults - just provides instructions.
"""
# Extract major.minor for display
try:
parts = version_str.split('.')
major_minor = f"{parts[0]}.{parts[1]}"
except (ValueError, IndexError):
major_minor = version_str
click.echo("\n" + "=" * 60)
click.echo("✅ Installation Complete!")
click.echo("=" * 60)
click.echo(f"\n📌 Python {version_str} has been installed successfully!")
click.echo("\n📚 How to use your new Python version:")
click.echo("-" * 60)
if os_name == 'linux' or os_name == 'darwin':
click.echo(f"\n1️⃣ Run scripts with the new version:")
click.echo(f" python{major_minor} your_script.py")
click.echo(f"\n2️⃣ Create a virtual environment:")
click.echo(f" python{major_minor} -m venv myproject")
click.echo(f" source myproject/bin/activate")
click.echo(f" python --version # Will show {version_str}")
click.echo(f"\n3️⃣ Check it's installed:")
click.echo(f" python{major_minor} --version")
elif os_name == 'windows':
click.echo(f"\n1️⃣ Use Python Launcher:")
click.echo(f" py -{major_minor} your_script.py")
click.echo(f"\n2️⃣ List all Python versions:")
click.echo(f" py --list")
click.echo(f"\n3️⃣ Create a virtual environment:")
click.echo(f" py -{major_minor} -m venv myproject")
click.echo(f" myproject\\Scripts\\activate")
click.echo("-" * 60)
click.echo("\n💡 Important: Your old Python version remains as system default.")
click.echo(" This prevents breaking system tools and existing scripts.")
click.echo(" Use the specific version command when you need the new Python.")
click.echo("\n⚠️ Note: Restart your terminal to ensure PATH is updated.")
@click.group(invoke_without_command=True)
@click.pass_context
@click.option('--version', is_flag=True, help='Show tool version')
def cli(ctx, version):
"""Python Version Manager - Check and install Python (does NOT modify system defaults)"""
if version:
click.echo("Python Version Manager v1.2.1")
ctx.exit()
if ctx.invoked_subcommand is None:
# Default behavior: just check version
ctx.invoke(check)
@cli.command()
def check():
"""Check current Python version against latest stable release"""
try:
local_ver, latest_ver, needs_update = check_python_version(silent=False)
if needs_update:
click.echo("\n💡 Tip: Run 'pyvm update' to upgrade Python")
sys.exit(1) # Exit code 1 indicates update available
else:
sys.exit(0) # Exit code 0 indicates up-to-date
except KeyboardInterrupt:
click.echo("\n\nOperation cancelled by user.")
sys.exit(130)
@cli.command()
@click.option('--auto', is_flag=True, help='Automatically proceed without confirmation')
def update(auto):
"""Download and install the latest Python version (does NOT modify system defaults)"""
try:
click.echo("🔍 Checking for updates...")
local_ver, latest_ver, needs_update = check_python_version(silent=True)
if not latest_ver:
click.echo("❌ Could not fetch latest version information.")
sys.exit(1)
click.echo(f"\n📊 Current version: {local_ver}")
click.echo(f"📊 Latest version: {latest_ver}")
if not needs_update:
click.echo("\n✅ You already have the latest version!")
sys.exit(0)
click.echo(f"\n🚀 Update available: {local_ver} → {latest_ver}")
# Confirm update
if not auto:
if not click.confirm("\nDo you want to proceed with the installation?"):
click.echo("Installation cancelled.")
sys.exit(0)
# Check admin privileges for some operations
os_name, arch = get_os_info()
click.echo(f"\n🖥️ Detected: {os_name.title()} ({arch})")
# Perform update based on OS
success = False
if os_name == 'windows':
success = update_python_windows(latest_ver)
elif os_name == 'linux':
success = update_python_linux(latest_ver)
elif os_name == 'darwin':
success = update_python_macos(latest_ver)
else:
click.echo(f"❌ Unsupported operating system: {os_name}")
sys.exit(1)
if success:
# Show usage instructions (safe, no system modifications)
show_python_usage_instructions(latest_ver, os_name)
else:
click.echo("\n⚠️ Installation process encountered issues.")
click.echo(" Please check the messages above.")
sys.exit(1)
except KeyboardInterrupt:
click.echo("\n\nOperation cancelled by user.")
sys.exit(130)
except Exception as e:
click.echo(f"\n❌ Error: {e}")
sys.exit(1)
@cli.command()
def info():
"""Show detailed system and Python information"""
try:
click.echo("=" * 50)
click.echo(" System Information")
click.echo("=" * 50)
os_name, arch = get_os_info()
click.echo(f"Operating System: {os_name.title()}")
click.echo(f"Architecture: {arch}")
click.echo(f"Python Version: {platform.python_version()}")
click.echo(f"Python Path: {sys.executable}")
click.echo(f"Platform: {platform.platform()}")
click.echo(f"\nAdmin/Sudo: {'Yes' if is_admin() else 'No'}")
# Show python3 command location if different
try:
result = subprocess.run(
["which", "python3"],
capture_output=True,
text=True,
check=False
)
if result.returncode == 0:
python3_path = result.stdout.strip()
if python3_path != sys.executable:
click.echo(f"python3 command: {python3_path}")
except Exception:
pass
click.echo("=" * 50)
except Exception as e:
click.echo(f"Error: {e}")
sys.exit(1)
def main():
"""Main entry point for the script"""
try:
cli()
except Exception as e:
click.echo(f"\n❌ Unexpected error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()