A Wikipedia user script to automate common TfD operations
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. return text.replace(tag, source)
  88. def _get_script():
  89. """
  90. Return the complete script.
  91. """
  92. with open(path.join(SCRIPT_ROOT, SCRIPT_FILE), "r") as fp:
  93. text = fp.read().decode("utf8")
  94. re_include = REPLACE_TAG.format(tag=r"INCLUDE:(.*?)")
  95. includes = re.findall(re_include, text)
  96. for include in includes:
  97. text = _do_include(text, include)
  98. replacements = {
  99. "VERSION": _get_version(),
  100. "VERSION_FULL": _get_full_version()
  101. }
  102. for tag, value in replacements.iteritems():
  103. text = text.replace(REPLACE_TAG.format(tag=tag), value)
  104. return text
  105. def _get_cookiejar():
  106. """
  107. Return a cookiejar to store the user's login info in.
  108. """
  109. cookiejar = LWPCookieJar(COOKIE_FILE)
  110. try:
  111. cookiejar.load()
  112. except LoadError:
  113. pass
  114. except IOError as err:
  115. if err.errno != errno.ENOENT:
  116. raise
  117. open(COOKIE_FILE, "w").close()
  118. chmod(COOKIE_FILE, stat.S_IRUSR|stat.S_IWUSR)
  119. return cookiejar
  120. def _get_site(site_url=SCRIPT_SITE):
  121. """
  122. Return the EarwigBot Site object where the script will be saved.
  123. This is hacky, but it allows us to upload the script without storing the
  124. user's password in a config file like EarwigBot normally does.
  125. """
  126. site = earwigbot.wiki.Site(
  127. base_url="https://" + site_url, script_path="/w",
  128. cookiejar=_get_cookiejar(), assert_edit="user")
  129. logged_in_as = site._get_username_from_cookies()
  130. if not logged_in_as or logged_in_as != SCRIPT_USER:
  131. password = getpass("Password for {user}: ".format(user=SCRIPT_USER))
  132. site._login((SCRIPT_USER, password))
  133. return site
  134. def main():
  135. """
  136. Main entry point for script.
  137. """
  138. if len(argv) > 1 and argv[1].lstrip("-").startswith("c"):
  139. print(_get_script(), end="")
  140. return
  141. if not _is_clean():
  142. print("Uncommitted changes in working directory. Stopping.")
  143. exit(1)
  144. if len(argv) > 1 and argv[1].lstrip("-").startswith("t"):
  145. site_url = SCRIPT_TEST
  146. else:
  147. site_url = SCRIPT_SITE
  148. print("Uploading script to [[{page}]] on {site}...".format(
  149. page=SCRIPT_PAGE, site=site_url))
  150. script = _get_script()
  151. site = _get_site(site_url)
  152. page = site.get_page(SCRIPT_PAGE)
  153. summary = EDIT_SUMMARY.format(version=_get_version())
  154. page.edit(script, summary, minor=False, bot=False)
  155. params = {
  156. "title": page.title.replace(" ", "_"),
  157. "oldid": "prev",
  158. "diff": "cur"
  159. }
  160. print("Done!")
  161. print(site.url + "/w/index.php?" + urlencode(params))
  162. if __name__ == "__main__":
  163. main()