# HG changeset patch # User Chris Cannam # Date 1309252929 -3600 # Node ID c20da421340668a012846dabea6521590cb4a020 # Parent 4d0f151fca08ff8b54ce65f71d3ef3b37f836abf Part-way through a more effective version using standard python ConfigParser diff -r 4d0f151fca08 -r c20da4213406 easyhg2.py --- a/easyhg2.py Tue Jun 28 10:08:53 2011 +0100 +++ b/easyhg2.py Tue Jun 28 10:22:09 2011 +0100 @@ -17,7 +17,7 @@ import urllib, urllib2, urlparse -from mercurial import ui, util, config, error +from mercurial import ui, util, error try: from mercurial.url import passwordmgr except: @@ -57,6 +57,8 @@ from Crypto.Cipher import AES import base64 +import ConfigParser # Mercurial version won't write files + #!!! should be in a class here def encrypt(text, key): @@ -116,7 +118,7 @@ uri = canonical_url(authuri) - pkey = ('%s@@%s' % (uri, user)).replace('=', '__') + pkey = base64.b64encode('%s@@%s' % (uri, user)).replace('=', '_') pekey = self.ui.config('easyhg', 'authkey') pfile = os.path.expanduser(self.ui.config('easyhg', 'authfile')) pdata = None @@ -144,22 +146,33 @@ layout.addWidget(passfield, 2, 1) remember = None + pcfg = None + if pekey and pfile: # load pwd from our cache file, decrypt with given key - pcfg = config.config() + pcfg = ConfigParser.RawConfigParser() fp = None + remember_default = False try: fp = open(pfile) except: self.ui.write("failed to open authfile %s\n" % pfile) if fp and not passwd: - pcfg.read(pfile) - pdata = pcfg.get('auth', pkey) + pcfg.readfp(fp) + try: + remember_default = pcfg.getboolean('preferences', 'remember') + except: + remember_default = False + try: + pdata = pcfg.get('auth', pkey) + except ConfigParser.NoOptionError: + pdata = None if pdata: cachedpwd = decrypt(pdata, pekey) passfield.setText(cachedpwd) fp.close() remember = QtGui.QCheckBox() + remember.setChecked(remember_default) remember.setText(_('Remember this password until EasyMercurial exits')) layout.addWidget(remember, 3, 1) @@ -188,13 +201,12 @@ user = userfield.text() passwd = passfield.text() - #!!! create pfile if necessary (with proper permissions), append auth data to it - if pekey and pfile: + if pekey and pfile and remember: ofp = None try: - ofp = open(pfile, 'a') + ofp = open(pfile, 'w') except: self.ui.write("failed to open authfile %s for writing\n" % pfile) raise @@ -207,12 +219,17 @@ self.ui.write("failed to set proper permissions on authfile %s\n" % pfile) raise - if ofp: + #!!! add these sections first... + + if remember.isChecked(): pdata = encrypt(passwd, pekey) - ofp.write('[auth]\n') - ofp.write(pkey + '=' + pdata + '\n') - ofp.close() - + pcfg.set('auth', pkey, pdata) + else: + pcfg.set('auth', pkey, '') + + pcfg.set('preferences', 'remember', remember.isChecked()) + pcfg.write(ofp) + ofp.close() # if passwd and keyring_key != '' and not from_keyring: # keyring_key = '%s@@%s' % (uri, user)