3
3
:license: MIT
4
4
"""
5
5
import os
6
- import sys
6
+ import warnings
7
7
8
- from .utils import trace
8
+ from .config import Configuration
9
+ from .utils import function_has_arg , string_types
9
10
from .version import format_version , meta
10
11
from .discover import iter_matching_entrypoints
11
12
12
13
PRETEND_KEY = "SETUPTOOLS_SCM_PRETEND_VERSION"
13
14
14
-
15
15
TEMPLATES = {
16
16
".py" : """\
17
17
# coding: utf-8
22
22
".txt" : "{version}" ,
23
23
}
24
24
25
- PY3 = sys .version_info > (3 ,)
26
- string_types = (str ,) if PY3 else (str , unicode ) # noqa
27
-
28
25
29
26
def version_from_scm (root ):
27
+ # TODO: Is it API?
30
28
return _version_from_entrypoint (root , "setuptools_scm.parse_scm" )
31
29
32
30
33
- def _version_from_entrypoint (root , entrypoint ):
34
- for ep in iter_matching_entrypoints (root , entrypoint ):
35
- version = ep .load ()(root )
31
+ def _call_entrypoint_fn (config , fn ):
32
+ if function_has_arg (fn , 'config' ):
33
+ return fn (config .absolute_root , config = config )
34
+ else :
35
+ warnings .warn ("parse functions are required to provide a named argument 'config' in the future." , PendingDeprecationWarning )
36
+ return fn (config .absolute_root )
37
+
38
+
39
+ def _version_from_entrypoint (config , entrypoint ):
40
+ for ep in iter_matching_entrypoints (config .absolute_root , entrypoint ):
41
+ version = _call_entrypoint_fn (config , ep .load ())
42
+
36
43
if version :
37
44
return version
38
45
@@ -55,27 +62,26 @@ def dump_version(root, version, write_to, template=None):
55
62
fp .write (template .format (version = version ))
56
63
57
64
58
- def _do_parse (root , parse ):
65
+ def _do_parse (config ):
59
66
pretended = os .environ .get (PRETEND_KEY )
60
67
if pretended :
61
68
# we use meta here since the pretended version
62
69
# must adhere to the pep to begin with
63
70
return meta (tag = pretended , preformatted = True )
64
71
65
- if parse :
66
- parse_result = parse ( root )
72
+ if config . parse :
73
+ parse_result = _call_entrypoint_fn ( config , config . parse )
67
74
if isinstance (parse_result , string_types ):
68
75
raise TypeError (
69
76
"version parse result was a string\n please return a parsed version"
70
77
)
71
- version = parse_result or _version_from_entrypoint (
72
- root , "setuptools_scm.parse_scm_fallback"
73
- )
78
+ version = parse_result or \
79
+ _version_from_entrypoint ( config , "setuptools_scm.parse_scm_fallback" )
80
+
74
81
else :
75
82
# include fallbacks after dropping them from the main entrypoint
76
- version = version_from_scm (root ) or _version_from_entrypoint (
77
- root , "setuptools_scm.parse_scm_fallback"
78
- )
83
+ version = _version_from_entrypoint (config , "setuptools_scm.parse_scm" ) or \
84
+ _version_from_entrypoint (config , "setuptools_scm.parse_scm_fallback" )
79
85
80
86
if version :
81
87
return version
@@ -88,7 +94,7 @@ def _do_parse(root, parse):
88
94
"metadata and will not work.\n \n "
89
95
"For example, if you're using pip, instead of "
90
96
"https://github.com/user/proj/archive/master.zip "
91
- "use git+https://github.com/user/proj.git#egg=proj" % root
97
+ "use git+https://github.com/user/proj.git#egg=proj" % config . absolute_root
92
98
)
93
99
94
100
@@ -99,6 +105,7 @@ def get_version(
99
105
write_to = None ,
100
106
write_to_template = None ,
101
107
relative_to = None ,
108
+ tag_regex = None ,
102
109
parse = None ,
103
110
):
104
111
"""
@@ -107,12 +114,18 @@ def get_version(
107
114
in the root of the repository to direct setuptools_scm to the
108
115
root of the repository by supplying ``__file__``.
109
116
"""
110
- if relative_to :
111
- root = os .path .join (os .path .dirname (relative_to ), root )
112
- root = os .path .abspath (root )
113
- trace ("root" , repr (root ))
114
-
115
- parsed_version = _do_parse (root , parse )
117
+
118
+ config = Configuration ()
119
+ config .root = root
120
+ config .version_scheme = version_scheme
121
+ config .local_scheme = local_scheme
122
+ config .write_to = write_to
123
+ config .write_to_template = write_to_template
124
+ config .relative_to = relative_to
125
+ config .tag_regex = tag_regex
126
+ config .parse = parse
127
+
128
+ parsed_version = _do_parse (config )
116
129
117
130
if parsed_version :
118
131
version_string = format_version (
0 commit comments