Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
:param str root: Path to project folder
:return setupmeta.scm.Scm: SCM used by project, if any
"""
if os.environ.get(setupmeta.SCM_DESCRIBE):
return Snapshot(root)
scm_root = find_scm_root(os.path.abspath(root), ".git")
if scm_root:
return Git(scm_root)
version_file = os.path.join(root, setupmeta.VERSION_FILE)
if os.path.isfile(version_file):
return Snapshot(root)
setupmeta.trace("could not determine SCM for '%s'" % root)
return None
"""
:param str|None setup_py_path: Given setup.py (when invoked from test)
"""
if not setup_py_path:
# Determine path to setup.py module from call stack
for frame in inspect.stack():
module = inspect.getmodule(frame[0])
if module and is_setup_py_path(module.__file__):
setup_py_path = module.__file__
trace("setup.py found from call stack: %s" % setup_py_path)
break
if not setup_py_path and sys.argv:
if is_setup_py_path(sys.argv[0]):
setup_py_path = sys.argv[0]
trace("setup.py found from sys.argv: %s" % setup_py_path)
if is_setup_py_path(setup_py_path):
setup_py_path = os.path.abspath(setup_py_path)
MetaDefs.project_dir = os.path.dirname(setup_py_path)
trace("project dir: %s" % MetaDefs.project_dir)
return
vdef = self.meta.definitions.get("version")
if vdef and vdef.source and vdef.source.lower().endswith("-info"):
# We already got version from PKG-INFO
return
cv = vdef.sources[0].value if vdef and vdef.sources else None
if self.problem:
if not cv:
self.meta.auto_fill("version", "0.0.0", "missing")
if self.strategy:
setupmeta.warn(self.problem)
setupmeta.trace("not auto-filling version due to problem: [%s]" % self.problem)
return
gv = self.scm.get_version()
if self.generate_version_file:
path = setupmeta.project_path(setupmeta.VERSION_FILE)
with open(path, "w") as fh:
fh.write("%s" % gv)
if gv.patch and "patch" not in self.strategy.bumpable:
msg = "patch version component should be .0 for versioning strategy '%s', " % self.strategy
msg += "'.%s' from current version tag '%s' will be ignored" % (gv.patch, gv)
setupmeta.warn(msg)
rendered = self.strategy.rendered(gv)
if cv and gv:
cvc = setupmeta.version_components(cv)
given = meta.value("versioning")
self.strategy = Strategy.from_meta(given)
self.enabled = bool(given and self.strategy and not self.strategy.problem)
self.scm = scm
self.generate_version_file = scm and scm.root != setupmeta.MetaDefs.project_dir and not os.environ.get(setupmeta.SCM_DESCRIBE)
self.problem = None
if not self.strategy:
self.problem = "setupmeta versioning not enabled"
elif self.strategy.problem:
self.problem = self.strategy.problem
elif not self.scm:
self.problem = "project not under a supported SCM"
setupmeta.trace("versioning given: '%s', strategy: [%s], problem: [%s]" % (given, self.strategy, self.problem))
def merge_sources(self, sources):
""" Record the fact that we saw this definition in 'sources' """
for entry in sources:
if not self.value and entry.value:
self.value = entry.value
trace("[-- %s] %s=%s" % (entry.source, self.key, entry.value))
self.sources.append(entry)
def auto_fill_version(self):
"""
Auto-fill version as defined by self.strategy
:param setupmeta.model.SetupMeta meta: Parent meta object
"""
pygradle_version = os.environ.get("PYGRADLE_PROJECT_VERSION")
if pygradle_version:
# Minimal support for https://github.com/linkedin/pygradle
self.meta.auto_fill("version", pygradle_version, "pygradle", override=True)
return
if not self.enabled:
setupmeta.trace("not auto-filling version, versioning is disabled")
return
vdef = self.meta.definitions.get("version")
if vdef and vdef.source and vdef.source.lower().endswith("-info"):
# We already got version from PKG-INFO
return
cv = vdef.sources[0].value if vdef and vdef.sources else None
if self.problem:
if not cv:
self.meta.auto_fill("version", "0.0.0", "missing")
if self.strategy:
setupmeta.warn(self.problem)
setupmeta.trace("not auto-filling version due to problem: [%s]" % self.problem)
def find_project_dir(self, setup_py_path):
"""
:param str|None setup_py_path: Given setup.py (when invoked from test)
"""
if not setup_py_path:
# Determine path to setup.py module from call stack
for frame in inspect.stack():
module = inspect.getmodule(frame[0])
if module and is_setup_py_path(module.__file__):
setup_py_path = module.__file__
trace("setup.py found from call stack: %s" % setup_py_path)
break
if not setup_py_path and sys.argv:
if is_setup_py_path(sys.argv[0]):
setup_py_path = sys.argv[0]
trace("setup.py found from sys.argv: %s" % setup_py_path)
if is_setup_py_path(setup_py_path):
setup_py_path = os.path.abspath(setup_py_path)
MetaDefs.project_dir = os.path.dirname(setup_py_path)
trace("project dir: %s" % MetaDefs.project_dir)