3
# Copyright (C) 2008 Canonical Ltd.
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.
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.
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/>.
26
LOCALEDIR = "/usr/share/locale"
27
locale.setlocale(locale.LC_ALL, '')
28
gettext.bindtextdomain('usbcreator', LOCALEDIR)
29
gettext.textdomain('usbcreator')
32
__builtin__._ = gettext.gettext
36
print >>sys.stderr, str(cmd)
37
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
38
stderr=sys.stderr, stdin=subprocess.PIPE)
42
def main(source, target, persist):
43
# Some of the code in this function was copied from Ubiquity's
46
sys.stdout.write(_('Copying files\n'))
48
if not os.path.exists(source) or not os.path.exists(target):
49
print >>sys.stderr, 'Source or target does not exist.'
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):
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' % \
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):
84
sourcefh = open(sourcepath, 'rb')
85
targetfh = open(targetpath, 'wb')
88
shutil.copyfileobj(sourcefh, targetfh)
91
print >>sys.stderr, str(e) + '\n'
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']:
106
f = open(os.path.join(target, filename), 'r')
109
for line in f.readlines():
110
line = line.strip('\n').split(' ')
115
if command.lower() == 'append':
116
pos = line.index(command) + 2
117
if label not in ('check', 'memtest', 'hd'):
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')
127
f = open(os.path.join(target, filename), 'w')
128
f.writelines(to_write)
130
print >>sys.stderr, str(e) + '\n'
131
print >>sys.stderr, 'Unable to add persistence to the ' \
132
'configuration (%s)\n' % filename
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])
143
sys.stdout.write(_('Creating persistence file\n'))
145
popen(['dd', 'if=/dev/zero', 'bs=1M', 'of=%s/casper-rw' % target, 'count=%s' % persist])
146
sys.stdout.write(_('Making persistence filesystem\n'))
148
popen(['mkfs.ext3', '-F', '%s/casper-rw' % target])
149
sys.stdout.write('Syncing media to disk. Please Wait\n')
153
if __name__ == '__main__':
158
opts, args = getopt.getopt(sys.argv[1:], 's:t:p:')
159
except getopt.GetoptError:
161
for opt, arg in opts:
168
if source and target:
169
main(source, target, persist)
172
print >> sys.stderr, \
173
'Source or target device not specified. Cannot continue.\n'