2011-10-26 22:03:15 +00:00
|
|
|
'''
|
|
|
|
Module structure:
|
|
|
|
|
|
|
|
this contain a class, which is indeed just a collection of functions
|
|
|
|
(the methods are all static).
|
|
|
|
It's meant to behave in a way which is similar to the command line, for
|
|
|
|
|
|
|
|
Notes: consider moving to a more typical usage (instantiate, then use method)
|
|
|
|
to make it more configurable (ie set the tomb executable path).
|
|
|
|
'''
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
class Tomb(object):
|
|
|
|
'''
|
|
|
|
This is just a collection of static methods, so you should NOT instantiate
|
|
|
|
|
|
|
|
The methods are meant to resemble the command line interface as much as
|
|
|
|
possible
|
|
|
|
|
|
|
|
There is no support to things like threading, multiprocessing, whatever.
|
|
|
|
If you want to interact asynchronously with tomb, just do it in a separate
|
|
|
|
layer.
|
|
|
|
'''
|
2011-10-30 21:07:16 +00:00
|
|
|
tombexec = 'tomb'
|
2011-10-26 22:03:15 +00:00
|
|
|
def _check_exec_path(self):
|
|
|
|
'''Checks, using which, if tomb is available.
|
|
|
|
Returns None on error, the path on success.
|
|
|
|
'''
|
|
|
|
try:
|
2011-11-02 23:00:48 +00:00
|
|
|
path = subprocess.check_output(['which', 'tomb'])
|
2011-10-26 22:03:15 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return None
|
|
|
|
return path
|
|
|
|
|
2011-10-31 23:36:14 +00:00
|
|
|
@classmethod
|
2011-11-01 02:07:05 +00:00
|
|
|
def check(cls, command, stdout=None, stderr=None, no_color=True, ignore_swap=False):
|
|
|
|
args = [command]
|
|
|
|
if no_color:
|
|
|
|
args += ['--no-color']
|
|
|
|
if ignore_swap:
|
|
|
|
args += ['--ignore-swap']
|
2011-10-31 23:36:14 +00:00
|
|
|
try:
|
2011-11-01 02:07:05 +00:00
|
|
|
subprocess.check_call([cls.tombexec, 'check'] + args, stdout=stdout, stderr=stderr)
|
2011-10-31 23:36:14 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2011-10-30 21:07:16 +00:00
|
|
|
@classmethod
|
2011-11-02 23:00:48 +00:00
|
|
|
def create(cls, tombpath, tombsize,keypath, stdout=None, stderr=None, no_color=True, ignore_swap=False):
|
2011-10-26 22:03:15 +00:00
|
|
|
'''If keypath is None, it will be created adjacent to the tomb.
|
|
|
|
This is unsafe, and you should NOT do it.
|
|
|
|
|
|
|
|
Note that this will invoke pinentry
|
2011-10-31 23:10:17 +00:00
|
|
|
|
|
|
|
no_color is supported as an option for short-lived retrocompatibility:
|
|
|
|
it will be removed as soon as no-color will be integrated
|
2011-10-26 22:03:15 +00:00
|
|
|
'''
|
|
|
|
args = [tombpath, '-s', tombsize]
|
|
|
|
if keypath is not None:
|
|
|
|
args += ['-k', keypath]
|
2011-10-31 23:10:17 +00:00
|
|
|
if no_color:
|
|
|
|
args += ['--no-color']
|
|
|
|
if ignore_swap:
|
|
|
|
args += ['--ignore-swap']
|
2011-10-26 22:03:15 +00:00
|
|
|
try:
|
2011-10-30 21:07:16 +00:00
|
|
|
subprocess.check_call([cls.tombexec, 'create'] + args, stdout=stdout, stderr=stderr)
|
2011-10-26 22:03:15 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def open(tombpath,keypath=None):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
#Debug stuff. Also useful for an example
|
|
|
|
print Tomb.create('/tmp/a.tomb', '10', '/tmp/akey')
|