diff options
author | Jan Pokorný <jpokorny@redhat.com> | 2015-12-22 15:30:35 +0100 |
---|---|---|
committer | Jan Pokorný <jpokorny@redhat.com> | 2016-02-01 23:53:15 +0100 |
commit | 229a27bbe7ab3fd1fd09c12fc594a0ff60c8a121 (patch) | |
tree | 8e8ec28447839a2325964f370fa7284390087a60 | |
parent | 50ec4e5de3a8725041c458fdd0f04a54994b1b2e (diff) | |
download | clufter-229a27bbe7ab3fd1fd09c12fc594a0ff60c8a121.tar.gz clufter-229a27bbe7ab3fd1fd09c12fc594a0ff60c8a121.tar.xz clufter-229a27bbe7ab3fd1fd09c12fc594a0ff60c8a121.zip |
filter: _try_edit: make "was file modified" test more robust
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
-rw-r--r-- | filter.py | 56 |
1 files changed, 30 insertions, 26 deletions
@@ -384,35 +384,39 @@ class XMLFilter(Filter, MetaPlugin): tmpdir = mkdtemp(prefix=pkg_name) reply, force = '', '' try: - tmp = NamedTemporaryFile(dir=tmpdir, suffix='.xml', delete=True) + tmp_name = "" + tmp = NamedTemporaryFile(dir=tmpdir, suffix='.xml', delete=False) with tmp as tmpfile: tmpfile.write(prompt) tmpfile.flush() - orig_mtime = stat(tmp.name).st_mtime - # XXX: Windows platform: delete=False, then close-popen-open - editor_args = shlex_split(editor) + [tmp.name] - assert len(editor_args) >= 2 - editor_args[0] = which(editor_args[0]) - try: - # pty.spawn doesn't work as nicely, - # /dev/tty may not be present (with open('/dev/tty') as si) - # and we decide whether to be interactive per - # sys.__stdin__ anyway - log.info("running `{0}'".format(' '.join(editor_args))) - check_call(editor_args, stdin=__stdin__) - except (CalledProcessError, IOError) as e: - raise FilterError(cls, str(e)) - except OSError: - raise FilterError(cls, "Editor `{0}' seems unavailable" - .format(editor)) - if orig_mtime == stat(tmp.name).st_mtime: - return None, force # no change occurred - # do not trust editors/sed/whatever to do a _real in-place_ - # modifications (sed definitely doesn't; see also - # http://www.pixelbeat.org/docs/unix_file_replacement.html), - # otherwise tmpfile.seek(0) would be enough - with open(tmp.name, 'r') as tmpfile: - reply = tmpfile.read().strip() + tmp_name = tmp.name + old_stat = stat(tmp_name) + + editor_args = shlex_split(editor) + [tmp_name] + assert len(editor_args) >= 2 + editor_args[0] = which(editor_args[0]) + try: + # pty.spawn doesn't work as nicely, + # /dev/tty may not be present (with open('/dev/tty') as si) + # and we decide whether to be interactive per + # sys.__stdin__ anyway + log.info("running `{0}'".format(' '.join(editor_args))) + check_call(editor_args, stdin=__stdin__) + except (CalledProcessError, IOError) as e: + raise FilterError(cls, str(e)) + except OSError: + raise FilterError(cls, "Editor `{0}' seems unavailable" + .format(editor)) + new_stat = stat(tmp_name) + if old_stat.st_size == new_stat.st_size \ + and old_stat.st_mtime == new_stat.st_mtime: + return None, force # no change occurred + # do not trust editors/sed/whatever to do a _real in-place_ + # modifications (sed definitely doesn't; see also + # http://www.pixelbeat.org/docs/unix_file_replacement.html), + # otherwise tmpfile.seek(0) would be enough + with open(tmp_name, 'r') as tmpfile: + reply = tmpfile.read().strip() finally: rmtree(tmpdir) if not reply: |