diff --git a/scripts/create_systemd_service.sh b/scripts/create_systemd_service.sh new file mode 100644 index 0000000..5c9f2fe --- /dev/null +++ b/scripts/create_systemd_service.sh @@ -0,0 +1,7 @@ +#!/bin/bash +SYSTEMD_SCRIPT_DIR=$( cd $(dirname "${BASH_SOURCE:=$0}") && pwd) +cp -f "$SYSTEMD_SCRIPT_DIR/super-project.service" /lib/systemd/system +chown root:root /lib/systemd/system/super-project.service + +systemctl daemon-reload +systemctl enable super-project.service \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 3c6e79c..fafa091 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,6 @@ [bdist_wheel] universal=1 + +[install] +install-scripts=/usr/sbin + diff --git a/setup.py b/setup.py index b042068..1aad8cd 100644 --- a/setup.py +++ b/setup.py @@ -3,12 +3,116 @@ from setuptools import setup import sys +import os +import platform + +from distutils.util import execute +from distutils.dir_util import copy_tree +from distutils.file_util import copy_file +from distutils.cmd import Command +from subprocess import call +from setuptools.command.install import install +from setuptools.command.develop import develop +from setuptools import setup, find_packages readme = open("README.rst").read() history = open("HISTORY.rst").read() sys.executable = "/usr/bin/python3" + +def udev_reload_rules(): + call(["udevadm", "control", "--reload-rules"]) + + +def udev_trigger(): + call( + [ + "udevadm", + "trigger", + ] + ) + +def install_udev_rules(raise_exception): + if check_root(): + copy_tree("udev/", "/etc/udev/rules.d/") + copy_file("ds4drv.conf", "/etc/") + execute(udev_reload_rules, (), "Reloading udev rules") + execute(udev_trigger, (), "Triggering udev rules") + else: + msg = 'You must have root privileges to install udev rules. Run "sudo python setup.py udev_rules"' + if raise_exception: + raise OSError(msg) + else: + print(msg) + + + +def system_daemon_reload(): + call(["systemctl", "daemon-reload"]) + +def system_daemon_enable_ds4drv_service(): + call(["systemctl", "enable", "ds4drv.service"]) + +def install_systemd_service_unit(raise_exception): + if check_root(): + copy_file("systemd/ds4drv.service", "/lib/systemd/system/") + execute(system_daemon_reload, (), "Reloading system daemon") + execute(system_daemon_enable_ds4drv_service, (), "Enabling ds4drv.service") + else: + msg = 'You must have root privileges to install ds4drv.service. Run "sudo python setup.py systemd"' + if raise_exception: + raise OSError(msg) + else: + print(msg) + + +def check_root(): + return os.geteuid() == 0 + + +def is_linux(): + return platform.system() == "Linux" + + +class InstallUdevRules(Command): + description = "install udev rules (requires root privileges)" + user_options = [] + + def initialize_options(self): + pass + + def finalize_options(self): + pass + + def run(self): + install_udev_rules(True) + +class InstallSystemdService(Command): + description = "install systemd service (requires root privileges)" + user_options = [] + + def initialize_options(self): + pass + + def finalize_options(self): + pass + + def run(self): + install_systemd_service_unit(True) + +class CustomInstall(install): + def run(self): + install.run(self) + install_udev_rules(True) + install_systemd_service_unit(True) + + +class CustomDevelop(develop): + def run(self): + develop.run(self) + + setup(name="ds4drv", version="0.5.5", description="A Sony DualShock 4 userspace driver for Linux", @@ -22,6 +126,7 @@ "ds4drv=ds4drv.__main__:main" ] }, + scripts=['scripts/ds4drv-pair'], packages=["ds4drv", "ds4drv.actions", "ds4drv.backends", @@ -35,5 +140,11 @@ "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.3", "Topic :: Games/Entertainment" - ] + ], + cmdclass={ + "udev_rules": InstallUdevRules, + "systemd": InstallSystemdService, + "install": CustomInstall, + "develop": CustomDevelop, + }, )