Просмотр исходного кода

ref: factor out distutils (#65329)

this module is no longer shipped in python 3.12

<!-- Describe your PR here. -->
anthony sottile 1 год назад
Родитель
Сommit
3e415dcc1e

+ 1 - 1
requirements-dev-frozen.txt

@@ -216,7 +216,7 @@ types-pytz==2022.1.2
 types-pyyaml==6.0.11
 types-redis==3.5.18
 types-requests==2.31.0.20231231
-types-setuptools==65.3.0
+types-setuptools==69.0.0.0
 types-simplejson==3.17.7.2
 typing-extensions==4.9.0
 tzdata==2023.3

+ 1 - 1
requirements-dev.txt

@@ -55,5 +55,5 @@ types-pyyaml
 # make sure to match close-enough to redis==
 types-redis<4
 types-requests>=2.31.0.20231231
-types-setuptools
+types-setuptools>=68
 types-simplejson>=3.17.7.2

+ 5 - 4
setup.py

@@ -2,9 +2,9 @@
 
 import os
 import sys
-from distutils.command.build import build as BuildCommand
 
 from setuptools import setup
+from setuptools.command.build import build as BuildCommand
 from setuptools.command.develop import develop as DevelopCommand
 from setuptools.command.sdist import sdist as SDistCommand
 
@@ -25,7 +25,8 @@ class SentrySDistCommand(SDistCommand):
     # If we are not a light build we want to also execute build_assets as
     # part of our source build pipeline.
     if not IS_LIGHT_BUILD:
-        sub_commands = SDistCommand.sub_commands + [
+        sub_commands = [
+            *SDistCommand.sub_commands,
             ("build_integration_docs", None),
             ("build_assets", None),
             ("build_js_sdk_registry", None),
@@ -34,9 +35,9 @@ class SentrySDistCommand(SDistCommand):
 
 class SentryBuildCommand(BuildCommand):
     def run(self):
-        from distutils import log as distutils_log
+        import logging
 
-        distutils_log.set_threshold(distutils_log.WARN)
+        logging.getLogger("sentry").setLevel(logging.WARNING)
 
         if not IS_LIGHT_BUILD:
             self.run_command("build_integration_docs")

+ 8 - 6
src/sentry/utils/distutils/commands/base.py

@@ -1,11 +1,13 @@
+import logging
 import os
 import os.path
 import shutil
 import sys
-from distutils import log
-from distutils.core import Command
 from subprocess import STDOUT, CalledProcessError, check_output
 
+from setuptools import Command
+
+log = logging.getLogger(__name__)
 _HERE = os.path.dirname(os.path.abspath(__file__))
 ROOT = os.path.realpath(os.path.join(_HERE, "../../../../.."))
 
@@ -101,7 +103,7 @@ class BaseBuildCommand(Command):
         # Otherwise we fetch build_lib from the build command.
         else:
             self.set_undefined_options("build", ("build_lib", "build_lib"))
-            log.debug("regular js build: build path is %s" % self.build_lib)
+            log.debug("regular js build: build path is %s", self.build_lib)
 
         if self.work_path is None:
             self.work_path = ROOT
@@ -121,7 +123,7 @@ class BaseBuildCommand(Command):
             sys.exit(1)
 
         if node_version[2] is not None:
-            log.info(f"using node ({node_version})")
+            log.info("using node (%s)", node_version)
             self._run_command(["yarn", "install", "--production", "--frozen-lockfile", "--quiet"])
 
     def _run_command(self, cmd, env=None):
@@ -130,7 +132,7 @@ class BaseBuildCommand(Command):
         try:
             return check_output(cmd, cwd=self.work_path, env=env, stderr=STDOUT)
         except CalledProcessError as err:
-            log.error(
+            log.exception(
                 "[%s] failed with exit code [%s] on [%s]:\n%s",
                 cmd_str,
                 err.returncode,
@@ -139,7 +141,7 @@ class BaseBuildCommand(Command):
             )
             raise
         except Exception:
-            log.error("command failed [%s] via [%s]", cmd_str, self.work_path)
+            log.exception("command failed [%s] via [%s]", cmd_str, self.work_path)
             raise
 
     def update_manifests(self):

+ 9 - 8
src/sentry/utils/distutils/commands/build_assets.py

@@ -3,14 +3,16 @@ import datetime
 # Import the stdlib json instead of sentry.utils.json, since this command is
 # run in setup.py
 import json  # NOQA
+import logging
 import os
 import os.path
 import sys
 import traceback
-from distutils import log
 
 from .base import BaseBuildCommand
 
+log = logging.getLogger(__name__)
+
 
 class BuildAssetsCommand(BaseBuildCommand):
     user_options = BaseBuildCommand.user_options + [
@@ -72,7 +74,7 @@ class BuildAssetsCommand(BaseBuildCommand):
             except Exception:
                 pass
             else:
-                log.info(f"pulled version information from '{json_path}'")
+                log.info("pulled version information from %r", json_path)
                 version, build = data["version"], data["build"]
 
         return {"version": version, "build": build}
@@ -99,11 +101,10 @@ class BuildAssetsCommand(BaseBuildCommand):
     def _build(self):
         version_info = self._get_package_version()
         log.info(
-            "building assets for {} v{} (build {})".format(
-                self.distribution.get_name(),
-                version_info["version"] or "UNKNOWN",
-                version_info["build"] or "UNKNOWN",
-            )
+            "building assets for %s v%s (build %s)",
+            self.distribution.get_name(),
+            version_info["version"] or "UNKNOWN",
+            version_info["build"] or "UNKNOWN",
         )
         if not version_info["version"] or not version_info["build"]:
             log.fatal("Could not determine sentry version or build")
@@ -118,7 +119,7 @@ class BuildAssetsCommand(BaseBuildCommand):
 
         log.info("writing version manifest")
         manifest = self._write_version_file(version_info)
-        log.info(f"recorded manifest\n{json.dumps(manifest, indent=2)}")
+        log.info("recorded manifest\n%s", json.dumps(manifest, indent=2))
 
     def _build_static(self):
         # By setting NODE_ENV=production, a few things happen

+ 3 - 1
src/sentry/utils/distutils/commands/build_integration_docs.py

@@ -1,8 +1,10 @@
+import logging
 import os.path
-from distutils import log
 
 from .base import ROOT, BaseBuildCommand
 
+log = logging.getLogger(__name__)
+
 
 class BuildIntegrationDocsCommand(BaseBuildCommand):
     description = "build integration docs"

+ 8 - 3
src/sentry/utils/distutils/commands/build_js_sdk_registry.py

@@ -1,17 +1,20 @@
 # NOTE: This is run external to sentry as well as part of the setup
 # process.  Thus we do not want to import non stdlib things here.
 
+import json  # NOQA
+
 # Import the stdlib json instead of sentry.utils.json, since this command is
 # run in setup.py
-import json  # NOQA
+import logging
 import os
-from distutils import log
 from urllib.request import urlopen
 
 import sentry
 
 from .base import BaseBuildCommand
 
+log = logging.getLogger(__name__)
+
 JS_SDK_REGISTRY_URL = (
     "https://release-registry.services.sentry.io/sdks/sentry.javascript.browser/versions"
 )
@@ -44,4 +47,6 @@ class BuildJsSdkRegistryCommand(BaseBuildCommand):
         try:
             sync_registry()
         except Exception:
-            log.error("error occurred while trying to fetch js sdk information from the registry")
+            log.exception(
+                "error occurred while trying to fetch js sdk information from the registry"
+            )

+ 1 - 1
tests/sentry/test_devimports.py

@@ -103,7 +103,7 @@ for xfail in {xfail!r}:
         raise SystemExit(f'unexpected success importing {{xfail}}')
 """
 
-    env = {"SENTRY_ENVIRONMENT": "production"}
+    env = {"SENTRY_ENVIRONMENT": "production", "SETUPTOOLS_USE_DISTUTILS": "stdlib"}
     ret = subprocess.run(
         (sys.executable, "-c", script),
         env=env,