Files
pytorch/tools/generate_torch_version.py
davidriazati@fb.com 4b96fc060b Remove distutils (#57040)
Summary:
[distutils](https://docs.python.org/3/library/distutils.html) is on its way out and will be deprecated-on-import for Python 3.10+ and removed in Python 3.12 (see [PEP 632](https://www.python.org/dev/peps/pep-0632/)). There's no reason for us to keep it around since all the functionality we want from it can be found in `setuptools` / `sysconfig`. `setuptools` includes a copy of most of `distutils` (which is fine to use according to the PEP), that it uses under the hood, so this PR also uses that in some places.

Fixes #56527
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57040

Pulled By: driazati

Reviewed By: nikithamalgifb

Differential Revision: D28051356

fbshipit-source-id: 1ca312219032540e755593e50da0c9e23c62d720
2021-04-29 12:10:11 -07:00

56 lines
2.2 KiB
Python

import argparse
import os
import subprocess
from pathlib import Path
from setuptools import distutils
from typing import Optional, Union
def get_sha(pytorch_root: Union[str, Path]) -> str:
try:
return subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=pytorch_root).decode('ascii').strip()
except Exception:
return 'Unknown'
def get_torch_version(sha: Optional[str] = None) -> str:
pytorch_root = Path(__file__).parent.parent
version = open('version.txt', 'r').read().strip()
if os.getenv('PYTORCH_BUILD_VERSION'):
assert os.getenv('PYTORCH_BUILD_NUMBER') is not None
build_number = int(os.getenv('PYTORCH_BUILD_NUMBER', ""))
version = os.getenv('PYTORCH_BUILD_VERSION', "")
if build_number > 1:
version += '.post' + str(build_number)
elif sha != 'Unknown':
if sha is None:
sha = get_sha(pytorch_root)
version += '+git' + sha[:7]
return version
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate torch/version.py from build and environment metadata.")
parser.add_argument("--is_debug", type=distutils.util.strtobool, help="Whether this build is debug mode or not.")
parser.add_argument("--cuda_version", type=str)
parser.add_argument("--hip_version", type=str)
args = parser.parse_args()
assert args.is_debug is not None
args.cuda_version = None if args.cuda_version == '' else args.cuda_version
args.hip_version = None if args.hip_version == '' else args.hip_version
pytorch_root = Path(__file__).parent.parent
version_path = pytorch_root / "torch" / "version.py"
sha = get_sha(pytorch_root)
version = get_torch_version(sha)
with open(version_path, 'w') as f:
f.write("__version__ = '{}'\n".format(version))
# NB: This is not 100% accurate, because you could have built the
# library code with DEBUG, but csrc without DEBUG (in which case
# this would claim to be a release build when it's not.)
f.write("debug = {}\n".format(repr(bool(args.is_debug))))
f.write("cuda = {}\n".format(repr(args.cuda_version)))
f.write("git_version = {}\n".format(repr(sha)))
f.write("hip = {}\n".format(repr(args.hip_version)))