123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- import argparse
- import http.client
- import os
- import pkg_resources
- import re
- import string
- import shutil
- import sys
- from pwman.util import config
- from pwman.data.factory import check_db_version
- try:
- import cryptography
- has_cryptography = True
- except ImportError:
- has_cryptography = False
- appname = "pwman3"
- try:
- version = pkg_resources.get_distribution('pwman3').version
- except pkg_resources.DistributionNotFound:
- version = "0.9.4"
- class PkgMetadata(object):
- def __init__(self):
- p = pkg_resources.get_distribution('pwman3')
- f = open(os.path.join(p.location+'-info', 'PKG-INFO'))
- lines = f.readlines()
- self.summary = lines[3].split(':')[-1].strip()
- self.description = ''.join(map(string.strip, lines[9:14]))
- self.author_email = lines[6].split(':')[-1].strip()
- self.author = lines[5].split(':')[-1].strip()
- self.home_page = lines[4].split(':')[-1].strip()
- try:
- pkg_meta = PkgMetadata()
- website = pkg_meta.home_page
- author = pkg_meta.author
- authoremail = pkg_meta.author_email
- description = pkg_meta.summary
- long_description = pkg_meta.description
- except IOError as E:
-
- description = "a command line password manager with support for multiple databases."
- website = 'http://pwman3.github.io/pwman3/'
- config_dir = os.path.expanduser("~/.pwman")
- def parser_options(formatter_class=argparse.HelpFormatter):
- parser = argparse.ArgumentParser(prog='pwman3',
- description=description,
- formatter_class=formatter_class)
- parser.add_argument('-c', '--config', dest='cfile',
- default=os.path.expanduser("~/.pwman/config"),
- help='cofiguration file to read')
- parser.add_argument('-d', '--database', dest='dbase')
- parser.add_argument('-i', '--import', nargs=2, dest='file_delim',
- help="Specify the file name and the delimeter type")
- return parser
- def get_conf(args):
- config_dir = os.path.expanduser("~/.pwman")
- if not os.path.isdir(config_dir):
- os.mkdir(config_dir)
- configp = config.Config(args.cfile, config.default_config)
- return configp
- def set_xsel(configp, OSX):
- if not OSX:
- xselpath = shutil.which("xsel") or ""
- configp.set_value("Global", "xsel", xselpath)
- elif OSX:
- pbcopypath = shutil.which("pbcopy") or ""
- configp.set_value("Global", "xsel", pbcopypath)
- def set_umask(configp):
- umask = configp.get_value("Global", "umask")
- if re.search(r'^\d{4}$', umask):
- os.umask(int(umask))
- def set_db(args, configp):
- if args.dbase:
- configp.set_value("Database", "dburi", args.dbase)
- configp.set_value("Global", "save", "False")
- def get_conf_options(args, OSX):
- configp = get_conf(args)
- xselpath = configp.get_value("Global", "xsel")
- if not xselpath:
- set_xsel(configp, OSX)
- set_db(args, configp)
- set_umask(configp)
- dburi = configp.get_value("Database", "dburi")
- return xselpath, dburi, configp
- def get_db_version(config, args):
- dburi = check_db_version(config.get_value("Database", "dburi"))
- return dburi
- def calculate_client_info():
- import hashlib
- import socket
- from getpass import getuser
- hashinfo = hashlib.sha256((socket.gethostname() + getuser()).encode())
- hashinfo = hashinfo.hexdigest()
- return hashinfo
- def is_latest_version(version, client_info):
- """check current version againt latest version"""
- try:
- conn = http.client.HTTPSConnection("pwman.tiram.it", timeout=0.5)
- conn.request("GET",
- "/is_latest/?current_version={}&os={}&hash={}".format(
- version, sys.platform, client_info))
- r = conn.getresponse()
- data = r.read()
- if r.status != 200:
- return None, True
- if data.decode().split(".") > version.split("."):
- return None, False
- else:
- return None, True
- except Exception as E:
- return E, True
|