#!/usr/bin/env python3 import os import shutil import subprocess import sys import tarfile from tempfile import TemporaryDirectory base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) def _get_lbug_version(): cmake_file = os.path.abspath(os.path.join(base_dir, "..", "CMakeLists.txt")) with open(cmake_file) as f: for line in f: if line.startswith("project(Lbug VERSION"): raw_version = line.split(" ")[2].strip() version_nums = raw_version.split(".") if len(version_nums) <= 3: return raw_version else: dev_suffix = version_nums[3] version = ".".join(version_nums[:3]) version += ".dev%s" % dev_suffix return version if __name__ == "__main__": if len(sys.argv) == 2: file_name = sys.argv[1] else: file_name = "ladybug-%s.tar.gz" % _get_lbug_version() print("Creating %s..." % file_name) with TemporaryDirectory() as tempdir: subprocess.check_call( [ "git", "archive", "--format", "tar", "-o", os.path.join(tempdir, "ladybug-source.tar"), "HEAD", ], cwd="../..", ) with tarfile.open(os.path.join(tempdir, "ladybug-source.tar")) as tar: tar.extractall( path=os.path.join(tempdir, "ladybug-source"), filter=None ) os.remove(os.path.join(tempdir, "ladybug-source.tar")) # git archive does not include submodule contents; copy them in explicitly. shutil.copytree( os.path.abspath(os.path.join(base_dir, "..", "tools/python_api")), os.path.join(tempdir, "ladybug-source/tools/python_api"), ignore=shutil.ignore_patterns("dataset"), dirs_exist_ok=True, ) # Remove components that are not needed for the pip package shutil.rmtree(os.path.join(tempdir, "ladybug-source/dataset")) shutil.rmtree(os.path.join(tempdir, "ladybug-source/examples")) shutil.rmtree(os.path.join(tempdir, "ladybug-source/benchmark")) shutil.rmtree(os.path.join(tempdir, "ladybug-source/logo")) shutil.rmtree(os.path.join(tempdir, "ladybug-source/extension")) shutil.rmtree(os.path.join(tempdir, "ladybug-source/test")) shutil.rmtree(os.path.join(tempdir, "ladybug-source/.github")) os.makedirs(os.path.join(tempdir, "ladybug")) for path in ["setup.py", "setup.cfg", "MANIFEST.in"]: shutil.copy2(path, os.path.join(tempdir, path)) shutil.copy2("../../LICENSE", os.path.join(tempdir, "LICENSE")) shutil.copy2("../../README.md", os.path.join(tempdir, "README.md")) shutil.copy2( "../../tools/python_api/pyproject.toml", os.path.join(tempdir, "pyproject.toml"), ) # The Python submodule now declares package metadata relative to the # project root, so stage the package sources into the sdist root. shutil.copytree( "../../tools/python_api/src_py", os.path.join(tempdir, "src_py"), dirs_exist_ok=True, ) shutil.copytree( "../../tools/python_api/test", os.path.join(tempdir, "test"), dirs_exist_ok=True, ) # Update the version in pyproject.toml with open(os.path.join(tempdir, "pyproject.toml"), "r") as f: lines = f.readlines() with open(os.path.join(tempdir, "pyproject.toml"), "w") as f: for line in lines: if line.startswith("version ="): f.write('version = "%s"\n' % _get_lbug_version()) else: f.write(line) shutil.copy2("README.md", os.path.join(tempdir, "README_PYTHON_BUILD.md")) subprocess.check_call([sys.executable, "setup.py", "egg_info"], cwd=tempdir) shutil.copy2( os.path.join(tempdir, "ladybug.egg-info", "PKG-INFO"), os.path.join(tempdir, "PKG-INFO"), ) with tarfile.open(file_name, "w:gz") as sdist: sdist.add(tempdir, "sdist")