-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpack_and_upload_to_ctan.py
163 lines (127 loc) · 5 KB
/
pack_and_upload_to_ctan.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
"""
This script is used to upload a package to CTAN.
It uses curl to send the POST request.
Without any parameter it does just a sort of validation
of the package to be uploaded
CAUTION: The validation is not as thorough as what `pkgcheck` does
If `--upload` is specified the package gets uploaded to CTAN.
If required it could be enhanced to be a generic script.
Based on a script written by Manfred Lotz
see https://gitlab.com/Lotz/pkgcheck/blob/master/ctan_upload.py
"""
import subprocess
import zipfile
from shutil import copyfile
from os import unlink
import toml
import re
import subprocess
def add_parm_from_toml(conf, parm, cmd):
"""
Adds command line parameter(s) for the curl command
- first get value from toml file or None in case
the `parm` is not specified
- if the value gotten is a list add command line args for each
iten in the list
- if the value gotten is just a simple value add command line
arg for that value
"""
val = conf.get(parm)
if val:
if isinstance(val, list):
for v in val:
cmd.append("-F")
cmd.append(parm + "=" + v)
else:
cmd.append("-F")
cmd.append(parm + "=" + val)
return cmd
def main(upload=False):
"""
Parse command line args and invoke curl to either
- validate an upload, or
- to upload a package to CTAN
"""
print('Checking the file for errors')
result = subprocess.check_output("biber --nolog --nodieonerror --noconf --tool dtk-bibliography.bib", shell=True)
result = result.decode('utf-8')
print(result)
print('Do not forget to update the .toml file, .tex and the README file in the sub-folder!')
# This TOML file is not included as it
# contains sensitive information
conf = toml.load('dtk_bibliography.toml')
pkg = conf["pkg"]
pkg_version = conf["pkg_version"]
pkg_zip = conf["pkg_zip"]
uploader = conf["uploader"]
email = conf["email"]
ctan_path = conf["ctan_path"]
with open("dtk-bibliography/README.md", "r+") as f:
data = f.read()
f.seek(0)
f.write(re.sub(r"\$.*\$", conf["announcement"], data))
f.truncate()
# certain parameters are always required
curl = ["curl"] + [
"-i",
"-X",
"POST",
"-F", "update=true",
"-F", "pkg=" + pkg,
"-F", "version=" + pkg_version,
"-F", "email=" + email,
"-F", "ctanPath=" + ctan_path,
"-F", "uploader=" + uploader,
"-F", "file=@" + pkg_zip,
]
# the following parameters are optional
add_parm_from_toml(conf, "summary", curl)
add_parm_from_toml(conf, "description", curl)
add_parm_from_toml(conf, "author", curl)
add_parm_from_toml(conf, "home", curl)
add_parm_from_toml(conf, "announcement", curl)
add_parm_from_toml(conf, "support", curl)
add_parm_from_toml(conf, "development", curl)
add_parm_from_toml(conf, "repository", curl)
add_parm_from_toml(conf, "bugs", curl)
add_parm_from_toml(conf, "note", curl)
add_parm_from_toml(conf, "licenses", curl)
add_parm_from_toml(conf, "topics", curl)
if upload == True:
curl.append("https://www.ctan.org/submit/upload")
print('Upload mode is on, uploading ZIP to CTAN!')
else:
curl.append("https://www.ctan.org/submit/validate")
print('Validation mode is on, no upload!')
rc = subprocess.run(curl).returncode
if rc != 0:
print(f"curl error: {rc}")
else:
print(f"curl had no error, returncode was: {rc}")
if __name__ == "__main__":
# get latest versions of all files
copyfile('dtk-authoryear.bbx' , './dtk-bibliography/dtk-authoryear.bbx')
copyfile('dtk-authoryear.dbx' , './dtk-bibliography/dtk-authoryear.dbx')
copyfile('dtk-bibliography.pdf', './dtk-bibliography/dtk-bibliography.pdf')
copyfile('dtk-bibliography.tex', './dtk-bibliography/dtk-bibliography.tex')
copyfile('dtk-bibliography.bib', './dtk-bibliography/dtk-bibliography.bib')
copyfile('dtk-logos.sty', './dtk-bibliography/dtk-logos.sty')
# create the zip file
with zipfile.ZipFile('dtk-bibliography.zip', 'w', zipfile.ZIP_DEFLATED) as z:
z.write('./dtk-bibliography/README.md')
z.write('./dtk-bibliography/dtk-authoryear.bbx')
z.write('./dtk-bibliography/dtk-authoryear.dbx')
z.write('./dtk-bibliography/dtk-bibliography.pdf')
z.write('./dtk-bibliography/dtk-bibliography.tex')
z.write('./dtk-bibliography/dtk-bibliography.bib')
z.write('./dtk-bibliography/dtk-logos.sty')
# remove files again
unlink('./dtk-bibliography/dtk-authoryear.bbx')
unlink('./dtk-bibliography/dtk-authoryear.dbx')
unlink('./dtk-bibliography/dtk-bibliography.pdf')
unlink('./dtk-bibliography/dtk-bibliography.tex')
unlink('./dtk-bibliography/dtk-bibliography.bib')
unlink('./dtk-bibliography/dtk-logos.sty')
# Update the README.MD in the subfolder!
main(False) # Set to True to upload