diff options
| author | Ulf Magnusson <ulfalizer@gmail.com> | 2019-05-29 02:19:14 +0200 |
|---|---|---|
| committer | Ulf Magnusson <ulfalizer@gmail.com> | 2019-05-29 02:47:44 +0200 |
| commit | 437c6ccebe96e022a112057df7db6a4bbbfeaba8 (patch) | |
| tree | 0ec71224014647948a66c010903464438ba98406 /kconfiglib.py | |
| parent | 15de4c0c9924cfeef1553fde21cb9b9e288f6238 (diff) | |
Use os.replace() if available in _save_old()
Gives *nix rename() semantics on Windows, with overwriting of the target
file.
Also limit the scope of the catch-all try-except.
Diffstat (limited to 'kconfiglib.py')
| -rw-r--r-- | kconfiglib.py | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/kconfiglib.py b/kconfiglib.py index 1527a00..98c39b9 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -1373,7 +1373,8 @@ class Kconfig(object): written. Errors are silently ignored if <filename>.old cannot be written (e.g. - due to being a directory). + due to being a directory, or <filename> being something like + /dev/null). verbose (default: True): If True and filename is None (automatically infer configuration @@ -6057,24 +6058,34 @@ def _touch_dep_file(path, sym_name): def _save_old(path): - # See write_config(). os.replace() would be nice here, but it's Python 3 - # (3.3+) only. + # See write_config() + + def copy(src, dst): + # Import as needed, to save some startup time + import shutil + shutil.copyfile(src, dst) + + if islink(path): + # Preserve symlinks + copy_fn = copy + elif hasattr(os, "replace"): + # Python 3 (3.3+) only. Best choice when available, because it + # removes <filename>.old on both *nix and Windows. + copy_fn = os.replace + elif os.name == "posix": + # Removes <filename>.old on POSIX systems + copy_fn = os.rename + else: + # Fall back on copying + copy_fn = copy try: - if os.name == "posix" and not islink(path): - # Will remove <filename>.old if it already exists on POSIX - # systems - os.rename(path, path + ".old") - else: - # Use copyfile() if 'path' is a symlink. The intention is probably - # to overwrite the target in that case. Only import shutil as - # needed, to save some startup time. - import shutil - shutil.copyfile(path, path + ".old") + copy_fn(path, path + ".old") except: - # Ignore errors from 'filename' missing as well as other errors. The + # Ignore errors from 'path' missing as well as other errors. # <filename>.old file is usually more of a nice-to-have, and not worth - # erroring out over e.g. if <filename>.old happens to be a directory. + # erroring out over e.g. if <filename>.old happens to be a directory or + # <filename> is something like /dev/null. pass |
