2019-09-13 03:55:16 +02:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import subprocess
|
2019-09-16 13:42:06 +02:00
|
|
|
import logging
|
|
|
|
|
2019-09-13 04:01:57 +02:00
|
|
|
try:
|
|
|
|
import ConfigParser
|
|
|
|
except:
|
|
|
|
import configparser as ConfigParser
|
|
|
|
|
2019-09-13 03:55:16 +02:00
|
|
|
config = ConfigParser.ConfigParser()
|
|
|
|
home_path = os.getenv('HOME')
|
|
|
|
config_path = os.path.join(home_path, '.config/git-credential-pass.ini')
|
|
|
|
config.read(config_path)
|
|
|
|
|
|
|
|
repo_info_regex = r'(\S+)=(.*)'
|
|
|
|
password_store_path = os.path.join(home_path, '.password-store')
|
|
|
|
|
2019-09-16 13:42:06 +02:00
|
|
|
FORMAT = '%(asctime)-15s %(message)s'
|
|
|
|
logging.basicConfig(format=FORMAT, filename='/tmp/git-credential-pass.log')
|
|
|
|
logger = logging.getLogger('base')
|
|
|
|
|
2019-09-13 03:55:16 +02:00
|
|
|
operation = sys.argv[1]
|
|
|
|
if operation != "get":
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
repo_info_string = sys.stdin.read()
|
|
|
|
repo_info = dict(re.findall(repo_info_regex, repo_info_string))
|
2019-09-16 13:42:06 +02:00
|
|
|
logger.warning('repo_info: %s', repo_info)
|
2019-09-13 03:55:16 +02:00
|
|
|
|
|
|
|
if 'host' not in repo_info:
|
|
|
|
sys.stderr.write("Host not set\n")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if operation == "get":
|
|
|
|
|
|
|
|
find_path = None
|
|
|
|
try:
|
|
|
|
find_path = config.get(repo_info['host'], 'target')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if not find_path:
|
|
|
|
cmd = '''
|
|
|
|
find {0} -name "*.gpg" | grep {1}| head -n1 | sed 's/.gpg$//g'
|
|
|
|
'''.format(password_store_path, repo_info['host'])
|
|
|
|
_find_path = subprocess.check_output(
|
|
|
|
cmd,
|
|
|
|
shell=True
|
|
|
|
)
|
2019-09-13 10:18:22 +02:00
|
|
|
find_path = _find_path.decode('utf8').replace(password_store_path, '').strip('\n')
|
2019-09-13 03:55:16 +02:00
|
|
|
|
|
|
|
client = subprocess.check_output(
|
|
|
|
[
|
|
|
|
'pass',
|
|
|
|
'show',
|
|
|
|
find_path,
|
|
|
|
]
|
|
|
|
)
|
2019-09-13 04:01:57 +02:00
|
|
|
extractor = client.decode('utf8').split('\n')
|
2019-09-13 03:55:16 +02:00
|
|
|
repo_logins = {}
|
|
|
|
repo_logins['login'] = ': '.join(extractor[1].split(': ')[1:])
|
|
|
|
repo_logins['password'] = extractor[0]
|
|
|
|
|
|
|
|
if repo_logins:
|
|
|
|
print("username=%s" % repo_logins['login'])
|
|
|
|
print("password=%s" % repo_logins['password'])
|
|
|
|
else:
|
|
|
|
sys.stderr.write("Couldn't find credentials for host\n")
|
|
|
|
sys.exit(1)
|