PageRenderTime 62ms CodeModel.GetById 55ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/silverlining/createconf.py

https://bitbucket.org/ianb/silverlining/
Python | 39 lines | 28 code | 5 blank | 6 comment | 3 complexity | 628feb21325f5735433ab1d82a703b6c MD5 | raw file
 1"""Creates ~/.silverlining.conf
 2
 3This builds the file from a template, and asks the user some questions
 4about how to build it.
 5"""
 6
 7import os
 8import tempita
 9
10template = tempita.Template.from_filename(
11    os.path.join(os.path.dirname(__file__), 'silverlining.conf.tmpl'))
12
13silverlining_conf = os.path.join(os.environ['HOME'], '.silverlining.conf')
14
15
16def create_conf():
17    """Create a brand-new ~/.silverlining.conf"""
18    print 'Creating %s' % silverlining_conf
19    username = raw_input('Your service-provider username: ')
20    api_key = raw_input('Your service-provider API key: ')
21    for path in ['id_rsa.pub', 'id_dsa.pub']:
22        pubkey_path = os.path.join(os.environ['HOME'], '.ssh', path)
23        if os.path.exists(pubkey_path):
24            print 'Using %s' % pubkey_path
25            fp = open(pubkey_path)
26            pubkey = fp.read().strip()
27            fp.close()
28            break
29    else:
30        pubkey = None
31        print "%s doesn't exist" % pubkey_path
32        print "  you won't automatically be able to login to new servers"
33    content = template.substitute(
34        username=username, api_key=api_key, pubkey=pubkey,
35        silverlining_location=os.path.dirname(__file__),
36        )
37    fp = open(silverlining_conf, 'w')
38    fp.write(content)
39    fp.close()