__init__.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. return ''
  60. config_dir = os.path.expanduser("~/.pwman")
  61. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  62. 'cls_timeout': '5',
  63. 'save': 'True'
  64. },
  65. 'Database': {'type': 'SQLite',
  66. 'filename': os.path.join(config_dir,
  67. "pwman.db")},
  68. 'Readline': {'history': os.path.join(config_dir,
  69. "history")}
  70. }
  71. def parser_options(formatter_class=argparse.HelpFormatter):
  72. parser = argparse.ArgumentParser(prog=appname,
  73. description=description,
  74. formatter_class=formatter_class)
  75. parser.add_argument('-c', '--config', dest='cfile',
  76. default=os.path.expanduser("~/.pwman/config"),
  77. help='cofiguration file to read')
  78. parser.add_argument('-d', '--database', dest='dbase')
  79. return parser
  80. def get_conf(args):
  81. config_dir = os.path.expanduser("~/.pwman")
  82. if not os.path.isdir(config_dir): # pragma: no cover
  83. os.mkdir(config_dir)
  84. configp = config.Config(args.cfile, default_config)
  85. return configp
  86. def set_xsel(config, OSX):
  87. if not OSX:
  88. xselpath = which("xsel")
  89. config.set_value("Global", "xsel", xselpath)
  90. elif OSX:
  91. pbcopypath = which("pbcopy")
  92. config.set_value("Global", "xsel", pbcopypath)
  93. def set_win_colors(config): # pragma: no cover
  94. if 'win' in sys.platform:
  95. colorama.init()
  96. def set_umask(config):
  97. umask = config.get_value("Global", "umask")
  98. if re.search(r'^\d{4}$', umask):
  99. os.umask(int(umask))
  100. else:
  101. raise config.ConfigException("Could not determine umask from config!")
  102. def set_db(args, configp):
  103. if args.dbase:
  104. configp.set_value("Database", "filename", args.dbase)
  105. configp.set_value("Global", "save", "False")
  106. def get_conf_options(args, OSX):
  107. configp = get_conf(args)
  108. xselpath = configp.get_value("Global", "xsel")
  109. if not xselpath:
  110. set_xsel(configp, OSX)
  111. set_win_colors(configp)
  112. set_db(args, configp)
  113. set_umask(configp)
  114. dbtype = configp.get_value("Database", "type")
  115. if not dbtype:
  116. raise Exception("Could not read the Database type from the config!")
  117. return xselpath, dbtype, configp
  118. def get_db_version(config, dbtype, args):
  119. if os.path.exists(config.get_value("Database", "filename")):
  120. dbver = factory.check_db_version(dbtype, config.get_value("Database",
  121. "filename"))
  122. else:
  123. dbver = __DB_FORMAT__
  124. return dbver