forked from qiniu/developer.qiniu.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_doc-update.py
executable file
·437 lines (354 loc) · 13 KB
/
_doc-update.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import commands
import time
import sys
import os
import re
import shutil
from optparse import OptionParser
import StringIO
cwddir = os.getcwd()
docsroot = cwddir + "/docs.qiniu.com"
guideroot = docsroot + "/guide"
faqroot = docsroot + "/faq"
toolsroot = docsroot + "/tools"
thirdresroot = docsroot + "/third-res"
apiroot = docsroot + "/apidoc" # apidoc repo dir
devtoolsroot = docsroot + "/devtools" # docs of devtools
commondir = docsroot + "/common-file" # common layouts and static files
jklpath = cwddir + "/jkl"
jklconfpath = cwddir + "/_jekyll_qiniu.yml"
githost = "https://github.com"
doc_lists = ["docs.qiniu.com","apidoc","c-sdk","php-sdk","perl-sdk","csharp-sdk","android-sdk","objc-sdk","python-sdk","ruby-sdk","java-sdk","nodejs-sdk","go-sdk"]
# where is common layout files
commonpos = ("qiniu", "docs.qiniu.com", "develop")
#commonpos = ("icattlecoder", "docs.qiniu.com", "feature_qrsbox")
def addtime(f):
def wrap(*args):
t = time.localtime()
print "%d-%d-%d %d:%d:%d " % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec),
f(*args)
return wrap
@addtime
def logprint(message):
print message
def execcmd(cmd):
logprint("EXEC: %s" % cmd)
status, output = commands.getstatusoutput(cmd)
logprint(output)
if status != 0:
return False
return True
def initcheck():
if not os.path.exists(docsroot):
try:
os.mkdir(docsroot)
except Exception as e:
logprint(str(e))
return False
if not os.path.exists(jklconfpath):
logprint("Error: Cannot find %s" % jklconfpath)
return False
if not (os.path.isfile(jklpath) and os.access(jklpath, os.X_OK)):
logprint("Error: Cannot find executable jkl at %s" % os.getcwd())
return False
return True
def curl(url, dest):
logprint("Info: downloading %s" % url)
if not execcmd("curl -fsSkL %s -o %s" % (url, dest)):
logprint("Error: download failed")
return False
else:
logprint("Info: download finished")
return True
def untar(srcfile):
"""
Untar a tar.gz file
"""
oldcwd = os.getcwd()
os.chdir(os.path.dirname(srcfile))
logprint("Info: untar %s" % srcfile)
if not execcmd("tar zxvf %s" % srcfile):
logprint("Error: untar failed")
os.chdir(oldcwd)
return False
else:
logprint("Info: untar finished")
os.chdir(oldcwd)
return True
def jklupload(dirpath):
"""
upload files to a bucket by tool `jkl`
"""
oldcwd = os.getcwd()
os.chdir(dirpath)
logprint("Info: uploading %s" % dirpath)
if not execcmd("%s --qiniu-config %s --qiniu --verbose" % (jklpath, jklconfpath)):
logprint("Error: Failed to uplaod docs")
os.chdir(oldcwd)
return False
else:
logprint("Info: upload docs finish")
os.chdir(oldcwd)
return True
def getcommonfile(user, repo, branch):
"""
Get common layout files from repo docs.qiniu.com
"""
oldcwd = os.getcwd()
if os.path.exists(commondir):
shutil.rmtree(commondir)
os.mkdir(commondir)
os.chdir(commondir)
downurl, package, untarpath, rootpath, changelog, docpath = getpath(user, repo, branch)
if not curl(downurl, package):
logprint("Error: fail to download common file, abort.")
os.chdir(oldcwd)
return False
if not untar(package):
logprint("Error: fail to untar, abort.")
os.chdir(oldcwd)
return False
layoutpath = "/".join([untarpath, "_layouts"])
configpath = "/".join([untarpath, "_config.yml"])
# copy _layouts/ and _config.yml to .
if not (execcmd("cp -R %s %s" % (layoutpath, commondir)) \
and execcmd("cp -R %s %s" % (configpath, commondir))):
logprint("Error: cp config file failed")
os.chdir(oldcwd)
return False
#
# TODO:remove tmp file
#
os.chdir(oldcwd)
return True
def getversion(path):
"""
Parse lasted version from CHANGELOG.md
return: string
"""
try:
f = open(path)
buf = f.read()
except Exception as e:
logprint("Error: %s" % str(e))
sys.exit(1)
r = re.compile("[vV][0-9]\.[0-9]\.[0-9]")
l = r.findall(buf)
if len(l) > 0 :
logprint("Info: get lasted version %s" % l[0][:2])
return l[0][:2]
else:
logprint("Error: Can not find version info")
return None
# update docs of a repo
def docupdate(user, repo, branch):
if repo == "docs.qiniu.com": # update /guide /tools /faq and static files
downurl, package, untarpath, rootpath, changelog, docpath = getpath(user, repo, branch)
# force to update layout files
if not getcommonfile(*commonpos):
return False
if not curl(downurl, package):
return False
if not untar(package):
return False
ver = getversion(untarpath + "/CHANGELOG.md")
execcmd("rm -rf %s %s %s %s" % ("/".join([guideroot,"guide", ver]), "/".join([toolsroot, "tools", ver]), "/".join([faqroot, "faq", ver]), "/".join([thirdresroot, "third-res", ver])))
execcmd("mkdir -p %s %s %s %s" % ("/".join([guideroot,"guide", ver]), "/".join([toolsroot, "tools", ver]), "/".join([faqroot, "faq", ver]), "/".join([thirdresroot, "third-res", ver])))
execcmd("cp -R %s %s" % (untarpath + "/guide/*", guideroot + "/guide/" + ver))
execcmd("cp -R %s %s" % (untarpath + "/tools/*", toolsroot + "/tools/" + ver))
execcmd("cp -R %s %s" % (untarpath + "/faq/*", faqroot + "/faq/" + ver))
execcmd("cp -R %s %s" % (untarpath + "/third-res/*", thirdresroot + "/third-res/" + ver))
execcmd("cp %s %s" % (commondir + "/_config.yml", guideroot))
execcmd("cp %s %s" % (commondir + "/_config.yml", toolsroot))
execcmd("cp %s %s" % (commondir + "/_config.yml", faqroot))
execcmd("cp %s %s" % (commondir + "/_config.yml", thirdresroot))
execcmd("cp -R %s %s" % (commondir + "/_layouts", guideroot))
execcmd("cp -R %s %s" % (commondir + "/_layouts", toolsroot))
execcmd("cp -R %s %s" % (commondir + "/_layouts", faqroot))
execcmd("cp -R %s %s" % (commondir + "/_layouts", thirdresroot))
redirectFolder("/".join([guideroot, "guide", ver]), "/".join([guideroot, "guide"]))
redirectFolder("/".join([faqroot, "faq", ver]), "/".join([faqroot, "faq"]))
redirectFolder("/".join([toolsroot, "tools", ver]), "/".join([toolsroot, "tools"]))
redirectFolder("/".join([thirdresroot, "third-res", ver]), "/".join([thirdresroot, "third-res"]))
jklupload(untarpath) # upload statics of docs.qiniu.com
jklupload(guideroot)
jklupload(toolsroot)
jklupload(faqroot)
jklupload(thirdresroot)
return True
if repo.find("-sdk") >= 0 or repo == "apidoc":
relrepo = repo
if repo == "go-sdk": relrepo = "api"
if repo == "apidoc": repo = "api"
downurl, package, untarpath, rootpath, changelog, docpath = getpath(user, relrepo, branch)
# use current layouts if exists, else fetch it
if not os.path.exists(commondir):
if not getcommonfile(*commonpos):
return False
# download repo tar
curl(downurl, package)
untar(package)
ver = getversion(changelog)
verpath = "/".join([rootpath, repo, ver])
# make new upload dir of the repo
execcmd("rm -rf %s" % verpath)
execcmd("mkdir -p %s" % verpath)
# copy doc files into upload dir
execcmd("cp -R %s %s" % (docpath + "/*", verpath))
# copy config files for jkl
execcmd("cp %s %s" % (commondir + "/_config.yml", rootpath))
execcmd("cp -R %s %s" % (commondir + "/_layouts", rootpath))
# add version header in all markdown files in upload dir
adddirver(verpath, ver)
# generate index.html from README.md
if os.path.exists(verpath + "/README.md"):
execcmd("cp %s %s" % (verpath + "/README.md", verpath + "/index.md"))
# generate redirect html file
redirectFolder(verpath, "/".join([rootpath, repo]))
# generate sites and upload to bucket
jklupload(rootpath)
def redirectFolder(srcdir, todir):
"""
在todir中生成srcdir中各文件相应的跳转页面
"""
if srcdir.endswith("/"): srcdir = srcdir[:-1]
if todir.endswith("/"): todir = todir[:-1]
logprint("Info: redirectFolder %s to %s" % (srcdir, todir))
for root, dirs, files in os.walk(srcdir):
for fname in files:
fpath = "/".join([root, fname])
rel = os.path.relpath(fpath, todir)
relself = os.path.relpath(fpath, srcdir)
if fpath.endswith(".md") or \
fpath.endswith(".markdown") or \
fpath.endswith(".html"):
redirect = os.path.splitext(rel)[0] + ".html"
topath = "/".join([todir, relself])
topath = os.path.splitext(topath)[0] + ".html"
gen301file(topath, redirect)
def forceCreate(filepath, mode):
"""
当文件所属的目录不存在时,自动创建,并返回文件句柄
"""
logprint("Info: Create file `%s`" % filepath)
basedir = os.path.dirname(filepath)
if not os.path.exists(basedir):
os.makedirs(basedir)
try:
fd = open(filepath, mode)
except Exception as e:
logprint("Error: " + str(e))
return None
return fd
def gen301file(topath, rdrcturl):
"""
* topath: path to put the html file
* rdrcturl: redirect to where
"""
logprint("Info: gen301file: %s redirect to %s" % (topath, rdrcturl))
content = """
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN">
<meta http-equiv="refresh" content="0.1;url=%s">
</head>
<body></body>
</html>
""" % rdrcturl
try:
f = forceCreate(topath, "w+")
f.seek(0)
f.write(content)
f.truncate()
except Exception as e:
logprint("Error: " + str(e))
return False
return True
def adddirver(dirpath, ver):
"""
Call addversion on all files under `dirpath`
"""
for root, dirs, files in os.walk(dirpath):
for fname in files:
fpath = "/".join([root, fname])
if fpath.endswith(".md") or fpath.endswith(".markdown"):
addversion(fpath, ver)
def addversion(mdpath, version):
"""
Add attribute "version" to a markdown file
"""
logprint("AddVersion: add `version: %s` to %s" % (version, mdpath))
try:
f = open(mdpath, 'r+')
oribuf = f.read()
except Exception as e:
logprint("AddVersion: %s" % str(e))
return False
buf = ''
rbuf = StringIO.StringIO(oribuf)
for line in rbuf.readlines():
if line.startswith("title:"):
line = "".join([line, "version: %s\n" % version])
buf = "".join([buf, line])
if line.startswith("version:"):
f.close()
return True
f.seek(0)
f.write(buf)
f.truncate()
f.close()
return True
# I need downurl, docpath, changelog, untarpath, rootpath, package
# repo: go-sdk -> api
def getpath(gituser, repo, branch):
baseurl = "/".join([githost, gituser])
downurl = "/".join([baseurl, repo, "archive", branch+".tar.gz"])
untarpath = "/".join([docsroot, "-".join([repo, branch])])
changelog = untarpath + "/CHANGELOG.md"
rootpath = "/".join([docsroot, repo])
package = "/".join([docsroot, ".".join([repo, branch, "tar.gz"])])
if repo in ("csharp-sdk", "objc-sdk"):
docpath = untarpath + "/Docs/"
elif repo == "apidoc":
docpath = untarpath + "/api/"
else:
docpath = untarpath + "/docs/"
return downurl, package, untarpath, rootpath, changelog, docpath
def optparse():
"""
Parse args.
"""
usage = 'Usage: ./doc-update.py [options]'
parser = OptionParser(usage=usage)
parser.add_option('-B', '--branch', help=u'choose which branch to update')
parser.add_option('-R', '--repo', help=u'set repo name: docs.qiniu.com | apidoc | c-sdk | php-sdk | perl-sdk | csharp-sdk | android-sdk | objc-sdk | python-sdk | ruby-sdk | java-sdk | nodejs-sdk | go-sdk')
parser.add_option('-U', '--user', help=u'set github name')
options = parser.parse_args()[0]
if options.repo:
repo = options.repo
else:
repo = "alldocs"
if options.branch:
branch = options.branch
else:
branch = "master"
if options.user:
user = options.user
else:
user = "qiniu"
return user, repo, branch
if __name__ == "__main__":
user, repo, branch = optparse()
if not initcheck():
logprint("Error: initcheck failed, abort.")
sys.exit(1)
if repo == "alldocs":
for x in doc_lists:
docupdate(user, x, branch)
logprint("")
else:
docupdate(user, repo, branch)