Files
dvc/setup.py
T
Saugat Pachhai 7c437cc349 make dependabot updates work (#6760)
Dependabot was throwing Illformed requirements `"< '3.10'"` for the
hdfs. But I noticed that it did accept other requirements where we
do similar. So it seems the indentation fixes it.

Even then, dependabot did not update any dependencies. I removed
setup.py file, after which immediately it started creating PRs.
So, I recknoed that it must have been due to some mechanism
in place from dependabot to disallow execution, so I tried to
remove few blocks of code from setup.py. And, I was eventually
able to figure out that it was from the block where we import version.

So, I added a try-except block and it magically again worked. :)

Let's see how it goes.
2021-10-08 14:46:47 +05:45

67 lines
2.4 KiB
Python

import importlib.util
import os
from pathlib import Path
from textwrap import dedent
from setuptools import setup
from setuptools.command.build_py import build_py as _build_py
# Prevents pkg_resources import in entry point script,
# see https://github.com/ninjaaron/fast-entry_points.
# This saves about 200 ms on startup time for non-wheel installs.
try:
import fastentrypoints # noqa: F401, pylint: disable=unused-import
except ImportError:
pass # not able to import when installing through pre-commit
# Read package meta-data from version.py
# see https://packaging.python.org/guides/single-sourcing-package-version/
try:
pkg_dir = os.path.dirname(os.path.abspath(__file__))
version_path = os.path.join(pkg_dir, "dvc", "version.py")
spec = importlib.util.spec_from_file_location("dvc.version", version_path)
dvc_version = importlib.util.module_from_spec(spec)
spec.loader.exec_module(dvc_version)
version = dvc_version.__version__ # noqa: F821
except Exception as exc: # pylint: disable=broad-except
# Dependabot seem to stop working when we don't handle this except block.
# Most likely, it's because of the restrictions in execution of the module.
# We workaround it, and print the error message if it also happens on other
# installations (though this message may likely be suppressed by build
# tools, eg: pip only shows this message on `--verbose` mode).
import sys
print("Could not load version info: ", exc, file=sys.stderr)
version = "UNKNOWN"
# To achieve consistency between the build version and the one provided
# by your package during runtime, you need to **pin** the build version.
#
# This custom class will replace the version.py module with a **static**
# `__version__` that your package can read at runtime, assuring consistency.
#
# References:
# - https://docs.python.org/3.7/distutils/extending.html
# - https://github.com/python/mypy
class build_py(_build_py):
def pin_version(self):
ver_file = Path(self.build_lib) / "dvc" / "version.py"
self.mkpath(os.fspath(ver_file.parent))
ver_file.write_text(
dedent(
f"""\
# AUTOGENERATED at build time by setup.py
__version__ = "{version}"
"""
)
)
def run(self):
self.execute(self.pin_version, ())
_build_py.run(self)
setup(version=version, cmdclass={"build_py": build_py})