Mercurial > hg > easyhg
comparison easyhg2.py @ 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 |
comparison
equal
deleted
inserted
replaced
432:4d0f151fca08 | 433:c20da4213406 |
---|---|
15 | 15 |
16 import sys, os, stat | 16 import sys, os, stat |
17 | 17 |
18 import urllib, urllib2, urlparse | 18 import urllib, urllib2, urlparse |
19 | 19 |
20 from mercurial import ui, util, config, error | 20 from mercurial import ui, util, error |
21 try: | 21 try: |
22 from mercurial.url import passwordmgr | 22 from mercurial.url import passwordmgr |
23 except: | 23 except: |
24 from mercurial.httprepo import passwordmgr | 24 from mercurial.httprepo import passwordmgr |
25 | 25 |
54 easyhg_qtapp = None | 54 easyhg_qtapp = None |
55 | 55 |
56 #!!! same as above for this? or just continue without remember feature? | 56 #!!! same as above for this? or just continue without remember feature? |
57 from Crypto.Cipher import AES | 57 from Crypto.Cipher import AES |
58 import base64 | 58 import base64 |
59 | |
60 import ConfigParser # Mercurial version won't write files | |
59 | 61 |
60 #!!! should be in a class here | 62 #!!! should be in a class here |
61 | 63 |
62 def encrypt(text, key): | 64 def encrypt(text, key): |
63 text = '%d.%s' % (len(text), text) | 65 text = '%d.%s' % (len(text), text) |
114 | 116 |
115 self.ui.write("want username and/or password for %s\n" % authuri) | 117 self.ui.write("want username and/or password for %s\n" % authuri) |
116 | 118 |
117 uri = canonical_url(authuri) | 119 uri = canonical_url(authuri) |
118 | 120 |
119 pkey = ('%s@@%s' % (uri, user)).replace('=', '__') | 121 pkey = base64.b64encode('%s@@%s' % (uri, user)).replace('=', '_') |
120 pekey = self.ui.config('easyhg', 'authkey') | 122 pekey = self.ui.config('easyhg', 'authkey') |
121 pfile = os.path.expanduser(self.ui.config('easyhg', 'authfile')) | 123 pfile = os.path.expanduser(self.ui.config('easyhg', 'authfile')) |
122 pdata = None | 124 pdata = None |
123 | 125 |
124 self.ui.write("pekey is %s\n" % pekey) | 126 self.ui.write("pekey is %s\n" % pekey) |
142 passfield.setText(passwd) | 144 passfield.setText(passwd) |
143 layout.addWidget(QtGui.QLabel(_('Password:')), 2, 0) | 145 layout.addWidget(QtGui.QLabel(_('Password:')), 2, 0) |
144 layout.addWidget(passfield, 2, 1) | 146 layout.addWidget(passfield, 2, 1) |
145 | 147 |
146 remember = None | 148 remember = None |
149 pcfg = None | |
150 | |
147 if pekey and pfile: | 151 if pekey and pfile: |
148 # load pwd from our cache file, decrypt with given key | 152 # load pwd from our cache file, decrypt with given key |
149 pcfg = config.config() | 153 pcfg = ConfigParser.RawConfigParser() |
150 fp = None | 154 fp = None |
155 remember_default = False | |
151 try: | 156 try: |
152 fp = open(pfile) | 157 fp = open(pfile) |
153 except: | 158 except: |
154 self.ui.write("failed to open authfile %s\n" % pfile) | 159 self.ui.write("failed to open authfile %s\n" % pfile) |
155 if fp and not passwd: | 160 if fp and not passwd: |
156 pcfg.read(pfile) | 161 pcfg.readfp(fp) |
157 pdata = pcfg.get('auth', pkey) | 162 try: |
163 remember_default = pcfg.getboolean('preferences', 'remember') | |
164 except: | |
165 remember_default = False | |
166 try: | |
167 pdata = pcfg.get('auth', pkey) | |
168 except ConfigParser.NoOptionError: | |
169 pdata = None | |
158 if pdata: | 170 if pdata: |
159 cachedpwd = decrypt(pdata, pekey) | 171 cachedpwd = decrypt(pdata, pekey) |
160 passfield.setText(cachedpwd) | 172 passfield.setText(cachedpwd) |
161 fp.close() | 173 fp.close() |
162 remember = QtGui.QCheckBox() | 174 remember = QtGui.QCheckBox() |
175 remember.setChecked(remember_default) | |
163 remember.setText(_('Remember this password until EasyMercurial exits')) | 176 remember.setText(_('Remember this password until EasyMercurial exits')) |
164 layout.addWidget(remember, 3, 1) | 177 layout.addWidget(remember, 3, 1) |
165 | 178 |
166 bb = QtGui.QDialogButtonBox() | 179 bb = QtGui.QDialogButtonBox() |
167 ok = bb.addButton(bb.Ok) | 180 ok = bb.addButton(bb.Ok) |
186 if ok: | 199 if ok: |
187 self.ui.write('Dialog accepted\n') | 200 self.ui.write('Dialog accepted\n') |
188 user = userfield.text() | 201 user = userfield.text() |
189 passwd = passfield.text() | 202 passwd = passfield.text() |
190 | 203 |
191 #!!! create pfile if necessary (with proper permissions), append auth data to it | 204 if pekey and pfile and remember: |
192 if pekey and pfile: | |
193 | 205 |
194 ofp = None | 206 ofp = None |
195 | 207 |
196 try: | 208 try: |
197 ofp = open(pfile, 'a') | 209 ofp = open(pfile, 'w') |
198 except: | 210 except: |
199 self.ui.write("failed to open authfile %s for writing\n" % pfile) | 211 self.ui.write("failed to open authfile %s for writing\n" % pfile) |
200 raise | 212 raise |
201 | 213 |
202 try: | 214 try: |
205 ofp.close() | 217 ofp.close() |
206 ofp = None | 218 ofp = None |
207 self.ui.write("failed to set proper permissions on authfile %s\n" % pfile) | 219 self.ui.write("failed to set proper permissions on authfile %s\n" % pfile) |
208 raise | 220 raise |
209 | 221 |
210 if ofp: | 222 #!!! add these sections first... |
223 | |
224 if remember.isChecked(): | |
211 pdata = encrypt(passwd, pekey) | 225 pdata = encrypt(passwd, pekey) |
212 ofp.write('[auth]\n') | 226 pcfg.set('auth', pkey, pdata) |
213 ofp.write(pkey + '=' + pdata + '\n') | 227 else: |
214 ofp.close() | 228 pcfg.set('auth', pkey, '') |
215 | 229 |
230 pcfg.set('preferences', 'remember', remember.isChecked()) | |
231 pcfg.write(ofp) | |
232 ofp.close() | |
216 | 233 |
217 # if passwd and keyring_key != '' and not from_keyring: | 234 # if passwd and keyring_key != '' and not from_keyring: |
218 # keyring_key = '%s@@%s' % (uri, user) | 235 # keyring_key = '%s@@%s' % (uri, user) |
219 ## keyring.set_password('Mercurial', keyring_key, passwd) | 236 ## keyring.set_password('Mercurial', keyring_key, passwd) |
220 self.add_password(realm, authuri, user, passwd) | 237 self.add_password(realm, authuri, user, passwd) |