jenkins-bot has submitted this change and it was merged.

Change subject: Bug 72680 - 'user-config.py' not found if runs with cron
......................................................................


Bug 72680 - 'user-config.py' not found if runs with cron

Changed search order for user-config.py.
See also bug 72918.

New order:
1. -dir: argument
2. PYWIKIBOT2_DIR
3. .
4. pwb.py directory
5. 'pywikibot' folder under:
      'Application Data' or 'AppData\Roaming' (Windows) or
      '.pywikibot' directory (Unix and similar) under the user's home

Also code improvements:
- in search logic
- removed TODO: Raise exception on not handled windows version

Bug: 72680
Change-Id: Id23afcdd2fa1a17710ca786e69c6ea985b86e870
---
M pwb.py
M pywikibot/config2.py
2 files changed, 22 insertions(+), 10 deletions(-)

Approvals:
  Merlijn van Deen: Looks good to me, approved
  jenkins-bot: Verified

Objections:
  Steinsplitter: There's a problem with this change, please improve



diff --git a/pwb.py b/pwb.py
index 3a53ef2..29ddc24 100644
--- a/pwb.py
+++ b/pwb.py
@@ -151,7 +151,10 @@
 
 # Search for user-config.py before creating one.
 try:
-    # user-config.py already exists
+    # If successful, user-config.py already exists in one of the candidate
+    # directories. See config2.py for details on search order.
+    # Use env var to communicate to config2.py pwb.py location (bug 72918).
+    os.environ['PYWIKIBOT2_DIR_PWB'] = os.path.split(__file__)[0]
     import pywikibot  # noqa
 except RuntimeError as err:
     # user-config.py to be created
diff --git a/pywikibot/config2.py b/pywikibot/config2.py
index 4d74265..e24ebca 100644
--- a/pywikibot/config2.py
+++ b/pywikibot/config2.py
@@ -138,11 +138,13 @@
 
     This is determined in the following order:
      1.  If the script was called with a -dir: argument, use the directory
-         provided in this argument
+         provided in this argument.
      2.  If the user has a PYWIKIBOT2_DIR environment variable, use the value
-         of it
-     3.  If user-config presents in current directory, use the current 
directory
-     4.  Use (and if necessary create) a 'pywikibot' folder under
+         of it.
+     3.  If user-config is present in current directory, use the current
+         directory.
+     4.  If user-config is present in pwb.py directory, use that directory
+     5.  Use (and if necessary create) a 'pywikibot' folder under
          'Application Data' or 'AppData\Roaming' (Windows) or
          '.pywikibot' directory (Unix and similar) under the user's home
          directory.
@@ -176,10 +178,14 @@
             base_dir = os.path.expanduser(base_dir)
             break
     else:
-        if 'PYWIKIBOT2_DIR' in os.environ:
-            base_dir = os.environ['PYWIKIBOT2_DIR']
+        if ('PYWIKIBOT2_DIR' in os.environ and
+                exists(os.path.abspath(os.environ['PYWIKIBOT2_DIR']))):
+            base_dir = os.path.abspath(os.environ['PYWIKIBOT2_DIR'])
         elif exists('.'):
-            return os.path.abspath('.')
+            base_dir = os.path.abspath('.')
+        elif ('PYWIKIBOT2_DIR_PWB' in os.environ and
+                exists(os.path.abspath(os.environ['PYWIKIBOT2_DIR_PWB']))):
+            base_dir = os.path.abspath(os.environ['PYWIKIBOT2_DIR_PWB'])
         else:
             base_dir_cand = []
             home = os.path.expanduser("~")
@@ -190,10 +196,11 @@
                     sub_dir = ["Application Data"]
                 elif win_version == 6:
                     sub_dir = ["AppData", "Roaming"]
+                else:
+                    raise WindowsError(u'Windows version %s not supported yet.'
+                                       % win_version)
                 base_dir_cand.extend([[home] + sub_dir + [DIRNAME_WIN],
                                      [home] + sub_dir + [DIRNAME_WIN_FBCK]])
-                #TODO: Throw exception otherwise to notify the user that the
-                #      version of Windows is not (yet) supported
             else:
                 base_dir_cand.append([home, DIRNAME_UNIX])
 
@@ -203,12 +210,14 @@
                     os.makedirs(dir, mode=0o700)
                 if exists(dir):
                     base_dir = dir
+                    break
 
     if not os.path.isabs(base_dir):
         base_dir = os.path.normpath(os.path.join(os.getcwd(), base_dir))
     # make sure this path is valid and that it contains user-config file
     if not os.path.isdir(base_dir):
         raise RuntimeError("Directory '%s' does not exist." % base_dir)
+    # check if user-config.py is in base_dir
     if not exists(base_dir):
         exc_text = "No user-config.py found in directory '%s'.\n" % base_dir
         if os.environ.get('PYWIKIBOT2_NO_USER_CONFIG', '0') == '1':

-- 
To view, visit https://gerrit.wikimedia.org/r/172096
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Id23afcdd2fa1a17710ca786e69c6ea985b86e870
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Mpaa <[email protected]>
Gerrit-Reviewer: Steinsplitter <[email protected]>
Gerrit-Reviewer: XZise <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to