Ladsgroup has uploaded a new change for review.
https://gerrit.wikimedia.org/r/172212
Change subject: Make pwb.py executable and add entry point to setuptools
......................................................................
Make pwb.py executable and add entry point to setuptools
That let people use bash alias for their scripts and they can command
pywikibot redirect double
instead of
python /path_to_pywikibot/pwb.py redirect double
almost all of python based packages are using this
Merge this or I09de6215f0d7174892c1f3f0cf7c739ea3b40813 not both
P.S. It's not a bad idea to merge both ;)
Change-Id: I4fc0aae1966a0535ce9b842656372c9d7d77bd09
---
M pwb.py
M setup.py
2 files changed, 97 insertions(+), 90 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/12/172212/1
diff --git a/pwb.py b/pwb.py
index 29ddc24..4bf4700 100644
--- a/pwb.py
+++ b/pwb.py
@@ -82,102 +82,104 @@
sys.path[0] = old_path0
pwb.argvu = old_argvu
-#### end of snippet
-if sys.version_info[0] not in (2, 3):
- raise RuntimeError("ERROR: Pywikibot only runs under Python 2 "
- "or Python 3")
-version = tuple(sys.version_info)[:3]
-if version < (2, 6, 5):
- raise RuntimeError("ERROR: Pywikibot only runs under Python 2.6.5 "
- "or higher")
-if version >= (3, ) and version < (3, 3):
- raise RuntimeError("ERROR: Pywikibot only runs under Python 3.3 "
- "or higher")
+def main():
+ if sys.version_info[0] not in (2, 3):
+ raise RuntimeError("ERROR: Pywikibot only runs under Python 2 "
+ "or Python 3")
+ version = tuple(sys.version_info)[:3]
+ if version < (2, 6, 5):
+ raise RuntimeError("ERROR: Pywikibot only runs under Python 2.6.5 "
+ "or higher")
+ if version >= (3, ) and version < (3, 3):
+ raise RuntimeError("ERROR: Pywikibot only runs under Python 3.3 "
+ "or higher")
-rewrite_path = os.path.dirname(sys.argv[0])
-if not os.path.isabs(rewrite_path):
- rewrite_path = os.path.abspath(os.path.join(os.curdir, rewrite_path))
+ rewrite_path = os.path.dirname(sys.argv[0])
+ if not os.path.isabs(rewrite_path):
+ rewrite_path = os.path.abspath(os.path.join(os.curdir, rewrite_path))
-sys.path = [sys.path[0], rewrite_path,
- os.path.join(rewrite_path, 'pywikibot', 'compat'),
- os.path.join(rewrite_path, 'externals')
- ] + sys.path[1:]
+ sys.path = [sys.path[0], rewrite_path,
+ os.path.join(rewrite_path, 'pywikibot', 'compat'),
+ os.path.join(rewrite_path, 'externals')
+ ] + sys.path[1:]
-# try importing the known externals, and raise an error if they are not found
-try:
- import httplib2
- if not hasattr(httplib2, '__version__'):
- print("httplib2 import problem: httplib2.__version__ does not exist.")
- if sys.version_info > (3, 3):
- print("Python 3.4+ has probably loaded externals/httplib2 "
- "although it doesnt have an __init__.py.")
- httplib2 = None
-except ImportError as e:
- print("ImportError: %s" % e)
- httplib2 = None
-
-if not httplib2:
- print("Python module httplib2 >= 0.6.0 is required.")
- print("Did you clone without --recursive?\n"
- "Try running 'git submodule update --init' "
- "or 'pip install httplib2'.")
- sys.exit(1)
-
-# httplib2 0.6.0 was released with __version__ as '$Rev$'
-# and no module variable CA_CERTS.
-if httplib2.__version__ == '$Rev$' and 'CA_CERTS' not in httplib2.__dict__:
- httplib2.__version__ = '0.6.0'
-from distutils.version import StrictVersion
-if StrictVersion(httplib2.__version__) < StrictVersion("0.6.0"):
- print("Python module httplib2 (%s) needs to be 0.6.0 or greater." %
- httplib2.__file__)
- print("Did you clone without --recursive?\n"
- "Try running 'git submodule update --init' "
- "or 'pip install --upgrade httplib2'.")
- sys.exit(1)
-
-del httplib2
-
-if sys.version_info[0] == 2 and sys.version_info[1] == 6:
+ # try importing the known externals, and raise an error if they are not
found
try:
- import ordereddict
- del ordereddict
+ import httplib2
+ if not hasattr(httplib2, '__version__'):
+ print("httplib2 import problem: httplib2.__version__ does not
exist.")
+ if sys.version_info > (3, 3):
+ print("Python 3.4+ has probably loaded externals/httplib2 "
+ "although it doesnt have an __init__.py.")
+ httplib2 = None
except ImportError as e:
print("ImportError: %s" % e)
- print("pywikibot depends on module ordereddict in Python 2.6.")
- print("Upgrade to Python 2.7, or run 'pip install ordereddict'")
+ httplib2 = None
+
+ if not httplib2:
+ print("Python module httplib2 >= 0.6.0 is required.")
+ print("Did you clone without --recursive?\n"
+ "Try running 'git submodule update --init' "
+ "or 'pip install httplib2'.")
sys.exit(1)
-# Search for user-config.py before creating one.
-try:
- # 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
- print("NOTE: 'user-config.py' was not found!")
- print("Please follow the prompts to create it:")
- run_python_file('generate_user_files.py',
- ['generate_user_files.py'] + sys.argv[1:],
- [])
- sys.exit(1)
+ # httplib2 0.6.0 was released with __version__ as '$Rev$'
+ # and no module variable CA_CERTS.
+ if httplib2.__version__ == '$Rev$' and 'CA_CERTS' not in httplib2.__dict__:
+ httplib2.__version__ = '0.6.0'
+ from distutils.version import StrictVersion
+ if StrictVersion(httplib2.__version__) < StrictVersion("0.6.0"):
+ print("Python module httplib2 (%s) needs to be 0.6.0 or greater." %
+ httplib2.__file__)
+ print("Did you clone without --recursive?\n"
+ "Try running 'git submodule update --init' "
+ "or 'pip install --upgrade httplib2'.")
+ sys.exit(1)
-if len(sys.argv) > 1:
- tryimport_pwb()
- fn = sys.argv[1]
- argv = sys.argv[1:]
- argvu = pwb.argvu[1:]
- if not fn.endswith('.py'):
- fn += '.py'
- if not os.path.exists(fn):
- testpath = os.path.join(os.path.split(__file__)[0], 'scripts', fn)
- if os.path.exists(testpath):
- fn = testpath
- else:
- raise OSError("%s not found!" % fn)
- run_python_file(fn, argv, argvu)
-elif __name__ == "__main__":
- print(__doc__)
+ del httplib2
+
+ if sys.version_info[0] == 2 and sys.version_info[1] == 6:
+ try:
+ import ordereddict
+ del ordereddict
+ except ImportError as e:
+ print("ImportError: %s" % e)
+ print("pywikibot depends on module ordereddict in Python 2.6.")
+ print("Upgrade to Python 2.7, or run 'pip install ordereddict'")
+ sys.exit(1)
+
+ # Search for user-config.py before creating one.
+ try:
+ # 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
+ print("NOTE: 'user-config.py' was not found!")
+ print("Please follow the prompts to create it:")
+ run_python_file('generate_user_files.py',
+ ['generate_user_files.py'] + sys.argv[1:],
+ [])
+ sys.exit(1)
+
+ if len(sys.argv) > 1:
+ tryimport_pwb()
+ fn = sys.argv[1]
+ argv = sys.argv[1:]
+ argvu = pwb.argvu[1:]
+ if not fn.endswith('.py'):
+ fn += '.py'
+ if not os.path.exists(fn):
+ testpath = os.path.join(os.path.split(__file__)[0], 'scripts', fn)
+ if os.path.exists(testpath):
+ fn = testpath
+ else:
+ raise OSError("%s not found!" % fn)
+ run_python_file(fn, argv, argvu)
+ elif __name__ == "__main__":
+ print(__doc__)
+
+main()
diff --git a/setup.py b/setup.py
index 4fdacda..454483f 100644
--- a/setup.py
+++ b/setup.py
@@ -116,5 +116,10 @@
'Environment :: Console',
'Programming Language :: Python :: 2.7'
],
- use_2to3=False
+ use_2to3=False,
+ entry_points={
+ 'console_scripts': [
+ 'pywikibot = pwb:main',
+ ]
+ }
)
--
To view, visit https://gerrit.wikimedia.org/r/172212
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4fc0aae1966a0535ce9b842656372c9d7d77bd09
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits