add get_version to python library

This commit is contained in:
Samuel Sadok
2018-03-30 15:28:00 -07:00
parent 9fb5fceea7
commit 34a95f971b
3 changed files with 57 additions and 1 deletions
+5
View File
@@ -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
+46
View File
@@ -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
+6 -1
View File
@@ -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)