Browse Source

move code checking for youtube-dl to helpers

Helmut Pozimski 6 years ago
parent
commit
5c45d3f6e6
2 changed files with 34 additions and 31 deletions
  1. 33 2
      lib_stov/helpers.py
  2. 1 29
      stov

+ 33 - 2
lib_stov/helpers.py

@@ -25,6 +25,8 @@ import signal
 import argparse
 import logging
 
+from distutils.spawn import find_executable
+
 from lib_stov import stov_exceptions
 from lib_stov import configuration
 from lib_stov import database
@@ -233,7 +235,7 @@ def setup_configuration(args):
         try:
             logger.debug(
                 _("Comparing current and running configuration version."))
-            CHECK_RESULT = conf.check_config()
+            check_result = conf.check_config()
         except stov_exceptions.ConfigFileReadErrorException as error:
             logger.error(error)
             sys.exit(1)
@@ -241,7 +243,7 @@ def setup_configuration(args):
             logger.error(error)
             sys.exit(1)
         else:
-            if not CHECK_RESULT:
+            if not check_result:
                 logger.info(
                     _("Your configuration needs to be updated, performing"
                       " update now."))
@@ -306,3 +308,32 @@ def setup_database(conf):
         LOGGER.error(error)
         sys.exit(1)
     return db
+
+
+def find_youtubedl(conf):
+    """
+    Tries to find youtube-dl and writes it's path to the configuration file
+    :param conf: configuration object
+    :type conf: lib_stov.configuration.Conf
+    """
+    if conf.values["youtube-dl"] == "":
+        youtubedl_path = find_executable("youtube-dl")
+        conf.values["youtube-dl"] = youtubedl_path
+        if os.access(conf.values["youtube-dl"], os.F_OK & os.R_OK & os.X_OK):
+            LOGGER.info(_("Found youtube-dl, writing it's path to the "
+                          "configuration file."))
+            LOGGER.debug("Opening configuration file.")
+            try:
+                conf.write_config()
+            except stov_exceptions.ConfigFileWriteErrorException as error:
+                LOGGER.error(error)
+                sys.exit(1)
+        else:
+            LOGGER.error(
+                _("Could not find youtube-dl, it either does not exist, "
+                  "is not readable or not executable. Please note that "
+                  "youtube-dl is not needed for the program to run but "
+                  "is needed to use the download option which won't work "
+                  "otherwise. If youtube-dl isn't found automatically, "
+                  "you may also enter the path to it in the configuration"
+                  " file."))

+ 1 - 29
stov

@@ -26,9 +26,7 @@ everything together, performing all the actions a user wants.
 """
 
 import sys
-import os
 import smtplib
-import subprocess
 import socket
 import logging
 from email.mime.multipart import MIMEMultipart
@@ -47,33 +45,7 @@ helpers.create_lock()
 CONF = helpers.setup_configuration(ARGUMENTS)
 LOGGER = logging.getLogger("stov")
 DB = helpers.setup_database(CONF)
-
-# youtube-dl is really a dependency but the program will run with limited
-# functionality without it so we need to check that here
-
-
-if CONF.values["youtube-dl"] == "":
-    YOUTUBE_DL_PATH = subprocess.Popen(["which", "youtube-dl"],
-                                       stdout=subprocess.PIPE)\
-        .communicate()[0]
-    CONF.values["youtube-dl"] = YOUTUBE_DL_PATH.strip()
-    if os.access(CONF.values["youtube-dl"], os.F_OK & os.R_OK & os.X_OK):
-        LOGGER.info(_("Found youtube-dl, writing it's path to the "
-                      "configuration file."))
-        LOGGER.debug("Opening configuration file.")
-        try:
-            CONF.write_config()
-        except stov_exceptions.ConfigFileWriteErrorException as error:
-            LOGGER.error(error)
-            sys.exit(1)
-    else:
-        LOGGER.error(_("Could not find youtube-dl, it either does not exist, "
-                       "is not readable or not executable. Please note that "
-                       "youtube-dl is not needed for the program to run but "
-                       "is needed to use the download option which won't work "
-                       "otherwise. If youtube-dl isn't found automatically, "
-                       "you may also enter the path to it in the configuration"
-                       " file."))
+helpers.find_youtubedl(CONF)
 
 # Variable to save the text that is later sent as e-mail
 MAIL_CONTENT = []