|
5 | 5 | from os.path import dirname, abspath |
6 | 6 |
|
7 | 7 |
|
8 | | -def scm_get_version_recursive_root(abs_path): |
9 | | - """ |
10 | | - Recursively climbs the parent folders, searching for a git root |
11 | | -
|
12 | | - :param abs_path: |
13 | | - :return: |
14 | | - """ |
15 | | - # noinspection PyUnresolvedReferences |
| 8 | +class GitCommandNotAvailable(Exception): |
| 9 | + def __str__(self): |
| 10 | + return "`git` command is not available ; please make sure that it is in your PATH." |
| 11 | + |
| 12 | + |
| 13 | +class ScmInformationNotFound(Exception): |
| 14 | + def __init__(self, initial_path): |
| 15 | + self.path = initial_path |
| 16 | + |
| 17 | + def __str__(self): |
| 18 | + return "Unable to find SCM information (/.git or /.hg folder, typically) " \ |
| 19 | + "in the parent folders of %s" % self.path |
| 20 | + |
| 21 | + |
| 22 | +class SetupToolsScmNotInstalled(Exception): |
| 23 | + def __str__(self): |
| 24 | + return "`setuptools_scm` package does not seem to be available in your python environment. Please install it" \ |
| 25 | + " to enable SCM version discovery (git, hg)." |
| 26 | + |
| 27 | + |
| 28 | +try: |
16 | 29 | from setuptools_scm import get_version |
17 | | - try: |
18 | | - return get_version(abs_path) |
19 | | - except LookupError as e: |
20 | | - parent_dir = dirname(abs_path) |
21 | | - if parent_dir == abs_path: |
22 | | - # cannot recurse anymore |
23 | | - raise e |
| 30 | + from setuptools_scm.utils import has_command |
| 31 | + has_git_command = has_command('git') |
| 32 | + |
| 33 | + def scm_get_version_recursive_root(abs_path, initial_path): |
| 34 | + """ |
| 35 | + Recursively climbs the parent folders, searching for a git root |
| 36 | +
|
| 37 | + :param abs_path: |
| 38 | + :return: |
| 39 | + """ |
| 40 | + try: |
| 41 | + return get_version(abs_path) |
| 42 | + except LookupError as e: |
| 43 | + parent_dir = dirname(abs_path) |
| 44 | + if parent_dir == abs_path: |
| 45 | + # cannot recurse anymore |
| 46 | + raise ScmInformationNotFound(initial_path) |
| 47 | + else: |
| 48 | + # recurse |
| 49 | + return scm_get_version_recursive_root(parent_dir, initial_path=initial_path) |
| 50 | + |
| 51 | + |
| 52 | + def get_version_using_setuptools_scm(module # type: ModuleType |
| 53 | + ): |
| 54 | + # type: (...) -> str |
| 55 | + """ |
| 56 | + get version from the source directory if it is under version control |
| 57 | + :param module: |
| 58 | + :return: |
| 59 | + """ |
| 60 | + # ...using setuptools_scm if available |
| 61 | + if not has_git_command: |
| 62 | + raise GitCommandNotAvailable() |
24 | 63 | else: |
25 | | - # recurse |
26 | | - return scm_get_version_recursive_root(parent_dir) |
27 | | - |
28 | | - |
29 | | -def get_version_using_setuptools_scm(module # type: ModuleType |
30 | | - ): |
31 | | - # type: (...) -> str |
32 | | - """ |
33 | | - get version from the source directory if it is under version control |
34 | | - :param module: |
35 | | - :return: |
36 | | - """ |
37 | | - # ...using setuptools_scm if available |
38 | | - return scm_get_version_recursive_root(abspath(module.__file__)) |
39 | | - |
40 | | - # if not isinstance(s4err, str): |
41 | | - # warn(" - (4) using setuptools_scm to find the svn/git version raised an error : [{}] {}" |
42 | | - # "".format(type(s4err).__name__, s4err)) |
43 | | - # else: |
44 | | - # warn(" - (4) impossible to locate the package in order to find the svn/git version: {}" |
45 | | - # "".format(s4err)) |
| 64 | + path = abspath(module.__file__) |
| 65 | + return scm_get_version_recursive_root(path, initial_path=path) |
| 66 | + |
| 67 | +except ImportError: |
| 68 | + # no |
| 69 | + def get_version_using_setuptools_scm(module # type: ModuleType |
| 70 | + ): |
| 71 | + # type: (...) -> str |
| 72 | + raise SetupToolsScmNotInstalled() |
0 commit comments