changeset 433:c20da4213406

Part-way through a more effective version using standard python ConfigParser
author Chris Cannam
date Tue, 28 Jun 2011 10:22:09 +0100
parents 4d0f151fca08
children f8aa7ac57993
files easyhg2.py
diffstat 1 files changed, 30 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- 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)