jenkins-bot has submitted this change and it was merged.
Change subject: Search user-config.py in default dir before creating a new one ......................................................................
Search user-config.py in default dir before creating a new one
Also logs directory of config-user.py.
Change-Id: Ic8ac542919e18ad16458d21e0f9713ade899b964 --- M pwb.py M pywikibot/bot.py M pywikibot/config2.py 3 files changed, 47 insertions(+), 28 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pwb.py b/pwb.py index befa539..16efbca 100644 --- a/pwb.py +++ b/pwb.py @@ -1,11 +1,14 @@ # -*- coding: utf-8 -*- -"""wrapper script to use rewrite in 'directory' mode - run scripts using -python pwb.py <name_of_script> <options> +"""Wrapper script to use Pywikibot in 'directory' mode. + +Run scripts using: + + python pwb.py <name_of_script> <options>
and it will use the package directory to store all user files, will fix up search paths so the package does not need to be installed, etc. """ -# (C) Pywikibot team, 2013 +# (C) Pywikibot team, 2014 # # Distributed under the terms of the MIT license. # @@ -24,12 +27,15 @@
def tryimport_pwb(): - # See if we can import pywikibot. If so, we need to patch pwb.argvu, too. - # If pywikibot is not available, we create a mock object to remove the - # need for if statements further on. + """Try to import pywikibot. + + If so, we need to patch pwb.argvu, too. + If pywikibot is not available, we create a mock object to remove the + need for if statements further on. + """ global pwb try: - import pywikibot + import pywikibot # noqa pwb = pywikibot except RuntimeError: pwb = lambda: None @@ -143,21 +149,18 @@ print("Upgrade to Python 2.7, or run 'pip install ordereddict'") sys.exit(1)
-if "PYWIKIBOT2_DIR" not in os.environ: - os.environ["PYWIKIBOT2_DIR"] = os.path.split(__file__)[0] - -for i, x in enumerate(sys.argv): - if x.startswith("-dir:"): - os.environ["PYWIKIBOT2_DIR"] = x[5:] - sys.argv.pop(i) - break - -user_config_path = os.path.join(os.environ["PYWIKIBOT2_DIR"], "user-config.py") -if not os.path.exists(user_config_path): - print("NOTE: %s was not found" % user_config_path) +# Search for user-config.py before creating one. +try: + # user-config.py already exists + import pywikibot # noqa +except RuntimeError as err: + # user-config.py to be created + print("NOTE: 'user-config.py' was not found!") print("Please follow the prompts to create it:") - path = 'generate_user_files.py' - run_python_file(path, [path], [path.decode('ascii')]) + run_python_file('generate_user_files.py', + ['generate_user_files.py'] + sys.argv[1:], + []) + sys.exit(1)
if len(sys.argv) > 1: tryimport_pwb() diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 52a31c0..6386c82 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -280,6 +280,9 @@ if hasattr(os, 'uname'): log(u'SYSTEM: %s' % unicode(os.uname()))
+ # config file dir + log(u'CONFIG FILE DIR: %s' % pywikibot.config2.base_dir) + all_modules = sys.modules.keys()
# These are the main dependencies of pywikibot. diff --git a/pywikibot/config2.py b/pywikibot/config2.py index 55453dc..7316d9f 100644 --- a/pywikibot/config2.py +++ b/pywikibot/config2.py @@ -163,12 +163,16 @@
if test_directory is not None: test_directory = os.path.abspath(test_directory) - NAME = "pywikibot" + + DIRNAME_WIN = u"Pywikibot" + DIRNAME_WIN_FBCK = u"pywikibot" + DIRNAME_UNIX = u".pywikibot" + base_dir = "" for arg in sys.argv[1:]: if arg.startswith("-dir:"): base_dir = arg[5:] - sys.argv.remove(arg) + base_dir = os.path.expanduser(base_dir) break else: if 'PYWIKIBOT2_DIR' in os.environ: @@ -176,20 +180,29 @@ elif exists('.'): return os.path.abspath('.') else: + base_dir_cand = [] home = os.path.expanduser("~") if sys.platform == 'win32': import platform win_version = int(platform.version()[0]) if win_version == 5: - base_dir = os.path.join(home, "Application Data", NAME) + sub_dir = ["Application Data"] elif win_version == 6: - base_dir = os.path.join(home, "AppData\Roaming", NAME) + sub_dir = ["AppData", "Roaming"] + 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 = os.path.join(home, "." + NAME) - if not os.path.isdir(base_dir): - os.makedirs(base_dir, mode=0o700) + base_dir_cand.append([home, DIRNAME_UNIX]) + + for dir in base_dir_cand: + dir = os.path.join(*dir) + if not os.path.isdir(dir): + os.makedirs(dir, mode=0o700) + if exists(dir): + base_dir = dir + 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
pywikibot-commits@lists.wikimedia.org