Skip to content

Commit

Permalink
Merge pull request #1103 from luxonis/revert_depthai_helpers
Browse files Browse the repository at this point in the history
Reverted changes that removed depthai_helper scripts
  • Loading branch information
Erol444 authored Aug 17, 2023
2 parents e8818a0 + 686f399 commit e7db187
Show file tree
Hide file tree
Showing 5 changed files with 557 additions and 0 deletions.
84 changes: 84 additions & 0 deletions depthai_helpers/app_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import os
import shutil
import signal
import subprocess
import sys
import time
from pathlib import Path

initEnv = os.environ.copy()
if "PYTHONPATH" in initEnv:
initEnv["PYTHONPATH"] += ":" + str(Path(__file__).parent.parent.absolute())
else:
initEnv["PYTHONPATH"] = str(Path(__file__).parent.parent.absolute())



def quoted(val):
return '"' + str(val) + '"'

class App:
def __init__(self, appName, appPath=None, appRequirements=None, appEntrypoint=None):
self.appName = appName
self.appPath = appPath or Path(__file__).parent.parent / "apps" / self.appName
self.venvPath = self.appPath / "venv"
self.appPip = str(self.venvPath / "bin" / "pip") if os.name != 'nt' else (self.venvPath / "Scripts" / "pip.exe")
self.appInterpreter = str(self.venvPath / "bin" / "python") if os.name != 'nt' else (self.venvPath / "Scripts" / "python.exe")
self.appRequirements = appRequirements or self.appPath / "requirements.txt"
self.appEntrypoint = appEntrypoint or self.appPath / "main.py"

def createVenv(self, force=False):
try:
subprocess.check_call(' '.join([quoted(sys.executable), '-m', 'venv', '-h']), env=initEnv, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except:
print(f"Error accessing \"venv\" module! Please try to install \"python3.{sys.version_info[1]}-venv\" or see oficial docs here - https://docs.python.org/3/library/venv.html", file=sys.stderr)
sys.exit(1)
try:
subprocess.check_call(' '.join([quoted(sys.executable), '-m', 'pip', '-h']), env=initEnv, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except:
print("Error accessing \"pip\" module! Please try to install \"python3-pip\" or see oficial docs here - https://pip.pypa.io/en/stable/installation/", file=sys.stderr)
sys.exit(1)

if not force and Path(self.appInterpreter).exists() and Path(self.appPip).exists():
print("Existing venv found.")
else:
if self.venvPath.exists():
print("Recreating venv...")
shutil.rmtree(self.venvPath)
else:
print("Creating venv...")
try:
subprocess.check_call(' '.join([quoted(sys.executable), '-m', 'venv', quoted(str(self.venvPath.absolute()))]), shell=True, env=initEnv, cwd=self.appPath)
except:
print(f"Error creating a new virtual environment using \"venv\" module! Please try to install \"python3.{sys.version_info[1]}-venv\" again", file=sys.stderr)
sys.exit(1)
print("Installing requirements...")
subprocess.check_call(' '.join([quoted(self.appInterpreter), '-m', 'pip', 'install', '-U', 'pip']), env=initEnv, shell=True, cwd=self.appPath)
subprocess.check_call(' '.join([quoted(self.appInterpreter), '-m', 'pip', 'install', '--prefer-binary', '-r', quoted(str(self.appRequirements))]), env=initEnv, shell=True, cwd=self.appPath)

def runApp(self, shouldRun = lambda: True):
# Passthrough args to the app
args = [quoted(arg) for arg in sys.argv[1:]]
args.insert(0, quoted(str(self.appEntrypoint)))
args.insert(0, quoted(self.appInterpreter))
if os.name == 'nt':
pro = subprocess.Popen(' '.join(args), env=initEnv, shell=True, cwd=self.appPath)
else:
pro = subprocess.Popen(' '.join(args), env=initEnv, shell=True, cwd=self.appPath, preexec_fn=os.setsid)
while shouldRun() and pro.poll() is None:
try:
time.sleep(1)
except KeyboardInterrupt:
break

# if pro.poll() is not None:
try:
if os.name == 'nt':
subprocess.call(['taskkill', '/F', '/T', '/PID', str(pro.pid)])
else:
os.killpg(os.getpgid(pro.pid), signal.SIGTERM)

except ProcessLookupError:
pass


41 changes: 41 additions & 0 deletions depthai_helpers/cli_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3

from types import SimpleNamespace


class RangeFloat(object):
def __init__(self, start, end):
self.start = start
self.end = end

def __eq__(self, other):
return self.start <= other <= self.end

def __contains__(self, item):
return self.__eq__(item)

def __iter__(self):
yield self

def __str__(self):
return '[{0},{1}]'.format(self.start, self.end)


PrintColors = SimpleNamespace(
HEADER="\033[95m",
BLUE="\033[94m",
GREEN="\033[92m",
RED="\033[91m",
WARNING="\033[1;5;31m",
FAIL="\033[91m",
ENDC="\033[0m",
BOLD="\033[1m",
UNDERLINE="\033[4m",
BLACK_BG_RED="\033[1;31;40m",
BLACK_BG_GREEN="\033[1;32;40m",
BLACK_BG_BLUE="\033[1;34;40m",
)


def cliPrint(msg, print_color):
print("{0}{1}{2}".format(print_color, msg, PrintColors.ENDC))
Loading

0 comments on commit e7db187

Please sign in to comment.