diff --git a/tools/odrive/__init__.py b/tools/odrive/__init__.py index e69de29b..6eca987b 100644 --- a/tools/odrive/__init__.py +++ b/tools/odrive/__init__.py @@ -0,0 +1,5 @@ + +# Standard convention is to add a __version__ attribute to the package +from .version import get_version +__version__ = get_version() +del get_version diff --git a/tools/odrive/version.py b/tools/odrive/version.py new file mode 100644 index 00000000..327c915c --- /dev/null +++ b/tools/odrive/version.py @@ -0,0 +1,46 @@ + +import re +import subprocess +import os +import sys + +def get_version(git_only=False): + """ + Returns the versions of the tools + If git_only is true, the version.txt file is ignored even + if it is present. + """ + script_dir = os.path.dirname(os.path.realpath(__file__)) + + # Try to read the version.txt file that is generated during + # the packaging step + version_file_path = os.path.join(script_dir, 'version.txt') + if os.path.exists(version_file_path) and git_only == False: + with open(version_file_path) as version_file: + return version_file.readline().rstrip('\n') + + try: + # Determine the current git commit version + git_result = subprocess.run(["git", "describe", "--always", "--tags", "--dirty=*"], + cwd=script_dir, + stdout=subprocess.PIPE, timeout=10) + git_tag = git_result.stdout.decode(sys.stdout.encoding) + + regex=r'.*v([0-9a-zA-Z]).([0-9a-zA-Z]).([0-9a-zA-Z])(.*)' + package_version_major = int(re.sub(regex, r"\1", git_tag)) + package_version_minor = int(re.sub(regex, r"\2", git_tag)) + package_version_revision = int(re.sub(regex, r"\3", git_tag)) + package_version_unreleased = (re.sub(regex, r"\4", git_tag) != "") + + if package_version_unreleased: + package_version_revision += 1 + + # TODO: fetch from Git describe + version = '{}.{}.{}'.format(package_version_major, package_version_minor, package_version_revision) + + if package_version_unreleased: + version += ".dev" + except Exception as ex: + print(ex) + version = "whatever version in " + script_dir + return version diff --git a/tools/odrvtool b/tools/odrvtool index d8d31bda..8d67203e 100755 --- a/tools/odrvtool +++ b/tools/odrvtool @@ -57,6 +57,8 @@ parser.add_argument("-s", "--serial-number", action="store", "If omitted, any device is accepted.") parser.add_argument("-v", "--verbose", action="store_true", help="print debug information") +parser.add_argument("--version", action="store_true", + help="print version information and exit") parser.set_defaults(path="usb") args = parser.parse_args() @@ -82,7 +84,10 @@ logger.debug(str(args)) app_shutdown_token = Event() try: - if args.command == 'shell': + if args.version == True: + print("ODrive control utility v" + odrive.__version__) + + elif args.command == 'shell': import odrive.shell odrive.shell.launch_shell(args, logger, printer, app_shutdown_token)