From e729ce8126a09c08bb15a6cc59f6a53a0e101f15 Mon Sep 17 00:00:00 2001 From: joncrall Date: Fri, 22 Nov 2024 12:09:04 -0500 Subject: [PATCH 1/4] refactor: Move main requirements.txt to requirements/runtime.txt --- requirements.txt => requirements/runtime.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename requirements.txt => requirements/runtime.txt (100%) diff --git a/requirements.txt b/requirements/runtime.txt similarity index 100% rename from requirements.txt rename to requirements/runtime.txt From ab5e35a540ffa2ed49ba32d8e3332931361570a8 Mon Sep 17 00:00:00 2001 From: joncrall Date: Fri, 22 Nov 2024 12:11:26 -0500 Subject: [PATCH 2/4] refactor: Split cv2 out of runtime requirements --- requirements/cv2-headless.txt | 7 +++++++ requirements/cv2.txt | 7 +++++++ requirements/runtime.txt | 17 ++++++++--------- 3 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 requirements/cv2-headless.txt create mode 100644 requirements/cv2.txt diff --git a/requirements/cv2-headless.txt b/requirements/cv2-headless.txt new file mode 100644 index 00000000..4a4caf18 --- /dev/null +++ b/requirements/cv2-headless.txt @@ -0,0 +1,7 @@ +# Generated dynamically via: ~/code/xcookie/xcookie/main.py::TemplateApplier._build_special_requirements +# xdev availpkg opencv-python-headless +# --prefer-binary +opencv-python-headless>=4.10.0.84 ; python_version < '4.0' and python_version >= '3.13' +opencv-python-headless>=4.5.5.64 ; python_version < '3.13' and python_version >= '3.11' +opencv-python-headless>=4.5.4.58 ; python_version < '3.11' and python_version >= '3.10' +opencv-python-headless>=3.4.15.55 ; python_version < '3.10' and python_version >= '3.9' diff --git a/requirements/cv2.txt b/requirements/cv2.txt new file mode 100644 index 00000000..157a97d5 --- /dev/null +++ b/requirements/cv2.txt @@ -0,0 +1,7 @@ +# Generated dynamically via: ~/code/xcookie/xcookie/main.py::TemplateApplier._build_special_requirements +# xdev availpkg opencv-python +# --prefer-binary +opencv-python>=4.10.0.84 ; python_version < '4.0' and python_version >= '3.13' +opencv-python>=4.5.5.64 ; python_version < '3.13' and python_version >= '3.11' +opencv-python>=4.5.4.58 ; python_version < '3.11' and python_version >= '3.10' +opencv-python>=3.4.15.55 ; python_version < '3.10' and python_version >= '3.9' diff --git a/requirements/runtime.txt b/requirements/runtime.txt index 24aa11dc..ced68cb5 100644 --- a/requirements/runtime.txt +++ b/requirements/runtime.txt @@ -1,10 +1,9 @@ -torch -torchvision -transformers -addict -yapf -timm -numpy -opencv-python +torch>=2.0.1 +torchvision>=0.15.2 +transformers>=4.33.2 +addict>=2.4.0 +yapf>=0.40.1 +timm>=0.9.7 +numpy>=1.26.0 supervision>=0.22.0 -pycocotools +pycocotools>=2.0.6 From 9c74f8fdb20a430ebb075423ac592bfc524b9b7f Mon Sep 17 00:00:00 2001 From: joncrall Date: Fri, 22 Nov 2024 12:15:11 -0500 Subject: [PATCH 3/4] refactor: readd base requirements.txt --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..9ca36da2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +-r requirements/runtime.txt +-r requirements/cv2.txt From f8ce2acf77c830be46300ce00c3a435e616dd663 Mon Sep 17 00:00:00 2001 From: joncrall Date: Fri, 22 Nov 2024 12:14:26 -0500 Subject: [PATCH 4/4] refactor: Update parse requirements for loose/strict dependencies --- setup.py | 95 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 31 deletions(-) diff --git a/setup.py b/setup.py index 275b6fc3..061fb47c 100644 --- a/setup.py +++ b/setup.py @@ -23,10 +23,10 @@ import glob import os import subprocess - -import subprocess +import re import sys + def install_torch(): try: import torch @@ -115,13 +115,16 @@ def get_extensions(): return ext_modules -def parse_requirements(fname="requirements.txt", with_version=True): - """Parse the package dependencies listed in a requirements file but strips +def parse_requirements(fname="requirements.txt", versions=False): + """ + Parse the package dependencies listed in a requirements file but strips specific versioning information. Args: fname (str): path to requirements file - with_version (bool, default=False): if True include version specs + versions (bool | str): + If true include version specs. + If strict, then pin to the minimum version. Returns: List[str]: list of requirements items @@ -129,65 +132,84 @@ def parse_requirements(fname="requirements.txt", with_version=True): CommandLine: python -c "import setup; print(setup.parse_requirements())" """ - import re - import sys - from os.path import exists - require_fpath = fname - def parse_line(line): - """Parse information from a line in a requirements text file.""" + def parse_line(line, dpath=""): + """ + Parse information from a line in a requirements text file + """ + # Remove inline comments + comment_pos = line.find(" #") + if comment_pos > -1: + line = line[:comment_pos] + if line.startswith("-r "): # Allow specifying requirements in other files - target = line.split(" ")[1] + target = os.path.join(dpath, line.split(" ")[1]) for info in parse_require_file(target): yield info else: + # See: https://www.python.org/dev/peps/pep-0508/ info = {"line": line} if line.startswith("-e "): info["package"] = line.split("#egg=")[1] - elif "@git+" in line: - info["package"] = line else: + if "--find-links" in line: + # setuptools does not seem to handle find links + line = line.split("--find-links")[0] + if ";" in line: + pkgpart, platpart = line.split(";") + # Handle platform specific dependencies + # setuptools.readthedocs.io/en/latest/setuptools.html + # #declaring-platform-specific-dependencies + plat_deps = platpart.strip() + info["platform_deps"] = plat_deps + else: + pkgpart = line + platpart = None + # Remove versioning from the package pat = "(" + "|".join([">=", "==", ">"]) + ")" - parts = re.split(pat, line, maxsplit=1) + parts = re.split(pat, pkgpart, maxsplit=1) parts = [p.strip() for p in parts] info["package"] = parts[0] if len(parts) > 1: op, rest = parts[1:] - if ";" in rest: - # Handle platform specific dependencies - # http://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-platform-specific-dependencies - version, platform_deps = map(str.strip, rest.split(";")) - info["platform_deps"] = platform_deps - else: - version = rest # NOQA + version = rest # NOQA info["version"] = (op, version) yield info def parse_require_file(fpath): + dpath = os.path.dirname(fpath) with open(fpath, "r") as f: for line in f.readlines(): line = line.strip() if line and not line.startswith("#"): - for info in parse_line(line): + for info in parse_line(line, dpath=dpath): yield info def gen_packages_items(): - if exists(require_fpath): + if os.path.exists(require_fpath): for info in parse_require_file(require_fpath): parts = [info["package"]] - if with_version and "version" in info: - parts.extend(info["version"]) + if versions and "version" in info: + if versions == "strict": + # In strict mode, we pin to the minimum version + if info["version"]: + # Only replace the first >= instance + verstr = "".join(info["version"]).replace(">=", "==", 1) + parts.append(verstr) + else: + parts.extend(info["version"]) if not sys.version.startswith("3.4"): # apparently package_deps are broken in 3.4 - platform_deps = info.get("platform_deps") - if platform_deps is not None: - parts.append(";" + platform_deps) + plat_deps = info.get("platform_deps") + if plat_deps is not None: + parts.append(";" + plat_deps) item = "".join(parts) - yield item + if item: + yield item packages = list(gen_packages_items()) return packages @@ -208,7 +230,18 @@ def gen_packages_items(): url="https://github.com/IDEA-Research/GroundingDINO", description="open-set object detector", license=license, - install_requires=parse_requirements("requirements.txt"), + # Note: does not include cv2 due to headless ambiguitiy. + install_requires=parse_requirements("requirements/runtime.txt", versions="loose"), + extras_require={ + "all": parse_requirements("runtime.txt", versions="loose"), + # Use can choose which type of cv2 to install + "cv2": parse_requirements("requirements/cv2.txt", versions="loose"), + "cv2-headless": parse_requirements("requirements/cv2-headless.txt", versions="loose"), + # Strict variant of requirements + "runtime-strict": parse_requirements("requirements/runtime.txt", versions="strict"), + "cv2-strict": parse_requirements("requirements/cv2.txt", versions="strict"), + "cv2-headless-strict": parse_requirements("requirements/cv2-headless.txt", versions="strict"), + }, packages=find_packages( exclude=( "configs",