summaryrefslogtreecommitdiffstats
path: root/pwman
blob: 642dc87c54d01d921f9b5697a9cd0b273ea51dce (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
"""
# Simple password manager
# Copyright (c) 2011 Michael Buesch <m@bues.ch>
# Licensed under the GNU/GPL version 2 or later.
"""

import sys
import getopt
import libpwman as pwman


def testEscape(t):
	t0 = t1 = '1-2x-y-a\tb c\\\\_ \\\\ x_-y\r\n_\tz\v__\\\\__\\-'
	if t:
		t0 = t1 = t
	nrIter = 3
	print "ORIG :", t0
	for i in range(0, nrIter):
		t1 = pwman.escapeCmd(t1)
		print "ESC%d : %s" % (i, t1)
	for i in range(0, nrIter):
		t1 = pwman.unescapeCmd(t1)
	print "UNESC:", t1
	assert(t1 == t0)

def usage():
	print "pwman v%d - lightweight password manager" % pwman.VERSION
	print ""
	print "Usage: %s [OPTIONS] [COMMANDS]" % sys.argv[0]
	print ""
	print "If COMMANDS are given, run these commands."
	print "Starts in interactive mode, otherwise."
	print ""
	print "Options:"
	print " -d|--database PATH      Use PATH as database file."
	print "                         If not given, %s is used." %\
						pwman.getDefaultDatabase()
	print " -U|--commit-clear-undo  The commit command clears undo queue."

def main():
	dbFile = None
	commitClearsUndo = False
	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
			"hd:U",
			[ "help", "database=", "escapetest=", "commit-clear-undo", ])
		for (o, v) in opts:
			if o in ("-h", "--help"):
				usage()
				return 0
			if o in ("-d", "--database"):
				dbFile = v
			if o == "--escapetest":
				testEscape(v)
				return 0
			if o in ("-U", "--commit-clear-undo"):
				commitClearsUndo = True
	except (getopt.GetoptError), e:
		usage()
		return 1
	commands = args
	if not dbFile:
		dbFile = pwman.getDefaultDatabase()
	if not dbFile:
		print "No database file specified"
		return 1
	print "Opening database '%s'..." % dbFile
	passphrase = pwman.readPassphrase("Master passphrase",
					  not pwman.fileExists(dbFile))
	if passphrase is None:
		return 1
	try:
		p = pwman.PWMan(dbFile, passphrase,
				commitClearsUndo=commitClearsUndo)
		if commands:
			for command in commands:
				p.runOneCommand(command)
		else:
			p.interactive()
	except (pwman.PWMan.Error), e:
		print "Error: " + str(e)
		return 1
	return 0

if __name__ == "__main__":
	sys.exit(main())
bues.ch cgit interface