jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/450214 )
Change subject: Rename all PYWIKIBOT2_* environment variables to PYWIKIBOT_* ......................................................................
Rename all PYWIKIBOT2_* environment variables to PYWIKIBOT_*
pywikibot.config2.py: Normalize environment variables and issue a deprecation for the old names. By normalizing environment variable names in config2.py there is no need to check for old names in any other module that imports pywikibot first.
Use the new enviroment variable names in all files.
There are only two modules that do not import pywikibot prior to checking for PYWIKIBOT2_* variables: generate_family_file and generate_user_file.
generate_family_file.py: Create a new function, _import_with_no_user_config, that will import the requested module without loading user-config.py. This function automatically sets PYWIKIBOT_NO_USER_CONFIG=2 and restores its original value after the import is completed. Use the new functon to import MWSite as Wiki.
generate_user_file.py: Use the new function introduced in generate_family_file.py to import pywikibot without user-config.py file.
PYWIKIBOT2_USER_CONFIG was only used in appveyor.yml. Rename it without deprecation.
Bug: T184674 Change-Id: I316358e51a62d19a3a0d6e8f7a707fda7b83bff4 --- M .appveyor.yml M HISTORY.rst M docs/conf.py M generate_family_file.py M generate_user_files.py M pwb.py M pywikibot/config2.py M pywikibot/site_detect.py M scripts/version.py M tests/README.rst M tests/aspects.py M tests/pwb/print_env.py M tests/ui_tests.bat M tests/ui_tests.py M tox.ini 15 files changed, 78 insertions(+), 61 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/.appveyor.yml b/.appveyor.yml index c733ec3..f0d2506 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -9,8 +9,8 @@ # See: http://stackoverflow.com/a/13751649/163740 CMD_IN_ENV: "cmd /E:ON /V:ON /C .\appveyor\run_with_env.cmd"
- PYWIKIBOT2_DIR: "%appdata%\Pywikibot" - PYWIKIBOT2_USER_CONFIG: "%appdata%\Pywikibot\user-config.py" + PYWIKIBOT_DIR: "%appdata%\Pywikibot" + PYWIKIBOT_USER_CONFIG: "%appdata%\Pywikibot\user-config.py"
PYSETUP_TEST_EXTRAS: "1"
@@ -93,9 +93,9 @@ - chcp 65001 - set PYTHONIOENCODING=utf8
- - "mkdir %PYWIKIBOT2_DIR%" - - "python -Werror::UserWarning -m generate_user_files -dir:%PYWIKIBOT2_DIR% -family:wikipedia -lang:en -v -debug" - - ps: "[IO.File]::AppendAllText($env:PYWIKIBOT2_USER_CONFIG, 'max_retries = 2; maximum_GET_length = 5000; transliteration_target = None;')" + - "mkdir %PYWIKIBOT_DIR%" + - "python -Werror::UserWarning -m generate_user_files -dir:%PYWIKIBOT_DIR% -family:wikipedia -lang:en -v -debug" + - ps: "[IO.File]::AppendAllText($env:PYWIKIBOT_USER_CONFIG, 'max_retries = 2; maximum_GET_length = 5000; transliteration_target = None;')"
- set PYSETUP_TEST_NO_UI=1 - "%CMD_IN_ENV% coverage run setup.py test" diff --git a/HISTORY.rst b/HISTORY.rst index 83d69f2..09bd15b 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -4,6 +4,7 @@ Current release ---------------
+* Drop the '2' from PYWIKIBOT2_DIR, PYWIKIBOT2_DIR_PWB, and PYWIKIBOT2_NO_USER_CONFIG environment variables. The old names are now deprecated. The other PYWIKIBOT2_* variables which were used only for testing purposes have been renamed without deprecation. (T184674) * Bugfixes and improvements * Localisation updates
diff --git a/docs/conf.py b/docs/conf.py index 5c0e12f..3cf3d95 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -256,7 +256,7 @@
def pywikibot_env(): """Allow pywikibot modules to be imported without a user-config.py.""" - os.environ['PYWIKIBOT2_NO_USER_CONFIG'] = '1' + os.environ['PYWIKIBOT_NO_USER_CONFIG'] = '1'
def pywikibot_script_docstring_fixups(app, what, name, obj, options, lines): diff --git a/generate_family_file.py b/generate_family_file.py index 7009717..0f235cd 100755 --- a/generate_family_file.py +++ b/generate_family_file.py @@ -15,6 +15,7 @@ import os import sys
+from os import environ, getenv # creating & retrieving urls if sys.version_info[0] > 2: from urllib.parse import urlparse @@ -22,18 +23,6 @@ else: from urlparse import urlparse
-# Disable user-config checks so the family can be created first, -# and then used when generating the user-config -_orig_no_user_config = os.environ.get('PYWIKIBOT2_NO_USER_CONFIG') -os.environ['PYWIKIBOT2_NO_USER_CONFIG'] = '2' -from pywikibot.site_detect import MWSite as Wiki # noqa: E402 - -# Reset this flag in case another script is run by pwb after this script -if not _orig_no_user_config: - del os.environ['PYWIKIBOT2_NO_USER_CONFIG'] -else: - os.environ['PYWIKIBOT2_NO_USER_CONFIG'] = _orig_no_user_config -
class FamilyFileGenerator(object):
@@ -197,7 +186,25 @@ f.write(" }[code]\n")
+def _import_with_no_user_config(*import_args): + """Return __import__(*import_args) without loading user-config.py.""" + orig_no_user_config = getenv('PYWIKIBOT_NO_USER_CONFIG') or getenv( + 'PYWIKIBOT2_NO_USER_CONFIG') + environ['PYWIKIBOT_NO_USER_CONFIG'] = '2' + result = __import__(*import_args) + # Reset this flag + if not orig_no_user_config: + del environ['PYWIKIBOT_NO_USER_CONFIG'] + else: + environ['PYWIKIBOT_NO_USER_CONFIG'] = orig_no_user_config + return result + + if __name__ == "__main__": + # Disable user-config checks so the family can be created first, + # and then used when generating the user-config + Wiki = _import_with_no_user_config( + 'pywikibot.site_detect').site_detect.MWSite if len(sys.argv) != 3: print(""" Usage: {module} <url> <short name> diff --git a/generate_user_files.py b/generate_user_files.py index 0dbd20c..57dda8d 100755 --- a/generate_user_files.py +++ b/generate_user_files.py @@ -17,18 +17,11 @@ from textwrap import fill from warnings import warn
+from generate_family_file import _import_with_no_user_config + # Disable user-config usage as we are creating it here -_orig_no_user_config = os.environ.get('PYWIKIBOT2_NO_USER_CONFIG') -os.environ['PYWIKIBOT2_NO_USER_CONFIG'] = '2' -import pywikibot # noqa: E402 -from pywikibot import config, __url__ # noqa: E402 - -# Reset this flag in case another script is run by pwb after this script -if not _orig_no_user_config: - del os.environ['PYWIKIBOT2_NO_USER_CONFIG'] -else: - os.environ['PYWIKIBOT2_NO_USER_CONFIG'] = _orig_no_user_config - +pywikibot = _import_with_no_user_config('pywikibot') +config, __url__ = pywikibot.config2, pywikibot.__url__ base_dir = pywikibot.config2.base_dir
try: @@ -77,7 +70,7 @@ msg = fill("""WARNING: Your user files will be created in the directory '%(new_base)s' you have chosen. To access these files, you will either have to use the argument "-dir:%(new_base)s" every time you run the bot, or set -the environment variable "PYWIKIBOT2_DIR" equal to this directory name in +the environment variable "PYWIKIBOT_DIR" equal to this directory name in your operating system. See your operating system documentation for how to set environment variables.""" % {'new_base': new_base}, width=76) pywikibot.output(msg) diff --git a/pwb.py b/pwb.py index 5270790..371b9e0 100755 --- a/pwb.py +++ b/pwb.py @@ -71,7 +71,7 @@ import pywikibot except RuntimeError: remove_modules() - os.environ['PYWIKIBOT2_NO_USER_CONFIG'] = '2' + os.environ['PYWIKIBOT_NO_USER_CONFIG'] = '2' import pywikibot pwb = pywikibot
@@ -169,7 +169,7 @@ _pwb_dir = os.path.split(__file__)[0] if sys.platform == 'win32' and sys.version_info[0] < 3: _pwb_dir = str(_pwb_dir) - os.environ[str('PYWIKIBOT2_DIR_PWB')] = _pwb_dir + os.environ['PYWIKIBOT_DIR_PWB'] = _pwb_dir import pywikibot pwb = pywikibot except RuntimeError: diff --git a/pywikibot/config2.py b/pywikibot/config2.py index 7f16263..552f060 100644 --- a/pywikibot/config2.py +++ b/pywikibot/config2.py @@ -4,15 +4,15 @@
User preferences are loaded from a python file called user-config.py, which may be located in directory specified by the environment variable -PYWIKIBOT2_DIR, or the same directory as pwb.py, or in a directory within +PYWIKIBOT_DIR, or the same directory as pwb.py, or in a directory within the users home. See get_base_dir for more information.
If user-config.py can not be found in any of those locations, this module -will fail to load unless the environment variable PYWIKIBOT2_NO_USER_CONFIG -is set to a value other than '0'. i.e. PYWIKIBOT2_NO_USER_CONFIG=1 will +will fail to load unless the environment variable PYWIKIBOT_NO_USER_CONFIG +is set to a value other than '0'. i.e. PYWIKIBOT_NO_USER_CONFIG=1 will allow config to load without a user-config.py. However, warnings will be shown if user-config.py was not loaded. -To prevent these warnings, set PYWIKIBOT2_NO_USER_CONFIG=2. +To prevent these warnings, set PYWIKIBOT_NO_USER_CONFIG=2.
Provides two functions to register family classes which can be used in the user-config: @@ -49,12 +49,13 @@
from distutils.version import StrictVersion from locale import getdefaultlocale +from os import getenv, environ from warnings import warn
from requests import __version__ as requests_version
from pywikibot.logging import error, output, warning -from pywikibot.tools import PY2 +from pywikibot.tools import PY2, issue_deprecation_warning
OSWIN32 = (sys.platform == 'win32')
@@ -65,12 +66,27 @@ import _winreg as winreg
+# Normalize old PYWIKIBOT2 environment variables and issue a deprecation warn. +for env_name in ( + 'PYWIKIBOT2_DIR', 'PYWIKIBOT2_DIR_PWB', 'PYWIKIBOT2_NO_USER_CONFIG', +): + if env_name not in environ: + continue + env_value = environ[env_name] + new_env_name = env_name.replace('PYWIKIBOT2_', 'PYWIKIBOT_') + del environ[env_name] + if new_env_name not in environ: + environ[new_env_name] = env_value + issue_deprecation_warning( + env_name + ' environment variable', new_env_name, 0, since='20180803') + + # This frozen set should contain all imported modules/variables, so it must # occur directly after the imports. At that point globals() only contains the # names and some magic variables (like __name__) _imports = frozenset(name for name in globals() if not name.startswith('_'))
-__no_user_config = os.environ.get('PYWIKIBOT2_NO_USER_CONFIG') +__no_user_config = getenv('PYWIKIBOT_NO_USER_CONFIG') if __no_user_config == '0': __no_user_config = None
@@ -281,7 +297,7 @@ This is determined in the following order: 1. If the script was called with a -dir: argument, use the directory provided in this argument. - 2. If the user has a PYWIKIBOT2_DIR environment variable, use the value + 2. If the user has a PYWIKIBOT_DIR environment variable, use the value of it. 3. If user-config is present in current directory, use the current directory. @@ -291,7 +307,7 @@ '.pywikibot' directory (Unix and similar) under the user's home directory.
- Set PYWIKIBOT2_NO_USER_CONFIG=1 to disable loading user-config.py + Set PYWIKIBOT_NO_USER_CONFIG=1 to disable loading user-config.py
@param test_directory: Assume that a user config file exists in this directory. Used to test whether placing a user config file in this @@ -316,14 +332,14 @@ base_dir = os.path.expanduser(base_dir) break else: - if ('PYWIKIBOT2_DIR' in os.environ and - exists(os.path.abspath(os.environ['PYWIKIBOT2_DIR']))): - base_dir = os.path.abspath(os.environ['PYWIKIBOT2_DIR']) + if ('PYWIKIBOT_DIR' in environ and + exists(os.path.abspath(environ['PYWIKIBOT_DIR']))): + base_dir = os.path.abspath(environ['PYWIKIBOT_DIR']) elif exists('.'): 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']) + elif ('PYWIKIBOT_DIR_PWB' in environ and + exists(os.path.abspath(environ['PYWIKIBOT_DIR_PWB']))): + base_dir = os.path.abspath(environ['PYWIKIBOT_DIR_PWB']) else: base_dir_cand = [] home = os.path.expanduser("~") diff --git a/pywikibot/site_detect.py b/pywikibot/site_detect.py index f1a0bfc..a591448 100644 --- a/pywikibot/site_detect.py +++ b/pywikibot/site_detect.py @@ -173,7 +173,7 @@ self.private_wiki = ('error' in info and info['error']['code'] == 'readapidenied') if self.private_wiki: - # user-config.py is not loaded because PYWIKIBOT2_NO_USER_CONFIG + # user-config.py is not loaded because PYWIKIBOT_NO_USER_CONFIG # is set to '2' by generate_family_file.py. # Prepare a temporary config for login. username = pywikibot.input( diff --git a/scripts/version.py b/scripts/version.py index f01897d..8819bc3 100755 --- a/scripts/version.py +++ b/scripts/version.py @@ -72,9 +72,9 @@ pywikibot.output('Toolforge hostname: {0}'.format( toolforge_env_hostname))
- check_environ('PYWIKIBOT2_DIR') - check_environ('PYWIKIBOT2_DIR_PWB') - check_environ('PYWIKIBOT2_NO_USER_CONFIG') + check_environ('PYWIKIBOT_DIR') + check_environ('PYWIKIBOT_DIR_PWB') + check_environ('PYWIKIBOT_NO_USER_CONFIG') pywikibot.output('Config base dir: {0}'.format(pywikibot.config2.base_dir)) for family, usernames in pywikibot.config2.usernames.items(): if usernames: diff --git a/tests/README.rst b/tests/README.rst index c201486..41650d9 100644 --- a/tests/README.rst +++ b/tests/README.rst @@ -190,13 +190,13 @@ 4. go to https://circleci.com/gh/<username>/pywikibot/edit#env-vars and add the following variables:
- - PYWIKIBOT2_NO_USER_CONFIG=2 + - PYWIKIBOT_NO_USER_CONFIG=2 - TOXENV=py27,py34
5. push changes into the forked git repository 6. watch the build at https://circleci.com/gh/<username>/pywikibot
-PYWIKIBOT2_NO_USER_CONFIG=2 is needed because 'python setup.py test' is run. +PYWIKIBOT_NO_USER_CONFIG=2 is needed because 'python setup.py test' is run.
TOXENV=py27,py34 is a workaround because CircleCI runs 'tox', but there is a bug in the CircleCI default 'py26' implementation. diff --git a/tests/aspects.py b/tests/aspects.py index 5d1e27d..9cff787 100644 --- a/tests/aspects.py +++ b/tests/aspects.py @@ -1495,19 +1495,19 @@ """Prepare the environment for running the pwb.py script.""" super(PwbTestCase, self).setUp() self.orig_pywikibot_dir = None - if 'PYWIKIBOT2_DIR' in os.environ: - self.orig_pywikibot_dir = os.environ['PYWIKIBOT2_DIR'] + if 'PYWIKIBOT_DIR' in os.environ: + self.orig_pywikibot_dir = os.environ['PYWIKIBOT_DIR'] base_dir = pywikibot.config.base_dir if OSWIN32 and PY2: base_dir = str(base_dir) - os.environ[str('PYWIKIBOT2_DIR')] = base_dir + os.environ[str('PYWIKIBOT_DIR')] = base_dir
def tearDown(self): """Restore the environment after running the pwb.py script.""" super(PwbTestCase, self).tearDown() - del os.environ['PYWIKIBOT2_DIR'] + del os.environ['PYWIKIBOT_DIR'] if self.orig_pywikibot_dir: - os.environ[str('PYWIKIBOT2_DIR')] = self.orig_pywikibot_dir + os.environ[str('PYWIKIBOT_DIR')] = self.orig_pywikibot_dir
def _execute(self, args, data_in=None, timeout=0, error=None): site = self.get_site() diff --git a/tests/pwb/print_env.py b/tests/pwb/print_env.py index b6ea59b..72ffd90 100644 --- a/tests/pwb/print_env.py +++ b/tests/pwb/print_env.py @@ -23,7 +23,7 @@ if k == 'USER_PASSWORD': continue # This only appears in subprocesses - if k in ['PYWIKIBOT2_DIR_PWB']: + if k == 'PYWIKIBOT_DIR_PWB': continue print("%r: %r" % (k, v))
diff --git a/tests/ui_tests.bat b/tests/ui_tests.bat index c497d6e..68da2b6 100644 --- a/tests/ui_tests.bat +++ b/tests/ui_tests.bat @@ -1,4 +1,4 @@ @echo off set PYTHONPATH=%~dp0..;%~dp0../externals -set PYWIKIBOT2_DIR=%~dp0.. +set PYWIKIBOT_DIR=%~dp0.. "%~dp0ui_tests.py" diff --git a/tests/ui_tests.py b/tests/ui_tests.py index 84d93fe..9576edc 100644 --- a/tests/ui_tests.py +++ b/tests/ui_tests.py @@ -8,7 +8,7 @@ # NOTE FOR RUNNING WINDOWS UI TESTS # # Windows UI tests have to be run using the tests\ui_tests.bat helper script. -# This will set PYTHONPATH and PYWIKIBOT2_DIR, and then run the tests. Do not +# This will set PYTHONPATH and PYWIKIBOT_DIR, and then run the tests. Do not # touch mouse or keyboard while the tests are running, as this might disturb the # interaction tests. # diff --git a/tox.ini b/tox.ini index 84bbe40..f6064dc 100644 --- a/tox.ini +++ b/tox.ini @@ -16,7 +16,7 @@ [testenv] setenv = VIRTUAL_ENV={envdir} - PYWIKIBOT2_NO_USER_CONFIG=2 + PYWIKIBOT_NO_USER_CONFIG=2 usedevelop = True commands = python setup.py test
pywikibot-commits@lists.wikimedia.org