~nskaggs/phablet-tools/add-lp-branch-support

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
#! /usr/bin/python
# This program is free software: you can redistribute it and/or modify it
# under the terms of the the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the applicable version of the GNU Lesser General Public
# License for more details.
#.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2013 Canonical, Ltd.

import argparse
import logging
import tempfile
import StringIO
import os
from phabletutils.device import AndroidBridge
from phabletutils import downloads
from phabletutils import fileutils

logging.basicConfig(level=logging.INFO, format='%(message)s')
log = logging.getLogger()
log.name = 'phablet-demo-setup'


basic_template = '''#!/bin/sh
set -ex
'''

SERVER_URL = 'http://people.canonical.com/~cwayne/phablet-tools'
TMP_DOWNLOAD_DIR = fileutils.create_temp_dir()


def download(artifact):
    '''Downloads demo content onto the host, to be pushed to device'''
    remote_file = '%s/%s' % (SERVER_URL, artifact)
    download_path = os.path.join(TMP_DOWNLOAD_DIR, artifact)
    if not os.path.isfile(download_path):
        downloads._download(remote_file, download_path)
    return download_path


def parse_arguments():
    '''Parses arguments passed in to script.'''
    parser = argparse.ArgumentParser(
        description='''phablet demo setup tool. Requires a prior run
                       of phablet-networking-setup -i. If called with
                       no options all demo content available is setup.''')
    parser.add_argument('-s',
                        '--serial',
                        help='''Device serial. Use when more than
                                one device is connected.''',
                        )
    parser.add_argument('--disable-fake-contacts',
                        action='store_true',
                        required=False,
                        default=False,
                        help='Disables setup of demo contacts.'
                        )
    parser.add_argument('--disable-fake-messages',
                        action='store_true',
                        required=False,
                        default=False,
                        help='Disables setup of demo messages.'
                        )
    parser.add_argument('--disable-fake-pictures',
                        action='store_true',
                        required=False,
                        default=False,
                        help='Disables setup of demo pictures.'
                        )
    return parser.parse_args()


def setup_fake_pictures(script):
    script.write('tar -C /home/phablet/Pictures/ --strip-components=1 -xzf '
                 '/tmp/pictures.tgz\n')
    script.write('chown -R phablet:phablet /home/phablet/Pictures\n')
    return script


def setup_fake_contacts(script):
    script.write('tar -C /home/phablet -xzf /tmp/contacts.tgz \n')
    script.write(' su - phablet '
                 '-c "/home/phablet/.local/bin/manage-address-books create"\n')
    script.write('chown -R phablet:phablet /home/phablet/.local \n')
    return script


def setup_fake_conversations(script):
    script.write('tar -C /home/phablet -xzf /tmp/conversations.tgz \n')
    script.write('chown -R phablet:phablet /home/phablet/.local \n')
    return script


def provision_device(adb, serial, script):
    script_dst = '/tmp/provision'
    adb.root()
    adb.push(script, script_dst)
    adb.chmod(script_dst, '700')
    adb.shell('sh /tmp/provision')
    log.info('Getting ready to restart')
    adb.shell('sync')
    adb.reboot()


def main(args):
    if args.serial:
        adb = AndroidBridge(args.serial)
    else:
        adb = AndroidBridge()
    dest = '/tmp'
    script = StringIO.StringIO()
    script.write(basic_template)
    if not args.disable_fake_contacts:
        log.info('Setting up for demo contacts')
        contact_tar = download('contacts.tgz')
        adb.push(contact_tar, dest)
        script = setup_fake_contacts(script)
    if not args.disable_fake_messages:
        log.info('Setting up for demo conversations')
        convo_tar = download('conversations.tgz')
        adb.push(convo_tar, dest)
        script = setup_fake_conversations(script)
    if not args.disable_fake_pictures:
        log.info('Setting up for demo pictures')
        pictures_tar = download('pictures.tgz')
        adb.push(pictures_tar, dest)
        script = setup_fake_pictures(script)
    script_file = tempfile.NamedTemporaryFile(delete=False)
    with script_file as f:
        f.write(script.getvalue())
    script.close()
    provision_device(adb, args.serial, script_file.name)


if __name__ == "__main__":
    args = parse_arguments()
    main(args)