Skip to content

Instantly share code, notes, and snippets.

@larsch
Created May 27, 2019 15:08
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 larsch/06c9230ad3975a45ad3163dc5084d5d3 to your computer and use it in GitHub Desktop.
Save larsch/06c9230ad3975a45ad3163dc5084d5d3 to your computer and use it in GitHub Desktop.
"""
A python keyring backend that uses 'git-credential-cache'
To enable, edit '~/.local/share/python_keyring/keyringrc.cfg':
[backend]
default-keyring=cachekeyring.CacheKeyring
keyring-path=/path/to/dir
To enable use with mercurial, edit ~/.hgrc:
[extensions]
mercurial_keyring=
"""
import keyring.backend
from subprocess import Popen, PIPE, STDOUT
from os import environ
import re
class CacheKeyring(keyring.backend.KeyringBackend):
"""A keyring that uses git-credential-cache
"""
priority = 1
def set_password(self, servicename, username, password):
#print('set_password', servicename, username, password)
environ["PATH"] = "/usr/lib/git-core:" + environ["PATH"]
username_utf8 = username.encode('utf-8')
password_utf8 = password.encode('utf-8')
servicename_utf8 = servicename.encode('utf-8')
record = "username={0}\nservicename={1}\npassword={2}\n".format(username_utf8, servicename_utf8, password_utf8)
p = Popen(['git-credential-cache', 'store'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
p.communicate(input=record)
def get_password(self, servicename, username):
#print('get_password', servicename, username)
environ["PATH"] = "/usr/lib/git-core:" + environ["PATH"]
username_utf8 = username.encode('utf-8')
servicename_utf8 = servicename.encode('utf-8')
record = "username={0}\nservicename={1}\n".format(username_utf8, servicename_utf8)
p = Popen(['git-credential-cache', 'get'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
stdout_data = p.communicate(input=record)[0].encode('utf-8')
m = re.search(r'password=(.*)\n', stdout_data)
if m:
return m.group(1)
else:
return None
def delete_password(self, servicename, username, password):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment