handle all plaintext passwd keys in config
authorJF Ding <jian-feng.ding@intel.com>
Sun, 3 Jun 2012 10:23:21 +0000 (18:23 +0800)
committerJF Ding <jian-feng.ding@intel.com>
Sun, 3 Jun 2012 11:31:19 +0000 (19:31 +0800)
gitbuildsys/conf.py

index c5af15e..8892ba9 100644 (file)
@@ -19,6 +19,7 @@
 from __future__ import with_statement
 import os, sys
 import base64
+from collections import defaultdict
 from ConfigParser import *
 import msger
 import errors
@@ -255,12 +256,13 @@ su_wrapper = $build__su_wrapper
 distconf = $build__distconf
 
 ; optional, repos definitions
-repo1.url=
-repo1.user=
-repo1.passwdx=
-repo2.url=
-repo2.user=
-repo2.passwdx=
+# repo1.url=
+# repo1.user=
+# repo1.passwd=
+; one more repo
+# repo2.url=
+# repo2.user=
+# repo2.passwd=
 
 [import]
 ; optional, for git author information
@@ -293,6 +295,8 @@ repo2.passwdx=
                     msger.error('No config file available')
 
         self.cfgparser.read(fpaths)
+
+        self.replaced_keys = defaultdict(list)
         self._check_passwd()
 
     def _lookfor_confs(self):
@@ -352,18 +356,22 @@ repo2.passwdx=
 
     def _check_passwd(self):
         for sec in self.DEFAULTS.keys():
-            if 'passwd' in self.DEFAULTS[sec]:
-                plainpass = self._get('passwd', sec)
-                if not plainpass:
-                    # None or ''
-                    continue
-
-                msger.warning('plaintext password in config file will '
-                              'be replaced by encoded one')
-                self.set('passwd',
-                         base64.b64encode(plainpass.encode('bz2')),
-                         sec)
-                self.update()
+            for key in self.options(sec):
+                if key.endswith('passwd'):
+                    plainpass = self._get(key, sec)
+                    if not plainpass:
+                        # None or ''
+                        continue
+
+                    self.replaced_keys[sec].append(key)
+                    self.set(key,
+                             base64.b64encode(plainpass.encode('bz2')),
+                             sec)
+
+        if self.replaced_keys:
+            msger.warning('plaintext password in config file will '
+                          'be replaced by encoded one')
+            self.update()
 
     def _get(self, opt, section='general'):
         try:
@@ -391,7 +399,12 @@ repo2.passwdx=
 
     def options(self, section='general'):
         try:
-            return  self.cfgparser.options(section)
+            opts = self.cfgparser.options(section)
+            if section in self.replaced_keys:
+                opts = list(set(opts) - set(self.replaced_keys[section]))
+
+            return opts
+
         except NoSectionError:
             if section in self.DEFAULTS:
                 return self.DEFAULTS[section]
@@ -410,9 +423,9 @@ repo2.passwdx=
             return self._get(opt, section)
 
     def set(self, opt, val, section='general', replace_opt=None):
-        if opt == 'passwd':
-            opt = 'passwdx'
-            replace_opt = 'passwd'
+        if opt.endswith('passwd'):
+            replace_opt = opt
+            opt += 'x'
         return self.cfgparser.set(section, opt, val, replace_opt)
 
     def update(self):