__init__.py 5.8 KB

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