Skip to content

Commit dd0d3c8

Browse files
authored
in util.version.py::get_project_path(), consider calling modules (#109)
When derived modules (i.e. viral-classify) are symlinked into viral-core and their versions are read from a VERSION file, only the path to the viral-core version file was used. This changes that behavior, so outer members of the call stack are checked for the presence of VERSION files as well (and if found, the version is read from the first/outermost one found). (i.e. when calling ./taxon-filter.py --version, with these changes, the version of viral-classify is correctly returned rather than the version of viral-core) Since this relies on the presence of a VERSION file to find the current "project path", this will fall back to the old behavior if viral-core has a VERSION file and a derived module lacks such a file. This does not change the behavior where a version file will not be created for a derived module when calling `./symlinked_derived_module.py --version`
1 parent d3e988a commit dd0d3c8

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

util/version.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,57 @@
55
__author__ = "dpark@broadinstitute.org"
66
__version__ = None
77

8+
# import built-in
89
import subprocess
910
import os
1011
import re
1112
import time, datetime
1213
import os.path
14+
import inspect
15+
16+
# module-level imports
1317
import util.misc
1418

1519

16-
def get_project_path():
17-
'''Return the absolute path of the top-level project, assumed to be the
18-
parent of the directory containing this script.'''
19-
# abspath converts relative to absolute path; expanduser interprets ~
20-
path = __file__ # path to this script
20+
def get_project_path(include_derived_modules=True):
21+
'''Return the absolute path of the top-level project,
22+
if include_derived_modules==False:
23+
path is assumed to be the parent of the directory containing this script.'''
24+
25+
# obtain path to this script (i.e. the script you're currently reading)
26+
path = __file__
27+
2128
path = os.path.expanduser(path) # interpret ~
22-
path = os.path.abspath(path) # convert to absolute path
29+
path = os.path.abspath(path) # abspath converts relative to absolute path
30+
2331
path = os.path.dirname(path) # containing directory: util
2432
path = os.path.dirname(path) # containing directory: main project dir
33+
34+
# If we want to be more careful, look at derived Python
35+
# modules calling this one, look for a VERSION file to
36+
# determine validity of the various containing directories
37+
if include_derived_modules:
38+
'''
39+
find a VERSION file relative to scripts in the Python call stack,
40+
looking from the outermost layer (closest to initial invocation)
41+
to the innermost call, and use the path of the first VERSION file found.
42+
43+
This allows a derived module with its own VERSION file
44+
to report its versions rather than the core version.
45+
46+
This falls back to using/finding the core path
47+
'''
48+
for called_item in inspect.stack()[::-1]:
49+
# resolve symlinks so the project_path is relative to the actual location of the invoked script
50+
# (i.e. taxon_filter.py --version should resolve viral-classify/VERSION, not viral-core/VERSION)
51+
called_item_realpath = os.path.realpath(called_item.filename)
52+
dir_containing_called_item = os.path.dirname(called_item_realpath)
53+
potential_version_file_path = f"{dir_containing_called_item}/VERSION"
54+
55+
if os.path.isfile(potential_version_file_path):
56+
path = dir_containing_called_item
57+
break
58+
2559
return path
2660

2761

@@ -42,7 +76,6 @@ def call_git_describe():
4276
os.chdir(cwd)
4377
return ver
4478

45-
4679
def release_file():
4780
return os.path.join(get_project_path(), 'VERSION')
4881

0 commit comments

Comments
 (0)