From 80e231ec4fdda2754adf2a7435c25f3478b5c708 Mon Sep 17 00:00:00 2001 From: Mylo <36931363+gitmylo@users.noreply.github.com> Date: Fri, 5 May 2023 16:49:03 +0200 Subject: [PATCH] Initial commit --- install.py | 4 ++++ install_requirements.txt | 5 +++++ main.py | 6 ++++++ run.bat | 8 ++++++++ run.sh | 2 ++ run_py.bat | 3 +++ run_python.bat | 3 +++ setup_tools/__init__.py | 0 setup_tools/commands.py | 2 ++ setup_tools/os.py | 5 +++++ setup_tools/requirements_parser.py | 27 ++++++++++++++++++++++++++ setup_tools/venv.py | 31 ++++++++++++++++++++++++++++++ 12 files changed, 96 insertions(+) create mode 100644 install.py create mode 100644 install_requirements.txt create mode 100644 main.py create mode 100644 run.bat create mode 100644 run.sh create mode 100644 run_py.bat create mode 100644 run_python.bat create mode 100644 setup_tools/__init__.py create mode 100644 setup_tools/commands.py create mode 100644 setup_tools/os.py create mode 100644 setup_tools/requirements_parser.py create mode 100644 setup_tools/venv.py diff --git a/install.py b/install.py new file mode 100644 index 0000000..fa53a7d --- /dev/null +++ b/install.py @@ -0,0 +1,4 @@ + + +def ensure_installed(): + pass \ No newline at end of file diff --git a/install_requirements.txt b/install_requirements.txt new file mode 100644 index 0000000..5eb0b97 --- /dev/null +++ b/install_requirements.txt @@ -0,0 +1,5 @@ +;;; Do not run pip install -r on this file, let the installer handle it. +;;; Every line will be executed like [package package package];[args];[condition];[comment]. +;;; If there's no condition, always install. +torch torchvision torchaudio;--index-url https://download.pytorch.org/whl/cu117;windows +torch torchvision torchaudio;;not windows \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..8b7ba0f --- /dev/null +++ b/main.py @@ -0,0 +1,6 @@ +from logging import info +from install import ensure_installed + +info('Checking installs and venv') + +ensure_installed() # Installs missing packages diff --git a/run.bat b/run.bat new file mode 100644 index 0000000..cb445aa --- /dev/null +++ b/run.bat @@ -0,0 +1,8 @@ +@echo off +where py -V > nul 2> nul +if %errorlevel%==0 ( + call run_py.bat +) else ( + call run_python.bat +) +pause \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..3e28625 --- /dev/null +++ b/run.sh @@ -0,0 +1,2 @@ +python main.py +read -n1 -r -p "Press any key to exit..." key diff --git a/run_py.bat b/run_py.bat new file mode 100644 index 0000000..6eb8f59 --- /dev/null +++ b/run_py.bat @@ -0,0 +1,3 @@ +@echo off +py main.py +pause \ No newline at end of file diff --git a/run_python.bat b/run_python.bat new file mode 100644 index 0000000..8f85016 --- /dev/null +++ b/run_python.bat @@ -0,0 +1,3 @@ +@echo off +python main.py +pause \ No newline at end of file diff --git a/setup_tools/__init__.py b/setup_tools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/setup_tools/commands.py b/setup_tools/commands.py new file mode 100644 index 0000000..18dba43 --- /dev/null +++ b/setup_tools/commands.py @@ -0,0 +1,2 @@ +def run_command(command, args=''): + pass diff --git a/setup_tools/os.py b/setup_tools/os.py new file mode 100644 index 0000000..b4a9f4c --- /dev/null +++ b/setup_tools/os.py @@ -0,0 +1,5 @@ +import os + + +def is_windows(): + return os.name == 'nt' diff --git a/setup_tools/requirements_parser.py b/setup_tools/requirements_parser.py new file mode 100644 index 0000000..00145b2 --- /dev/null +++ b/setup_tools/requirements_parser.py @@ -0,0 +1,27 @@ +from .os import is_windows + + +def parse_requirements(req_file='install_requirements.txt'): + + # Some stuff for the install_requirements + windows = is_windows() + + requirements_parsed = [] + with open(req_file, 'r') as file: + for line in file.readlines(): + line = line.strip() + + # Make sure it can be unpacked + semis = line.count(';') + add_semis = (2 - semis) * ';' + line = line + add_semis + + packages, args, condition = line.split(';')[:3] + # if eval(condition): + if condition: + if eval(condition): + for package in packages.split(' '): + package = package.strip() + if package: + requirements_parsed.append(f'{package} {args}') + return requirements_parsed diff --git a/setup_tools/venv.py b/setup_tools/venv.py new file mode 100644 index 0000000..407e6fe --- /dev/null +++ b/setup_tools/venv.py @@ -0,0 +1,31 @@ +import sys +from .os import is_windows +import os + +venv_name = 'venv' +venv_activate_path = f'{venv_name}/' + 'Scripts/activate.bat' if is_windows() else 'bin/activate' + + +def get_base_prefix_compat(): + """Get base/real prefix, or sys.prefix if there is none.""" + return getattr(sys, "base_prefix", None) or getattr(sys, "real_prefix", None) or sys.prefix + + +def in_venv(): + return get_base_prefix_compat() != sys.prefix + + +def activate_venv(): + if not os.path.isdir(venv_name): + pass + # Launch the main.py with the venv + exit() # Exit after the venv'ed version exits + + +def ensure_venv(): + if not in_venv(): + print('activating venv') + activate_venv() + else: + print('in venv') + print('after venv in venv:', in_venv())