| | @@ -120,68 +119,72 @@ class WriteConf(KssCommand): |
| 120 | 119 | source_files = [] |
| 121 | 120 | retval = True |
| 122 | 121 | cnt = 0 |
| 123 | | - for _mod in modules: |
| 124 | | - _file = files[cnt] |
| 122 | + try: |
| 125 | 123 | try: |
| 126 | | - exec("from karesansui.lib.parser.%s import %sParser as Parser" % (_mod,_mod,)) |
| 127 | | - |
| 128 | | - self.up_progress(5) |
| 129 | | - parser = Parser() |
| 130 | | - |
| 131 | | - # 辞書オペレータに追加 |
| 132 | | - self.up_progress(5) |
| 133 | | - if opts.php is True: |
| 134 | | - conf_arr = php_array_to_python_dict(open(_file).read()) |
| 135 | | - else: |
| 136 | | - exec("conf_arr = %s" % open(_file).read()) |
| 137 | | - dop.addconf(_mod,conf_arr) |
| 138 | | - |
| 139 | | - """ |
| 140 | | - 必要ならここで配列操作 |
| 141 | | - 通常は、配列操作後の辞書が_fileに書き込まれているので必要ない |
| 142 | | - dop.add (_mod,"foo","bar") |
| 143 | | - dop.delete(_mod,"foo") |
| 144 | | - """ |
| 145 | | - |
| 146 | | - # 設定ファイル一覧に作成(バックアップ用) |
| 147 | | - self.up_progress(5) |
| 148 | | - source_file = parser.source_file() |
| 149 | | - for _afile in source_file: |
| 150 | | - _bak_afile = "%s.%s" % (_afile,uniq_id) |
| 151 | | - copy_file(_afile,_bak_afile) |
| 152 | | - source_files = source_files + source_file |
| 153 | | - |
| 154 | | - # 辞書に戻す |
| 155 | | - self.up_progress(5) |
| 156 | | - conf_arr = dop.getconf(_mod) |
| 157 | | - #dop.preprint_r(_mod) |
| 158 | | - |
| 159 | | - # 設定ファイルに書き込み |
| 160 | | - self.up_progress(5) |
| 161 | | - extra_args = {} |
| 162 | | - extra_args["include"] = opts.include |
| 163 | | - if opts.early_exit is True: |
| 164 | | - retval = retval and parser.write_conf(conf_arr,extra_args=extra_args) |
| 165 | | - else: |
| 166 | | - retval = parser.write_conf(conf_arr,extra_args=extra_args) and retval |
| 167 | | - |
| 168 | | - if opts.delete is True: |
| 169 | | - os.unlink(_file) |
| 170 | | - |
| 171 | | - finally: |
| 172 | | - cnt = cnt + 1 |
| 173 | | - |
| 174 | | - if retval is False: |
| 175 | | - for _afile in source_files: |
| 176 | | - _bak_afile = "%s.%s" % (_afile,uniq_id) |
| 177 | | - os.unlink(_afile) |
| 178 | | - copy_file(_bak_afile,_afile) |
| 124 | + for _mod in modules: |
| 125 | + _file = files[cnt] |
| 126 | + exec("from karesansui.lib.parser.%s import %sParser as Parser" % (_mod,_mod,)) |
| 127 | + |
| 128 | + self.up_progress(5) |
| 129 | + parser = Parser() |
| 130 | + |
| 131 | + # 辞書オペレータに追加 |
| 132 | + self.up_progress(5) |
| 133 | + if opts.php is True: |
| 134 | + conf_arr = php_array_to_python_dict(open(_file).read()) |
| 135 | + else: |
| 136 | + exec("conf_arr = %s" % open(_file).read()) |
| 137 | + dop.addconf(_mod,conf_arr) |
| 138 | + |
| 139 | + """ |
| 140 | + 必要ならここで配列操作 |
| 141 | + 通常は、配列操作後の辞書が_fileに書き込まれているので必要ない |
| 142 | + dop.add (_mod,"foo","bar") |
| 143 | + dop.delete(_mod,"foo") |
| 144 | + """ |
| 145 | + |
| 146 | + # 設定ファイル一覧に作成(バックアップ用) |
| 147 | + self.up_progress(5) |
| 148 | + source_file = parser.source_file() |
| 149 | + for _afile in source_file: |
| 150 | + file_prefix = "%s." % _afile |
| 151 | + file_dirname = os.path.dirname(_afile) |
| 152 | + _bak_afile = tempfile.mkstemp(prefix=file_prefix, dir=file_dirname)[1] |
| 153 | + copy_file(_afile,_bak_afile) |
| 154 | + source_files.append((_afile, _bak_afile)) |
| 155 | + |
| 156 | + # 辞書に戻す |
| 157 | + self.up_progress(5) |
| 158 | + conf_arr = dop.getconf(_mod) |
| 159 | + #dop.preprint_r(_mod) |
| 160 | + |
| 161 | + # 設定ファイルに書き込み |
| 162 | + self.up_progress(5) |
| 163 | + extra_args = {} |
| 164 | + extra_args["include"] = opts.include |
| 165 | + if opts.early_exit is True: |
| 166 | + retval = retval and parser.write_conf(conf_arr,extra_args=extra_args) |
| 167 | + else: |
| 168 | + retval = parser.write_conf(conf_arr,extra_args=extra_args) and retval |
| 169 | + |
| 170 | + if retval is False: |
| 171 | + raise KssCommandOptException("ERROR: %s write configure failure" % _mod) |
| 172 | + |
| 173 | + if opts.delete is True: |
| 174 | + os.unlink(_file) |
| 175 | + |
| 176 | + cnt = cnt + 1 |
| 177 | + except: |
| 178 | + # 例外ありで、バックアップファイルを使って設定の切り戻し |
| 179 | + for _afile, _bak_afile in source_files: |
| 180 | + os.unlink(_afile) |
| 181 | + copy_file(_bak_afile,_afile) |
| 182 | + self.logger.warning("rollback write conf - %s to %s" % (_bak_afile, _afile)) |
| 183 | + raise |
| 184 | + finally: |
| 185 | + # 例外なしで、バックアップファイルを全て削除 |
| 186 | + for _afile, _bak_afile in source_files: |
| 179 | 187 | os.unlink(_bak_afile) |
| 180 | | - raise KssCommandOptException("ERROR: write configure failure") |
| 181 | | - |
| 182 | | - for _afile in source_files: |
| 183 | | - _bak_afile = "%s.%s" % (_afile,uniq_id) |
| 184 | | - os.unlink(_bak_afile) |
| 185 | 188 | |
| 186 | 189 | if opts.post_command is not None: |
| 187 | 190 | if opts.post_command[0:4] == "b64:": |