-
Notifications
You must be signed in to change notification settings - Fork 7
/
docbook-manual-repo.py
executable file
·200 lines (154 loc) · 5.61 KB
/
docbook-manual-repo.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
#!/usr/bin/python -u
# Integrates with docbook-manual.py to build manuals for all tagged
# versions and development branches in the Git repo
import getopt
import os
from os import path
import re
import sys
import time
# Absolute path to docbook-manual.py
manualscript = path.dirname(path.abspath(__file__)) + '/docbook-manual.py'
# Regular expressions of refs to ignore
ignorelist = map(re.compile, [
'HEAD',
'->',
'-1\.0\.[\w\d]+',
'-1\.1\.[\w\d]+'
])
# Script options
options = "hr:cfda"
long_options = ["help", "ref=", "current", "force", "delete",
"all", "pdf", "html", "release"]
def usage():
print '''Usage: docbook-manual-repo /path/to/mantisbt/repo \
/path/to/install [<lang> ...]
Options: -h | --help Print this usage message
-r | --ref Select what refs to build
-c | --current Build for current branch (no checkout)
-f | --force Ignore timestamps and force building
-d | --delete Delete install directories before building
--html Build HTML manual
--pdf Build PDF manual
--release Build single file types used for
release tarballs
-a | --all Build all manual types'''
#end usage()
def ignore(ref):
'''Decide which refs to ignore based on regexen listed in 'ignorelist'.
'''
ignore = False
for regex in ignorelist:
if len(regex.findall(ref)) > 0:
ignore = True
return ignore
#end ignore()
def git_current_branch():
''' Returns the current git branch's name or the current commit SHA
if we are in detached HEAD state
'''
gitcmd = 'git symbolic-ref --quiet --short HEAD || git rev-parse HEAD'
return os.popen(gitcmd).read().rstrip()
def git_checkout(branch):
os.system('git checkout -f %s >/dev/null' % branch)
def main():
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], options, long_options)
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
refs = None
current = False
force = False
pass_opts = ""
for opt, val in opts:
if opt in ("-h", "--help"):
usage()
sys.exit(0)
elif opt in ("-r", "--ref"):
refs = val.split(",")
elif opt in ("-c", "--current"):
current = True
elif opt in ("-f", "--force"):
force = True
elif opt in ("-d", "--delete"):
pass_opts += " -d"
elif opt in ("-a", "--all"):
pass_opts += " -a"
elif opt == "--html":
pass_opts += " --html"
elif opt == "--pdf":
pass_opts += " --pdf"
elif opt == "--release":
pass_opts += " --release"
if len(args) < 2:
usage()
sys.exit(1)
repo = args[0]
installroot = args[1]
languages = []
if len(sys.argv) > 2:
languages = args[2:]
if not current:
# Update repo from default remote
print "Updating repository in '%s' from default remote" % repo
os.chdir(repo)
os.system('git fetch')
os.system('git remote prune origin')
if not current and refs is None:
# List refs from remote branches and tags
branches = os.popen('git branch -r').read().split()
tags = os.popen('git tag -l').read().split()
# Filter refs using ignore()
refs = [ref for ref in branches + tags if not ignore(ref)]
# Regex to strip 'origin/' from ref names
refnameregex = re.compile('(?:[a-zA-Z0-9-.]+/)?(.*)')
curbranch = git_current_branch()
if current:
refs = [curbranch]
# For each ref, checkout (unless working on current branch) and call
# docbook-manual.py, tracking last build timestamp to prevent
# building a manual if there have been no commits since last build
for ref in refs:
print "\nGenerating documentation for '%s'" % ref
manualpath = path.join(installroot, refnameregex.search(ref).group(1))
if not current:
git_checkout(ref)
# Get timestamp of last change to docbook sources from git
lastchange = os.popen('git log --pretty="format:%ct" -n1 -- docbook'
).read()
buildfile = path.join(manualpath, '.build')
lastbuild = 0
if path.exists(buildfile):
f = open(buildfile, 'r')
lastbuild = f.read()
f.close()
if lastchange > lastbuild or force or current:
buildcommand = '%s %s %s %s %s' % (
manualscript,
pass_opts,
path.abspath('docbook'),
manualpath, ' '.join(languages)
)
print "Calling: " + buildcommand
if(os.system(buildcommand)):
print 'here'
f = open(buildfile, 'w')
f.write(lastchange)
f.close()
else:
# Get last build's timestamp from buildfile's modified time
mtime = float(os.path.getmtime(buildfile))
print("Docbook source unchanged since last build (%s)" %
time.strftime("%a %Y-%m-%d %H:%M:%S", time.localtime(mtime))
)
# 'touch' the flag file to bump the modified time
os.utime(buildfile, None)
# Reset repository to originally checked-out branch
if curbranch != git_current_branch():
print "\nRestoring originally checked-out branch"
git_checkout(curbranch)
#end main
if __name__ == '__main__':
main()