__init__.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 Tiram <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. import sys
  25. import re
  26. import colorama
  27. from .util import config
  28. from pwman.data import factory
  29. from pwman.data.database import __DB_FORMAT__
  30. appname = "pwman3"
  31. try:
  32. version = pkg_resources.get_distribution('pwman3').version
  33. except pkg_resources.DistributionNotFound: # pragma: no cover
  34. version = "0.5"
  35. website = "http://pwman3.github.io/pwman3/"
  36. author = "Oz Nahum Tiram"
  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 database locked by "
  43. "a\nmaster password which is AES encrypted.")
  44. _db_warn = (u"pwman3 detected that you are using the old database format"
  45. " which is insecure."
  46. " pwman3 will try to automatically convert the database now."
  47. "\n"
  48. "If you choose not to convert the database, pwman3, will quit."
  49. "\nYou can check the help (pwman3 -h) or look at the manpage how "
  50. "to convert the database manually."
  51. )
  52. def which(cmd): # pragma: no cover
  53. _, cmdname = os.path.split(cmd)
  54. for path in os.environ["PATH"].split(os.pathsep):
  55. cmd = os.path.join(path, cmdname)
  56. if os.path.isfile(cmd) and os.access(cmd, os.X_OK): # pragma: no cover
  57. return cmd
  58. return ''
  59. config_dir = os.path.expanduser("~/.pwman")
  60. def parser_options(formatter_class=argparse.HelpFormatter): # pragma: no cover
  61. parser = argparse.ArgumentParser(prog=appname,
  62. description=description,
  63. formatter_class=formatter_class)
  64. parser.add_argument('-c', '--config', dest='cfile',
  65. default=os.path.expanduser("~/.pwman/config"),
  66. help='cofiguration file to read')
  67. parser.add_argument('-d', '--database', dest='dbase')
  68. parser.add_argument('-i', '--import', dest='import_file',
  69. type=argparse.FileType())
  70. return parser
  71. def get_conf(args):
  72. config_dir = os.path.expanduser("~/.pwman")
  73. if not os.path.isdir(config_dir): # pragma: no cover
  74. os.mkdir(config_dir)
  75. configp = config.Config(args.cfile, config.default_config)
  76. return configp
  77. def set_xsel(configp, OSX):
  78. if not OSX:
  79. xselpath = which("xsel")
  80. configp.set_value("Global", "xsel", xselpath)
  81. elif OSX:
  82. pbcopypath = which("pbcopy")
  83. configp.set_value("Global", "xsel", pbcopypath)
  84. def set_win_colors(config): # pragma: no cover
  85. if 'win' in sys.platform:
  86. colorama.init()
  87. def set_umask(configp):
  88. umask = configp.get_value("Global", "umask")
  89. if re.search(r'^\d{4}$', umask):
  90. os.umask(int(umask))
  91. def set_db(args, configp):
  92. if args.dbase:
  93. configp.set_value("Database", "filename", args.dbase)
  94. configp.set_value("Global", "save", "False")
  95. def get_conf_options(args, OSX):
  96. configp = get_conf(args)
  97. xselpath = configp.get_value("Global", "xsel")
  98. if not xselpath: # pragma: no cover
  99. set_xsel(configp, OSX)
  100. set_win_colors(configp)
  101. set_db(args, configp)
  102. set_umask(configp)
  103. dbtype = configp.get_value("Database", "type")
  104. return xselpath, dbtype, configp
  105. def get_db_version(config, dbtype, args):
  106. # This method is seriously biased towards SQLite.
  107. # TODO: make this more Postgresql\Network Database friendly
  108. if os.path.exists(config.get_value("Database", "filename")):
  109. dbver = factory.check_db_version(dbtype, config.get_value("Database",
  110. "filename"))
  111. else:
  112. dbver = __DB_FORMAT__
  113. return dbver