tomb

the crypto undertaker
git clone git://parazyd.org/tomb.git
Log | Files | Refs | README | LICENSE

tomb.py (3027B)


      1 '''
      2 Module structure:
      3 
      4 this contain a class, which is indeed just a collection of functions
      5 (the methods are all static).
      6 It's meant to behave in a way which is similar to the command line, for
      7     
      8 Notes: consider moving to a more typical usage (instantiate, then use method)
      9 to make it more configurable (ie set the tomb executable path).
     10 '''
     11 
     12 import subprocess
     13 
     14 class Tomb(object):
     15     '''
     16     This is just a collection of static methods, so you should NOT instantiate
     17 
     18     The methods are meant to resemble the command line interface as much as
     19     possible
     20 
     21     There is no support to things like threading, multiprocessing, whatever.
     22     If you want to interact asynchronously with tomb, just do it in a separate
     23     layer.
     24     '''
     25     tombexec = 'tomb'
     26     def _check_exec_path(self):
     27         '''Checks, using which, if tomb is available.
     28         Returns None on error, the path on success.
     29         '''
     30         try:
     31             path = subprocess.check_output(['which', 'tomb'])
     32         except subprocess.CalledProcessError:
     33             return None
     34         return path
     35 
     36     @classmethod
     37     def check(cls, command, stdout=None, stderr=None, no_color=True, ignore_swap=False):
     38         args = [command]
     39         if no_color:
     40             args += ['--no-color']
     41         if ignore_swap:
     42             args += ['--ignore-swap']
     43         try:
     44             subprocess.check_call([cls.tombexec, 'check'] + args, stdout=stdout, stderr=stderr)
     45         except subprocess.CalledProcessError:
     46             return False
     47         return True
     48 
     49     @classmethod
     50     def create(cls, tombpath, tombsize,keypath, stdout=None, stderr=None,
     51             no_color=True, ignore_swap=False):
     52         '''If keypath is None, it will be created adjacent to the tomb.
     53         This is unsafe, and you should NOT do it.
     54 
     55         Note that this will invoke pinentry
     56 
     57         no_color is supported as an option for short-lived retrocompatibility:
     58             it will be removed as soon as no-color will be integrated
     59         '''
     60         args = [tombpath, '-s', tombsize]
     61         if keypath is not None:
     62             args += ['-k', keypath]
     63         if no_color:
     64             args += ['--no-color']
     65         if ignore_swap:
     66             args += ['--ignore-swap']
     67         try:
     68             subprocess.check_call([cls.tombexec, 'create'] + args, stdout=stdout, stderr=stderr)
     69         except subprocess.CalledProcessError:
     70             return False
     71         return True
     72 
     73     @classmethod
     74     def open(cls, tombpath,keypath=None, no_color=True, ignore_swap=False):
     75         args = [tombpath]
     76         if keypath is not None:
     77             args += ['-k', keypath]
     78         if no_color:
     79             args += ['--no-color']
     80         if ignore_swap:
     81             args += ['--ignore-swap']
     82         try:
     83             subprocess.check_call([cls.tombexec, 'open'] + args)
     84         except subprocess.CalledProcessError:
     85             return False
     86         return True
     87 
     88 
     89 if __name__ == '__main__':
     90     #Debug stuff. Also useful for an example
     91     print Tomb.create('/tmp/a.tomb', '10', '/tmp/akey')