Skip to content

Commit

Permalink
Make cam_test.py available through the depthai binary. (#983)
Browse files Browse the repository at this point in the history
* Expose cam_test through depthai_cli too.

* Change the argument parsing to allow more checks to be done by argparse

* Use subparsers for subcommands (like cam_test) to print better help.

---------

Co-authored-by: Matevz Morato <[email protected]>
  • Loading branch information
zrezke and Matevz Morato authored Feb 26, 2024
1 parent 8b8a4da commit ee001db
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 25 deletions.
20 changes: 0 additions & 20 deletions depthai_cli/depthai.py

This file was deleted.

49 changes: 49 additions & 0 deletions depthai_cli/depthai_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
from pathlib import Path
import subprocess

here = os.path.dirname(os.path.realpath(__file__))

if os.path.exists(os.path.join(here, "cam_test.py")): # Installed package
CAM_TEST_PATH = Path(here) / "cam_test.py"
else:
CAM_TEST_PATH = (
Path(here) / ".." / "utilities" / "cam_test.py"
) # Execution from source
CAM_TEST_PATH = str(CAM_TEST_PATH)


def cli() -> int:
import argparse
import sys
import depthai as dai
parser = argparse.ArgumentParser(description="DepthAI CLI", add_help=True)
parser.add_argument("-v", "--version", action="store_true", help="Print version and exit.")
parser.add_argument("-l", "--list-devices", action="store_true", help="List connected devices.")
subparsers = parser.add_subparsers(dest="command", help="Sub-commands")
# Define the parser for the "cam_test" command
cam_test_parser = subparsers.add_parser("cam_test", help="Commands and options for cam_test", add_help=False)
cam_test_parser.add_argument("args", nargs=argparse.REMAINDER, help="Arguments to pass to cam_test")

# subparser REMINDER args would get parsed too if we used parse_args, so we have to handle unknown args manually
args, unknown_args = parser.parse_known_args()
if args.command == "cam_test":
cam_test_path = CAM_TEST_PATH
return subprocess.run([sys.executable, cam_test_path] + cam_test_parser.parse_args().args[1:]).returncode
# Parse other subcommands here
elif unknown_args:
parser.error(f"Unrecognized arguments: {unknown_args}") # handles exit internally
elif args.version:
print(dai.__version__)
return 0
elif args.list_devices:
print(dai.Device.getAllConnectedDevices())
return 0
else:
# No recognized commands, print help
parser.print_help()
return 1


if __name__ == "__main__":
exit(cli())
23 changes: 19 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

### NAME
MODULE_NAME = 'depthai'
DEPTHAI_CLI_MODULE_NAME = 'depthai_cli'

### VERSION
here = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -92,6 +93,20 @@ def run(self):
self.build_extension(ext)

def build_extension(self, ext):
if ext.name == DEPTHAI_CLI_MODULE_NAME:
# Copy cam_test.py and it's dependencies to depthai_cli/
cam_test_path = os.path.join(here, "utilities", "cam_test.py")
cam_test_dest = os.path.join(self.build_lib, DEPTHAI_CLI_MODULE_NAME, "cam_test.py")
cam_test_gui_path = os.path.join(here, "utilities", "cam_test_gui.py")
cam_test_gui_dest = os.path.join(self.build_lib, DEPTHAI_CLI_MODULE_NAME, "cam_test_gui.py")
stress_test_path = os.path.join(here, "utilities", "stress_test.py")
stress_test_dest = os.path.join(self.build_lib, DEPTHAI_CLI_MODULE_NAME, "stress_test.py")
files_to_copy = [(cam_test_path, cam_test_dest), (cam_test_gui_path, cam_test_gui_dest), (stress_test_path, stress_test_dest)]
for src, dst in files_to_copy:
with open(src, "r") as f:
with open(dst, "w") as f2:
f2.write(f.read())
return

extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
# required for auto-detection of auxiliary "native" libs
Expand Down Expand Up @@ -205,11 +220,11 @@ def build_extension(self, ext):
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/luxonis/depthai-python",
ext_modules=[CMakeExtension(MODULE_NAME)],
ext_modules=[CMakeExtension(MODULE_NAME), Extension(DEPTHAI_CLI_MODULE_NAME, sources=[])],
cmdclass={
'build_ext': CMakeBuild
'build_ext': CMakeBuild,
},
packages=["depthai_cli"],
packages=[DEPTHAI_CLI_MODULE_NAME],
zip_safe=False,
classifiers=[
"Development Status :: 4 - Beta",
Expand Down Expand Up @@ -237,7 +252,7 @@ def build_extension(self, ext):
python_requires='>=3.6',
entry_points={
"console_scripts": [
'depthai=depthai_cli.depthai:cli'
f'depthai={DEPTHAI_CLI_MODULE_NAME}.depthai_cli:cli'
]
}
)
2 changes: 1 addition & 1 deletion utilities/cam_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def socket_type_pair(arg):
parser.add_argument("-h", "--help", action="store_true", default=False,
help="Show this help message and exit") # So you can forward --help to stress test, without it being consumed by cam_test.py

args, _unknown = parser.parse_known_args()
args = parser.parse_args()

# Set timeouts before importing depthai
os.environ["DEPTHAI_CONNECTION_TIMEOUT"] = str(args.connection_timeout)
Expand Down

0 comments on commit ee001db

Please sign in to comment.