#!/usr/bin/env python """ # Simple password manager # Copyright (c) 2011 Michael Buesch # 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())