__init__.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 argparse
  22. import http.client
  23. import os
  24. import pkg_resources
  25. import re
  26. import string
  27. import sys
  28. from pwman.util import config
  29. from pwman.data.factory import check_db_version
  30. try:
  31. import cryptography # noqa
  32. has_cryptography = True
  33. except ImportError:
  34. has_cryptography = False
  35. appname = "pwman3"
  36. try:
  37. version = pkg_resources.get_distribution('pwman3').version
  38. except pkg_resources.DistributionNotFound: # pragma: no cover
  39. version = "0.9.1"
  40. class PkgMetadata(object):
  41. def __init__(self):
  42. p = pkg_resources.get_distribution('pwman3')
  43. f = open(os.path.join(p.location+'-info', 'PKG-INFO'))
  44. lines = f.readlines()
  45. self.summary = lines[3].split(':')[-1].strip()
  46. self.description = ''.join(map(string.strip, lines[9:14]))
  47. self.author_email = lines[6].split(':')[-1].strip()
  48. self.author = lines[5].split(':')[-1].strip()
  49. self.home_page = lines[4].split(':')[-1].strip()
  50. try:
  51. pkg_meta = PkgMetadata()
  52. website = pkg_meta.home_page
  53. author = pkg_meta.author
  54. authoremail = pkg_meta.author_email
  55. description = pkg_meta.summary
  56. long_description = pkg_meta.description
  57. except IOError as E:
  58. # this should only happen once when installing the package
  59. description = "a command line password manager with support for multiple databases." # noqa
  60. website = 'http://pwman3.github.io/pwman3/'
  61. def which(cmd): # pragma: no cover
  62. _, cmdname = os.path.split(cmd)
  63. for path in os.environ["PATH"].split(os.pathsep):
  64. cmd = os.path.join(path, cmdname)
  65. if os.path.isfile(cmd) and os.access(cmd, os.X_OK): # pragma: no cover
  66. return cmd
  67. return ''
  68. config_dir = os.path.expanduser("~/.pwman")
  69. def parser_options(formatter_class=argparse.HelpFormatter): # pragma: no cover
  70. parser = argparse.ArgumentParser(prog='pwman3',
  71. description=description,
  72. formatter_class=formatter_class)
  73. parser.add_argument('-c', '--config', dest='cfile',
  74. default=os.path.expanduser("~/.pwman/config"),
  75. help='cofiguration file to read')
  76. parser.add_argument('-d', '--database', dest='dbase')
  77. parser.add_argument('-i', '--import', nargs=2, dest='file_delim',
  78. help="Specify the file name and the delimeter type")
  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, config.default_config)
  85. return configp
  86. def set_xsel(configp, OSX):
  87. if not OSX:
  88. xselpath = which("xsel")
  89. configp.set_value("Global", "xsel", xselpath)
  90. elif OSX:
  91. pbcopypath = which("pbcopy")
  92. configp.set_value("Global", "xsel", pbcopypath)
  93. def set_umask(configp):
  94. umask = configp.get_value("Global", "umask")
  95. if re.search(r'^\d{4}$', umask):
  96. os.umask(int(umask))
  97. def set_db(args, configp):
  98. if args.dbase:
  99. configp.set_value("Database", "dburi", args.dbase)
  100. configp.set_value("Global", "save", "False")
  101. def get_conf_options(args, OSX):
  102. configp = get_conf(args)
  103. xselpath = configp.get_value("Global", "xsel")
  104. if not xselpath: # pragma: no cover
  105. set_xsel(configp, OSX)
  106. set_db(args, configp)
  107. set_umask(configp)
  108. dburi = configp.get_value("Database", "dburi")
  109. return xselpath, dburi, configp
  110. def get_db_version(config, args):
  111. dburi = check_db_version(config.get_value("Database", "dburi"))
  112. return dburi
  113. def calculate_client_info(): # pragma: no cover
  114. import hashlib
  115. import socket
  116. from getpass import getuser
  117. hashinfo = hashlib.sha256((socket.gethostname() + getuser()).encode())
  118. hashinfo = hashinfo.hexdigest()
  119. return hashinfo
  120. def is_latest_version(version, client_info): # pragma: no cover
  121. """check current version againt latest version"""
  122. try:
  123. conn = http.client.HTTPConnection("pwman.tiram.it", timeout=0.5)
  124. conn.request("GET",
  125. "/is_latest/?current_version={}&os={}&hash={}".format(
  126. version, sys.platform, client_info))
  127. r = conn.getresponse()
  128. data = r.read() # This will return entire content.
  129. if data.decode().split(".") > version.split("."):
  130. return None, False
  131. else:
  132. return None, True
  133. except Exception as E:
  134. return E, True