commit 0057426e2323e7b9ba274c7ea85b99c2b7c869fc Author: DJ Gillespie Date: Thu Dec 12 10:08:04 2024 -0700 add install-chromedriver v0.1.0 package code diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..273e5cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,63 @@ +# Python +*.pyc +*.pyo +*.pyd +__pycache__/ +*.pdb +*.cover +*.log + +# Poetry +.poetry/ +*.toml.lock + +# Virtual Environment +venv/ +env/ +.env/ +.venv/ + +# PyInstaller +/dist/ +/build/ +/*.spec + +# Coverage +.coverage +.coverage.* + +# IDEs and editors +.vscode/ +.idea/ +*.swp +*.swo +*~ +*.bak + +# macOS +.DS_Store + +# Windows +Thumbs.db + +# Jupyter Notebooks +.ipynb_checkpoints/ + +# Miscellaneous +*.tgz +*.tar.gz +*.egg-info/ +*.egg + +# Poetry build artifacts +*.whl +*.tar.gz + +# pyenv +.python-version + +# Docker +.docker/ + +# Temporary files +*.log diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..52145d7 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,23 @@ +[tool.poetry] +name = "install-chromedriver" +version = "0.1.0" +description = "Simple CLI utility to install the matching chromedriver to your currently installed chrome in either your PATH, or your virtualenv, if one is enabled" +authors = ["Kenny's Mom "] +license = "MIT" + +[tool.poetry.dependencies] +python = ">=3.8,<3.14" +typer = "0.9.0" +webdriver-manager = "^4.0.2" + +[tool.poetry.dev-dependencies] +pyinstaller ="^6.11.1" + +[tool.poetry.scripts] +install-chromedriver = "cli:app" +build-executable = "scripts:install" +build-all = "scripts:build_all" + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" \ No newline at end of file diff --git a/scripts.py b/scripts.py new file mode 100644 index 0000000..92e5d45 --- /dev/null +++ b/scripts.py @@ -0,0 +1,27 @@ +from pathlib import Path +import subprocess +import sys + + +def install(): + HERE = Path(__file__).parent.absolute() + path_to_main = str(HERE / "src" / "cli.py") + try: + subprocess.check_call([ + "pyinstaller", + "--onefile", + "--name=install-chromedriver", + path_to_main + ]) + print("Executable built successfully.") + except subprocess.CalledProcessError: + print("PyInstaller failed.") + sys.exit(1) + + +def build_all(): + # Run poetry build + subprocess.run(["poetry", "build"], check=True) + + # Run the build-executable script + subprocess.run(["poetry", "run", "build-executable"], check=True) \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/cli.py b/src/cli.py new file mode 100644 index 0000000..d84143f --- /dev/null +++ b/src/cli.py @@ -0,0 +1,8 @@ +import typer +from install_chromedriver.install_chromedriver import install_chrome + +app = typer.Typer() +app.command()(install_chrome) + +if __name__ == "__main__": + app() diff --git a/src/install_chromedriver/__init__.py b/src/install_chromedriver/__init__.py new file mode 100644 index 0000000..77c0281 --- /dev/null +++ b/src/install_chromedriver/__init__.py @@ -0,0 +1,3 @@ +if __name__ == "__main__": + from cli import app + app() \ No newline at end of file diff --git a/src/install_chromedriver/install_chromedriver.py b/src/install_chromedriver/install_chromedriver.py new file mode 100644 index 0000000..bb30301 --- /dev/null +++ b/src/install_chromedriver/install_chromedriver.py @@ -0,0 +1,30 @@ +import os +import pathlib +import shutil +import typer +from typing_extensions import Annotated + +from webdriver_manager.chrome import ChromeDriverManager + + +def get_first_writable_directory(path): + + path_parts = pathlib.Path(path).parts + + for i in range(len(path_parts), 0, -1): + test_path = os.path.join(*path_parts[:i]) + if os.access(test_path, os.W_OK): + return test_path + return None + + +def install_chrome( + path: Annotated[str, typer.Option( + help="Directory in which to install chromedriver. Defaults to current virtualenv, if in a venv, else it defaults to placing the bin in the first writable directory on your PATH.")] = + os.path.join(os.environ.get("VIRTUAL_ENV") or get_first_writable_directory(os.environ.get("PATH")),'bin')): + chromedriver_bin = os.path.join(path, 'chromedriver') + if os.path.exists(chromedriver_bin): + os.remove(chromedriver_bin) + new_bin = ChromeDriverManager().install().replace('THIRD_PARTY_NOTICES.','') + shutil.copy(new_bin, chromedriver_bin) + os.chmod(chromedriver_bin, 0o755) diff --git a/src/install_chromedriver/pyinstaller.py b/src/install_chromedriver/pyinstaller.py new file mode 100644 index 0000000..abf6a32 --- /dev/null +++ b/src/install_chromedriver/pyinstaller.py @@ -0,0 +1,19 @@ +import PyInstaller.__main__ +from pathlib import Path + +HERE = Path(__file__).parent.absolute() +path_to_main = str(HERE / "cli.py") + + +def install(): + PyInstaller.__main__.run([ + path_to_main, + '--onefile', + '--windowed', + '--debug=all' + # other pyinstaller options... + ]) + + +if __name__ == "__main__": + install() \ No newline at end of file