__init__.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_file(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. if not os.path.exists(args.cfile):
  84. config.set_config(default_config)
  85. return config
  86. else:
  87. configp = config.Config(args.cfile)
  88. return configp
  89. def set_xsel(config, OSX):
  90. if not OSX:
  91. xselpath = which("xsel")
  92. config.set_value("Global", "xsel", xselpath)
  93. elif OSX:
  94. pbcopypath = which("pbcopy")
  95. config.set_value("Global", "xsel", pbcopypath)
  96. def set_win_colors(config): # pragma: no cover
  97. if 'win' in sys.platform:
  98. colorama.init()
  99. def set_umask(config):
  100. umask = config.get_value("Global", "umask")
  101. if re.search(r'^\d{4}$', umask):
  102. os.umask(int(umask))
  103. else:
  104. raise config.ConfigException("Could not determine umask from config!")
  105. def set_db(args):
  106. if args.dbase:
  107. config.set_value("Database", "filename", args.dbase)
  108. config.set_value("Global", "save", "False")
  109. def get_conf_options(args, OSX):
  110. config = get_conf_file(args)
  111. xselpath = config.get_value("Global", "xsel")
  112. if not xselpath:
  113. set_xsel(config, OSX)
  114. set_win_colors(config)
  115. set_db(args)
  116. set_umask(config)
  117. dbtype = config.get_value("Database", "type")
  118. if not dbtype:
  119. raise Exception("Could not read the Database type from the config!")
  120. return xselpath, dbtype
  121. def get_db_version(config, dbtype, args):
  122. if os.path.exists(config.get_value("Database", "filename")):
  123. dbver = factory.check_db_version(dbtype)
  124. else:
  125. dbver = __DB_FORMAT__
  126. return dbver