From e59d6f13124a1b12bf742c7024af4e7fab9e5638 Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Sun, 3 Mar 2019 22:47:47 +0800 Subject: [PATCH] Register pre-install and post-install in a stack --- comfortable_swipe/status.py | 2 +- cpp/service/status.cpp | 7 ++--- setup.py | 55 ++++++++++++++++++++++++++----------- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/comfortable_swipe/status.py b/comfortable_swipe/status.py index 1d59c29..6a51500 100644 --- a/comfortable_swipe/status.py +++ b/comfortable_swipe/status.py @@ -9,7 +9,7 @@ from comfortable_swipe.constants import NAME, exe def print_status(): service.status() print('autostart is', autostart.get_status().upper()) - print('{} is {}'.format(NAME, 'RUNNING' if is_running() else 'STOPPED')) + print('{} program is {}'.format(NAME, 'RUNNING' if is_running() else 'STOPPED')) def is_running(): diff --git a/cpp/service/status.cpp b/cpp/service/status.cpp index e33ffed..f745937 100644 --- a/cpp/service/status.cpp +++ b/cpp/service/status.cpp @@ -48,11 +48,10 @@ namespace comfortable_swipe::service if (config.count("threshold") > 0) { auto & threshold = config["threshold"]; - // check if regex pattern matches threshold - std::cmatch matches; - bool ok = (std::regex_match(threshold.data(), matches, std::regex("^\\d+(?:\\.\\d+)??$")) != 0); + // std::cmatch matches; + // bool ok = (std::regex_match(threshold.data(), matches, std::regex("^\\d+(?:\\.\\d+)??$")) != 0); // print status of threshold - std::printf(" %9s = %s (%s)\n", "threshold", threshold.data(), ok ? "VALID" : "INVALID"); + std::printf(" %9s = %s\n", "threshold", threshold.data()); // ok ? "VALID" : "INVALID" } else std::printf(" %9s is OFF\n", "threshold"); diff --git a/setup.py b/setup.py index a986b03..f146c6a 100644 --- a/setup.py +++ b/setup.py @@ -9,6 +9,7 @@ from setuptools import setup, find_packages from setuptools.extension import Extension from setuptools.command.develop import develop from setuptools.command.install import install +from setuptools.command.easy_install import easy_install from wheel.bdist_wheel import bdist_wheel @@ -58,16 +59,21 @@ try: ) for extension_name in extension_names] - # add post_install script to install method - class Install(install): - def run(self): - self.pre_install() - install.run(self) - self.post_install() + class Command: + outer_installer = None + outer_uninstaller = None + def pre_install(self): - pass + # make sure only outer install script will run post_install + if self.__class__.outer_installer is not None: return + self.__class__.outer_installer = self + print('running pre_install') + def post_install(self): + # make sure post_install will only run when command is outer installer + if self.__class__.outer_installer is not self: return print('running post_install') + # create program/config directories if not os.path.exists(os.path.dirname(CONFIG)): os.makedirs(os.path.dirname(CONFIG)) @@ -93,23 +99,40 @@ try: print('Autostart created at', autostart.target_path()) print('\nInstallation successful\nTry running: {} start'.format(NAME)) - - class Develop(develop): - def run(self): - self.pre_uninstall() if self.uninstall else Install.pre_install(self) - develop.run(self) - self.post_uninstall() if self.uninstall else Install.post_install(self) def pre_uninstall(self): + # make sure only outer uninstall script will run post_uninstall + if self.__class__.outer_uninstaller is not None: return + self.__class__.outer_uninstaller = self print('running pre_uninstall') + + + def post_uninstall(self): + # make sure post_uninstall will only run when command is outer uninstaller + if self.__class__.outer_uninstaller is not self: return + print('running post_uninstall') from comfortable_swipe import autostart if autostart.get_status() is autostart.ON: print('Removing autostart at', autostart.target_path()) autostart.set_status(autostart.OFF) - def post_uninstall(self): - pass + + # add post_install script to install method + def wrap_command(base_cls, uninstall=False): + class InstallCommand(Command, base_cls): + def run(self): + self.pre_uninstall() if uninstall and self.uninstall else self.pre_install() + base_cls.run(self) + self.post_uninstall() if uninstall and self.uninstall else self.post_install() + + InstallCommand.__name__ = base_cls.__name__ + return InstallCommand # Override command classes here - cmdclass = dict(Install=Install, develop=Develop, bdist_wheel=bdist_wheel) + cmdclass = dict( + install=wrap_command(install), + easy_install=wrap_command(easy_install), + develop=wrap_command(develop, uninstall=True), + bdist_wheel=wrap_command(bdist_wheel) + ) # setup python script setup_script = setup(