From 855a7871159a448e6b99f3b3516410d4573824e3 Mon Sep 17 00:00:00 2001 From: voluntas Date: Tue, 23 Jan 2024 12:55:20 +0900 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E3=81=A6=20Ruff=20=E3=81=AB=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.md | 2 + pypath.py | 21 +- pyproject.toml | 4 +- run.py | 596 ++++++++++++++++++++++++++----------------------- setup.py | 34 +-- 5 files changed, 350 insertions(+), 307 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6df0ca28..7dc4d93b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,8 @@ ## develop +- [CHANGE] フォーマッターを Ruff に変更する + - @voluntas - [UPDATE] nanobind を 1.8.0 に上げる - @voluntas - [UPDATE] actions/setup-python@v5 に上げる diff --git a/pypath.py b/pypath.py index 8515c1a4..0f1eb988 100644 --- a/pypath.py +++ b/pypath.py @@ -61,7 +61,9 @@ def get_python_include_dir(python_version: str): # if Python.h not found (or python_include_dir is None), try to find a # suitable include dir - found_python_h = python_include_dir is not None and os.path.exists(os.path.join(python_include_dir, "Python.h")) + found_python_h = python_include_dir is not None and os.path.exists( + os.path.join(python_include_dir, "Python.h") + ) if not found_python_h: # NOTE(opadron): these possible prefixes must be guarded against @@ -92,7 +94,14 @@ def get_python_include_dir(python_version: str): if python_inc is not None: python_inc2 = os.path.join(python_inc, ".".join(map(str, sys.version_info[:2]))) - all_candidate_prefixes = [include_py, include_dir, include, plat_include, python_inc, python_inc2] + all_candidate_prefixes = [ + include_py, + include_dir, + include, + plat_include, + python_inc, + python_inc2, + ] candidate_prefixes: list[str] = [pre for pre in all_candidate_prefixes if pre] candidate_versions: tuple[str, ...] = (python_version,) @@ -163,7 +172,7 @@ def get_python_library(python_version: str): masd = sysconfig.get_config_var("multiarchsubdir") if masd: if masd.startswith(os.sep): - masd = masd[len(os.sep):] + masd = masd[len(os.sep) :] libdir_masd = os.path.join(libdir, masd) if os.path.exists(libdir_masd): libdir = libdir_masd @@ -218,7 +227,9 @@ def _guess_python_library(python_version: str): assert not isinstance(libdir_a, int) if libdir_a is None: libdest = sysconfig.get_config_var("LIBDEST") - candidate_libdirs.append(os.path.abspath(os.path.join(libdest, "..", "libs") if libdest else "libs")) + candidate_libdirs.append( + os.path.abspath(os.path.join(libdest, "..", "libs") if libdest else "libs") + ) libdir_b = sysconfig.get_config_var("LIBDIR") for libdir in (libdir_a, libdir_b): if libdir is None: @@ -227,7 +238,7 @@ def _guess_python_library(python_version: str): masd = sysconfig.get_config_var("multiarchsubdir") if masd: if masd.startswith(os.sep): - masd = masd[len(os.sep):] + masd = masd[len(os.sep) :] candidate_libdirs.append(os.path.join(libdir, masd)) candidate_libdirs.append(libdir) diff --git a/pyproject.toml b/pyproject.toml index 86f30c7d..63a6f6e2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,4 @@ dev-dependencies = [ [tool.ruff] line-length = 100 -indent-width = 4 - -exclude = [".venv", "run.py", "pypath.py", "setup.py"] \ No newline at end of file +indent-width = 4 \ No newline at end of file diff --git a/run.py b/run.py index 5ee21fad..218710de 100644 --- a/run.py +++ b/run.py @@ -9,34 +9,33 @@ import zipfile from typing import Callable, Dict, List, NamedTuple, Optional, Union -from pypath import (get_python_include_dir, get_python_library, - get_python_version) +from pypath import get_python_include_dir, get_python_library, get_python_version def mkdir_p(path: str): if os.path.exists(path): - print(f'mkdir -p {path} => already exists') + print(f"mkdir -p {path} => already exists") return os.makedirs(path, exist_ok=True) - print(f'mkdir -p {path} => directory created') + print(f"mkdir -p {path} => directory created") -if platform.system() == 'Windows': - PATH_SEPARATOR = ';' +if platform.system() == "Windows": + PATH_SEPARATOR = ";" else: - PATH_SEPARATOR = ':' + PATH_SEPARATOR = ":" def add_path(path: str, is_after=False): # logging.debug(f'add_path: {path}') - if 'PATH' not in os.environ: - os.environ['PATH'] = path + if "PATH" not in os.environ: + os.environ["PATH"] = path return if is_after: - os.environ['PATH'] = os.environ['PATH'] + PATH_SEPARATOR + path + os.environ["PATH"] = os.environ["PATH"] + PATH_SEPARATOR + path else: - os.environ['PATH'] = path + PATH_SEPARATOR + os.environ['PATH'] + os.environ["PATH"] = path + PATH_SEPARATOR + os.environ["PATH"] def read_version_file(path: str) -> Dict[str, str]: @@ -47,14 +46,14 @@ def read_version_file(path: str) -> Dict[str, str]: line = line.strip() # コメント行 - if line[:1] == '#': + if line[:1] == "#": continue # 空行 if len(line) == 0: continue - [a, b] = map(lambda x: x.strip(), line.split('=', 2)) + [a, b] = map(lambda x: x.strip(), line.split("=", 2)) versions[a] = b.strip('"') return versions @@ -62,10 +61,10 @@ def read_version_file(path: str) -> Dict[str, str]: def versioned(func): def wrapper(version, version_file, *args, **kwargs): - if 'ignore_version' in kwargs: - if kwargs.get('ignore_version'): + if "ignore_version" in kwargs: + if kwargs.get("ignore_version"): rm_rf(version_file) - del kwargs['ignore_version'] + del kwargs["ignore_version"] if os.path.exists(version_file): ver = open(version_file).read() @@ -74,7 +73,7 @@ def wrapper(version, version_file, *args, **kwargs): r = func(version=version, *args, **kwargs) - with open(version_file, 'w') as f: + with open(version_file, "w") as f: f.write(version) return r @@ -84,23 +83,23 @@ def wrapper(version, version_file, *args, **kwargs): def rm_rf(path: str): if not os.path.exists(path): - print(f'rm -rf {path} => path not found') + print(f"rm -rf {path} => path not found") return if os.path.isfile(path) or os.path.islink(path): os.remove(path) - print(f'rm -rf {path} => file removed') + print(f"rm -rf {path} => file removed") if os.path.isdir(path): shutil.rmtree(path) - print(f'rm -rf {path} => directory removed') + print(f"rm -rf {path} => directory removed") def cmd(args, **kwargs): - print(f'+{args} {kwargs}') - if 'check' not in kwargs: - kwargs['check'] = True - if 'resolve' in kwargs: - resolve = kwargs['resolve'] - del kwargs['resolve'] + print(f"+{args} {kwargs}") + if "check" not in kwargs: + kwargs["check"] = True + if "resolve" in kwargs: + resolve = kwargs["resolve"] + del kwargs["resolve"] else: resolve = True if resolve: @@ -110,7 +109,7 @@ def cmd(args, **kwargs): def download(url: str, output_dir: Optional[str] = None, filename: Optional[str] = None) -> str: if filename is None: - output_path = urllib.parse.urlparse(url).path.split('/')[-1] + output_path = urllib.parse.urlparse(url).path.split("/")[-1] else: output_path = filename @@ -121,7 +120,7 @@ def download(url: str, output_dir: Optional[str] = None, filename: Optional[str] return output_path try: - if shutil.which('curl') is not None: + if shutil.which("curl") is not None: cmd(["curl", "-fLo", output_path, url]) else: cmd(["wget", "-cO", output_path, url]) @@ -138,21 +137,23 @@ def download(url: str, output_dir: Optional[str] = None, filename: Optional[str] # # 単一のディレクトリに格納されている場合はそのディレクトリ名を返す。 # そうでない場合は None を返す。 -def _is_single_dir(infos: List[Union[zipfile.ZipInfo, tarfile.TarInfo]], - get_name: Callable[[Union[zipfile.ZipInfo, tarfile.TarInfo]], str], - is_dir: Callable[[Union[zipfile.ZipInfo, tarfile.TarInfo]], bool]) -> Optional[str]: +def _is_single_dir( + infos: List[Union[zipfile.ZipInfo, tarfile.TarInfo]], + get_name: Callable[[Union[zipfile.ZipInfo, tarfile.TarInfo]], str], + is_dir: Callable[[Union[zipfile.ZipInfo, tarfile.TarInfo]], bool], +) -> Optional[str]: # tarfile: ['path', 'path/to', 'path/to/file.txt'] # zipfile: ['path/', 'path/to/', 'path/to/file.txt'] # どちらも / 区切りだが、ディレクトリの場合、後ろに / が付くかどうかが違う dirname = None for info in infos: name = get_name(info) - n = name.rstrip('/').find('/') + n = name.rstrip("/").find("/") if n == -1: # ルートディレクトリにファイルが存在している if not is_dir(info): return None - dir = name.rstrip('/') + dir = name.rstrip("/") else: dir = name[0:n] # ルートディレクトリに2個以上のディレクトリが存在している @@ -174,7 +175,7 @@ def is_single_dir_zip(zip: zipfile.ZipFile) -> Optional[str]: # 解凍した上でファイル属性を付与する def _extractzip(z: zipfile.ZipFile, path: str): z.extractall(path) - if platform.system() == 'Windows': + if platform.system() == "Windows": return for info in z.infolist(): if info.is_dir(): @@ -183,7 +184,7 @@ def _extractzip(z: zipfile.ZipFile, path: str): mod = info.external_attr >> 16 if (mod & 0o120000) == 0o120000: # シンボリックリンク - with open(filepath, 'r') as f: + with open(filepath, "r") as f: src = f.read() os.remove(filepath) with cd(os.path.dirname(filepath)): @@ -219,7 +220,7 @@ def _extractzip(z: zipfile.ZipFile, path: str): def extract(file: str, output_dir: str, output_dirname: str, filetype: Optional[str] = None): path = os.path.join(output_dir, output_dirname) print(f"Extract {file} to {path}") - if filetype == 'gzip' or file.endswith('.tar.gz'): + if filetype == "gzip" or file.endswith(".tar.gz"): rm_rf(path) with tarfile.open(file) as t: dir = is_single_dir_tar(t) @@ -234,7 +235,7 @@ def extract(file: str, output_dir: str, output_dirname: str, filetype: Optional[ if path != path2: print(f"mv {path2} {path}") os.replace(path2, path) - elif filetype == 'zip' or file.endswith('.zip'): + elif filetype == "zip" or file.endswith(".zip"): rm_rf(path) with zipfile.ZipFile(file) as z: dir = is_single_dir_zip(z) @@ -252,7 +253,7 @@ def extract(file: str, output_dir: str, output_dirname: str, filetype: Optional[ print(f"mv {path2} {path}") os.replace(path2, path) else: - raise Exception('file should end with .tar.gz or .zip') + raise Exception("file should end with .tar.gz or .zip") @versioned @@ -261,10 +262,11 @@ def install_webrtc(version, source_dir, install_dir, platform: str): filename = f'webrtc.{platform}.{"zip" if win else "tar.gz"}' rm_rf(os.path.join(source_dir, filename)) archive = download( - f'https://github.com/shiguredo-webrtc-build/webrtc-build/releases/download/{version}/{filename}', - output_dir=source_dir) - rm_rf(os.path.join(install_dir, 'webrtc')) - extract(archive, output_dir=install_dir, output_dirname='webrtc') + f"https://github.com/shiguredo-webrtc-build/webrtc-build/releases/download/{version}/{filename}", + output_dir=source_dir, + ) + rm_rf(os.path.join(install_dir, "webrtc")) + extract(archive, output_dir=install_dir, output_dirname="webrtc") class WebrtcInfo(NamedTuple): @@ -275,34 +277,40 @@ class WebrtcInfo(NamedTuple): libcxx_dir: str -def get_webrtc_info(webrtcbuild: bool, source_dir: str, build_dir: str, install_dir: str) -> WebrtcInfo: - webrtc_source_dir = os.path.join(source_dir, 'webrtc') - webrtc_build_dir = os.path.join(build_dir, 'webrtc') - webrtc_install_dir = os.path.join(install_dir, 'webrtc') +def get_webrtc_info( + webrtcbuild: bool, source_dir: str, build_dir: str, install_dir: str +) -> WebrtcInfo: + webrtc_source_dir = os.path.join(source_dir, "webrtc") + webrtc_build_dir = os.path.join(build_dir, "webrtc") + webrtc_install_dir = os.path.join(install_dir, "webrtc") if webrtcbuild: return WebrtcInfo( - version_file=os.path.join(source_dir, 'webrtc-build', 'VERSION'), - webrtc_include_dir=os.path.join(webrtc_source_dir, 'src'), - webrtc_library_dir=os.path.join(webrtc_build_dir, 'obj') - if platform.system() == 'Windows' else webrtc_build_dir, clang_dir=os.path.join( - webrtc_source_dir, 'src', 'third_party', 'llvm-build', 'Release+Asserts'), - libcxx_dir=os.path.join(webrtc_source_dir, 'src', 'third_party', 'libc++', 'src'),) + version_file=os.path.join(source_dir, "webrtc-build", "VERSION"), + webrtc_include_dir=os.path.join(webrtc_source_dir, "src"), + webrtc_library_dir=os.path.join(webrtc_build_dir, "obj") + if platform.system() == "Windows" + else webrtc_build_dir, + clang_dir=os.path.join( + webrtc_source_dir, "src", "third_party", "llvm-build", "Release+Asserts" + ), + libcxx_dir=os.path.join(webrtc_source_dir, "src", "third_party", "libc++", "src"), + ) else: return WebrtcInfo( - version_file=os.path.join(webrtc_install_dir, 'VERSIONS'), - webrtc_include_dir=os.path.join(webrtc_install_dir, 'include'), - webrtc_library_dir=os.path.join(install_dir, 'webrtc', 'lib'), - clang_dir=os.path.join(install_dir, 'llvm', 'clang'), - libcxx_dir=os.path.join(install_dir, 'llvm', 'libcxx'), + version_file=os.path.join(webrtc_install_dir, "VERSIONS"), + webrtc_include_dir=os.path.join(webrtc_install_dir, "include"), + webrtc_library_dir=os.path.join(install_dir, "webrtc", "lib"), + clang_dir=os.path.join(install_dir, "llvm", "clang"), + libcxx_dir=os.path.join(install_dir, "llvm", "libcxx"), ) @versioned def install_rootfs(version, install_dir, conf): - rootfs_dir = os.path.join(install_dir, 'rootfs') + rootfs_dir = os.path.join(install_dir, "rootfs") rm_rf(rootfs_dir) - cmd(['multistrap', '--no-auth', '-a', 'arm64', '-d', rootfs_dir, '-f', conf]) + cmd(["multistrap", "--no-auth", "-a", "arm64", "-d", rootfs_dir, "-f", conf]) # 絶対パスのシンボリックリンクを相対パスに置き換えていく for dir, _, filenames in os.walk(rootfs_dir): for filename in filenames: @@ -327,8 +335,10 @@ def install_rootfs(version, install_dir, conf): os.symlink(relpath, linkpath) # なぜかシンボリックリンクが登録されていないので作っておく - link = os.path.join(rootfs_dir, 'usr', 'lib', 'aarch64-linux-gnu', 'tegra', 'libnvbuf_fdmap.so') - file = os.path.join(rootfs_dir, 'usr', 'lib', 'aarch64-linux-gnu', 'tegra', 'libnvbuf_fdmap.so.1.0.0') + link = os.path.join(rootfs_dir, "usr", "lib", "aarch64-linux-gnu", "tegra", "libnvbuf_fdmap.so") + file = os.path.join( + rootfs_dir, "usr", "lib", "aarch64-linux-gnu", "tegra", "libnvbuf_fdmap.so.1.0.0" + ) if os.path.exists(file) and not os.path.exists(link): os.symlink(os.path.basename(file), link) @@ -339,11 +349,11 @@ def __init__(self, cwd): def __enter__(self): self._old_cwd = os.getcwd() - print(f'pushd {self._old_cwd} --> {self._cwd}') + print(f"pushd {self._old_cwd} --> {self._cwd}") os.chdir(self._cwd) def __exit__(self, exctype, excvalue, trace): - print(f'popd {self._old_cwd} <-- {self._cwd}') + print(f"popd {self._old_cwd} <-- {self._cwd}") os.chdir(self._old_cwd) return False @@ -356,49 +366,65 @@ def git_clone_shallow(url, hash, dir): rm_rf(dir) mkdir_p(dir) with cd(dir): - cmd(['git', 'init']) - cmd(['git', 'remote', 'add', 'origin', url]) - cmd(['git', 'fetch', '--depth=1', 'origin', hash]) - cmd(['git', 'reset', '--hard', 'FETCH_HEAD']) + cmd(["git", "init"]) + cmd(["git", "remote", "add", "origin", url]) + cmd(["git", "fetch", "--depth=1", "origin", hash]) + cmd(["git", "reset", "--hard", "FETCH_HEAD"]) @versioned -def install_llvm(version, install_dir, - tools_url, tools_commit, - libcxx_url, libcxx_commit, - buildtools_url, buildtools_commit): - llvm_dir = os.path.join(install_dir, 'llvm') +def install_llvm( + version, + install_dir, + tools_url, + tools_commit, + libcxx_url, + libcxx_commit, + buildtools_url, + buildtools_commit, +): + llvm_dir = os.path.join(install_dir, "llvm") rm_rf(llvm_dir) mkdir_p(llvm_dir) with cd(llvm_dir): # tools の update.py を叩いて特定バージョンの clang バイナリを拾う - git_clone_shallow(tools_url, tools_commit, 'tools') - with cd('tools'): - cmd(['python3', - os.path.join('clang', 'scripts', 'update.py'), - '--output-dir', os.path.join(llvm_dir, 'clang')]) + git_clone_shallow(tools_url, tools_commit, "tools") + with cd("tools"): + cmd( + [ + "python3", + os.path.join("clang", "scripts", "update.py"), + "--output-dir", + os.path.join(llvm_dir, "clang"), + ] + ) # 特定バージョンの libcxx を利用する - git_clone_shallow(libcxx_url, libcxx_commit, 'libcxx') + git_clone_shallow(libcxx_url, libcxx_commit, "libcxx") # __config_site のために特定バージョンの buildtools を取得する - git_clone_shallow(buildtools_url, buildtools_commit, 'buildtools') - with cd('buildtools'): - cmd(['git', 'reset', '--hard', buildtools_commit]) - shutil.copyfile(os.path.join(llvm_dir, 'buildtools', 'third_party', 'libc++', '__config_site'), - os.path.join(llvm_dir, 'libcxx', 'include', '__config_site')) + git_clone_shallow(buildtools_url, buildtools_commit, "buildtools") + with cd("buildtools"): + cmd(["git", "reset", "--hard", buildtools_commit]) + shutil.copyfile( + os.path.join(llvm_dir, "buildtools", "third_party", "libc++", "__config_site"), + os.path.join(llvm_dir, "libcxx", "include", "__config_site"), + ) @versioned def install_boost(version, source_dir, install_dir, sora_version, platform: str): win = platform.startswith("windows_") - filename = f'boost-{version}_sora-cpp-sdk-{sora_version}_{platform}.{"zip" if win else "tar.gz"}' + filename = ( + f'boost-{version}_sora-cpp-sdk-{sora_version}_{platform}.{"zip" if win else "tar.gz"}' + ) rm_rf(os.path.join(source_dir, filename)) archive = download( - f'https://github.com/shiguredo/sora-cpp-sdk/releases/download/{sora_version}/{filename}', - output_dir=source_dir) - rm_rf(os.path.join(install_dir, 'boost')) - extract(archive, output_dir=install_dir, output_dirname='boost') + f"https://github.com/shiguredo/sora-cpp-sdk/releases/download/{sora_version}/{filename}", + output_dir=source_dir, + ) + rm_rf(os.path.join(install_dir, "boost")) + extract(archive, output_dir=install_dir, output_dirname="boost") @versioned @@ -407,10 +433,11 @@ def install_lyra(version, source_dir, install_dir, sora_version, platform: str): filename = f'lyra-{version}_sora-cpp-sdk-{sora_version}_{platform}.{"zip" if win else "tar.gz"}' rm_rf(os.path.join(source_dir, filename)) archive = download( - f'https://github.com/shiguredo/sora-cpp-sdk/releases/download/{sora_version}/{filename}', - output_dir=source_dir) - rm_rf(os.path.join(install_dir, 'lyra')) - extract(archive, output_dir=install_dir, output_dirname='lyra') + f"https://github.com/shiguredo/sora-cpp-sdk/releases/download/{sora_version}/{filename}", + output_dir=source_dir, + ) + rm_rf(os.path.join(install_dir, "lyra")) + extract(archive, output_dir=install_dir, output_dirname="lyra") @versioned @@ -419,28 +446,29 @@ def install_sora(version, source_dir, install_dir, platform: str): filename = f'sora-cpp-sdk-{version}_{platform}.{"zip" if win else "tar.gz"}' rm_rf(os.path.join(source_dir, filename)) archive = download( - f'https://github.com/shiguredo/sora-cpp-sdk/releases/download/{version}/{filename}', - output_dir=source_dir) - rm_rf(os.path.join(install_dir, 'sora')) - extract(archive, output_dir=install_dir, output_dirname='sora') + f"https://github.com/shiguredo/sora-cpp-sdk/releases/download/{version}/{filename}", + output_dir=source_dir, + ) + rm_rf(os.path.join(install_dir, "sora")) + extract(archive, output_dir=install_dir, output_dirname="sora") @versioned def install_cmake(version, source_dir, install_dir, platform: str, ext): - url = f'https://github.com/Kitware/CMake/releases/download/v{version}/cmake-{version}-{platform}.{ext}' + url = f"https://github.com/Kitware/CMake/releases/download/v{version}/cmake-{version}-{platform}.{ext}" path = download(url, source_dir) - extract(path, install_dir, 'cmake') + extract(path, install_dir, "cmake") @versioned def install_openh264(version, source_dir, install_dir): - rm_rf(os.path.join(source_dir, 'openh264')) - rm_rf(os.path.join(install_dir, 'openh264')) - git_clone_shallow('https://github.com/cisco/openh264.git', - version, os.path.join(source_dir, 'openh264')) - with cd(os.path.join(source_dir, 'openh264')): - cmd([ - 'make', f'PREFIX={os.path.join(install_dir, "openh264")}', 'install-headers']) + rm_rf(os.path.join(source_dir, "openh264")) + rm_rf(os.path.join(install_dir, "openh264")) + git_clone_shallow( + "https://github.com/cisco/openh264.git", version, os.path.join(source_dir, "openh264") + ) + with cd(os.path.join(source_dir, "openh264")): + cmd(["make", f'PREFIX={os.path.join(install_dir, "openh264")}', "install-headers"]) class PlatformTarget(object): @@ -451,46 +479,46 @@ def __init__(self, os, osver, arch): @property def package_name(self): - if self.os == 'windows': - return f'windows_{self.arch}' - if self.os == 'macos': - return f'macos_{self.arch}' - if self.os == 'ubuntu': - return f'ubuntu-{self.osver}_{self.arch}' - if self.os == 'raspberry-pi-os': - return f'raspberry-pi-os_{self.arch}' - if self.os == 'jetson': - return 'ubuntu-20.04_armv8_jetson' - raise Exception('error') + if self.os == "windows": + return f"windows_{self.arch}" + if self.os == "macos": + return f"macos_{self.arch}" + if self.os == "ubuntu": + return f"ubuntu-{self.osver}_{self.arch}" + if self.os == "raspberry-pi-os": + return f"raspberry-pi-os_{self.arch}" + if self.os == "jetson": + return "ubuntu-20.04_armv8_jetson" + raise Exception("error") def get_build_platform() -> PlatformTarget: os = platform.system() - if os == 'Windows': - os = 'windows' + if os == "Windows": + os = "windows" osver = None - elif os == 'Darwin': - os = 'macos' + elif os == "Darwin": + os = "macos" osver = None - elif os == 'Linux': - release = read_version_file('/etc/os-release') - os = release['NAME'] - if os == 'Ubuntu': - os = 'ubuntu' - osver = release['VERSION_ID'] + elif os == "Linux": + release = read_version_file("/etc/os-release") + os = release["NAME"] + if os == "Ubuntu": + os = "ubuntu" + osver = release["VERSION_ID"] else: - raise Exception(f'OS {os} not supported') + raise Exception(f"OS {os} not supported") pass else: - raise Exception(f'OS {os} not supported') + raise Exception(f"OS {os} not supported") arch = platform.machine() - if arch in ('AMD64', 'x86_64'): - arch = 'x86_64' - elif arch in ('aarch64', 'arm64'): - arch = 'arm64' + if arch in ("AMD64", "x86_64"): + arch = "x86_64" + elif arch in ("aarch64", "arm64"): + arch = "arm64" else: - raise Exception(f'Arch {arch} not supported') + raise Exception(f"Arch {arch} not supported") return PlatformTarget(os, osver, arch) @@ -498,178 +526,183 @@ def get_build_platform() -> PlatformTarget: BASE_DIR = os.path.abspath(os.path.dirname(__file__)) -def install_deps(build_platform: PlatformTarget, target_platform: PlatformTarget, source_dir, build_dir, install_dir): - version = read_version_file('VERSION') +def install_deps( + build_platform: PlatformTarget, + target_platform: PlatformTarget, + source_dir, + build_dir, + install_dir, +): + version = read_version_file("VERSION") # multistrap を使った sysroot の構築 - if target_platform.os == 'jetson': - conf = os.path.join('multistrap', f'{target_platform.package_name}.conf') + if target_platform.os == "jetson": + conf = os.path.join("multistrap", f"{target_platform.package_name}.conf") # conf ファイルのハッシュ値をバージョンとする - version_md5 = hashlib.md5(open(conf, 'rb').read()).hexdigest() + version_md5 = hashlib.md5(open(conf, "rb").read()).hexdigest() install_rootfs_args = { - 'version': version_md5, - 'version_file': os.path.join(install_dir, 'rootfs.version'), - 'install_dir': install_dir, - 'conf': conf, + "version": version_md5, + "version_file": os.path.join(install_dir, "rootfs.version"), + "install_dir": install_dir, + "conf": conf, } install_rootfs(**install_rootfs_args) # WebRTC - if target_platform.os == 'windows': - webrtc_platform = f'windows_{target_platform.arch}' - elif target_platform.os == 'macos': - webrtc_platform = f'macos_{target_platform.arch}' - elif target_platform.os == 'ios': - webrtc_platform = 'ios' - elif target_platform.os == 'android': - webrtc_platform = 'android' - elif target_platform.os == 'ubuntu': - webrtc_platform = f'ubuntu-{target_platform.osver}_{target_platform.arch}' - elif target_platform.os == 'raspberry-pi-os': - webrtc_platform = f'raspberry-pi-os_{target_platform.arch}' - elif target_platform.os == 'jetson': - webrtc_platform = 'ubuntu-20.04_armv8' + if target_platform.os == "windows": + webrtc_platform = f"windows_{target_platform.arch}" + elif target_platform.os == "macos": + webrtc_platform = f"macos_{target_platform.arch}" + elif target_platform.os == "ios": + webrtc_platform = "ios" + elif target_platform.os == "android": + webrtc_platform = "android" + elif target_platform.os == "ubuntu": + webrtc_platform = f"ubuntu-{target_platform.osver}_{target_platform.arch}" + elif target_platform.os == "raspberry-pi-os": + webrtc_platform = f"raspberry-pi-os_{target_platform.arch}" + elif target_platform.os == "jetson": + webrtc_platform = "ubuntu-20.04_armv8" else: - raise Exception(f'Unknown platform {target_platform.os}') + raise Exception(f"Unknown platform {target_platform.os}") install_webrtc_args = { - 'version': version['WEBRTC_BUILD_VERSION'], - 'version_file': os.path.join(install_dir, 'webrtc.version'), - 'source_dir': source_dir, - 'install_dir': install_dir, - 'platform': webrtc_platform, + "version": version["WEBRTC_BUILD_VERSION"], + "version_file": os.path.join(install_dir, "webrtc.version"), + "source_dir": source_dir, + "install_dir": install_dir, + "platform": webrtc_platform, } install_webrtc(**install_webrtc_args) webrtc_info = get_webrtc_info(False, source_dir, build_dir, install_dir) webrtc_version = read_version_file(webrtc_info.version_file) - if build_platform.os == 'ubuntu': + if build_platform.os == "ubuntu": # LLVM - tools_url = webrtc_version['WEBRTC_SRC_TOOLS_URL'] - tools_commit = webrtc_version['WEBRTC_SRC_TOOLS_COMMIT'] - libcxx_url = webrtc_version['WEBRTC_SRC_THIRD_PARTY_LIBCXX_SRC_URL'] - libcxx_commit = webrtc_version['WEBRTC_SRC_THIRD_PARTY_LIBCXX_SRC_COMMIT'] - buildtools_url = webrtc_version['WEBRTC_SRC_BUILDTOOLS_URL'] - buildtools_commit = webrtc_version['WEBRTC_SRC_BUILDTOOLS_COMMIT'] + tools_url = webrtc_version["WEBRTC_SRC_TOOLS_URL"] + tools_commit = webrtc_version["WEBRTC_SRC_TOOLS_COMMIT"] + libcxx_url = webrtc_version["WEBRTC_SRC_THIRD_PARTY_LIBCXX_SRC_URL"] + libcxx_commit = webrtc_version["WEBRTC_SRC_THIRD_PARTY_LIBCXX_SRC_COMMIT"] + buildtools_url = webrtc_version["WEBRTC_SRC_BUILDTOOLS_URL"] + buildtools_commit = webrtc_version["WEBRTC_SRC_BUILDTOOLS_COMMIT"] install_llvm_args = { - 'version': - f'{tools_url}.{tools_commit}.' - f'{libcxx_url}.{libcxx_commit}.' - f'{buildtools_url}.{buildtools_commit}', - 'version_file': os.path.join(install_dir, 'llvm.version'), - 'install_dir': install_dir, - 'tools_url': tools_url, - 'tools_commit': tools_commit, - 'libcxx_url': libcxx_url, - 'libcxx_commit': libcxx_commit, - 'buildtools_url': buildtools_url, - 'buildtools_commit': buildtools_commit, + "version": f"{tools_url}.{tools_commit}." + f"{libcxx_url}.{libcxx_commit}." + f"{buildtools_url}.{buildtools_commit}", + "version_file": os.path.join(install_dir, "llvm.version"), + "install_dir": install_dir, + "tools_url": tools_url, + "tools_commit": tools_commit, + "libcxx_url": libcxx_url, + "libcxx_commit": libcxx_commit, + "buildtools_url": buildtools_url, + "buildtools_commit": buildtools_commit, } install_llvm(**install_llvm_args) # Boost install_boost_args = { - 'version': version['BOOST_VERSION'], - 'version_file': os.path.join(install_dir, 'boost.version'), - 'source_dir': source_dir, - 'install_dir': install_dir, - 'sora_version': version['SORA_CPP_SDK_VERSION'], - 'platform': target_platform.package_name, + "version": version["BOOST_VERSION"], + "version_file": os.path.join(install_dir, "boost.version"), + "source_dir": source_dir, + "install_dir": install_dir, + "sora_version": version["SORA_CPP_SDK_VERSION"], + "platform": target_platform.package_name, } install_boost(**install_boost_args) # Lyra install_lyra_args = { - 'version': version['LYRA_VERSION'], - 'version_file': os.path.join(install_dir, 'lyra.version'), - 'source_dir': source_dir, - 'install_dir': install_dir, - 'sora_version': version['SORA_CPP_SDK_VERSION'], - 'platform': target_platform.package_name, + "version": version["LYRA_VERSION"], + "version_file": os.path.join(install_dir, "lyra.version"), + "source_dir": source_dir, + "install_dir": install_dir, + "sora_version": version["SORA_CPP_SDK_VERSION"], + "platform": target_platform.package_name, } install_lyra(**install_lyra_args) # Sora C++ SDK install_sora_args = { - 'version': version['SORA_CPP_SDK_VERSION'], - 'version_file': os.path.join(install_dir, 'sora.version'), - 'source_dir': source_dir, - 'install_dir': install_dir, - 'platform': target_platform.package_name, + "version": version["SORA_CPP_SDK_VERSION"], + "version_file": os.path.join(install_dir, "sora.version"), + "source_dir": source_dir, + "install_dir": install_dir, + "platform": target_platform.package_name, } install_sora(**install_sora_args) # CMake install_cmake_args = { - 'version': version['CMAKE_VERSION'], - 'version_file': os.path.join(install_dir, 'cmake.version'), - 'source_dir': source_dir, - 'install_dir': install_dir, - 'platform': '', - 'ext': 'tar.gz' + "version": version["CMAKE_VERSION"], + "version_file": os.path.join(install_dir, "cmake.version"), + "source_dir": source_dir, + "install_dir": install_dir, + "platform": "", + "ext": "tar.gz", } - if build_platform.os == 'windows' and build_platform.arch == 'x86_64': - install_cmake_args['platform'] = 'windows-x86_64' - install_cmake_args['ext'] = 'zip' - elif build_platform.os == 'macos': - install_cmake_args['platform'] = 'macos-universal' - elif build_platform.os == 'ubuntu' and build_platform.arch == 'x86_64': - install_cmake_args['platform'] = 'linux-x86_64' - elif build_platform.os == 'ubuntu' and build_platform.arch == 'arm64': - install_cmake_args['platform'] = 'linux-aarch64' + if build_platform.os == "windows" and build_platform.arch == "x86_64": + install_cmake_args["platform"] = "windows-x86_64" + install_cmake_args["ext"] = "zip" + elif build_platform.os == "macos": + install_cmake_args["platform"] = "macos-universal" + elif build_platform.os == "ubuntu" and build_platform.arch == "x86_64": + install_cmake_args["platform"] = "linux-x86_64" + elif build_platform.os == "ubuntu" and build_platform.arch == "arm64": + install_cmake_args["platform"] = "linux-aarch64" else: - raise Exception('Failed to install CMake') + raise Exception("Failed to install CMake") install_cmake(**install_cmake_args) - if build_platform.os == 'macos': - add_path(os.path.join(install_dir, 'cmake', 'CMake.app', 'Contents', 'bin')) + if build_platform.os == "macos": + add_path(os.path.join(install_dir, "cmake", "CMake.app", "Contents", "bin")) else: - add_path(os.path.join(install_dir, 'cmake', 'bin')) + add_path(os.path.join(install_dir, "cmake", "bin")) - if build_platform.os != 'windows': + if build_platform.os != "windows": # OpenH264 install_openh264_args = { - 'version': version['OPENH264_VERSION'], - 'version_file': os.path.join(install_dir, 'openh264.version'), - 'source_dir': source_dir, - 'install_dir': install_dir, + "version": version["OPENH264_VERSION"], + "version_file": os.path.join(install_dir, "openh264.version"), + "source_dir": source_dir, + "install_dir": install_dir, } install_openh264(**install_openh264_args) def cmake_path(path: str) -> str: - return path.replace('\\', '/') + return path.replace("\\", "/") # 標準出力をキャプチャするコマンド実行。シェルの `cmd ...` や $(cmd ...) と同じ def cmdcap(args, **kwargs): # 3.7 でしか使えない # kwargs['capture_output'] = True - kwargs['stdout'] = subprocess.PIPE - kwargs['stderr'] = subprocess.PIPE - kwargs['encoding'] = 'utf-8' + kwargs["stdout"] = subprocess.PIPE + kwargs["stderr"] = subprocess.PIPE + kwargs["encoding"] = "utf-8" return cmd(args, **kwargs).stdout.strip() def main(): build_platform = get_build_platform() - target = os.getenv('SORA_SDK_TARGET') + target = os.getenv("SORA_SDK_TARGET") if target is None: target_platform = build_platform - elif target == 'ubuntu-20.04_armv8_jetson': - target_platform = PlatformTarget('jetson', None, 'armv8') + elif target == "ubuntu-20.04_armv8_jetson": + target_platform = PlatformTarget("jetson", None, "armv8") else: - raise Exception(f'Unknown target {target}') + raise Exception(f"Unknown target {target}") - if target_platform.os == 'macos': + if target_platform.os == "macos": # macOS のターゲットは必ず arm64 として扱う - target_platform = PlatformTarget(build_platform.os, build_platform.osver, 'arm64') + target_platform = PlatformTarget(build_platform.os, build_platform.osver, "arm64") - source_dir = os.path.join(BASE_DIR, '_source', target_platform.package_name) - build_dir = os.path.join(BASE_DIR, '_build', target_platform.package_name) - install_dir = os.path.join(BASE_DIR, '_install', target_platform.package_name) + source_dir = os.path.join(BASE_DIR, "_source", target_platform.package_name) + build_dir = os.path.join(BASE_DIR, "_build", target_platform.package_name) + install_dir = os.path.join(BASE_DIR, "_install", target_platform.package_name) mkdir_p(source_dir) mkdir_p(build_dir) mkdir_p(install_dir) @@ -677,32 +710,26 @@ def main(): with cd(BASE_DIR): install_deps(build_platform, target_platform, source_dir, build_dir, install_dir) - configuration = 'Release' + configuration = "Release" webrtc_info = get_webrtc_info(False, source_dir, build_dir, install_dir) cmake_args = [] - cmake_args.append(f'-DCMAKE_BUILD_TYPE={configuration}') - cmake_args.append(f'-DTARGET_OS={target_platform.os}') - cmake_args.append( - f"-DBOOST_ROOT={cmake_path(os.path.join(install_dir, 'boost'))}") - cmake_args.append( - f"-DLYRA_DIR={cmake_path(os.path.join(install_dir, 'lyra'))}") - cmake_args.append( - f"-DWEBRTC_INCLUDE_DIR={cmake_path(webrtc_info.webrtc_include_dir)}") - cmake_args.append( - f"-DWEBRTC_LIBRARY_DIR={cmake_path(webrtc_info.webrtc_library_dir)}") - cmake_args.append( - f"-DSORA_DIR={cmake_path(os.path.join(install_dir, 'sora'))}") - cmake_args.append( - f"-DOPENH264_DIR={cmake_path(os.path.join(install_dir, 'openh264'))}") + cmake_args.append(f"-DCMAKE_BUILD_TYPE={configuration}") + cmake_args.append(f"-DTARGET_OS={target_platform.os}") + cmake_args.append(f"-DBOOST_ROOT={cmake_path(os.path.join(install_dir, 'boost'))}") + cmake_args.append(f"-DLYRA_DIR={cmake_path(os.path.join(install_dir, 'lyra'))}") + cmake_args.append(f"-DWEBRTC_INCLUDE_DIR={cmake_path(webrtc_info.webrtc_include_dir)}") + cmake_args.append(f"-DWEBRTC_LIBRARY_DIR={cmake_path(webrtc_info.webrtc_library_dir)}") + cmake_args.append(f"-DSORA_DIR={cmake_path(os.path.join(install_dir, 'sora'))}") + cmake_args.append(f"-DOPENH264_DIR={cmake_path(os.path.join(install_dir, 'openh264'))}") python_version = get_python_version() cmake_args.append(f"-DPYTHON_VERSION_STRING={python_version}") cmake_args.append(f"-DPYTHON_INCLUDE_DIR={get_python_include_dir(python_version)}") cmake_args.append(f"-DPYTHON_EXECUTABLE={cmake_path(sys.executable)}") cmake_args.append(f"-DPYTHON_LIBRARY={cmake_path(get_python_library(python_version))}") - if target_platform.os == 'ubuntu': + if target_platform.os == "ubuntu": # クロスコンパイルの設定。 # 本来は toolchain ファイルに書く内容 cmake_args += [ @@ -710,8 +737,8 @@ def main(): f"-DCMAKE_CXX_COMPILER={os.path.join(webrtc_info.clang_dir, 'bin', 'clang++')}", f"-DLIBCXX_INCLUDE_DIR={cmake_path(os.path.join(webrtc_info.libcxx_dir, 'include'))}", ] - elif target_platform.os == 'macos': - sysroot = cmdcap(['xcrun', '--sdk', 'macosx', '--show-sdk-path']) + elif target_platform.os == "macos": + sysroot = cmdcap(["xcrun", "--sdk", "macosx", "--show-sdk-path"]) cmake_args += [ "-DCMAKE_SYSTEM_PROCESSOR=arm64", "-DCMAKE_OSX_ARCHITECTURES=arm64", @@ -721,53 +748,58 @@ def main(): "-DCMAKE_CXX_COMPILER_TARGET=aarch64-apple-darwin", f"-DCMAKE_SYSROOT={sysroot}", ] - elif target_platform.os == 'jetson': - sysroot = os.path.join(install_dir, 'rootfs') + elif target_platform.os == "jetson": + sysroot = os.path.join(install_dir, "rootfs") cmake_args += [ - '-DCMAKE_SYSTEM_NAME=Linux', - '-DCMAKE_SYSTEM_PROCESSOR=aarch64', + "-DCMAKE_SYSTEM_NAME=Linux", + "-DCMAKE_SYSTEM_PROCESSOR=aarch64", f"-DCMAKE_C_COMPILER={os.path.join(webrtc_info.clang_dir, 'bin', 'clang')}", - '-DCMAKE_C_COMPILER_TARGET=aarch64-linux-gnu', + "-DCMAKE_C_COMPILER_TARGET=aarch64-linux-gnu", f"-DCMAKE_CXX_COMPILER={os.path.join(webrtc_info.clang_dir, 'bin', 'clang++')}", - '-DCMAKE_CXX_COMPILER_TARGET=aarch64-linux-gnu', - f'-DCMAKE_FIND_ROOT_PATH={sysroot}', - '-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER', - '-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=BOTH', - '-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH', - '-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH', - f'-DCMAKE_SYSROOT={sysroot}', + "-DCMAKE_CXX_COMPILER_TARGET=aarch64-linux-gnu", + f"-DCMAKE_FIND_ROOT_PATH={sysroot}", + "-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER", + "-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=BOTH", + "-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH", + "-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH", + f"-DCMAKE_SYSROOT={sysroot}", f"-DLIBCXX_INCLUDE_DIR={cmake_path(os.path.join(webrtc_info.libcxx_dir, 'include'))}", f"-DPython_ROOT_DIR={cmake_path(os.path.join(sysroot, 'usr', 'include', 'python3.8'))}", "-DNB_SUFFIX=.cpython-38-aarch64-linux-gnu.so", ] - sora_src_dir = os.path.join('src', 'sora_sdk') - sora_build_dir = os.path.join(build_dir, 'sora_sdk') - if target_platform.os == 'windows': - sora_build_target_dir = os.path.join(build_dir, 'sora_sdk', configuration) + sora_src_dir = os.path.join("src", "sora_sdk") + sora_build_dir = os.path.join(build_dir, "sora_sdk") + if target_platform.os == "windows": + sora_build_target_dir = os.path.join(build_dir, "sora_sdk", configuration) else: - sora_build_target_dir = os.path.join(build_dir, 'sora_sdk') + sora_build_target_dir = os.path.join(build_dir, "sora_sdk") mkdir_p(sora_build_dir) with cd(sora_build_dir): - cmd(['cmake', BASE_DIR, *cmake_args]) - cmd(['cmake', '--build', '.', '--config', configuration]) + cmd(["cmake", BASE_DIR, *cmake_args]) + cmd(["cmake", "--build", ".", "--config", configuration]) for file in os.listdir(sora_src_dir): - if file.startswith('sora_sdk_ext.') and ( - file.endswith('.so') or file.endswith('.dylib') or file.endswith('.pyd')): + if file.startswith("sora_sdk_ext.") and ( + file.endswith(".so") or file.endswith(".dylib") or file.endswith(".pyd") + ): os.remove(os.path.join(sora_src_dir, file)) for file in os.listdir(sora_build_target_dir): - if file.startswith('sora_sdk_ext.') and ( - file.endswith('.so') or file.endswith('.dylib') or file.endswith('.pyd')): - shutil.copyfile(os.path.join(sora_build_target_dir, file), - os.path.join(sora_src_dir, file)) + if file.startswith("sora_sdk_ext.") and ( + file.endswith(".so") or file.endswith(".dylib") or file.endswith(".pyd") + ): + shutil.copyfile( + os.path.join(sora_build_target_dir, file), os.path.join(sora_src_dir, file) + ) - for file in os.listdir(os.path.join(install_dir, 'lyra', 'share', 'model_coeffs')): - shutil.copyfile(os.path.join(install_dir, 'lyra', 'share', 'model_coeffs', file), - os.path.join(sora_src_dir, 'model_coeffs', file)) + for file in os.listdir(os.path.join(install_dir, "lyra", "share", "model_coeffs")): + shutil.copyfile( + os.path.join(install_dir, "lyra", "share", "model_coeffs", file), + os.path.join(sora_src_dir, "model_coeffs", file), + ) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/setup.py b/setup.py index 6945d319..6fa3ee1a 100644 --- a/setup.py +++ b/setup.py @@ -14,13 +14,13 @@ def run_setup(build_platform, target_platform): plat = None - if target_platform.os == 'jetson': - plat = 'manylinux_2_17_aarch64.manylinux2014_aarch64' - elif target_platform.os == 'ubuntu' and target_platform.arch == 'x86_64': - if target_platform.osver == '20.04': - plat = 'manylinux_2_17_x86_64.manylinux2014_x86_64' - if target_platform.osver == '22.04': - plat = 'manylinux_2_17_x86_64.manylinux2014_x86_64' + if target_platform.os == "jetson": + plat = "manylinux_2_17_aarch64.manylinux2014_aarch64" + elif target_platform.os == "ubuntu" and target_platform.arch == "x86_64": + if target_platform.osver == "20.04": + plat = "manylinux_2_17_x86_64.manylinux2014_x86_64" + if target_platform.osver == "22.04": + plat = "manylinux_2_17_x86_64.manylinux2014_x86_64" class bdist_wheel(_bdist_wheel): def finalize_options(self): @@ -29,19 +29,19 @@ def finalize_options(self): def get_tag(self): _, _, plat2 = super().get_tag() - impl = 'cp' + sysconfig.get_config_var("py_version_nodot") + impl = "cp" + sysconfig.get_config_var("py_version_nodot") return impl, impl, plat if plat is not None else plat2 setup( url="https://github.com/shiguredo/sora-python-sdk", - packages=['sora_sdk', 'sora_sdk.model_coeffs'], - package_dir={'': 'src'}, + packages=["sora_sdk", "sora_sdk.model_coeffs"], + package_dir={"": "src"}, package_data={ - 'sora_sdk': ['sora_sdk_ext.*', 'model_coeffs/*'], + "sora_sdk": ["sora_sdk_ext.*", "model_coeffs/*"], }, include_package_data=True, cmdclass={ - 'bdist_wheel': bdist_wheel, + "bdist_wheel": bdist_wheel, }, ) @@ -49,17 +49,17 @@ def get_tag(self): def main(): build_platform = get_build_platform() - target = os.getenv('SORA_SDK_TARGET') + target = os.getenv("SORA_SDK_TARGET") if target is None: target_platform = build_platform - elif target == 'ubuntu-20.04_armv8_jetson': - target_platform = PlatformTarget('jetson', None, 'armv8') + elif target == "ubuntu-20.04_armv8_jetson": + target_platform = PlatformTarget("jetson", None, "armv8") else: - raise Exception(f'Unknown target {target}') + raise Exception(f"Unknown target {target}") with cd(BASE_DIR): run_setup(build_platform, target_platform) -if __name__ == '__main__': +if __name__ == "__main__": main()