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
|
#!/usr/bin/env python3
# this file is part of checkbox.
#
# copyright 2014 canonical ltd.
# written by:
# maciej kisielewski <maciej.kisielewski@canonical.com>
#
# checkbox is free software: you can redistribute it and/or modify
# it under the terms of the gnu general public license version 3,
# as published by the free software foundation.
#
# checkbox is distributed in the hope that it will be useful,
# but without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose. see the
# gnu general public license for more details.
#
# you should have received a copy of the gnu general public license
# along with checkbox. if not, see <http://www.gnu.org/licenses/>.
"""
Download and extract .deb packages necessary to run checkbox-touch
Extraction is done to specific directories as required by click package
For typical development iteration, after hacking something in plainbox,
run ./get-libs --plainbox-only
"""
import apt
import apt_pkg
import argparse
import collections
import os
import shutil
import subprocess
import sys
import tarfile
import tempfile
import urllib.request
import zipfile
# arch_list contains list of architectures for which the packages should be
# downloaded
arch_list = ['i386', 'amd64', 'armhf']
# multiarch_list contains full architecture name as used in the directories
# in contents of click package
multiarch_list = ["i386-linux-gnu", "x86_64-linux-gnu", "arm-linux-gnueabihf"]
def copy_tree(src, dest, preserve_symlinks=False):
"""
copy files from src to dest using rsync
"""
links_option = "-l" if preserve_symlinks else "-L"
parent_dir = os.path.split(os.path.abspath(dest))[0]
# adding trailing slash if it's not already there
src = os.path.join(src, '')
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
subprocess.check_call(["rsync", "-a", links_option, src, dest])
def prepare_uris(get_autopilot):
"""
prepare_uris function builds caches for architectures defined in arch_list
and builds a dictionary of URLs indexed by (package_name, arch) tuple.
It returns that dictionary.
"""
# additional packages needed to support autopilot tests. these were obtained by
# running apt-cache depends on python3-autopilot
ap_packages = ['python3-autopilot', 'python3-psutil', 'python3-testtools', 'python3-pil',
'libjs-jquery', 'libjs-underscore', 'libwebp5', 'libwebpmux1', 'python3-decorator',
'python3-testscenarios', 'python3-subunit', 'python3-fixtures', 'python3-junitxml',
'python3-extras', 'python3-evdev']
packages = ['libpython3.4', 'pyotherside', 'python3-xlsxwriter',
'python3-jinja2', 'python3-markupsafe', 'python3-padme',
'qml-module-qtquick-qchartjs', 'python3-requests',
'python3-urllib3']
if get_autopilot:
packages += ap_packages
# uris will serve as a database of uris from which to download packages
uris = dict()
Source = collections.namedtuple('Source', ['uri', 'repositories'])
sources = {
'armhf': [Source('http://ports.ubuntu.com/ubuntu-ports',
'main restricted universe'),
Source('http://ppa.launchpad.net/checkbox-dev/ppa/ubuntu',
'main')],
'i386': [Source('http://archive.ubuntu.com/ubuntu',
'main restricted universe'),
Source('http://ppa.launchpad.net/checkbox-dev/ppa/ubuntu',
'main')],
'amd64': [Source('http://archive.ubuntu.com/ubuntu',
'main restricted universe'),
Source('http://ppa.launchpad.net/checkbox-dev/ppa/ubuntu',
'main')],
}
for arch in arch_list:
print('Getting information about packages for {0} arch.'.format(arch))
# prepare sources.list for apt
with tempfile.TemporaryDirectory() as tmp:
new_etc_apt = os.path.join(tmp, 'etc', 'apt')
os.makedirs(new_etc_apt)
# copy over trusted.gpg
shutil.copyfile('/etc/apt/trusted.gpg',
os.path.join(new_etc_apt, 'trusted.gpg'))
# copy over additional keyrings
if os.path.exists('/etc/apt/trusted.gpg.d'):
shutil.copytree('/etc/apt/trusted.gpg.d',
os.path.join(new_etc_apt, 'trusted.gpg.d'))
sources_list = open(os.path.join(new_etc_apt, 'sources.list'), "w")
for source in sources[arch]:
sources_list.write(
"deb [arch={arch}] {uri} vivid {repositories}\n"
.format(arch=arch, uri=source.uri,
repositories=source.repositories))
sources_list.close()
apt_pkg.config["Apt::Architecture"] = arch
cache = apt.Cache(rootdir=tmp)
cache.update()
cache.open(None)
for pkg in packages:
if pkg not in cache or len(cache[pkg].versions) < 1:
# package not found
raise Exception('Package {0} not found for arch {1}'
.format(pkg, arch))
# use first uri available
uris[pkg, arch] = cache[pkg].versions[0].uri
# return filled dictionary
return uris
def get_package_from_url_and_extract(url, target_dir):
filename = os.path.join(target_dir, url.split('/')[-1])
print('retrieving {0}'.format(url))
urllib.request.urlretrieve(url, filename)
subprocess.check_call(["dpkg", "-x", filename, target_dir])
def extract_plainbox(src_tarball, version):
with tempfile.TemporaryDirectory() as tmp:
tarball = tarfile.open(src_tarball)
members = [member for member in tarball.getmembers()
if member.name.startswith(
"plainbox-{}/plainbox/".format(version)) or
member.name.startswith(
"plainbox-{}/plainbox.egg-info".format(version))]
tarball.extractall(tmp, members=members)
copy_tree(
os.path.join(tmp, "plainbox-{}".format(version), "plainbox"),
os.path.join('lib', 'py', 'plainbox'),
preserve_symlinks=1)
# copy plainbox.egg
copy_tree(
os.path.join(tmp, "plainbox-{}".format(version),
'plainbox.egg-info'),
os.path.join('lib', 'py', 'plainbox.egg-info'),
preserve_symlinks=1)
def get_plainbox_from_pypi(version='0.22.2'):
# plaibox_pypi contains URL from which to download plainbox
plainbox_pypi = ('https://pypi.python.org/packages/source'
'/p/plainbox/plainbox-{}.tar.gz'.format(version))
with tempfile.TemporaryDirectory() as tmp:
filename = os.path.join(tmp, plainbox_pypi.split('/')[-1])
print('retrieving {0}'.format(plainbox_pypi))
urllib.request.urlretrieve(plainbox_pypi, filename)
extract_plainbox(filename, version)
def get_local_plainbox():
# TODO: user shouldn't specify version of local plainbox
plainbox_setup_path = os.path.join(os.getcwd(), '..', 'plainbox',
'setup.py')
plainbox_version = subprocess.check_output([plainbox_setup_path,
'--version']).decode(sys.stdout.encoding).strip()
plainbox_setup_dir = os.path.split(plainbox_setup_path)[0]
print('building local plainbox')
ret = subprocess.check_call([plainbox_setup_path, '-q', 'sdist'],
cwd=plainbox_setup_dir)
if ret != 0:
raise Exception(
"Could not build plainbox using {}. Check above output"
.format(plainbox_setup_path))
filename = os.path.join(plainbox_setup_dir, 'dist', 'plainbox-{}.tar.gz'
.format(plainbox_version))
extract_plainbox(filename, plainbox_version)
def main():
parser = argparse.ArgumentParser("Get necessary libs for checkbox-touch")
parser.add_argument("--get-pypi-plainbox",
action='store_true',
help="Use plainbox downloaded from pypi")
parser.add_argument("--plainbox-only", action='store_true',
help="Get plainbox only")
parser.add_argument("--get-autopilot",
action='store_true',
help="Download libs for autopilot")
args = parser.parse_args()
if args.get_pypi_plainbox:
get_plainbox_from_pypi()
else:
get_local_plainbox()
if args.plainbox_only:
# skip downloading rest of the libs
return
uris = prepare_uris(args.get_autopilot)
# libs_urls contains list of .deb packages that will be downloaded and
# extracted. After extraction contents of ./usr/lib are copied to ./lib
libs_urls = [uris['libpython3.4', arch] for arch in arch_list]
for lib in libs_urls:
with tempfile.TemporaryDirectory() as tmp:
get_package_from_url_and_extract(lib, tmp)
# TODO: remove unwanted files from the extracted tree (e.g. *.h)
copy_tree(
os.path.join(tmp, 'usr', 'lib'), 'lib',
preserve_symlinks=1)
# python3_libs_urls contains list of .deb packages that will be downloaded
# and extracted. After extraction contents of
# ./usr/lib/python3/dist-packages are copied to ./lib/py.
python3_libs = ['python3-xlsxwriter', 'python3-jinja2',
'python3-markupsafe', 'python3-padme', 'python3-requests',
'python3-urllib3']
# additional libs needed to support autopilot tests. these were obtained by
# running apt-cache depends on python3-autopilot
python3_ap_libs = ['python3-autopilot', 'python3-psutil',
'python3-pil', 'python3-decorator', 'python3-testtools',
'python3-subunit', 'python3-testscenarios', 'python3-junitxml', 'python3-fixtures',
'python3-extras', 'python3-evdev']
if args.get_autopilot:
python3_libs += python3_ap_libs
python3_libs_urls = [
uris[lib, arch] for arch in arch_list for lib in python3_libs]
for pylib in python3_libs_urls:
with tempfile.TemporaryDirectory() as tmp:
get_package_from_url_and_extract(pylib, tmp)
copy_tree(
os.path.join(tmp, 'usr', 'lib', 'python3',
'dist-packages'),
os.path.join('lib', 'py'),
preserve_symlinks=1)
# qml_plugins_url contains list of .deb packages that will be downloaded
# and extracted. After extraction contents of
# ./usr/lib/{architecture}/qt5/qmlare copied to ./lib/{architecture}.
# {architecture} may be one of multiarch_list
qml_plugins_urls = [uris['pyotherside', arch] for arch in arch_list]
qml_plugins_urls += [uris['qml-module-qtquick-qchartjs', arch] for arch in arch_list]
for qml_plugin in qml_plugins_urls:
with tempfile.TemporaryDirectory() as tmp:
get_package_from_url_and_extract(qml_plugin, tmp)
for arch in multiarch_list:
src = os.path.join(tmp, 'usr', 'lib', arch, 'qt5', 'qml')
dest = os.path.join('lib', arch)
if os.path.exists(src) and os.path.isdir(src):
copy_tree(src, dest, preserve_symlinks=1)
# Remove the python3.4 directory
# currently it only holds a few config-directories with symlinks
# and those are not used by anything from this location
if os.path.exists('lib/python3.4'):
shutil.rmtree('lib/python3.4')
if __name__ == "__main__":
main()
|