Revision 0ae01e26 image_creator/main.py
b/image_creator/main.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
from image_creator import get_os_class |
35 | 35 |
from image_creator import __version__ as version |
36 |
from image_creator import FatalError |
|
36 | 37 |
from image_creator.disk import Disk |
37 | 38 |
from image_creator.util import get_command |
38 | 39 |
|
... | ... | |
43 | 44 |
dd = get_command('dd') |
44 | 45 |
|
45 | 46 |
|
46 |
class FatalError(Exception): |
|
47 |
pass |
|
47 |
def check_writable_dir(option, opt_str, value, parser): |
|
48 |
dirname = os.path.dirname(value) |
|
49 |
name = os.path.basename(value) |
|
50 |
if dirname and not os.path.isdir(dirname): |
|
51 |
parser.error("`%s' is not an existing directory" % dirname) |
|
48 | 52 |
|
53 |
if not name: |
|
54 |
parser.error("`%s' is not a valid file name" % dirname) |
|
49 | 55 |
|
50 |
def check_writable_dir(option, opt_str, value, parser): |
|
51 |
if not os.path.isdir(value): |
|
52 |
raise OptionValueError("%s is not a valid directory name" % value) |
|
53 | 56 |
setattr(parser.values, option.dest, value) |
54 | 57 |
|
55 | 58 |
|
56 | 59 |
def parse_options(input_args): |
57 |
usage = "Usage: %prog [options] <input_media> <name>"
|
|
60 |
usage = "Usage: %prog [options] <input_media>" |
|
58 | 61 |
parser = optparse.OptionParser(version=version, usage=usage) |
59 | 62 |
|
60 |
parser.add_option("-o", "--outdir", type="string", dest="outdir", |
|
61 |
default=".", action="callback", callback=check_writable_dir, |
|
62 |
help="Output files to DIR [default: working dir]", |
|
63 |
metavar="DIR") |
|
64 |
|
|
65 | 63 |
parser.add_option("-f", "--force", dest="force", default=False, |
66 | 64 |
action="store_true", help="Overwrite output files if they exist") |
67 | 65 |
|
... | ... | |
77 | 75 |
help="Don't shrink any partition before extracting the image", |
78 | 76 |
action="store_false") |
79 | 77 |
|
78 |
parser.add_option("-o", "--outfile", type="string", dest="outfile", |
|
79 |
default=None, action="callback", callback=check_writable_dir, |
|
80 |
help="Output image file", |
|
81 |
metavar="FILE") |
|
82 |
|
|
80 | 83 |
parser.add_option("-u", "--upload", dest="upload", default=False, |
81 | 84 |
help="Upload image to a pithos repository using kamaki", |
82 | 85 |
action="store_true") |
... | ... | |
86 | 89 |
|
87 | 90 |
options, args = parser.parse_args(input_args) |
88 | 91 |
|
89 |
if len(args) != 2:
|
|
90 |
parser.error('input media or name are missing')
|
|
92 |
if len(args) != 1:
|
|
93 |
parser.error('Wrong number of arguments')
|
|
91 | 94 |
options.source = args[0] |
92 |
options.name = args[1] |
|
93 |
|
|
94 | 95 |
if not os.path.exists(options.source): |
95 |
parser.error('Input media is not accessible')
|
|
96 |
parser.error('input media is not accessible')
|
|
96 | 97 |
|
97 | 98 |
if options.register: |
98 | 99 |
options.upload = True |
99 | 100 |
|
101 |
if options.outfile is None and not options.upload: |
|
102 |
parser.error('either outfile (-o) or upload (-u) must be set.') |
|
103 |
|
|
100 | 104 |
return options |
101 | 105 |
|
102 | 106 |
|
103 |
def main():
|
|
107 |
def image_creator():
|
|
104 | 108 |
|
105 | 109 |
options = parse_options(sys.argv[1:]) |
106 | 110 |
|
... | ... | |
109 | 113 |
% os.path.basename(sys.argv[0])) |
110 | 114 |
|
111 | 115 |
if not options.force: |
112 |
for extension in ('diskdump', 'meta'):
|
|
113 |
filename = "%s/%s.%s" % (options.outdir, options.name, extension)
|
|
116 |
for extension in ('', '.meta'):
|
|
117 |
filename = "%s%s" % (options.outfile, extension)
|
|
114 | 118 |
if os.path.exists(filename): |
115 | 119 |
raise FatalError("Output file %s exists " |
116 | 120 |
"(use --force to overwrite it)." % filename) |
... | ... | |
133 | 137 |
|
134 | 138 |
size = options.shrink and dev.shrink() or dev.size() |
135 | 139 |
metadata['size'] = str(size // 2 ** 20) |
140 |
|
|
141 |
outfile = "" |
|
142 |
if options.outfile is not None: |
|
143 |
outfile = options.outfile |
|
144 |
f = open('%s.%s' % (options.outfile, 'meta'), 'w') |
|
145 |
try: |
|
146 |
for key in metadata.keys(): |
|
147 |
f.write("%s=%s\n" % (key, metadata[key])) |
|
148 |
finally: |
|
149 |
f.close() |
|
150 |
else: |
|
151 |
outfd, outfile = tmpfile.mkstemp() |
|
152 |
os.close(outfd) |
|
153 |
|
|
136 | 154 |
dd('if=%s' % dev.device, |
137 |
'of=%s/%s.%s' % (options.outdir, options.name, 'diskdump'),
|
|
155 |
'of=%s' % outfile,
|
|
138 | 156 |
'bs=4M', 'count=%d' % ((size + 1) // 2 ** 22)) |
139 | 157 |
|
140 |
f = open('%s/%s.%s' % (options.outdir, options.name, 'meta'), 'w') |
|
141 |
for key in metadata.keys(): |
|
142 |
f.write("%s=%s\n" % (key, metadata[key])) |
|
143 |
f.close() |
|
144 | 158 |
finally: |
145 | 159 |
disk.cleanup() |
146 | 160 |
|
147 | 161 |
#The image is ready, lets call kamaki if necessary |
148 | 162 |
if options.upload: |
149 |
pass |
|
163 |
pass |
|
164 |
|
|
165 |
if options.outfile is None: |
|
166 |
os.unlink(outfile) |
|
150 | 167 |
|
151 | 168 |
return 0 |
152 | 169 |
|
153 | 170 |
COLOR_BLACK = "\033[00m" |
154 | 171 |
COLOR_RED = "\033[1;31m" |
155 | 172 |
|
156 |
if __name__ == '__main__':
|
|
173 |
def main():
|
|
157 | 174 |
try: |
158 |
ret = main()
|
|
175 |
ret = image_creator()
|
|
159 | 176 |
sys.exit(ret) |
160 | 177 |
except FatalError as e: |
161 | 178 |
print >> sys.stderr, "\n%sError: %s%s\n" % (COLOR_RED, e, COLOR_BLACK) |
162 | 179 |
sys.exit(1) |
163 | 180 |
|
181 |
|
|
182 |
if __name__ == '__main__': |
|
183 |
main() |
|
164 | 184 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
Also available in: Unified diff