33:license: MIT
44"""
55import os
6- import sys
6+ import warnings
77
8- from .utils import trace
8+ from .config import Configuration
9+ from .utils import function_has_arg , string_types
910from .version import format_version , meta
1011from .discover import iter_matching_entrypoints
1112
1213PRETEND_KEY = "SETUPTOOLS_SCM_PRETEND_VERSION"
1314
14-
1515TEMPLATES = {
1616 ".py" : """\
1717 # coding: utf-8
2222 ".txt" : "{version}" ,
2323}
2424
25- PY3 = sys .version_info > (3 ,)
26- string_types = (str ,) if PY3 else (str , unicode ) # noqa
27-
2825
2926def version_from_scm (root ):
27+ # TODO: Is it API?
3028 return _version_from_entrypoint (root , "setuptools_scm.parse_scm" )
3129
3230
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+
3643 if version :
3744 return version
3845
@@ -55,27 +62,26 @@ def dump_version(root, version, write_to, template=None):
5562 fp .write (template .format (version = version ))
5663
5764
58- def _do_parse (root , parse ):
65+ def _do_parse (config ):
5966 pretended = os .environ .get (PRETEND_KEY )
6067 if pretended :
6168 # we use meta here since the pretended version
6269 # must adhere to the pep to begin with
6370 return meta (tag = pretended , preformatted = True )
6471
65- if parse :
66- parse_result = parse ( root )
72+ if config . parse :
73+ parse_result = _call_entrypoint_fn ( config , config . parse )
6774 if isinstance (parse_result , string_types ):
6875 raise TypeError (
6976 "version parse result was a string\n please return a parsed version"
7077 )
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+
7481 else :
7582 # 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" )
7985
8086 if version :
8187 return version
@@ -88,7 +94,7 @@ def _do_parse(root, parse):
8894 "metadata and will not work.\n \n "
8995 "For example, if you're using pip, instead of "
9096 "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
9298 )
9399
94100
@@ -99,6 +105,7 @@ def get_version(
99105 write_to = None ,
100106 write_to_template = None ,
101107 relative_to = None ,
108+ tag_regex = None ,
102109 parse = None ,
103110):
104111 """
@@ -107,12 +114,18 @@ def get_version(
107114 in the root of the repository to direct setuptools_scm to the
108115 root of the repository by supplying ``__file__``.
109116 """
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 )
116129
117130 if parsed_version :
118131 version_string = format_version (
0 commit comments