diff options
author | Daniel Watkins <oddbloke@ubuntu.com> | 2020-06-19 12:03:53 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-19 12:03:53 -0400 |
commit | 40e72860e6a7d8876731cc1cfda4e499d119f2a1 (patch) | |
tree | cdf607d7bd9242a0ac27642c8820fc6226e3b411 /cloudinit | |
parent | d083a0315faf67c00acdecdc3f95c700edf6ba06 (diff) | |
download | vyos-cloud-init-40e72860e6a7d8876731cc1cfda4e499d119f2a1.tar.gz vyos-cloud-init-40e72860e6a7d8876731cc1cfda4e499d119f2a1.zip |
util: add ensure_dir_exists parameter to write_file (#443)
This allows us to disable the `ensure_dir` call when it isn't
appropriate.
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/util.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py index 23d24c4c..81369652 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -1800,7 +1800,15 @@ def chmod(path, mode): os.chmod(path, real_mode) -def write_file(filename, content, mode=0o644, omode="wb", preserve_mode=False): +def write_file( + filename, + content, + mode=0o644, + omode="wb", + preserve_mode=False, + *, + ensure_dir_exists=True +): """ Writes a file with the given content and sets the file mode as specified. Restores the SELinux context if possible. @@ -1811,6 +1819,9 @@ def write_file(filename, content, mode=0o644, omode="wb", preserve_mode=False): @param omode: The open mode used when opening the file (w, wb, a, etc.) @param preserve_mode: If True and `filename` exists, preserve `filename`s current mode instead of applying `mode`. + @param ensure_dir_exists: If True (the default), ensure that the directory + containing `filename` exists before writing to + the file. """ if preserve_mode: @@ -1820,7 +1831,8 @@ def write_file(filename, content, mode=0o644, omode="wb", preserve_mode=False): except OSError: pass - ensure_dir(os.path.dirname(filename)) + if ensure_dir_exists: + ensure_dir(os.path.dirname(filename)) if 'b' in omode.lower(): content = encode_text(content) write_type = 'bytes' |