Merge lp:~gcollura/phablet-tools/click-buddy-python into lp:phablet-tools

Proposed by Giulio Collura
Status: Needs review
Proposed branch: lp:~gcollura/phablet-tools/click-buddy-python
Merge into: lp:phablet-tools
Diff against target: 344 lines (+142/-190)
1 file modified
click-buddy (+142/-190)
To merge this branch: bzr merge lp:~gcollura/phablet-tools/click-buddy-python
Reviewer Review Type Date Requested Status
Ubuntu Phablet Team Pending
Review via email: mp+239673@code.launchpad.net

Commit message

Rewrite click-buddy in python 3. Add more options.

Description of the change

Rewrite click-buddy in python 3.
Add more options, such as --extra-params, which allows to add custom cmake parameters during compilation time.

Thanks for your time,
Giulio

To post a comment you must log in.

Unmerged revisions

331. By Giulio Collura

Rewrite click-buddy in python, add option to manage more cmake flags

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'click-buddy'
2--- click-buddy 2014-09-09 14:46:55 +0000
3+++ click-buddy 2014-10-26 22:09:23 +0000
4@@ -1,4 +1,4 @@
5-#!/bin/sh
6+#!/usr/bin/python3
7 # This program is free software: you can redistribute it and/or modify it
8 # under the terms of the the GNU General Public License version 3, as
9 # published by the Free Software Foundation.
10@@ -12,193 +12,145 @@
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 #
14-# Copyright (C) 2014 Canonical, Ltd.
15+# Copyright (C) 2014 Giulio Collura <giulio.collura@gmail.com>
16 #
17-# Author: Sergio Schvezov <sergio.schvezov@canonical.com>
18-
19-export LC_ALL=C
20-
21-usage() {
22-cat <<EOF
23-usage: $0 [OPTIONS]
24-
25-Creates and runs click apps
26-
27-OPTIONS:
28- -h Show this message
29- -s Specify the serial of the device to install (see adb $ADBOPTS devices)
30-
31- --bzr-source bzr sources used to backreference the package
32- --dir directory holding the package to build
33- --provision Install resulting click and run tests
34- --no-clean Don't clean temporary directories
35- --extra-deps Packages required in the chroot which are not part of the SDK
36- --arch Target architecture, requires a created click chroot
37- (defaults to arch all)
38- --framework Target click framework, requires a created click chroot
39- (defaults to ubuntu-sdk-13.10)
40-EOF
41-}
42-
43-ADBOPTS=""
44-PROVISION=""
45-SOURCE=""
46-BZR_SOURCE=""
47-NO_CLEAN=""
48-EXTRA_DEPS=""
49-ARCH="all"
50-FRAMEWORK="ubuntu-sdk-13.10"
51-MAINTMODE=""
52-
53-TEST_DIR='tests/autopilot'
54-DEVICE_USER='phablet'
55-
56-ARGS=$(getopt -o s:h -l "maint-mode,extra-deps:,bzr-source:,provision,no-clean,framework:,dir:,arch:,help" -n "$0" -- "$@")
57-
58-if [ $? -ne 0 ] ; then
59- exit 1
60-fi
61-eval set -- "$ARGS"
62-
63-while true; do
64- case "$1" in
65- -h|--help)
66- usage
67- exit 0
68- ;;
69- -s)
70- shift
71- ADBOPTS="-s $1"
72- shift
73- ;;
74- --provision)
75- shift
76- PROVISION=1
77- ;;
78- --bzr-source)
79- shift
80- BZR_SOURCE="$1"
81- shift
82- ;;
83- --dir)
84- shift
85- SOURCE="$(readlink -f $1)"
86- shift
87- ;;
88- --no-clean)
89- shift
90- NO_CLEAN=1
91- ;;
92- --arch)
93- shift
94- ARCH="$1"
95- shift
96- ;;
97- --framework)
98- shift
99- FRAMEWORK="$1"
100- shift
101- ;;
102- --extra-deps)
103- shift
104- EXTRA_DEPS="$1"
105- shift
106- ;;
107- --maint-mode)
108- shift
109- MAINTMODE=1
110- ;;
111- --)
112- shift
113- break
114- ;;
115- esac
116-done
117-
118-builddir=$(mktemp -d)
119-installdir=$(mktemp -d)
120-if [ -z "$NO_CLEAN" ]; then
121- trap 'rm -rf "$builddir" "$installdir"' EXIT
122-fi
123-
124-if [ -z "$SOURCE" ]; then
125- SOURCE="$(readlink -f $(pwd))"
126-fi
127-
128-if [ ! -f $SOURCE/CMakeLists.txt ]; then
129- echo "$SOURCE not a valid source dir"
130- usage
131- exit 1
132-fi
133-
134-workdir=$(pwd)
135-cd $builddir
136-
137-CMAKE_PARAMS="-DINSTALL_TESTS=off -DCLICK_MODE=on \
138- -DBZR_REVNO=$(cd $SOURCE; bzr revno)"
139-
140-if [ -n "$BZR_SOURCE" ]; then
141- CMAKE_PARAMS="$CMAKE_PARAMS -DBZR_SOURCE=$BZR_SOURCE"
142-fi
143-
144-set -e
145-if [ "$ARCH" = "all" ]; then
146- cmake "$SOURCE" $CMAKE_PARAMS
147- make DESTDIR=$installdir install
148-else
149- if [ -n "$EXTRA_DEPS" ] && [ -z "$MAINTMODE" ]; then
150- echo "click chroot still doesn\'t support sessions and deps outside the"
151- echo "default sdk were requested, you can optionally use click maint"
152- echo "to install $EXTRA_DEPS by running"
153- echo " click chroot -a$ARCH -f$FRAMEWORK maint apt-get update"
154- echo " click chroot -a$ARCH -f$FRAMEWORK maint apt-get install $EXTRA_DEPS"
155- echo
156- echo "Your chroot will be unclean from then on, so if you want to"
157- echo "guarantee clean builds the chroot would need to be recreated."
158- echo
159- echo "Use the undocumented --maint-mode if you want this automated."
160- exit 1
161- fi
162- cd "$SOURCE"
163- if [ -n "$MAINTMODE" ]; then
164- trap 'click chroot -a$ARCH -f$FRAMEWORK maint apt-get autoremove --yes $EXTRA_DEPS' \
165- EXIT HUP INT TERM
166- click chroot -a$ARCH -f$FRAMEWORK maint apt-get update
167- click chroot -a$ARCH -f$FRAMEWORK maint apt-get install --yes $EXTRA_DEPS
168- fi
169- click chroot -a$ARCH -f$FRAMEWORK run cmake $CMAKE_PARAMS
170- click chroot -a$ARCH -f$FRAMEWORK run make
171- click chroot -a$ARCH -f$FRAMEWORK run make DESTDIR=$installdir install
172-fi
173-
174-if [ ! -f "$installdir/manifest.json" ]; then
175- echo Building failed, check environment
176- exit 1
177-fi
178-
179-click build $installdir
180-if [ "$workdir" != "$(pwd)" ]; then
181- cp *.click $workdir
182-fi
183-
184-if [ -n "$PROVISION" ]; then
185- set -e
186- for click in *.click; do
187- adb $ADBOPTS push "$click" /tmp
188- adb $ADBOPTS shell sudo -u $DEVICE_USER bash -ic "pkcon install-local --allow-untrusted /tmp/$click"
189- done
190-
191- adb $ADBOPTS shell mkdir -p /home/$DEVICE_USER/autopilot
192- adb $ADBOPTS push $SOURCE/tests/autopilot /home/$DEVICE_USER/autopilot
193- adb $ADBOPTS shell chown -R "$DEVICE_USER":"$DEVICE_USER" /home/$DEVICE_USER/autopilot
194- set +e
195-
196- echo Allowing autopilot to play well with apparmor
197- phablet-config autopilot --dbus-probe enable
198- echo "Ready to run autopilot"
199-fi
200-
201-if [ -n "$NO_CLEAN" ]; then
202- echo build dir was $builddir
203- echo install dir was $installdir
204-fi
205+# Author: Giulio Collura <giulio.collura@gmail.com>
206+
207+import argparse
208+import tempfile
209+import sys
210+import os
211+import shutil
212+import subprocess
213+
214+from phabletutils.device import AndroidBridge
215+from phabletutils import fileutils
216+
217+from click.chroot import ClickChroot
218+from gi.repository import Click
219+from click.build import ClickBuildError, ClickBuilder
220+
221+
222+def error(*msgs):
223+ print("ERROR:", *msgs, file=sys.stderr)
224+
225+
226+def execute(command):
227+ proc = subprocess.Popen(command, stderr=subprocess.STDOUT, shell=True)
228+ return proc.communicate()
229+
230+
231+def parse_arguments():
232+ parser = argparse.ArgumentParser(description="Creates and runs click apps")
233+
234+ parser.add_argument("-s", "--serial",
235+ help="Specify the serial of the device to install (see adb devices)")
236+ parser.add_argument("-d", "--dir",
237+ help="Directory holding the package to build, it must have a CMakeLists.txt")
238+ parser.add_argument("--bzr-source",
239+ help="bzr sources used to backreference the package")
240+ parser.add_argument("-p", "--provision", help="Install resulting click and run tests",
241+ action="store_true")
242+ parser.add_argument("-f", "--framework", default="ubuntu-sdk-14.04",
243+ help="Target click framework, requires a created click chroot")
244+ parser.add_argument("--extra-params", default="",
245+ help="Set extra cmake parameters")
246+ parser.add_argument("-a", "--arch", default="all",
247+ help="Target architecture, requires a created click chroot")
248+ parser.add_argument("--extra-deps",
249+ help="Packages required in the chroot which are not part of the SDK")
250+ parser.add_argument("--maint-mode",
251+ help="Automatically remove installed packages from the chroot environment",
252+ action="store_true")
253+ return parser.parse_args()
254+
255+
256+def main(args):
257+ build_dir_td = tempfile.TemporaryDirectory(prefix='click-buddy-build-')
258+ install_dir_td = tempfile.TemporaryDirectory(prefix='click-buddy-install-')
259+
260+ build_dir = build_dir_td.name
261+ install_dir = install_dir_td.name
262+ working_dir = os.getcwd()
263+
264+ if args.dir:
265+ source_dir = os.path.realpath(args.dir)
266+ else:
267+ source_dir = os.getcwd()
268+
269+ if not os.path.isfile(os.path.join(source_dir, "CMakeLists.txt")):
270+ error("{0} not a valid source dir".format(source_dir))
271+ return -1
272+
273+ cmake_params = "-DINSTALL_TESTS=off -DCLICK_MODE=on " + args.extra_params
274+
275+ os.chdir(source_dir)
276+ bzr_revno, err = execute("bzr revno")
277+
278+ if bzr_revno:
279+ print(bzr_revno)
280+ cmake_params += " -DBZR_REVNO={0} ".format(bzr_revno)
281+
282+ if args.bzr_source:
283+ cmake_params += " -DBZR_SOURCE={0} ".format(args.bzr_source)
284+
285+ os.chdir(build_dir)
286+
287+ if args.arch == 'all':
288+ execute("cmake {source} {params}".format(source=source_dir, params=cmake_params))
289+ execute("make DESTDIR={destdir} install".format(destdir=install_dir))
290+ else:
291+ chroot = ClickChroot(args.arch, args.framework, "click")
292+ if not chroot.exists():
293+ error("create a chroot: click chroot create")
294+ return -1
295+ if args.extra_deps:
296+ chroot.maint(*["apt-get", "update"])
297+ chroot.maint(*["apt-get", "install", "--yes"] + args.extra_deps.split(" "))
298+ chroot.run(*["cmake", cmake_params, source_dir])
299+ chroot.run(*["make"])
300+ chroot.run(*["make", "DESTDIR={0}".format(install_dir), "install"])
301+
302+ manifest = os.path.join(install_dir, "manifest.json")
303+ if not os.path.isfile(manifest):
304+ error("Building failed, check environment")
305+ return -1
306+
307+ # execute("click build {dir}".format(dir=install_dir.name))
308+ builder = ClickBuilder()
309+ builder.add_file(install_dir, ".")
310+
311+ try:
312+ click_pkg = builder.build(working_dir, manifest_path=manifest)
313+ except ClickBuildError as e:
314+ error(e)
315+ return -1
316+
317+ print("Click package:", click_pkg)
318+ try:
319+ print("Validating:", click_pkg)
320+ subprocess.check_call(['click-review', click_pkg])
321+ except subprocess.CalledProcessError as e:
322+ error(e)
323+ return -1
324+
325+ if args.provision:
326+ if args.serial:
327+ adb = AndroidBridge(args.serial)
328+ else:
329+ adb = AndroidBridge()
330+ adb.push(click_pkg, "/tmp")
331+ output = adb.shell("pkcon install-local --allow-untrusted /tmp/%s"
332+ % os.path.basename(click_pkg))
333+ print(output.decode('utf-8'))
334+
335+ #TODO: add autopilot support
336+ # Cleanup chroot
337+ if args.extra_deps and args.maint_mode:
338+ chroot.maint(*["apt-get", "autoremove", "--yes"] + args.extra_deps.split(" "))
339+
340+
341+if __name__ == '__main__':
342+ args = parse_arguments()
343+ sys.exit(main(args))
344+

Subscribers

People subscribed via source and target branches