diff options
| -rw-r--r-- | kconfiglib.py | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/kconfiglib.py b/kconfiglib.py index 6ffac90..b854186 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -1244,7 +1244,8 @@ class Kconfig(object): .format(self.config_prefix, sym.name, val)) def write_config(self, filename, - header="# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n"): + header="# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n", + save_old=True): r""" Writes out symbol values in the .config format. The format matches the C implementation, including ordering. @@ -1264,7 +1265,19 @@ class Kconfig(object): Text that will be inserted verbatim at the beginning of the file. You would usually want each line to start with '#' to make it a comment, and include a final terminating newline. + + save_old (default: True): + If True and <filename> already exists, a copy of it will be saved to + .<filename>.old in the same directory before the new configuration is + written. The leading dot is added only if the filename doesn't + already start with a dot. + + Errors are silently ignored if .<filename>.old cannot be written + (e.g. due to being a directory). """ + if save_old: + _save_old(filename) + with self._open(filename, "w") as f: f.write(header) @@ -5776,6 +5789,31 @@ def _touch_dep_file(sym_name): os.close(os.open( sym_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o644)) +def _save_old(path): + # See write_config() + + dirname, basename = os.path.split(path) + if basename.startswith("."): + basename += ".old" + else: + basename = "." + basename + ".old" + backup = os.path.join(dirname, basename) + + try: + # os.replace() would be nice here, but it's Python 3 (3.3+) only + if os.name == "posix": + # Will remove .<filename>.old if it already exists on POSIX + # systems + os.rename(path, backup) + else: + import shutil + shutil.copyfile(path, backup) + except: + # Ignore errors from 'filename' missing as well as other errors. The + # backup file is more of a nice-to-have, and not worth erroring out + # over e.g. if .<filename>.old happens to be a directory. + pass + def _decoding_error(e, filename, macro_linenr=None): # Gives the filename and context for UnicodeDecodeError's, which are a pain # to debug otherwise. 'e' is the UnicodeDecodeError object. |
