~phablet-team/phablet-tools/trunk

97.2.3 by sergio.schvezov at canonical
Fixing shebang
1
#! /usr/bin/python
97.2.1 by Sergio Schvezov
Initial working demo setup tool
2
# This program is free software: you can redistribute it and/or modify it
3
# under the terms of the the GNU General Public License version 3, as
4
# published by the Free Software Foundation.
5
#
6
# This program is distributed in the hope that it will be useful, but
7
# WITHOUT ANY WARRANTY; without even the implied warranties of
8
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
9
# PURPOSE.  See the applicable version of the GNU Lesser General Public
10
# License for more details.
285.2.1 by Michał Sawicz
Fix pep8 errors.
11
#
97.2.1 by Sergio Schvezov
Initial working demo setup tool
12
# You should have received a copy of the GNU General Public License
13
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
#
15
# Copyright (C) 2013 Canonical, Ltd.
16
17
import argparse
18
import logging
97.2.2 by Sergio Schvezov
Script call on remote
19
import tempfile
20
import StringIO
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
21
import os
97.2.1 by Sergio Schvezov
Initial working demo setup tool
22
from phabletutils.device import AndroidBridge
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
23
from phabletutils import downloads
194.2.2 by Chris Wayne
Using fileutils.create_temp_dir()
24
from phabletutils import fileutils
97.2.1 by Sergio Schvezov
Initial working demo setup tool
25
26
logging.basicConfig(level=logging.INFO, format='%(message)s')
27
log = logging.getLogger()
28
log.name = 'phablet-demo-setup'
29
30
31
basic_template = '''#!/bin/sh
97.2.2 by Sergio Schvezov
Script call on remote
32
set -ex
97.2.1 by Sergio Schvezov
Initial working demo setup tool
33
'''
34
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
35
SERVER_URL = 'http://people.canonical.com/~cwayne/phablet-tools'
194.2.2 by Chris Wayne
Using fileutils.create_temp_dir()
36
TMP_DOWNLOAD_DIR = fileutils.create_temp_dir()
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
37
38
39
def download(artifact):
40
    '''Downloads demo content onto the host, to be pushed to device'''
41
    remote_file = '%s/%s' % (SERVER_URL, artifact)
42
    download_path = os.path.join(TMP_DOWNLOAD_DIR, artifact)
43
    if not os.path.isfile(download_path):
44
        downloads._download(remote_file, download_path)
45
    return download_path
46
182.1.10 by Loïc Minier
PEP8 fixes.
47
97.2.1 by Sergio Schvezov
Initial working demo setup tool
48
def parse_arguments():
49
    '''Parses arguments passed in to script.'''
50
    parser = argparse.ArgumentParser(
51
        description='''phablet demo setup tool. Requires a prior run
52
                       of phablet-networking-setup -i. If called with
53
                       no options all demo content available is setup.''')
54
    parser.add_argument('-s',
55
                        '--serial',
56
                        help='''Device serial. Use when more than
57
                                one device is connected.''',
58
                        )
59
    parser.add_argument('--disable-fake-contacts',
60
                        action='store_true',
61
                        required=False,
62
                        default=False,
97.2.4 by sergio.schvezov at canonical
Fixing help message
63
                        help='Disables setup of demo contacts.'
97.2.1 by Sergio Schvezov
Initial working demo setup tool
64
                        )
65
    parser.add_argument('--disable-fake-messages',
66
                        action='store_true',
67
                        required=False,
68
                        default=False,
97.2.4 by sergio.schvezov at canonical
Fixing help message
69
                        help='Disables setup of demo messages.'
97.2.1 by Sergio Schvezov
Initial working demo setup tool
70
                        )
71
    parser.add_argument('--disable-fake-pictures',
72
                        action='store_true',
73
                        required=False,
74
                        default=False,
97.2.4 by sergio.schvezov at canonical
Fixing help message
75
                        help='Disables setup of demo pictures.'
97.2.1 by Sergio Schvezov
Initial working demo setup tool
76
                        )
77
    return parser.parse_args()
78
79
97.2.2 by Sergio Schvezov
Script call on remote
80
def setup_fake_pictures(script):
81
    script.write('tar -C /home/phablet/Pictures/ --strip-components=1 -xzf '
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
82
                 '/tmp/pictures.tgz\n')
97.2.2 by Sergio Schvezov
Script call on remote
83
    return script
84
85
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
86
def setup_fake_contacts(script):
87
    script.write('tar -C /home/phablet -xzf /tmp/contacts.tgz \n')
319.1.1 by Giulio Collura
fix bug #1373779
88
    script.write('/home/phablet/.local/bin/manage-address-books create \n')
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
89
    return script
90
91
97.2.2 by Sergio Schvezov
Script call on remote
92
def setup_fake_conversations(script):
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
93
    script.write('tar -C /home/phablet -xzf /tmp/conversations.tgz \n')
97.2.2 by Sergio Schvezov
Script call on remote
94
    return script
95
96
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
97
def provision_device(adb, serial, script):
98
    script_dst = '/tmp/provision'
97.2.2 by Sergio Schvezov
Script call on remote
99
    adb.push(script, script_dst)
100
    adb.chmod(script_dst, '700')
319.1.1 by Giulio Collura
fix bug #1373779
101
    adb.shell('sh ' + script_dst)
97.2.2 by Sergio Schvezov
Script call on remote
102
    log.info('Getting ready to restart')
103
    adb.shell('sync')
104
105
106
def main(args):
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
107
    if args.serial:
108
        adb = AndroidBridge(args.serial)
109
    else:
110
        adb = AndroidBridge()
111
    dest = '/tmp'
97.2.2 by Sergio Schvezov
Script call on remote
112
    script = StringIO.StringIO()
113
    script.write(basic_template)
97.2.1 by Sergio Schvezov
Initial working demo setup tool
114
    if not args.disable_fake_contacts:
97.2.2 by Sergio Schvezov
Script call on remote
115
        log.info('Setting up for demo contacts')
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
116
        contact_tar = download('contacts.tgz')
117
        adb.push(contact_tar, dest)
118
        script = setup_fake_contacts(script)
119
    if not args.disable_fake_messages:
120
        log.info('Setting up for demo conversations')
121
        convo_tar = download('conversations.tgz')
122
        adb.push(convo_tar, dest)
97.2.2 by Sergio Schvezov
Script call on remote
123
        script = setup_fake_conversations(script)
97.2.1 by Sergio Schvezov
Initial working demo setup tool
124
    if not args.disable_fake_pictures:
97.2.2 by Sergio Schvezov
Script call on remote
125
        log.info('Setting up for demo pictures')
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
126
        pictures_tar = download('pictures.tgz')
127
        adb.push(pictures_tar, dest)
97.2.2 by Sergio Schvezov
Script call on remote
128
        script = setup_fake_pictures(script)
182.1.10 by Loïc Minier
PEP8 fixes.
129
    script_file = tempfile.NamedTemporaryFile(delete=False)
97.2.2 by Sergio Schvezov
Script call on remote
130
    with script_file as f:
131
        f.write(script.getvalue())
182.1.10 by Loïc Minier
PEP8 fixes.
132
    script.close()
194.2.1 by Chris Wayne
Make phablet-demo-setup work on r/o image
133
    provision_device(adb, args.serial, script_file.name)
97.2.1 by Sergio Schvezov
Initial working demo setup tool
134
135
136
if __name__ == "__main__":
137
    args = parse_arguments()
138
    main(args)