-
Notifications
You must be signed in to change notification settings - Fork 1
/
yspavectl
executable file
·63 lines (54 loc) · 2.27 KB
/
yspavectl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /usr/bin/env python
# (c) Samuel Vincent Creshal <[email protected]> 2014
import argparse,getpass,sys, readline, os
from yspave import pave, crypto, util, pwgen, commands, metadata
from colorama import Fore as fg
actions = metadata.actions % (pave.PaveCfg.pwgen_bits,pave.PaveCfg.pwgen_bits)
parser = argparse.ArgumentParser (prog=metadata.appname+'ctl', description=actions,formatter_class=argparse.RawTextHelpFormatter, epilog=metadata.examples)
parser.add_argument('--version', '-v', action='version', version='%(prog)s '+pave.appvers)
parser.add_argument('--file','-f', help='Database file',nargs='?',default=pave.db_filename)
parser.add_argument('--config-file','-c',help='Configuration file',nargs='?',default=pave.config_filename)
parser.add_argument('--pwgen-mode','-m',choices=pwgen.modes,nargs='?')
parser.add_argument('action', choices=metadata.verbs, default=None,nargs='?')
parser.add_argument('query',default=None,nargs='?',help='Search string')
args = parser.parse_args()
if os.path.exists (args.config_file):
config = pave.PaveCfg (args.config_file, args.pwgen_mode)
else:
if args.config_file == pave.config_filename: #implicitly create default file
config = pave.PaveCfg (None, args.pwgen_mode)
else: raise IOError ('Config file %s does not exist!'%args.config_file)
#This is a special case -- one-shot password generator, not operating on the database at all
if args.action == 'pwgen':
print (pwgen.PwGen(config).mkpass (args.query))
sys.exit (0)
while True:
try:
password = getpass.getpass ('Database password: ')
db = crypto.PaveDB (password, args.file, config)
del (password)
break
except EOFError: sys.exit (0)
except KeyboardInterrupt: sys.exit (1)
except:pass
cmd = commands.Commands(config, db)
if args.action:
#single command mode
cmd.dispatch (args.action, args.query)
else:
#interactive mode
while True:
try:
action, query = map (lambda x: x.strip(), (util.prompt ('> ')+' ').split(' ',1))
if not action: raise EOFError
except EOFError: sys.exit (0)
except KeyboardInterrupt: sys.exit (1)
if action in ['help','?']:
print (actions+metadata.examples)
else:
try:
cmd.dispatch (action, query)
except (Exception,KeyboardInterrupt) as e:
import traceback
traceback.print_exc()
print (fg.YELLOW+str(e)+fg.RESET)