From 5dfb72980edfa10b7af2fac41cd95d8be7e2ddc3 Mon Sep 17 00:00:00 2001 From: Hui Zhang Date: Fri, 6 May 2022 11:50:00 +0000 Subject: [PATCH] add commmit to version info --- setup.py | 97 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 31 deletions(-) diff --git a/setup.py b/setup.py index 912fdd6d..8cd687d8 100644 --- a/setup.py +++ b/setup.py @@ -28,6 +28,7 @@ from setuptools.command.install import install HERE = Path(os.path.abspath(os.path.dirname(__file__))) VERSION = '1.0.0a' +COMMITID = 'none' base = [ "editdistance", @@ -97,22 +98,31 @@ requirements = { } -def write_version_py(filename='paddlespeech/__init__.py'): - import paddlespeech - if hasattr(paddlespeech, - "__version__") and paddlespeech.__version__ == VERSION: - return - with open(filename, "a") as f: - f.write(f"\n__version__ = '{VERSION}'\n") +def check_call(cmd: str, shell=False, executable=None): + try: + sp.check_call( + cmd.split(), + shell=shell, + executable="/bin/bash" if shell else executable) + except sp.CalledProcessError as e: + print( + f"{__file__}:{inspect.currentframe().f_lineno}: CMD: {cmd}, Error:", + e.output, + file=sys.stderr) + raise e -def remove_version_py(filename='paddlespeech/__init__.py'): - with open(filename, "r") as f: - lines = f.readlines() - with open(filename, "w") as f: - for line in lines: - if "__version__" not in line: - f.write(line) +def check_output(cmd: str, shell=False): + try: + out_bytes = sp.check_output(cmd.split()) + except sp.CalledProcessError as e: + out_bytes = e.output # Output generated before error + code = e.returncode # Return code + print( + f"{__file__}:{inspect.currentframe().f_lineno}: CMD: {cmd}, Error:", + out_bytes, + file=sys.stderr) + return out_bytes.strip().decode('utf8') @contextlib.contextmanager @@ -132,24 +142,12 @@ def read(*names, **kwargs): return fp.read() -def check_call(cmd: str, shell=False, executable=None): - try: - sp.check_call( - cmd.split(), - shell=shell, - executable="/bin/bash" if shell else executable) - except sp.CalledProcessError as e: - print( - f"{__file__}:{inspect.currentframe().f_lineno}: CMD: {cmd}, Error:", - e.output, - file=sys.stderr) - raise e - - def _remove(files: str): for f in files: f.unlink() +################################# Install ################################## + def _post_install(install_lib_dir): # tools/make @@ -202,8 +200,45 @@ class UploadCommand(Command): sys.exit() -write_version_py() +################################# Version ################################## +def write_version_py(filename='paddlespeech/__init__.py'): + import paddlespeech + if hasattr(paddlespeech, + "__version__") and paddlespeech.__version__ == VERSION: + return + with open(filename, "a") as f: + out_str = f"\n__version__ = '{VERSION}'\n" + print(out_str) + f.write(f"\n__version__ = '{VERSION}'\n") + + COMMITID = check_output("git rev-parse HEAD") + with open(filename, 'a') as f: + out_str = f"\n__commit__ = '{COMMITID}'\n" + print(out_str) + f.write(f"\n__commit__ = '{COMMITID}'\n") + + print(f"{inspect.currentframe().f_code.co_name} done") + + +def remove_version_py(filename='paddlespeech/__init__.py'): + with open(filename, "r") as f: + lines = f.readlines() + with open(filename, "w") as f: + for line in lines: + if "__version__" in line or "__commit__" in line: + continue + f.write(line) + print(f"{inspect.currentframe().f_code.co_name} done") + + +@contextlib.contextmanager +def version_info(): + write_version_py() + yield + remove_version_py() + +################################# Steup ################################## setup_info = dict( # Metadata name='paddlespeech', @@ -273,6 +308,6 @@ setup_info = dict( ] }) -setup(**setup_info) -remove_version_py() +with version_info(): + setup(**setup_info)