~ubuntu-branches/ubuntu/maverick/usb-creator/maverick

« back to all changes in this revision

Viewing changes to scripts/install.py

  • Committer: Bazaar Package Importer
  • Author(s): Evan Dandrea, Evan Dandrea, Roderick B. Greening
  • Date: 2009-08-26 21:16:18 UTC
  • Revision ID: james.westby@ubuntu.com-20090826211618-d1p2wkg661375fz9
Tags: 0.2.3
[ Evan Dandrea ]
* Depend on python-qt4-dbus.  Thanks Daniel T. Chen (LP: #404553).
* New KDE icon.  Thanks Jonathan Riddell and Ken Wimer!
* Massively cleaned up the structure of the usb-creator code.
* Replaced the HAL backend with a DeviceKit-disks backend.
* Added a Windows frontend and backend (built outside the archive).
* Manage the install routine and progress feedback in separate threads,
  rather than a separate process.
* Replace dependency on parted and mtools with devicekit-disks.

[ Roderick B. Greening ]
* Update ui file name for KDE.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
# Copyright (C) 2008 Canonical Ltd.
4
 
 
5
 
# This program is free software: you can redistribute it and/or modify
6
 
# it under the terms of the GNU General Public License version 3,
7
 
# as published by the Free Software Foundation.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
 
17
 
import getopt
18
 
import os
19
 
import stat
20
 
import sys
21
 
import shutil
22
 
import subprocess
23
 
import gettext
24
 
import locale
25
 
 
26
 
LOCALEDIR = "/usr/share/locale"
27
 
locale.setlocale(locale.LC_ALL, '')
28
 
gettext.bindtextdomain('usbcreator', LOCALEDIR)
29
 
gettext.textdomain('usbcreator')
30
 
 
31
 
import __builtin__
32
 
__builtin__._ = gettext.gettext
33
 
 
34
 
 
35
 
def popen(cmd):
36
 
    print >>sys.stderr, str(cmd)
37
 
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
38
 
        stderr=sys.stderr, stdin=subprocess.PIPE)
39
 
    process.communicate()
40
 
    return process
41
 
 
42
 
def main(source, target, persist):
43
 
    # Some of the code in this function was copied from Ubiquity's
44
 
    # scripts/install.py
45
 
 
46
 
    sys.stdout.write(_('Copying files\n'))
47
 
    sys.stdout.flush()
48
 
    if not os.path.exists(source) or not os.path.exists(target):
49
 
        print >>sys.stderr, 'Source or target does not exist.'
50
 
        sys.exit(1)
51
 
    for dirpath, dirnames, filenames in os.walk(source):
52
 
        sp = dirpath[len(source) + 1:]
53
 
        for name in dirnames + filenames:
54
 
            relpath = os.path.join(sp, name)
55
 
            sourcepath = os.path.join(source, relpath)
56
 
            targetpath = os.path.join(target, relpath)
57
 
            st = os.lstat(sourcepath)
58
 
            mode = stat.S_IMODE(st.st_mode)
59
 
            if stat.S_ISLNK(st.st_mode):
60
 
                if os.path.lexists(targetpath):
61
 
                    os.unlink(targetpath)
62
 
                linkto = os.readlink(sourcepath)
63
 
                #os.symlink(linkto, targetpath)
64
 
                # FIXME: Handle this somehow?
65
 
                sys.stderr.write('Tried to symlink %s -> %s\n' % \
66
 
                    (linkto, targetpath))
67
 
                pass
68
 
            elif stat.S_ISDIR(st.st_mode):
69
 
                if not os.path.isdir(targetpath):
70
 
                    os.mkdir(targetpath, mode)
71
 
            elif stat.S_ISCHR(st.st_mode):
72
 
                os.mknod(targetpath, stat.S_IFCHR | mode, st.st_rdev)
73
 
            elif stat.S_ISBLK(st.st_mode):
74
 
                os.mknod(targetpath, stat.S_IFBLK | mode, st.st_rdev)
75
 
            elif stat.S_ISFIFO(st.st_mode):
76
 
                os.mknod(targetpath, stat.S_IFIFO | mode)
77
 
            elif stat.S_ISSOCK(st.st_mode):
78
 
                os.mknod(targetpath, stat.S_IFSOCK | mode)
79
 
            elif stat.S_ISREG(st.st_mode):
80
 
                if os.path.exists(targetpath):
81
 
                    os.unlink(targetpath)
82
 
                fail = False
83
 
                try:
84
 
                    sourcefh = open(sourcepath, 'rb')
85
 
                    targetfh = open(targetpath, 'wb')
86
 
                    # TODO: md5 check.
87
 
                    try:
88
 
                        shutil.copyfileobj(sourcefh, targetfh)
89
 
                    except Exception, e:
90
 
                        fail = True
91
 
                        print >>sys.stderr, str(e) + '\n'
92
 
                finally:
93
 
                    sourcefh.close()
94
 
                    targetfh.close()
95
 
                    if fail:
96
 
                        sys.exit(1)
97
 
    
98
 
    # Modify contents to be suitable for a USB drive.
99
 
    popen(['rm', '-rf', '%s/syslinux' % target])
100
 
    popen(['mv', '%s/isolinux' % target, '%s/syslinux' % target])
101
 
    popen(['mv', '%s/syslinux/isolinux.cfg' % target,
102
 
            '%s/syslinux/syslinux.cfg' % target])
103
 
    for filename in ['syslinux/syslinux.cfg', 'syslinux/text.cfg']:
104
 
        f = None
105
 
        try:
106
 
            f = open(os.path.join(target, filename), 'r')
107
 
            label = ''
108
 
            to_write = []
109
 
            for line in f.readlines():
110
 
                line = line.strip('\n').split(' ')
111
 
                for l in line:
112
 
                    if l:
113
 
                        command = l
114
 
                        break
115
 
                if command.lower() == 'append':
116
 
                    pos = line.index(command) + 2
117
 
                    if label not in ('check', 'memtest', 'hd'):
118
 
                        if persist != '0':
119
 
                            line.insert(pos, 'persistent')
120
 
                        line.insert(pos, 'cdrom-detect/try-usb=true')
121
 
                    if label not in ('memtest', 'hd'):
122
 
                        line.insert(pos, 'noprompt')
123
 
                elif command.lower() == 'label':
124
 
                    label = line[1].strip()
125
 
                to_write.append(' '.join(line) + '\n')
126
 
            f.close()
127
 
            f = open(os.path.join(target, filename), 'w')
128
 
            f.writelines(to_write)
129
 
        except Exception, e:
130
 
            print >>sys.stderr, str(e) + '\n'
131
 
            print >>sys.stderr, 'Unable to add persistence to the ' \
132
 
                'configuration (%s)\n' % filename
133
 
        finally:
134
 
            if f:
135
 
                f.close()
136
 
    
137
 
    # /syslinux.cfg is present to work around a bug.
138
 
    # TODO: find bug number.  Wasn't this fixed in Intrepid?
139
 
    popen(['cp', '%s/syslinux/syslinux.cfg' % target,
140
 
        '%s/syslinux.cfg' % target])
141
 
    
142
 
    if persist != '0':
143
 
        sys.stdout.write(_('Creating persistence file\n'))
144
 
        sys.stdout.flush()
145
 
        popen(['dd', 'if=/dev/zero', 'bs=1M', 'of=%s/casper-rw' % target, 'count=%s' % persist])
146
 
        sys.stdout.write(_('Making persistence filesystem\n'))
147
 
        sys.stdout.flush()
148
 
        popen(['mkfs.ext3', '-F', '%s/casper-rw' % target])
149
 
    sys.stdout.write('Syncing media to disk. Please Wait\n')
150
 
    sys.stdout.flush()
151
 
    popen(['sync'])
152
 
 
153
 
if __name__ == '__main__':
154
 
    source = ''
155
 
    target = ''
156
 
    persist = 0
157
 
    try:
158
 
        opts, args = getopt.getopt(sys.argv[1:], 's:t:p:')
159
 
    except getopt.GetoptError:
160
 
        sys.exit(1)
161
 
    for opt, arg in opts:
162
 
        if opt == '-s':
163
 
            source = arg
164
 
        elif opt == '-t':
165
 
            target = arg
166
 
        elif opt == '-p':
167
 
            persist = arg
168
 
    if source and target:
169
 
        main(source, target, persist)
170
 
        sys.exit(0)
171
 
    else:
172
 
        print >> sys.stderr, \
173
 
            'Source or target device not specified.  Cannot continue.\n'
174
 
        sys.exit(1)
175