Jelajahi Sumber

move lock file handling to helpers

Helmut Pozimski 6 tahun lalu
induk
melakukan
407847f268
2 mengubah file dengan 56 tambahan dan 45 penghapusan
  1. 54 1
      lib_stov/helpers.py
  2. 2 44
      stov

+ 54 - 1
lib_stov/helpers.py

@@ -23,6 +23,9 @@ import os
 import sys
 import signal
 import argparse
+import logging
+
+LOGGER = logging.getLogger("stov")
 
 
 def initialize_gettext():
@@ -59,7 +62,7 @@ def sighandler(signum, frame):
     elif signum == 15:
         print(_("Received SIGTERM, quitting..."), file=sys.stderr)
     os.killpg(os.getpid(), 1)
-    os.remove("/tmp/stov.lock")
+    remove_lock()
     sys.exit(0)
 
 
@@ -131,3 +134,53 @@ def parse_arguments():
                         help=_("disables the subscription with the "
                                "provided ID"))
     return parser
+
+
+def create_lock():
+    """
+    Creates the lock file for stov.
+    """
+
+    if os.access("/tmp/stov.lock", os.F_OK):
+        try:
+            lock_file = open("/tmp/stov.lock", "r")
+        except IOError:
+            LOGGER.error(_("The lock file could not be opened, please "
+                           "check that it exists and is readable, "
+                           "quitting now"))
+            sys.exit(1)
+        old_pid = lock_file.read().strip()
+        if os.access("/proc/" + old_pid, os.F_OK):
+            LOGGER.error(_("The lock file already exists, probably another "
+                           "instance of this program is already running\n"
+                           "if you are sure this is not the case, delete it "
+                           "manually and try again!"))
+            sys.exit(1)
+        lock_file.close()
+        if not os.access("/proc/" + old_pid, os.F_OK):
+            try:
+                os.remove("/tmp/stov.lock")
+            except os.error:
+                LOGGER.error(_("The old lock file could not be deleted!"))
+    try:
+        LOCK_FILE = open("/tmp/stov.lock", "w")
+        LOCK_FILE.write(str(os.getpid()))
+        LOCK_FILE.close()
+    except IOError:
+        print(_("The lock file could not be created, please check that /tmp"
+                " is writable and properly configured, quitting now."),
+              file=sys.stderr)
+        sys.exit(1)
+
+
+def remove_lock():
+    """
+    Removes the lock file when exiting the application.
+    """
+    try:
+        os.remove("/tmp/stov.lock")
+        sys.exit(0)
+    except os.error:
+        LOGGER.error(_("Could not delete the lock file. Please check what "
+                       "went wrong and clean up manually!"))
+        sys.exit(1)

+ 2 - 44
stov

@@ -44,42 +44,7 @@ helpers.initialize_gettext()
 helpers.setup_sighandler()
 PARSER = helpers.parse_arguments()
 ARGUMENTS = PARSER.parse_args()
-
-# Create the lock file which is used to determine if another instance is
-# already running by chance, the program shouldn't be run in this case since
-# we want to prevent concurent access to the database.
-
-if os.access("/tmp/stov.lock", os.F_OK):
-    try:
-        LOCK_FILE = open("/tmp/stov.lock", "r")
-    except IOError:
-        print(_("The lock file could not be opened, please check that "
-                "it exists and is readable, quitting now"),
-              file=sys.stderr)
-        sys.exit(1)
-    OLD_PID = LOCK_FILE.read().strip()
-    if os.access("/proc/" + OLD_PID, os.F_OK):
-        print(_("The lock file already exists, probably another "
-                "instance of this program is already running\n"
-                "if you are sure this is not the case, delete it"
-                " manually and try again!"), file=sys.stderr)
-        sys.exit(1)
-    LOCK_FILE.close()
-    if os.access("/proc/" + OLD_PID, os.F_OK) is not True:
-        try:
-            os.remove("/tmp/stov.lock")
-        except os.error:
-            print(_("The old lock file could not be deleted!"),
-                  file=sys.stderr)
-try:
-    LOCK_FILE = open("/tmp/stov.lock", "w")
-    LOCK_FILE.write(str(os.getpid()))
-    LOCK_FILE.close()
-except IOError:
-    print(_("The lock file could not be created, please check that /tmp"
-            " is writable and properly configured, quitting now."),
-          file=sys.stderr)
-    sys.exit(1)
+helpers.create_lock()
 
 # Check if the configuration directory exists and is writeable. If it
 # doesnt, create it using the configuration class.
@@ -533,12 +498,5 @@ elif ARGUMENTS.version is True:
 else:
     PARSER.print_help()
 
-# Remove the lock file and end the program so it can be run again
 
-try:
-    os.remove("/tmp/stov.lock")
-    sys.exit(0)
-except os.error:
-    LOGGER.error(_("Could not delete the lock file. Please check what "
-                   "went wrong and clean up manually!"))
-    sys.exit(1)
+helpers.remove_lock()