create config during installation and get rid of version.py

This commit is contained in:
2020-11-05 01:49:18 +01:00
parent 73b6e8fa0b
commit ce2bf53e21
5 changed files with 114 additions and 95 deletions

View File

@@ -14,12 +14,6 @@
# along with pyinotifyd. If not, see <http://www.gnu.org/licenses/>.
#
__all__ = [
"Pyinotifyd",
"filemanager",
"scheduler",
"watch"]
import argparse
import asyncio
import logging
@@ -28,10 +22,10 @@ import pyinotify
import signal
import sys
from pyinotifyd.version import __version__ as version
from pyinotifyd.watch import Watch, EventMap
from pyinotifyd.install import install_systemd_service
from pyinotifyd.install import uninstall_systemd_service
from pyinotifyd._install import install, uninstall
__version__ = "0.0.1"
class Pyinotifyd:
@@ -166,10 +160,10 @@ def main():
root_logger.addHandler(ch)
if args.install:
sys.exit(install_systemd_service(myname))
sys.exit(install(myname))
if args.uninstall:
sys.exit(uninstall_systemd_service(myname))
sys.exit(uninstall(myname))
try:
config = {}

108
pyinotifyd/_install.py Executable file
View File

@@ -0,0 +1,108 @@
#!/usr/bin/env python3
# pyinotifyd is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyinotifyd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyinotifyd. If not, see <http://www.gnu.org/licenses/>.
#
import logging
import os
import shutil
import sys
SYSTEMD_PATH = "/usr/lib/systemd/system"
def _check_root():
if os.getuid() != 0:
logging.error("you need to have root privileges, please try again")
return False
return True
def _check_systemd():
return os.path.isdir(SYSTEMD_PATH)
def install(name):
if not _check_root():
sys.exit(2)
pkg_dir = os.path.dirname(__file__)
if not _check_systemd():
logging.warning(
"systemd service file will not be installed,"
"because systemd is not installed")
else:
dst = f"{SYSTEMD_PATH}/{name}.service"
src = f"{pkg_dir}/misc/{name}.service"
try:
shutil.copy2(src, dst)
except Exception as e:
logging.error(f"unable to copy systemd service file: {e}")
else:
logging.info("systemd service file installed")
config_dir = f"/etc/{name}"
if os.path.isdir(config_dir):
logging.info(f"config dir {config_dir} exists")
else:
try:
os.mkdir(config_dir)
except Exception as e:
logging.error(f"unable to create config dir {config_dir}: {e}")
sys.exit(3)
else:
logging.info(f"config dir {config_dir} created")
dst = f"{config_dir}/config.py"
src = f"{pkg_dir}/docs/config.py.example"
if os.path.exists(dst):
logging.info(f"config file {dst} exists")
else:
try:
shutil.copy2(src, dst)
except Exception as e:
logging.error(f"unable to copy config file to {dst}: {e}")
sys.exit(4)
else:
logging.info("example config file copied to {dst}")
logging.info("{name} successfully installed")
def uninstall(name):
if not _check_root():
sys.exit(2)
if _check_systemd():
path = f"{SYSTEMD_PATH}/{name}.service"
if not os.path.exists(path):
logging.info("systemd service is not installed")
else:
try:
os.remove(path)
except Exception as e:
logging.error(f"unable to delete: {e}")
sys.exit(3)
else:
logging.info("systemd service file uninstalled")
config_dir = f"/etc/{name}"
if os.path.isdir(config_dir):
logging.warning(
f"config dir {config_dir} still exists, please delete manually")
logging.info("{name} successfully uninstalled")

View File

@@ -1,82 +0,0 @@
#!/usr/bin/env python3
# pyinotifyd is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyinotifyd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyinotifyd. If not, see <http://www.gnu.org/licenses/>.
#
import logging
import os
import shutil
def check_root():
if os.getuid() != 0:
logging.error("you need to have root privileges to install "
"the systemd service file, please try again")
return False
return True
def check_systemd():
if not check_root():
return False
if not os.path.isdir("/usr/lib/systemd/system"):
logging.error("systemd is not running on your system")
return False
return True
def install_systemd_service(name):
if not check_systemd():
return 1
dst_path = "/usr/lib/systemd/system/{name}.service"
if os.path.exists(dst_path):
logging.error("systemd service file is already installed")
return 1
pkg_dir = os.path.dirname(__file__)
src_path = f"{pkg_dir}/docs/{name}.service"
try:
shutil.copy2(src_path, dst_path)
except Exception as e:
logging.error(f"unable to copy: {e}")
return 1
logging.info("systemd service file successfully installed")
return 0
def uninstall_systemd_service(name):
if not check_systemd():
return 1
path = "/usr/lib/systemd/system/{name}.service"
if not os.path.exists(path):
logging.info("systemd service is not installed")
return 0
try:
os.remove(path)
except Exception as e:
logging.error(f"unable to delete: {e}")
return 1
return 0

View File

@@ -1 +0,0 @@
__version__ = "0.0.1"

View File

@@ -1,2 +1,2 @@
[metadata]
version = attr: pyinotifyd.version.__version__
version = attr: pyinotifyd.__version__