[Reproducible-commits] [diffoscope] 01/03: Properly handle SIGTERM and do cleanup temp files
Jérémy Bobbio
lunar at moszumanska.debian.org
Thu Sep 3 15:58:26 UTC 2015
This is an automated email from the git hooks/post-receive script.
lunar pushed a commit to branch master
in repository diffoscope.
commit 437d0212aed8e0dd444da14dbf8e3d050c79796e
Author: Jérémy Bobbio <lunar at debian.org>
Date: Thu Sep 3 15:46:44 2015 +0000
Properly handle SIGTERM and do cleanup temp files
Thanks Mattia Rizzolo for reporting this and how he did work around this
deficiency.
Closes: #788568
---
diffoscope/__main__.py | 6 ++++++
tests/test_main.py | 22 ++++++++++++++++++++++
2 files changed, 28 insertions(+)
diff --git a/diffoscope/__main__.py b/diffoscope/__main__.py
index e53a1db..6971c2f 100644
--- a/diffoscope/__main__.py
+++ b/diffoscope/__main__.py
@@ -25,6 +25,7 @@ from contextlib import contextmanager
import logging
import codecs
import os
+import signal
import sys
import traceback
from diffoscope import logger, VERSION, set_locale
@@ -126,9 +127,14 @@ def run_diffoscope(parsed_args):
return 0
+def sigterm_handler(signo, stack_frame):
+ sys.exit(2)
+
+
def main(args=None):
if args is None:
args = sys.argv[1:]
+ signal.signal(signal.SIGTERM, sigterm_handler)
try:
parser = create_parser()
parsed_args = parser.parse_args(args)
diff --git a/tests/test_main.py b/tests/test_main.py
index 2509190..87c306b 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -18,6 +18,9 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
+import os
+import signal
+import tempfile
import pytest
from diffoscope.__main__ import main
@@ -57,3 +60,22 @@ def test_non_existing_files_with_new_file(capsys):
assert '--- /nonexisting1' in out
assert '+++ /nonexisting2' in out
assert 'Trying to compare two non-existing files.' in out
+
+TEST_TAR1_PATH = os.path.join(os.path.dirname(__file__), 'data/test1.tar')
+TEST_TAR2_PATH = os.path.join(os.path.dirname(__file__), 'data/test2.tar')
+
+def test_remove_temp_files_on_sigterm(tmpdir, monkeypatch):
+ args = [TEST_TAR1_PATH, TEST_TAR2_PATH]
+ pid = os.fork()
+ if pid == 0:
+ def suicide(*args):
+ os.kill(os.getpid(), signal.SIGTERM)
+ monkeypatch.setattr('diffoscope.comparators.text.TextFile.compare', suicide)
+ tempfile.tempdir = str(tmpdir)
+ with pytest.raises(SystemExit) as excinfo:
+ main(args)
+ os._exit(excinfo.value.code)
+ else:
+ _, ret = os.waitpid(pid, 0)
+ assert (ret >> 8) == 2 # having received SIGTERM is trouble
+ assert os.listdir(str(tmpdir)) == []
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the Reproducible-commits
mailing list