Merge pull request #1400 from andersk/atomic-open-permissions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Preserve expected permissions in atomic open_file
authored by David Lord · committed by GitHub · parents b24c51f, b812566 · Browse files · 3 files·+77 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+77 added−3 removed
- tests:+43 added−0 removed
- other:+34 added−3 removed
Provenance
- Commit
e0ad5a6- Tree
8637f85- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
b24c51f- Parent 2
b812566
3 files changed+77 added−3 removed
modified:click/_compat.py+32 added-3 removed
502 unmodified lines503 # Atomic writes are more complicated. They work by opening a file504 # as a proxy in the same folder and then using the fdopen505 # functionality to wrap it in a Python file. Then we wrap it in an506 # atomic file that moves the file over on close.507- import tempfile508- fd, tmp_filename = tempfile.mkstemp(dir=os.path.dirname(filename),509- prefix='.__atomic-write')507+ import errno508+ import random509 510+ try:511+ perm = os.stat(filename).st_mode512+ except OSError:513+ perm = None514+515+ flags = os.O_RDWR | os.O_CREAT | os.O_EXCL516+ if 'b' in mode:517+ flags |= getattr(os, 'O_BINARY', 0)518+519+ while True:520+ tmp_filename = os.path.join(521+ os.path.dirname(filename),522+ '.__atomic-write%08x' % (random.randrange(1 << 32),),523+ )524+ try:525+ fd = os.open(tmp_filename, flags, 0o666 if perm is None else perm)526+ break527+ except OSError as e:528+ if e.errno == errno.EEXIST or (529+ os.name == "nt"530+ and e.errno == errno.EACCES531+ and os.path.isdir(dir)532+ and os.access(dir, os.W_OK)533+ ):534+ continue535+ raise536+537+ if perm is not None:538+ os.chmod(tmp_filename, perm) # in case perm includes bits in umask539+540 if encoding is not None:541 f = io.open(fd, mode, encoding=encoding, errors=errors)542 else:543 f = os.fdopen(fd, mode)201 unmodified linesmodified:tests/test_utils.py+43 added
1 import os2+import stat3 import sys4 5 import pytest6 291 unmodified lines298 assert result.exception is None299 assert result.output == 'foobar\nmeep\n'300 301 302+@pytest.mark.skipif(WIN, reason='os.chmod() is not fully supported on Windows.')303+@pytest.mark.parametrize('permissions', [304+ 0o400,305+ 0o444,306+ 0o600,307+ 0o644,308+ ])309+def test_open_file_atomic_permissions_existing_file(runner, permissions):310+ with runner.isolated_filesystem():311+ with open('existing.txt', 'w') as f:312+ f.write('content')313+ os.chmod('existing.txt', permissions)314+315+ @click.command()316+ @click.argument('filename')317+ def cli(filename):318+ click.open_file(filename, 'w', atomic=True).close()319+320+ result = runner.invoke(cli, ['existing.txt'])321+ assert result.exception is None322+ assert stat.S_IMODE(os.stat('existing.txt').st_mode) == permissions323+324+325+@pytest.mark.skipif(WIN, reason='os.stat() is not fully supported on Windows.')326+def test_open_file_atomic_permissions_new_file(runner):327+ with runner.isolated_filesystem():328+ @click.command()329+ @click.argument('filename')330+ def cli(filename):331+ click.open_file(filename, 'w', atomic=True).close()332+333+ # Create a test file to get the expected permissions for new files334+ # according to the current umask.335+ with open('test.txt', 'w'):336+ pass337+ permissions = stat.S_IMODE(os.stat('test.txt').st_mode)338+339+ result = runner.invoke(cli, ['new.txt'])340+ assert result.exception is None341+ assert stat.S_IMODE(os.stat('new.txt').st_mode) == permissions342+343+344 @pytest.mark.xfail(WIN and not PY2, reason='God knows ...')345 def test_iter_keepopenfile(tmpdir):346 expected = list(map(str, range(10)))347 p = tmpdir.mkdir('testdir').join('testfile')15 unmodified linesmodified:CHANGES.rst+2 added
27 unmodified lines28 - Handle ``env MYPATH=''`` as though the option were not passed.29 :issue:`1196`30 - It is once again possible to call ``next(bar)`` on an active31 progress bar instance. :issue:`1125`32+- ``open_file`` with ``atomic=True`` retains permissions of existing33+ files and respects the current umask for new files. :issue:`1376`34 35 36 Version 7.037 -----------540 unmodified lines