Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup: replace deprecated distutils by setuptools #223

Merged
merged 4 commits into from Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Expand Up @@ -137,7 +137,7 @@ jobs:
bash binutils default-jdk-headless dpkg-dev gcc gdb iputils-ping kmod
libc6-dev pkg-config python3 python3-apt python3-distutils-extra
python3-launchpadlib python3-psutil python3-pytest python3-pytest-cov
python3-requests python3-systemd valgrind
python3-requests python3-setuptools python3-systemd valgrind
- name: Build Java subdir
run: >
python3 -m coverage run ./setup.py build_java_subdir
Expand Down Expand Up @@ -173,7 +173,7 @@ jobs:
bash binutils default-jdk-headless dpkg-dev gcc gdb iputils-ping kmod
libc6-dev pkg-config python3 python3-apt python3-distutils-extra
python3-launchpadlib python3-psutil python3-pytest python3-pytest-cov
python3-systemd valgrind
python3-setuptools python3-systemd valgrind
- name: Install
run: >
sudo python3 -m coverage run
Expand Down Expand Up @@ -296,8 +296,8 @@ jobs:
dbus dbus-x11 dirmngr dpkg-dev gcc gdb gir1.2-gtk-3.0 gir1.2-wnck-3.0
gnome-icon-theme gpg gvfs-daemons psmisc python3 python3-apt
python3-dbus python3-distutils-extra python3-gi python3-launchpadlib
python3-pyqt5 python3-pytest python3-pytest-cov ubuntu-dbgsym-keyring
ubuntu-keyring valgrind xvfb
python3-pyqt5 python3-pytest python3-pytest-cov python3-setuptools
ubuntu-dbgsym-keyring ubuntu-keyring valgrind xvfb
- name: Install
run: >
sudo python3 -m coverage run
Expand Down
76 changes: 17 additions & 59 deletions setup.py
Expand Up @@ -5,61 +5,26 @@
# TODO: Address following pylint complaints
# pylint: disable=invalid-name

# distutils-extra needs porting, pylint: disable=deprecated-module
import distutils.command.build
import distutils.command.clean
import distutils.core
import distutils.version
import glob
import logging
import os.path
import subprocess
import sys

from setuptools_apport.java import register_java_sub_commands

try:
import DistUtilsExtra.auto
from DistUtilsExtra.command.build_extra import build_extra
except ImportError:
sys.stderr.write(
"To build Apport you need https://launchpad.net/python-distutils-extra\n"
)
sys.exit(1)

assert (
distutils.version.StrictVersion(DistUtilsExtra.auto.__version__) >= "2.24"
), "needs DistUtilsExtra.auto >= 2.24"

BASH_COMPLETIONS = "share/bash-completion/completions/"


class build_java_subdir(distutils.core.Command):
"""Java crash handler build command."""

description = "Compile java components of Apport"
user_options: list[tuple[str, str, str]] = []

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
oldwd = os.getcwd()
os.chdir("java")
release = "7"

javac = ["javac", "-source", release, "-target", release]
subprocess.check_call(javac + glob.glob("com/ubuntu/apport/*.java"))
subprocess.check_call(
["jar", "cvf", "apport.jar"] + glob.glob("com/ubuntu/apport/*.class")
)
subprocess.check_call(javac + ["testsuite/crash.java"])
subprocess.check_call(
["jar", "cvf", "crash.jar", "crash.class"], cwd="testsuite"
)

os.chdir(oldwd)


class clean_java_subdir(DistUtilsExtra.auto.clean_build_tree):
"""Java crash handler clean command."""

Expand All @@ -75,6 +40,7 @@ class install_fix_hashbangs(DistUtilsExtra.auto.install_auto):
"""Fix hashbang lines in scripts in data dir."""

def _fix_symlinks_in_bash_completion(self):
log = logging.getLogger(__name__)
autoinstalled_completion_dir = os.path.join(
self.install_data, "share", "apport", "bash-completion"
)
Expand All @@ -89,7 +55,7 @@ def _fix_symlinks_in_bash_completion(self):
if not os.path.exists(dest):
continue

distutils.log.info("Convert %s into a symlink to %s...", dest, source)
log.info("Convert %s into a symlink to %s...", dest, source)
os.remove(dest)
os.symlink(source, dest)

Expand All @@ -103,6 +69,7 @@ def _fix_symlinks_in_bash_completion(self):
os.rmdir(autoinstalled_completion_dir)

def run(self):
log = logging.getLogger(__name__)
DistUtilsExtra.auto.install_auto.run(self)
self._fix_symlinks_in_bash_completion()
new_hashbang = f"#!{sys.executable.rsplit('.', 1)[0]}\n"
Expand All @@ -121,7 +88,7 @@ def run(self):
# ignore data files like spinner.gif
continue
if lines[0].startswith("#!") and "python" in lines[0]:
distutils.log.info("Updating hashbang of %s", f)
log.info("Updating hashbang of %s", f)
lines[0] = new_hashbang
with open(f, "w", encoding="utf-8") as fd:
for line in lines:
Expand All @@ -132,21 +99,6 @@ def run(self):
# main
#

optional_data_files = []
cmdclass: dict[str, object] = {"install": install_fix_hashbangs}

# if we have Java available, build the Java crash handler
try:
subprocess.check_call(["javac", "-version"], stderr=subprocess.PIPE)

distutils.command.build.build.sub_commands.append(("build_java_subdir", None))
optional_data_files.append(("share/java", ["java/apport.jar"]))
cmdclass["build_java_subdir"] = build_java_subdir
cmdclass["clean"] = clean_java_subdir
print("Java support: Enabled")
except (OSError, subprocess.CalledProcessError):
print("Java support: Java not available, not building Java crash handler")

from apport.ui import __version__ # noqa: E402, pylint: disable=C0413

# determine systemd unit directory
Expand All @@ -163,13 +115,15 @@ def run(self):
systemd_unit_dir = "/lib/systemd/system"
systemd_tmpfiles_dir = "/usr/lib/tmpfiles.d"

cmdclass = register_java_sub_commands(build_extra, install_fix_hashbangs)
DistUtilsExtra.auto.setup(
name="apport",
author="Martin Pitt",
author_email="martin.pitt@ubuntu.com",
url="https://launchpad.net/apport",
license="gpl",
description="intercept, process, and report crashes and bug reports",
packages=["apport", "apport.crashdb_impl", "apport.packaging_impl"],
version=__version__,
data_files=[
("share/doc/apport/", glob.glob("doc/*.txt")),
Expand All @@ -180,7 +134,11 @@ def run(self):
("/lib/udev/rules.d", glob.glob("udev/*.rules")),
(systemd_unit_dir, glob.glob("data/systemd/*.s*")),
(systemd_tmpfiles_dir, glob.glob("data/systemd/*.conf")),
]
+ optional_data_files,
cmdclass=cmdclass,
],
cmdclass={
"build": build_extra,
"clean": clean_java_subdir,
"install": install_fix_hashbangs,
}
| cmdclass,
)
Empty file added setuptools_apport/__init__.py
Empty file.
98 changes: 98 additions & 0 deletions setuptools_apport/java.py
@@ -0,0 +1,98 @@
"""Setuptools extension to build the Java subdirectory."""

import functools
import glob
import logging
import os
import pathlib
import subprocess
import typing

from setuptools import Command, Distribution


# pylint: disable-next=invalid-name
class build_java(Command):
"""Compile Java components of Apport"""

description = __doc__
user_options: list[tuple[str, str, str]] = []

def initialize_options(self) -> None:
"""Set or (reset) all options/attributes/caches to their default values"""

def finalize_options(self) -> None:
"""Set final values for all options/attributes"""

# pylint: disable-next=no-self-use
def run(self) -> None:
"""Build the Java .class and .jar files."""
oldwd = os.getcwd()
os.chdir("java")
release = "7"

javac = ["javac", "-source", release, "-target", release]
subprocess.check_call(javac + glob.glob("com/ubuntu/apport/*.java"))
subprocess.check_call(
["jar", "cvf", "apport.jar"] + glob.glob("com/ubuntu/apport/*.class")
)
subprocess.check_call(javac + ["testsuite/crash.java"])
subprocess.check_call(
["jar", "cvf", "crash.jar", "crash.class"], cwd="testsuite"
)

os.chdir(oldwd)


# pylint: disable-next=invalid-name
class install_java(Command):
"""Install Java components of Apport."""

def __init__(self, dist: Distribution, **kwargs: dict[str, typing.Any]) -> None:
super().__init__(dist, **kwargs)
self.initialize_options()

def initialize_options(self) -> None:
"""Set default values for all the options that this command supports."""
self.install_dir: typing.Optional[str] = None

def finalize_options(self) -> None:
"""Set final values for all the options that this command supports."""
self.set_undefined_options("install_data", ("install_dir", "install_dir"))

def _install_data_files(self, dst_path: str, src_files: list[pathlib.Path]) -> None:
assert self.install_dir
for src_file in src_files:
target = pathlib.Path(self.install_dir) / dst_path / src_file.name
self.mkpath(str(target.parent))
self.copy_file(str(src_file), str(target), preserve_mode=False)

def run(self) -> None:
"""Install the Java .jar files."""
self._install_data_files("share/java", [pathlib.Path("java/apport.jar")])


@functools.cache
def has_java(unused_command: Command) -> bool:
"""Check if the Java compiler is available."""
try:
subprocess.run(["javac", "-version"], capture_output=True, check=True)
except (OSError, subprocess.CalledProcessError):
logging.getLogger(__name__).warning(

Check warning on line 81 in setuptools_apport/java.py

View check run for this annotation

Codecov / codecov/patch

setuptools_apport/java.py#L80-L81

Added lines #L80 - L81 were not covered by tests
"Java support: Java not available, not building Java crash handler"
)
return False

Check warning on line 84 in setuptools_apport/java.py

View check run for this annotation

Codecov / codecov/patch

setuptools_apport/java.py#L84

Added line #L84 was not covered by tests
return True


def register_java_sub_commands(
build: type[Command], install: type[Command]
) -> dict[str, type[Command]]:
"""Plug the Java extension into setuptools.

Return a dictionary with the added command classes which needs to
be passed to the `setup` call as `cmdclass` parameter.
"""
build.sub_commands.append(("build_java_subdir", has_java))
install.sub_commands.append(("install_java", has_java))
return {"build_java_subdir": build_java, "install_java": install_java}
2 changes: 1 addition & 1 deletion tests/run-linters
Expand Up @@ -14,7 +14,7 @@

set -eu

PYTHON_FILES="apport apport_python_hook.py data problem_report.py setup.py tests"
PYTHON_FILES="apport apport_python_hook.py data problem_report.py setup.py setuptools_apport tests"
PYTHON_SCRIPTS=$(find bin data gtk kde -type f -executable ! -name apport-bug ! -name apport-collect ! -name is-enabled ! -name root_info_wrapper)

check_hardcoded_names() {
Expand Down