Skip to content

Commit

Permalink
feat: attempt to check whether packages are installed
Browse files Browse the repository at this point in the history
  • Loading branch information
kelly-sovacool committed Feb 20, 2024
1 parent 851da2e commit 98200a9
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
#!/usr/bin/env python
"""
Wrapper for the champagne CLI.
Allows running the CLI out-of-the-box without first installing it with pip.
"""
import importlib.util
import os
import re
import sys
import tomllib

# add script directory to the path to allow champagne CLI to work out-of-the-box
# without the need to install it via pip first
SCRIPT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "src")
sys.path.append(SCRIPT_DIR)
from src.__main__ import main


def check_packages_installed(proj_file="pyproject.toml"):
"""Check whether required dependencies are installed.
WARNING: Does not check for the required version.
"""

with open(proj_file, "rb") as file:
proj_data = tomllib.load(file)
package_deps = {pkg.split()[0] for pkg in proj_data["project"]["dependencies"]}
missing_pkgs = {pkg for pkg in package_deps if not importlib.util.find_spec(pkg)}
if any(missing_pkgs):
raise ImportError(
f"Required Python packages are not installed: {', '.join(missing_pkgs)}"
)


if (
__name__ == "__main__"
): # this block is adapted from the bin/champagne file created by pip
check_packages_installed()
sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0])
sys.exit(main())

0 comments on commit 98200a9

Please sign in to comment.