|
| 1 | +import argparse |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | + |
| 6 | + |
| 7 | +def run_subprocess(command): |
| 8 | + try: |
| 9 | + process = subprocess.Popen(command, |
| 10 | + stdout=subprocess.PIPE, |
| 11 | + stderr=subprocess.PIPE, |
| 12 | + universal_newlines=True) |
| 13 | + |
| 14 | + # Read output and error in real-time |
| 15 | + for line in process.stdout: |
| 16 | + print(line.strip()) |
| 17 | + for line in process.stderr: |
| 18 | + print(line.strip()) |
| 19 | + |
| 20 | + # Wait for the subprocess to finish |
| 21 | + process.wait() |
| 22 | + |
| 23 | + # Get the return code |
| 24 | + return_code = process.returncode |
| 25 | + |
| 26 | + if return_code != 0: |
| 27 | + print(f'Command failed with return code {return_code}') |
| 28 | + |
| 29 | + except subprocess.CalledProcessError as e: |
| 30 | + print(f'Command failed with return code {e.returncode}') |
| 31 | + print('Error output:') |
| 32 | + print(e.output.decode()) |
| 33 | + |
| 34 | + |
| 35 | +def pytorch3d_links(): |
| 36 | + try: |
| 37 | + import torch |
| 38 | + except ImportError as e: |
| 39 | + print('Pytorch is not installed.') |
| 40 | + raise e |
| 41 | + cuda_version = torch.version.cuda |
| 42 | + if cuda_version is None: |
| 43 | + print('Pytorch is cpu only.') |
| 44 | + raise NotImplementedError |
| 45 | + |
| 46 | + pyt_version_str = torch.__version__.split('+')[0].replace('.', '') |
| 47 | + cuda_version_str = torch.version.cuda.replace('.', '') |
| 48 | + version_str = ''.join([ |
| 49 | + f'py3{sys.version_info.minor}_cu', cuda_version_str, |
| 50 | + f'_pyt{pyt_version_str}' |
| 51 | + ]) |
| 52 | + pytorch3d_links = f'https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/{version_str}/download.html' # noqa: E501 |
| 53 | + return pytorch3d_links |
| 54 | + |
| 55 | + |
| 56 | +def mmcv_links(): |
| 57 | + try: |
| 58 | + import torch |
| 59 | + except ImportError as e: |
| 60 | + print('Pytorch is not installed.') |
| 61 | + raise e |
| 62 | + cuda_version = torch.version.cuda |
| 63 | + if cuda_version is None: |
| 64 | + print('Pytorch is cpu only.') |
| 65 | + raise NotImplementedError |
| 66 | + |
| 67 | + cuda_version_str = torch.version.cuda.replace('.', '') |
| 68 | + pyt_version = torch.__version__.split('+')[0].split('.') |
| 69 | + pyt_version_mmcv = pyt_version[0] + '.' + pyt_version[1] |
| 70 | + mmcv_links = f'https://download.openmmlab.com/mmcv/dist/cu{cuda_version_str}/torch{pyt_version_mmcv}/index.html' # noqa: E501 |
| 71 | + return mmcv_links |
| 72 | + |
| 73 | + |
| 74 | +def install_package(line): |
| 75 | + pat = '(' + '|'.join(['>=', '==', '>', '<', '<=', '@']) + ')' |
| 76 | + parts = re.split(pat, line, maxsplit=1) |
| 77 | + package_name = parts[0].strip() |
| 78 | + print('installing', package_name) |
| 79 | + if package_name == 'pytorch3d': |
| 80 | + links = pytorch3d_links() |
| 81 | + run_subprocess( |
| 82 | + [sys.executable, '-m', 'pip', 'install', 'pytorch3d', '-f', links]) |
| 83 | + elif package_name == 'mmcv': |
| 84 | + links = mmcv_links() |
| 85 | + run_subprocess( |
| 86 | + [sys.executable, '-m', 'pip', 'install', line, '-f', links]) |
| 87 | + else: |
| 88 | + run_subprocess([sys.executable, '-m', 'pip', 'install', line]) |
| 89 | + |
| 90 | + |
| 91 | +def install_requires(fname): |
| 92 | + with open(fname, 'r') as f: |
| 93 | + for line in f.readlines(): |
| 94 | + line = line.strip() |
| 95 | + if line: |
| 96 | + install_package(line) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == '__main__': |
| 100 | + parser = argparse.ArgumentParser( |
| 101 | + description='Install Embodiedscan from pre-built package.') |
| 102 | + parser.add_argument('mode', default=None) |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + install_requires('requirements/base.txt') |
| 106 | + if args.mode == 'visual' or args.mode == 'all': |
| 107 | + install_requires('requirements/visual.txt') |
| 108 | + |
| 109 | + if args.mode == 'run' or args.mode == 'all': |
| 110 | + install_requires('requirements/run.txt') |
0 commit comments