Skip to content

Instantly share code, notes, and snippets.

@chengyuhui
Created March 30, 2020 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chengyuhui/0dec61e00526420d61a6ef54cee0377a to your computer and use it in GitHub Desktop.
Save chengyuhui/0dec61e00526420d61a6ef54cee0377a to your computer and use it in GitHub Desktop.
KWallet wrapper for git credential helper
#!/usr/bin/env python3
from sys import stdin, argv
import subprocess
import json
if len(argv) != 2:
print("Usage: " + argv[0] + " <action: get|store>")
exit(1)
action = argv[1]
known = {}
while True:
line = stdin.readline().strip().split('=')
if len(line) != 2:
break
known[line[0]] = line[1]
if argv[1] == 'get':
url = 'py_' + known['protocol'] + '://' + known['host']
shell = ['kwallet-query', '-f', 'git', '-r', url, 'kdewallet']
command = subprocess.Popen(
shell, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
output, errors = command.communicate()
output = output.decode('utf-8').strip()
if not output.startswith('Failed'):
result = json.loads(output)
print('username=' + result['username'])
print('password=' + result['password'])
if argv[1] == 'store':
j = json.dumps({
'username': known['username'],
'password': known['password']
})
print(j)
url = 'py_' + known['protocol'] + '://' + known['host']
shell = ['kwallet-query', '-f', 'git', '-w', url, 'kdewallet', '-v']
command = subprocess.Popen(
shell, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, text=True)
output, errors = command.communicate(input=j)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment