Tomb/extras/pytomb/tomblib/tomb.py
Jaromil 3eb93acc18 Directory reorganization
This commit re-organizes all the source distribution contents to
present users with the simple script, while moving the rest in extras.
Also autoconf/automake scripts were removed, back to minimalism.

The rationale of this change is that Tomb really only consists of a
script and users with no extra needs should just be presented with
it with no need for anything else. Any other thing on top of the Tomb
script is an extra and can be even distributed separately or integrated
in distributions.
2013-05-18 17:29:37 +02:00

92 lines
3.0 KiB
Python

'''
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.
'''
tombexec = 'tomb'
def _check_exec_path(self):
'''Checks, using which, if tomb is available.
Returns None on error, the path on success.
'''
try:
path = subprocess.check_output(['which', 'tomb'])
except subprocess.CalledProcessError:
return None
return path
@classmethod
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']
try:
subprocess.check_call([cls.tombexec, 'check'] + args, stdout=stdout, stderr=stderr)
except subprocess.CalledProcessError:
return False
return True
@classmethod
def create(cls, tombpath, tombsize,keypath, stdout=None, stderr=None,
no_color=True, ignore_swap=False):
'''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
no_color is supported as an option for short-lived retrocompatibility:
it will be removed as soon as no-color will be integrated
'''
args = [tombpath, '-s', tombsize]
if keypath is not None:
args += ['-k', keypath]
if no_color:
args += ['--no-color']
if ignore_swap:
args += ['--ignore-swap']
try:
subprocess.check_call([cls.tombexec, 'create'] + args, stdout=stdout, stderr=stderr)
except subprocess.CalledProcessError:
return False
return True
@classmethod
def open(cls, tombpath,keypath=None, no_color=True, ignore_swap=False):
args = [tombpath]
if keypath is not None:
args += ['-k', keypath]
if no_color:
args += ['--no-color']
if ignore_swap:
args += ['--ignore-swap']
try:
subprocess.check_call([cls.tombexec, 'open'] + args)
except subprocess.CalledProcessError:
return False
return True
if __name__ == '__main__':
#Debug stuff. Also useful for an example
print Tomb.create('/tmp/a.tomb', '10', '/tmp/akey')