Browse Source

search client_secrets also in share/local and HOME

Arnau Sanchez 10 years ago
parent
commit
9f7abceaf2
2 changed files with 13 additions and 4 deletions
  1. 8 0
      youtube_upload/lib.py
  2. 5 4
      youtube_upload/main.py

+ 8 - 0
youtube_upload/lib.py

@@ -1,3 +1,4 @@
+import os
 import sys
 import locale
 import random
@@ -53,6 +54,13 @@ def string_to_dict(string):
         pairs = [s.strip() for s in string.split(",")]
         return dict(pair.split("=") for pair in pairs)
 
+def get_first_existing_filename(prefixes, relative_path):
+    """Get the first existing filename of relative_path seeking on prefixes directories."""
+    for prefix in prefixes:
+        path = os.path.join(prefix, relative_path)
+        if os.path.exists(path):
+            return path
+
 def retriable_exceptions(fun, retriable_exceptions, max_retries=None):
     """Run function and retry on some exceptions (with exponential backoff)."""
     retry = 0

+ 5 - 4
youtube_upload/main.py

@@ -117,12 +117,13 @@ def run_main(parser, options, args, output=sys.stdout):
         parser.print_usage()
         msg = "Some required option are missing: %s" % ", ".join(missing)
         raise OptionsMissing(msg)
-
-    default_client_secrets = \
-        os.path.join(sys.prefix, "share/youtube_upload/client_secrets.json")
     home = os.path.expanduser("~")
+    default_client_secrets = lib.get_first_existing_filename(
+        [sys.prefix, os.path.join(sys.prefix, "local")],
+        "share/youtube_upload/client_secrets.json")  
     default_credentials = os.path.join(home, ".youtube-upload-credentials.json")
-    client_secrets = options.client_secrets or default_client_secrets
+    client_secrets = options.client_secrets or default_client_secrets or \
+        os.path.join(home, ".client_secrets.json")
     credentials = options.credentials_file or default_credentials
     debug("Using client secrets: {0}".format(client_secrets))
     debug("Using credentials file: {0}".format(credentials))