__init__.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
  18. # ============================================================================
  19. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. # ============================================================================
  21. import os
  22. import pkg_resources
  23. import argparse
  24. from pwman.util import config
  25. import sys
  26. import re
  27. from pwman.data import factory
  28. from pwman.data.database import __DB_FORMAT__
  29. import colorama
  30. appname = "pwman3"
  31. try:
  32. version = pkg_resources.get_distribution('pwman3').version
  33. except pkg_resources.DistributionNotFound: # pragma: no cover
  34. version = u"0.5"
  35. website = "http://pwman3.github.io/pwman3/"
  36. author = "Oz Nahum"
  37. authoremail = "nahumoz@gmail.com"
  38. description = "a command line password management application."
  39. keywords = "password management sqlite crypto"
  40. long_description = (u"Pwman3 aims to provide a simple but powerful command "
  41. "line interface for password management.\nIt allows one "
  42. "to store your password in a SQLite database locked by "
  43. "a\nmaster password which can be encrypted with different "
  44. "algorithms (e.g AES, Blowfish, DES3, IDEA, etc.).")
  45. _db_warn = (u"pwman3 detected that you are using the old database format"
  46. " which is insecure."
  47. " pwman3 will try to automatically convert the database now."
  48. "\n"
  49. "If you choose not to convert the database, pwman3, will quit."
  50. "\nYou can check the help (pwman3 -h) or look at the manpage how "
  51. "to convert the database manually."
  52. )
  53. def which(cmd):
  54. _, cmdname = os.path.split(cmd)
  55. for path in os.environ["PATH"].split(os.pathsep):
  56. cmd = os.path.join(path, cmdname)
  57. if os.path.isfile(cmd) and os.access(cmd, os.X_OK): # pragma: no cover
  58. return cmd
  59. config_dir = os.path.expanduser("~/.pwman")
  60. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  61. 'cls_timeout': '5',
  62. 'save': 'True'
  63. },
  64. 'Database': {'type': 'SQLite',
  65. 'filename': os.path.join(config_dir,
  66. "pwman.db")},
  67. 'Readline': {'history': os.path.join(config_dir,
  68. "history")}
  69. }
  70. def parser_options(formatter_class=argparse.HelpFormatter):
  71. parser = argparse.ArgumentParser(prog=appname,
  72. description=description,
  73. formatter_class=formatter_class)
  74. parser.add_argument('-c', '--config', dest='cfile',
  75. default=os.path.expanduser("~/.pwman/config"),
  76. help='cofiguration file to read')
  77. parser.add_argument('-d', '--database', dest='dbase')
  78. return parser
  79. def get_conf(args):
  80. config_dir = os.path.expanduser("~/.pwman")
  81. if not os.path.isdir(config_dir): # pragma: no cover
  82. os.mkdir(config_dir)
  83. configp = config.Config(args.cfile, default_config)
  84. return configp
  85. def set_xsel(config, OSX):
  86. if not OSX:
  87. xselpath = which("xsel")
  88. config.set_value("Global", "xsel", xselpath)
  89. elif OSX:
  90. pbcopypath = which("pbcopy")
  91. config.set_value("Global", "xsel", pbcopypath)
  92. def set_win_colors(config): # pragma: no cover
  93. if 'win' in sys.platform:
  94. colorama.init()
  95. def set_umask(config):
  96. umask = config.get_value("Global", "umask")
  97. if re.search(r'^\d{4}$', umask):
  98. os.umask(int(umask))
  99. else:
  100. raise config.ConfigException("Could not determine umask from config!")
  101. def set_db(args, configp):
  102. if args.dbase:
  103. configp.set_value("Database", "filename", args.dbase)
  104. configp.set_value("Global", "save", "False")
  105. def get_conf_options(args, OSX):
  106. configp = get_conf(args)
  107. xselpath = configp.get_value("Global", "xsel")
  108. if not xselpath:
  109. set_xsel(configp, OSX)
  110. set_win_colors(configp)
  111. set_db(args, configp)
  112. set_umask(configp)
  113. dbtype = configp.get_value("Database", "type")
  114. if not dbtype:
  115. raise Exception("Could not read the Database type from the config!")
  116. return xselpath, dbtype, configp
  117. def get_db_version(config, dbtype, args):
  118. if os.path.exists(config.get_value("Database", "filename")):
  119. dbver = factory.check_db_version(dbtype, config.get_value("Database",
  120. "filename"))
  121. else:
  122. dbver = __DB_FORMAT__
  123. return dbver