A Wikipedia user script to automate common TfD operations
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

202 行
6.3 KiB

  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. import re
  37. from sys import argv
  38. import time
  39. from urllib import urlencode
  40. import earwigbot
  41. import git
  42. SCRIPT_SITE = "en.wikipedia.org"
  43. SCRIPT_TEST = "test.wikipedia.org"
  44. SCRIPT_USER = "The Earwig"
  45. SCRIPT_FILE = "tfdclerk.js"
  46. SCRIPT_SDIR = "src"
  47. COOKIE_FILE = ".cookies"
  48. REPLACE_TAG = "@TFDCLERK_{tag}@"
  49. EDIT_SUMMARY = "Updating script with latest version ({version})"
  50. SCRIPT_PAGE = "User:{user}/{file}".format(user=SCRIPT_USER, file=SCRIPT_FILE)
  51. SCRIPT_ROOT = path.dirname(path.abspath(__file__))
  52. REPO = git.Repo(SCRIPT_ROOT)
  53. def _is_clean():
  54. """
  55. Return whether there are uncommitted changes in the working directory.
  56. """
  57. return not REPO.git.status(porcelain=True, untracked_files="no")
  58. def _get_version():
  59. """
  60. Return the current script version as a hex ID.
  61. """
  62. return REPO.commit().hexsha[:10]
  63. def _get_full_version():
  64. """
  65. Return the current script version as a human-readable string.
  66. """
  67. date = time.gmtime(REPO.commit().committed_date)
  68. datefmt = time.strftime("%H:%M, %-d %B %Y (UTC)", date)
  69. return "{hash} ({date})".format(hash=_get_version(), date=datefmt)
  70. def _do_include(text, include):
  71. """
  72. Replace an include directive inside the script with a source file.
  73. """
  74. with open(path.join(SCRIPT_ROOT, SCRIPT_SDIR, include), "r") as fp:
  75. source = fp.read().decode("utf8")
  76. hs_tag = REPLACE_TAG.format(tag="HEADER_START")
  77. he_tag = REPLACE_TAG.format(tag="HEADER_END")
  78. if hs_tag in source and he_tag in source:
  79. lines = source.splitlines()
  80. head_start = [i for i, line in enumerate(lines) if hs_tag in line][0]
  81. head_end = [i for i, line in enumerate(lines) if he_tag in line][0]
  82. del lines[head_start:head_end + 1]
  83. source = "\n".join(lines)
  84. tag = REPLACE_TAG.format(tag="INCLUDE:" + include)
  85. if text[:text.index(tag)][-2:] == "\n\n" and source.startswith("\n"):
  86. source = source[1:] # Remove extra newline
  87. if include.endswith(".css"):
  88. lines = ['"' + line.ljust(75) + '" +'
  89. for line in source.strip().splitlines()]
  90. if lines and lines[-1]:
  91. lines[-1] = lines[-1][:-2] # Strip off last +
  92. source = "\n".join(lines)
  93. return text.replace(tag, source)
  94. def _get_script():
  95. """
  96. Return the complete script.
  97. """
  98. with open(path.join(SCRIPT_ROOT, SCRIPT_FILE), "r") as fp:
  99. text = fp.read().decode("utf8")
  100. re_include = REPLACE_TAG.format(tag=r"INCLUDE:(.*?)")
  101. includes = re.findall(re_include, text)
  102. for include in includes:
  103. text = _do_include(text, include)
  104. replacements = {
  105. "VERSION": _get_version(),
  106. "VERSION_FULL": _get_full_version()
  107. }
  108. for tag, value in replacements.iteritems():
  109. text = text.replace(REPLACE_TAG.format(tag=tag), value)
  110. return text
  111. def _get_cookiejar():
  112. """
  113. Return a cookiejar to store the user's login info in.
  114. """
  115. cookiejar = LWPCookieJar(COOKIE_FILE)
  116. try:
  117. cookiejar.load()
  118. except LoadError:
  119. pass
  120. except IOError as err:
  121. if err.errno != errno.ENOENT:
  122. raise
  123. open(COOKIE_FILE, "w").close()
  124. chmod(COOKIE_FILE, stat.S_IRUSR|stat.S_IWUSR)
  125. return cookiejar
  126. def _get_site(site_url=SCRIPT_SITE):
  127. """
  128. Return the EarwigBot Site object where the script will be saved.
  129. This is hacky, but it allows us to upload the script without storing the
  130. user's password in a config file like EarwigBot normally does.
  131. """
  132. site = earwigbot.wiki.Site(
  133. base_url="https://" + site_url, script_path="/w",
  134. cookiejar=_get_cookiejar(), assert_edit="user")
  135. logged_in_as = site._get_username_from_cookies()
  136. if not logged_in_as or logged_in_as != SCRIPT_USER:
  137. password = getpass("Password for {user}: ".format(user=SCRIPT_USER))
  138. site._login((SCRIPT_USER, password))
  139. return site
  140. def main():
  141. """
  142. Main entry point for script.
  143. """
  144. if len(argv) > 1 and argv[1].lstrip("-").startswith("c"):
  145. print(_get_script(), end="")
  146. return
  147. if not _is_clean():
  148. print("Uncommitted changes in working directory. Stopping.")
  149. exit(1)
  150. if len(argv) > 1 and argv[1].lstrip("-").startswith("t"):
  151. site_url = SCRIPT_TEST
  152. else:
  153. site_url = SCRIPT_SITE
  154. print("Uploading script to [[{page}]] on {site}...".format(
  155. page=SCRIPT_PAGE, site=site_url))
  156. script = _get_script()
  157. site = _get_site(site_url)
  158. page = site.get_page(SCRIPT_PAGE)
  159. summary = EDIT_SUMMARY.format(version=_get_version())
  160. page.edit(script, summary, minor=False, bot=False)
  161. params = {
  162. "title": page.title.replace(" ", "_"),
  163. "oldid": "prev",
  164. "diff": "cur"
  165. }
  166. print("Done!")
  167. print(site.url + "/w/index.php?" + urlencode(params))
  168. if __name__ == "__main__":
  169. main()