undertaker.py (2289B)
1 import subprocess 2 from tempfile import NamedTemporaryFile 3 4 import parser 5 6 class Undertaker(object): 7 ''' 8 This is similar to Tomb class, and provides a wrapper on undertaker. 9 10 TODO: 11 * methods for automagical scan 12 * output parsing, giving meaningful output 13 14 Due to the non-interactive nature of undertaker, it's simpler than Tomb 15 ''' 16 undertakerexec = 'undertaker' 17 @classmethod 18 def check(cls, paths): 19 '''Will check if there are keys available there, as in --path 20 21 paths can be a string (one address), or a list of 22 ''' 23 #TODO: more solid check: something like 24 if type(paths) is not str: 25 out = [] 26 for p in paths: 27 try: 28 res = cls.check(p) 29 except: 30 continue 31 else: 32 if res: 33 out.extend(res) 34 return out 35 36 buf = NamedTemporaryFile() 37 try: 38 subprocess.check_call([cls.undertakerexec, paths, '--batch', 39 '--path'], stderr=buf) 40 except subprocess.CalledProcessError as exc: 41 return False 42 43 out = [] 44 buf.seek(0) 45 for line in buf: 46 ret = parser.parse_line(line) 47 if ret and ret['type'] == 'found': 48 out.append(ret['content']) 49 return out 50 51 52 @classmethod 53 def get(cls, paths): 54 ''' 55 Similar to check, but truly get the key content. 56 If paths is iterable, stop at the first successful path 57 ''' 58 if type(paths) is not str: 59 for p in paths: 60 try: 61 res = cls.get(p) 62 except: 63 continue 64 else: 65 if res: 66 return res 67 buf = NamedTemporaryFile() 68 try: 69 subprocess.check_call([cls.undertakerexec, paths, '--batch'], 70 stdout=buf) 71 except subprocess.CalledProcessError: 72 return False 73 buf.seek(0) 74 return buf.read() 75 76 77 if __name__ == '__main__': 78 Undertaker.undertakerexec = '/home/davide/coding/projects/tomb/src/undertaker' 79 print Undertaker.get('near:///home/davide/Desktop/testing.tomb')