dotfiles

Configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | Submodules | README | LICENSE

offlineimap.py (1827B)


      1 #!/usr/bin/env python
      2 import re
      3 import subprocess
      4 import getpass
      5 import os
      6 import sys
      7 if sys.platform == 'linux2':
      8     import gnomekeyring as gkey
      9 
     10 user = getpass.getuser()
     11 home = os.getenv('HOME')
     12 
     13 def get_keychain_pass(account=None, server=None):
     14     if sys.platform == 'darwin':
     15         command = 'sudo -u ' + user + \
     16             ' /usr/bin/security -v find-internet-password -g -a ' + account +\
     17             ' -s ' + server + ' ' + home + '/Library/Keychains/login.keychain'
     18         output = subprocess.check_output(command, shell=True,
     19                                          stderr=subprocess.STDOUT)
     20         outtext = [l for l in output.splitlines()
     21                    if l.startswith('password: ')][0]
     22 
     23         return re.match(r'password: "(.*)"', outtext).group(1)
     24 
     25     elif sys.platform == 'linux2':
     26 
     27         attrs = {'server': server, 'protocol': 'http'}
     28         items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
     29         return (items[0].attributes['user'], items[0].secret)[1]
     30 
     31     else:
     32         raise Exception('Platform "' + sys.platform + '" not supported.')
     33 
     34 
     35 def set_keychain_pass(protocol, server, user, password):
     36     if sys.platform != 'linux2':
     37         raise Exception('Only Linux is supported for setting passwords.')
     38 
     39     attrs = {'user': user,
     40              'server': server,
     41              'protocol': protocol}
     42     name = 'offlineimap ' + server
     43 
     44     gkey.item_create_sync(gkey.get_default_keyring_sync(),
     45                           gkey.ITEM_NETWORK_PASSWORD,
     46                           name, attrs, password, True)
     47 
     48 if __name__ == '__main__':
     49     if sys.argv[1] == 'create':
     50         set_keychain_pass(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
     51     else:
     52         account = sys.argv[1]
     53         server = sys.argv[2]
     54         print(get_keychain_pass(account=account, server=server))
     55