-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathupload.py
100 lines (82 loc) · 3.27 KB
/
upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
run `python upload.py --help` for usage
requires GitPython
"""
import argparse
import datetime
import os
import re
import git
import pywikibot
SUMMARY = "Updating {} ({} @ {})"
HEADER_COMMENT = "/* Uploaded from branch {}, commit {} */\n"
SCRIPT_NAME = "reply-link"
def get_branch_and_hash():
"""Gets the branch that the repo is currently on, as well as the hash
of the most recent commit to it"""
repo = git.Repo(os.getcwd())
branch, sha1 = "", ""
try:
branch = repo.active_branch
sha1 = branch.commit.hexsha
except AttributeError:
branch = next(x for x in repo.branches if x.name == repo.active_branch)
sha1 = branch.commit.id
except:
return "unknown", "unknown"
return branch, sha1
def update_doc_time(site: pywikibot.Site, script_root: str):
"""Update the time on the docs."""
print("Updating script documentation page.")
page = pywikibot.Page(site, title="User:Enterprisey/" + SCRIPT_NAME)
docs_wikitext = page.get()
date = re.search(r"start date and age\|\d+\|\d+\|\d+",
docs_wikitext).group(0)
now = datetime.datetime.now()
revised_date = "start date and age|%d|%d|%d" %\
(now.year, now.month, now.day)
page.text = docs_wikitext.replace(date, revised_date)
def save_callback(_page, e):
if e:
print("Error updating the \"updated\" time: " + str(e))
else:
print("Success! Updated the \"updated\" time on the documentation")
page.save(summary="Updating {} \"updated\" time".format(SCRIPT_NAME),
callback=save_callback)
def upload(site: pywikibot.Site, text: str, page_title: str, summary: str):
script_page = pywikibot.Page(site, page_title)
script_page.text = text
script_page.save(summary=summary)
def main():
"""The main function"""
# Parse the arguments
parser = argparse.ArgumentParser(prog="upload.py",
description="Upload reply-link")
parser.add_argument("-t", "--test", action="store_true",
help="Upload to testwiki (default: upload to enwiki)")
group = parser.add_mutually_exclusive_group()
group.add_argument("-d", "--dev", action="store_true",
help="On the wiki, filename will have a -dev suffix")
group.add_argument("-b", "--both", action="store_true",
help="Upload to both main & dev versions")
args = parser.parse_args()
wiki = "test" if args.test else "en"
site = pywikibot.Site(wiki, "wikipedia")
site.login()
username = site.user()
script_root = "User:{}/{}".format(username, SCRIPT_NAME)
title = script_root + ("-dev" if args.dev else "") + ".js"
print("Uploading to {} on {}.wikipedia.org...".format(title, wiki))
local_script = SCRIPT_NAME + ".js"
print("Reading from {}...".format(local_script))
with open(local_script, "r") as target_file:
branch, sha1 = get_branch_and_hash()
summary = SUMMARY.format(local_script, sha1[:7], branch)
text = HEADER_COMMENT.format(branch, sha1[:7]) + target_file.read()
upload(site, text, title, summary)
if args.both:
upload(site, text, script_root + "-dev.js", summary)
if not args.test and not args.dev:
update_doc_time(site, script_root)
if __name__ == "__main__":
main()