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
|
#!/usr/bin/python
import subprocess
import glob
import sys
import os
LANGS = [ "C", "es", "ru", "pt-br", "de", "fr", "uk" ]
FLAVOURS = [ "html", "epub", "pdf" ]
RELEASE = "trusty"
TMP_DIR = os.path.expanduser("~/tmp/")
UNPACKED_USRSHARE_DIR = os.path.join(TMP_DIR, "1/usr/share")
OUTPUT_DIR = os.getenv("OUTPUT_DIR",
default=os.path.expanduser("~/public_html/packaging-guide/"))
APT_LINES = [ "deb http://ppa.launchpad.net/ubuntu-packaging-guide-team/ppa/ubuntu %s main" % RELEASE,
"deb http://archive.ubuntu.com/ubuntu %s main" % RELEASE ]
PARTIAL_DIR = os.path.join(TMP_DIR, "lists/partial")
ETC_APT_DIR = os.path.join(TMP_DIR, "etc/apt")
TRUSTED_DIR = os.path.join(ETC_APT_DIR, "trusted.gpg.d")
SOURCES_LIST_FILE = os.path.join(ETC_APT_DIR, "sources.list")
LOCAL_KEYRING = os.path.join(os.path.abspath(os.path.dirname(__file__)), "keyring.gpg")
def safe_remove(what, contents_only=False):
if os.path.exists(what):
if os.path.isdir(what):
if contents_only or subprocess.call(["rm", "-rf", what]) != 0:
os.system("rm -rf %s/*" % what)
if os.path.isfile(what):
os.remove(what)
def safe_mkdirs(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def write_sources_list():
safe_remove(SOURCES_LIST_FILE)
f = open(SOURCES_LIST_FILE, "a")
f.write("\n".join(APT_LINES))
f.close()
def call_with_apt_args(s):
apt_args = "-o", "Dir::State::lists=%s" % os.path.join(TMP_DIR, "lists"), \
"-o", "Dir::Etc=%s" % ETC_APT_DIR, \
"-o", "Dir::State=%s" % TMP_DIR, \
"-o", "Debug::NoLocking=true", "-q=2"
l = list(s.split(" "))
l.extend(apt_args)
retcode = subprocess.call(l)
return retcode
def replace_C_locale(l):
return map(lambda a: a.replace("-C", ""), l)
def download_packages():
os.chdir(TMP_DIR)
safe_mkdirs(PARTIAL_DIR)
safe_mkdirs(ETC_APT_DIR)
safe_mkdirs(TRUSTED_DIR)
subprocess.call(["cp", LOCAL_KEYRING, TRUSTED_DIR])
write_sources_list()
call_with_apt_args("apt-get update")
packages = [ "libjs-sphinxdoc", "ubuntu-packaging-guide-common",
"libjs-jquery", "libjs-underscore" ]
for lang in LANGS:
for flavour in FLAVOURS:
packages += [ "ubuntu-packaging-guide-%s-%s" % (flavour, lang) ]
packages = replace_C_locale(packages)
for pkg in packages:
if call_with_apt_args("aptitude download %s" % pkg) == 0:
deb_files = glob.glob("%s_*.deb" % pkg)
if not deb_files:
print("No .deb files. apt error?")
sys.exit(1)
deb_file = deb_files[0]
if subprocess.call(["dpkg", "-x", deb_file, "1"]) != 0:
return False
safe_remove(deb_file)
else:
return False
return True
def cleanup_tmp():
safe_remove(TMP_DIR)
safe_mkdirs(TMP_DIR)
def cleanup_output():
safe_remove(OUTPUT_DIR, contents_only=True)
safe_mkdirs(OUTPUT_DIR)
def move_files():
items = []
for lang in LANGS:
items = [ "ubuntu-packaging-guide-epub-%s/ubuntu-packaging-guide.epub" % lang,
"ubuntu-packaging-guide-html-%s" % lang,
"ubuntu-packaging-guide-pdf-%s/ubuntu-packaging-guide.pdf.gz" % lang ]
items = replace_C_locale(items)
lang_output_dir = os.path.join(OUTPUT_DIR, lang)
safe_mkdirs(lang_output_dir)
for item in items:
subprocess.call(["cp", "-r",
os.path.join(UNPACKED_USRSHARE_DIR, "doc/", item),
lang_output_dir])
for (old_dir, new_dir) in [ ("ubuntu-packaging-guide-html-%s/singlehtml" % lang, "singlehtml"),
("ubuntu-packaging-guide-html-%s" % lang, "html")]:
subprocess.call(["cp", "-r",
os.path.join(lang_output_dir, old_dir.replace("-C", "")),
os.path.join(lang_output_dir, new_dir)])
def fix_singlehtml_search():
for lang in LANGS:
lang_output_dir = os.path.join(OUTPUT_DIR, lang)
subprocess.call(["ln", "-s",
os.path.join(lang_output_dir.replace("/C", ""), "html/search.html"),
os.path.join(lang_output_dir.replace("/C", ""), "singlehtml/")])
def add_static_files():
for js_file in filter(lambda a: os.path.islink(a),
glob.glob(os.path.join(UNPACKED_USRSHARE_DIR, "ubuntu-packaging-guide/_static/*.js"))):
which = os.path.basename(js_file)
os.remove(js_file)
subprocess.call(["cp", "-f",
os.path.join(UNPACKED_USRSHARE_DIR, "javascript/sphinxdoc/1.0", which),
js_file])
replacements = set()
for root, dirs, files in os.walk(OUTPUT_DIR):
for directory in dirs:
for which in [ "_static", "_images" ]:
if which in directory:
replacements.add(os.path.join(root, directory))
for replacement in replacements:
safe_remove(replacement)
which = replacement.split("/")[-1]
subprocess.call(["cp", "-r",
os.path.join(UNPACKED_USRSHARE_DIR, "ubuntu-packaging-guide", which),
replacement])
# add translations.js
for lang in LANGS:
js_location = "doc/ubuntu-packaging-guide-html-%s/_static/translations.js" % lang
js_path = os.path.join(UNPACKED_USRSHARE_DIR, js_location)
if os.path.exists(js_path):
lang_output_dir = os.path.join(OUTPUT_DIR, lang)
for flavour in [ "html", "singlehtml" ]:
subprocess.call(["cp", js_path, os.path.join(lang_output_dir, flavour, "_static")])
def unpack_zip_files():
for lang in LANGS:
for zipfile in [ "ubuntu-packaging-guide.pdf.gz",
"ubuntu-packaging-guide.epub.gz" ]:
zip_path = os.path.join(OUTPUT_DIR, lang)
os.chdir(zip_path)
if os.path.exists(zipfile):
subprocess.call(["gunzip", zipfile])
os.chdir(OUTPUT_DIR)
def move_C_locale_bits():
os.chdir(OUTPUT_DIR)
for item in glob.glob("C/*"):
subprocess.call(["mv", item, "."])
safe_remove("C")
def copy_landing_page(maindir):
subprocess.call(["cp", os.path.join(maindir, "index.html"), OUTPUT_DIR])
subprocess.call(["cp", os.path.join(maindir, "home.css"), OUTPUT_DIR])
subprocess.call(["cp", "-r", os.path.join(maindir, "img"), OUTPUT_DIR])
if __name__ == "__main__":
pwd = os.getcwd()
cleanup_tmp()
if not download_packages():
sys.exit(1)
cleanup_output()
move_files()
add_static_files()
unpack_zip_files()
move_C_locale_bits()
fix_singlehtml_search()
copy_landing_page(pwd)
os.chdir(pwd)
|