A Wikipedia user script to automate common TfD operations
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. """
  24. This Python script updates the on-wiki copy of tfdclerk.
  25. You need to have EarwigBot and GitPython installed:
  26. $ pip install earwigbot gitpython
  27. Then, simply:
  28. $ python release.py
  29. """
  30. from __future__ import print_function, unicode_literals
  31. from cookielib import LWPCookieJar, LoadError
  32. import errno
  33. from getpass import getpass
  34. from os import chmod, path
  35. import stat
  36. from sys import argv
  37. import time
  38. from urllib import urlencode
  39. import earwigbot
  40. import git
  41. SCRIPT_SITE = "en.wikipedia.org"
  42. SCRIPT_USER = "The Earwig"
  43. SCRIPT_FILE = "tfdclerk.js"
  44. COOKIE_FILE = ".cookies"
  45. REPLACE_TAG = "@TFDCLERK_{tag}@"
  46. EDIT_SUMMARY = "Updating script with latest version. ({version})"
  47. SCRIPT_PAGE = "User:{user}/{file}".format(user=SCRIPT_USER, file=SCRIPT_FILE)
  48. SCRIPT_ROOT = path.dirname(path.abspath(__file__))
  49. REPO = git.Repo(SCRIPT_ROOT)
  50. if len(argv) > 1 and argv[1].lstrip("-").startswith("t"):
  51. SCRIPT_SITE = "test.wikipedia.org"
  52. def _is_clean():
  53. """
  54. Return whether there are uncommitted changes in the working directory.
  55. """
  56. return not REPO.git.status(porcelain=True, untracked_files="no")
  57. def _get_version():
  58. """
  59. Return the current script version as a hex ID.
  60. """
  61. return REPO.commit().hexsha[:10]
  62. def _get_full_version():
  63. """
  64. Return the current script version as a human-readable string.
  65. """
  66. date = time.gmtime(REPO.commit().committed_date)
  67. datefmt = time.strftime("%H:%M, %-d %B %Y (UTC)", date)
  68. return "{hash} ({date})".format(hash=_get_version(), date=datefmt)
  69. def _get_script():
  70. """
  71. Return the complete script.
  72. """
  73. with open(path.join(SCRIPT_ROOT, SCRIPT_FILE), "r") as fp:
  74. text = fp.read().decode("utf8")
  75. replacements = {
  76. "VERSION": _get_version(),
  77. "VERSION_FULL": _get_full_version()
  78. }
  79. for tag, value in replacements.iteritems():
  80. text = text.replace(REPLACE_TAG.format(tag=tag), value)
  81. return text
  82. def _get_cookiejar():
  83. """
  84. Return a cookiejar to store the user's login info in.
  85. """
  86. cookiejar = LWPCookieJar(COOKIE_FILE)
  87. try:
  88. cookiejar.load()
  89. except LoadError:
  90. pass
  91. except IOError as err:
  92. if err.errno != errno.ENOENT:
  93. raise
  94. open(COOKIE_FILE, "w").close()
  95. chmod(COOKIE_FILE, stat.S_IRUSR|stat.S_IWUSR)
  96. return cookiejar
  97. def _get_site():
  98. """
  99. Return the EarwigBot Site object where the script will be saved.
  100. This is hacky, but it allows us to upload the script without storing the
  101. user's password in a config file like EarwigBot normally does.
  102. """
  103. site = earwigbot.wiki.Site(
  104. base_url="https://" + SCRIPT_SITE, script_path="/w",
  105. cookiejar=_get_cookiejar(), assert_edit="user")
  106. logged_in_as = site._get_username_from_cookies()
  107. if not logged_in_as or logged_in_as != SCRIPT_USER:
  108. password = getpass("Password for {user}: ".format(user=SCRIPT_USER))
  109. site._login((SCRIPT_USER, password))
  110. return site
  111. def main():
  112. """
  113. Main entry point for script.
  114. """
  115. if not _is_clean():
  116. print("Uncommitted changes in working directory. Stopping.")
  117. exit(1)
  118. print("Uploading script to [[{page}]] on {site}...".format(
  119. page=SCRIPT_PAGE, site=SCRIPT_SITE))
  120. script = _get_script()
  121. site = _get_site()
  122. page = site.get_page(SCRIPT_PAGE)
  123. summary = EDIT_SUMMARY.format(version=_get_version())
  124. page.edit(script, summary, minor=False, bot=False)
  125. params = {
  126. "title": page.title.replace(" ", "_"),
  127. "oldid": "prev",
  128. "diff": "cur"
  129. }
  130. print("Done!")
  131. print(site.url + "/w/index.php?" + urlencode(params))
  132. if __name__ == "__main__":
  133. main()