|
| 1 | +import platform |
| 2 | +import subprocess |
| 3 | +from pathlib import Path |
| 4 | +from venv import EnvBuilder |
| 5 | + |
| 6 | + |
| 7 | +class Colors: |
| 8 | + """ANSI color codes""" |
| 9 | + |
| 10 | + BLACK = "\033[0;30m" |
| 11 | + RED = "\033[0;31m" |
| 12 | + GREEN = "\033[0;32m" |
| 13 | + BROWN = "\033[0;33m" |
| 14 | + BLUE = "\033[0;34m" |
| 15 | + PURPLE = "\033[0;35m" |
| 16 | + CYAN = "\033[0;36m" |
| 17 | + LIGHT_GRAY = "\033[0;37m" |
| 18 | + DARK_GRAY = "\033[1;30m" |
| 19 | + LIGHT_RED = "\033[1;31m" |
| 20 | + LIGHT_GREEN = "\033[1;32m" |
| 21 | + YELLOW = "\033[1;33m" |
| 22 | + LIGHT_BLUE = "\033[1;34m" |
| 23 | + LIGHT_PURPLE = "\033[1;35m" |
| 24 | + LIGHT_CYAN = "\033[1;36m" |
| 25 | + LIGHT_WHITE = "\033[1;37m" |
| 26 | + BOLD = "\033[1m" |
| 27 | + FAINT = "\033[2m" |
| 28 | + ITALIC = "\033[3m" |
| 29 | + UNDERLINE = "\033[4m" |
| 30 | + BLINK = "\033[5m" |
| 31 | + NEGATIVE = "\033[7m" |
| 32 | + CROSSED = "\033[9m" |
| 33 | + END = "\033[0m" |
| 34 | + # cancel SGR codes if we don't write to a terminal |
| 35 | + if not __import__("sys").stdout.isatty(): |
| 36 | + for attr in dir(): |
| 37 | + if not attr.startswith("_"): |
| 38 | + if isinstance(getattr(__class__, attr), str): |
| 39 | + setattr(__class__, attr, "") |
| 40 | + elif __import__("platform").system() == "Windows": |
| 41 | + kernel32 = __import__("ctypes").windll.kernel32 |
| 42 | + kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7) |
| 43 | + del kernel32 |
| 44 | + |
| 45 | + |
| 46 | +venv_dir = Path() / ".venv" |
| 47 | +requirements_file = Path("requirements.txt") |
| 48 | +website_dir = Path("website") |
| 49 | +if not platform.platform().startswith("Windows"): |
| 50 | + venv_bin = venv_dir / "bin" |
| 51 | + venv_python = venv_bin / "python" |
| 52 | + venv_pre_commit = venv_bin / "pre-commit" |
| 53 | +else: |
| 54 | + venv_bin = venv_dir / "Scripts" |
| 55 | + venv_python = venv_bin / "python.exe" |
| 56 | + venv_pre_commit = venv_bin / "pre-commit.exe" |
| 57 | + |
| 58 | +if not venv_dir.exists(): |
| 59 | + print(f"Creating virtualenv in {venv_dir}") |
| 60 | + EnvBuilder(with_pip=True).create(venv_dir) |
| 61 | + |
| 62 | +subprocess.run([venv_python, "-m", "pip", "install", "-U", "pip"], check=True) |
| 63 | +subprocess.run([venv_python, "-m", "pip", "install", "-U", "pre-commit"], check=True) |
| 64 | +install_pdf_tooling = False |
| 65 | +if requirements_file.exists(): |
| 66 | + answer = input(f"{Colors.GREEN}Install Robot Framework + browser batteries for PDF generation?{Colors.END} {Colors.YELLOW}[y/N]: {Colors.END}").strip().lower() |
| 67 | + install_pdf_tooling = answer in {"y", "yes"} |
| 68 | + if install_pdf_tooling: |
| 69 | + subprocess.run([venv_python, "-m", "pip", "install", "-r", str(requirements_file)], check=True) |
| 70 | + subprocess.run([venv_python, "-m", "Browser.entry", "install", "chromium", "--with-deps"], check=True) |
| 71 | + |
| 72 | +subprocess.run([venv_pre_commit, "install"], check=True) |
| 73 | + |
| 74 | +if website_dir.exists(): |
| 75 | + npm_cmd = ["npm", "install"] |
| 76 | + print(f"Running {' '.join(npm_cmd)} in {website_dir}") |
| 77 | + subprocess.run(npm_cmd, cwd=website_dir, check=True) |
| 78 | + |
| 79 | +activate_script = ( |
| 80 | + "source .venv/bin/activate" |
| 81 | + if not platform.platform().startswith("Windows") |
| 82 | + else ".venv\\Scripts\\activate" |
| 83 | +) |
| 84 | + |
| 85 | +print(f"\n\nVirtualenv '{Colors.GREEN}{venv_dir}{Colors.END}' is ready and up-to-date.") |
| 86 | +print(f"Run '{Colors.GREEN}{activate_script}{Colors.END}' to activate the virtualenv.") |
0 commit comments