Guest User

Untitled

a guest
Jan 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. """RAM disk creation (MacOS X only for now).
  4.  
  5. Usage:
  6.  
  7. with ramdisk(10240000, 'ใƒ†ใ‚นใƒˆ1็•ช') as x:
  8. print "Mountpoint is: %s" % x.path
  9. # use it
  10. print 'Closing...'
  11. """
  12.  
  13. import os
  14. import pipes
  15. import subprocess
  16. import sys
  17. import uuid
  18. from contextlib import contextmanager
  19.  
  20.  
  21. DEFAULT_SIZE = 1024000
  22.  
  23.  
  24. if sys.platform != 'darwin':
  25. raise OSError('Only MacOS X supported for now')
  26.  
  27.  
  28. import plistlib
  29.  
  30.  
  31. class RamDisk(object):
  32. def __init__(self, size=DEFAULT_SIZE, name=None):
  33. if not name:
  34. name = 'ramdisk-%s' % uuid.uuid4().hex
  35. self.device = subprocess.check_output(
  36. ['/usr/bin/hdiutil', 'attach' ,'-nomount' ,'ram://%i' % ((size + 511) / 512)]
  37. ).strip()
  38. subprocess.check_output(
  39. ['/usr/sbin/diskutil', 'erasevolume', 'hfsx', name, self.device]
  40. )
  41. self.path = plistlib.readPlistFromString(
  42. subprocess.check_output(
  43. ['/usr/sbin/diskutil', 'info', '-plist', self.device]
  44. )
  45. )['MountPoint']
  46.  
  47. def close(self):
  48. subprocess.check_output(
  49. ['/usr/sbin/diskutil', 'eject', self.device]
  50. )
  51.  
  52.  
  53. @contextmanager
  54. def ramdisk(size=DEFAULT_SIZE, name=None):
  55. x = RamDisk(size, name)
  56. yield x
  57. x.close()
Add Comment
Please, Sign In to add comment