OLD | NEW |
1 #!/usr/bin/python2.6.2 | 1 #!/usr/bin/python2.6.2 |
2 # Copyright 2009, Google Inc. | 2 # Copyright 2009, Google Inc. |
3 # All rights reserved. | 3 # All rights reserved. |
4 # | 4 # |
5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
7 # met: | 7 # met: |
8 # | 8 # |
9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 | 285 |
286 def Download(url, prefix_dir): | 286 def Download(url, prefix_dir): |
287 """Downloads single file at |url| to |prefix_dir|. | 287 """Downloads single file at |url| to |prefix_dir|. |
288 | 288 |
289 Returns: | 289 Returns: |
290 local_path: the path to the downloaded file, or None if download was | 290 local_path: the path to the downloaded file, or None if download was |
291 unsuccessful.""" | 291 unsuccessful.""" |
292 | 292 |
293 parsed_url = urlparse.urlparse(url) | 293 parsed_url = urlparse.urlparse(url) |
294 path = parsed_url[2] | 294 path = parsed_url[2] |
295 cut = path.count('/') - 1 | |
296 name = path.rsplit('/', 1)[1] | 295 name = path.rsplit('/', 1)[1] |
297 local_path = os.path.join(prefix_dir, name) | 296 local_path = os.path.join(prefix_dir, name) |
298 | 297 |
299 if not os.path.exists(prefix_dir): | 298 if not os.path.exists(prefix_dir): |
300 os.mkdir(prefix_dir) | 299 os.mkdir(prefix_dir) |
301 urllib.urlretrieve(url, local_path) | 300 urllib.urlretrieve(url, local_path) |
302 | 301 |
303 if not os.path.exists(local_path): | 302 if not os.path.exists(local_path): |
304 logging.error('Could not download %s to %s' % (url, local_path)) | 303 logging.error('Could not download %s to %s' % (url, local_path)) |
305 return None | 304 return None |
(...skipping 13 matching lines...) Expand all Loading... |
319 RegMaybeDel(path, name, ERROR) | 318 RegMaybeDel(path, name, ERROR) |
320 | 319 |
321 | 320 |
322 def MountDiskImage(dmg_path): | 321 def MountDiskImage(dmg_path): |
323 """Mounts disk image. | 322 """Mounts disk image. |
324 | 323 |
325 Args: | 324 Args: |
326 dmg_path: path to image that will be mounted. | 325 dmg_path: path to image that will be mounted. |
327 | 326 |
328 Returns: | 327 Returns: |
329 Path to mounted disk on success or None on failure. | 328 Tuple contaiing device path and mounted path on success, |
| 329 or None on failure. |
330 """ | 330 """ |
331 mount_path = None | 331 mount_path = None |
332 logging.info('Mounting %s...' % dmg_path) | 332 logging.info('Mounting %s...' % dmg_path) |
333 | 333 |
334 cmd = ['hdiutil', 'attach', '"' + dmg_path + '"'] | 334 cmd = ['hdiutil', 'attach', '"' + dmg_path + '"'] |
335 (return_code, output) = RunWithOutput(cmd) | 335 (return_code, output) = RunWithOutput(cmd) |
336 | 336 |
337 if return_code == 0 and output: | 337 if return_code == 0 and output: |
338 # Attempt to grab the mounted path from the command output. | 338 # Attempt to grab the mounted path from the command output. |
339 # This should be safe regardless of actual output. | 339 # This should be safe regardless of actual output. |
340 mount_path = output.strip().split('\n')[-1].split('\t')[-1] | 340 new_device = output.strip().split('\n')[-1].split('\t') |
| 341 device = new_device[0].strip() |
| 342 mount_path = new_device[-1] |
341 | 343 |
342 logging.info('Disk image mounted at %s' % mount_path) | 344 logging.info('Device %s mounted at %s' % (device,mount_path)) |
343 | 345 |
344 # Wait for mounting operation to complete. | 346 # Wait for mounting operation to complete. |
345 time.sleep(10) | 347 time.sleep(10) |
346 if os.path.exists(mount_path): | 348 if os.path.exists(mount_path): |
347 logging.info('Mount point is accessible.') | 349 logging.info('Mount point is accessible.') |
348 else: | 350 else: |
349 mount_path = None | 351 mount_path = None |
350 | 352 |
351 if mount_path is None: | 353 if mount_path is None or device is None: |
352 logging.error('Could not mount properly.') | 354 logging.error('Could not mount properly.') |
| 355 return None |
353 | 356 |
354 return mount_path | 357 return (device, mount_path) |
355 | 358 |
356 | 359 |
357 def UnmountDiskImage(mount_path): | 360 def UnmountDiskImage(device): |
358 """Unmounts disk image. | 361 """Unmounts disk image. |
359 | 362 |
360 Args: | 363 Args: |
361 mount_path: path to unmount. | 364 device: path to device to be detached |
362 | 365 |
363 Returns: | 366 Returns: |
364 True on success. | 367 True on success. |
365 """ | 368 """ |
366 logging.info('Unmounting %s...' % mount_path) | 369 logging.info('Unmounting device %s...' % device) |
367 | 370 |
368 if not os.path.exists(mount_path): | 371 return Run(['hdiutil detach', '"' + device + '"', '-force']) == 0 |
369 logging.warn('Nothing is mounted at this path.') | |
370 return True | |
371 | |
372 Run(['umount', '"' + mount_path + '"']) | |
373 | |
374 time.sleep(10) | |
375 if os.path.exists(mount_path): | |
376 logging.error('Image is still mounted at path:"%s"', mount_path) | |
377 return False | |
378 else: | |
379 return True | |
OLD | NEW |