|
22 | 22 | _VERSION_FILENAME = os.path.join(PKG_PATHNAME, '_version.py') |
23 | 23 |
|
24 | 24 |
|
| 25 | +def read_version_py(path): |
| 26 | + """ Read a _version.py file in a safe way. """ |
| 27 | + with open(path, 'r') as fp: |
| 28 | + code = compile(fp.read(), 'chaco._version', 'exec') |
| 29 | + context = {} |
| 30 | + exec(code, context) |
| 31 | + return context['git_revision'], context['full_version'] |
| 32 | + |
| 33 | + |
25 | 34 | def git_version(): |
26 | 35 | """ Parse version information from the current git commit. |
27 | 36 |
|
@@ -85,17 +94,19 @@ def write_version_py(filename=_VERSION_FILENAME): |
85 | 94 | # write_version_py(), otherwise the import of _version messes |
86 | 95 | # up the build under Python 3. |
87 | 96 | fullversion = VERSION |
| 97 | + chaco_version_path = os.path.join( |
| 98 | + os.path.dirname(__file__), 'chaco', '_version.py') |
88 | 99 | if os.path.exists('.git'): |
89 | 100 | git_rev, dev_num = git_version() |
90 | 101 | elif os.path.exists(filename): |
91 | 102 | # must be a source distribution, use existing version file |
92 | 103 | try: |
93 | | - from chaco._version import git_revision as git_rev |
94 | | - from chaco._version import full_version as full_v |
95 | | - except ImportError: |
96 | | - msg = ("Unable to import 'git_revision' or 'full_revision'. " |
97 | | - "Try removing {} and the build directory before building.") |
98 | | - raise ImportError(msg.format(_VERSION_FILENAME)) |
| 104 | + git_revision, full_version = read_version_py(chaco_version_path) |
| 105 | + except (SyntaxError, KeyError): |
| 106 | + raise RuntimeError("Unable to read git_revision. Try removing " |
| 107 | + "chaco/_version.py and the build directory " |
| 108 | + "before building.") |
| 109 | + |
99 | 110 |
|
100 | 111 | match = re.match(r'.*?\.dev(?P<dev_num>\d+)', full_v) |
101 | 112 | if match is None: |
|
0 commit comments