diff --git a/dtrx/dtrx.py b/dtrx/dtrx.py index bf4bf05..4fb1297 100755 --- a/dtrx/dtrx.py +++ b/dtrx/dtrx.py @@ -1353,15 +1353,28 @@ def magic_map_matches(self, output, magic_map): def try_by_magic(self, filename): try: - process = subprocess.Popen(["file", "-zL", filename], stdout=subprocess.PIPE) - status = process.wait() - if status != 0: + result = subprocess.run( + ["file", "-zL", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True + ) + if result.returncode != 0: return [] + # if output contains 'ERROR:[', there was an error unzipping the + # first archive entry. re-run without -z. + output = result.stdout.split("\n")[0] + if "ERROR:[" in output: + result = subprocess.run( + ["file", "-L", filename], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + if result.returncode != 0: + return [] + output = result.stdout.split("\n")[0] + except FileNotFoundError: logger.error("'file' command not found, skipping magic test") return [] - output = process.stdout.readline().decode("ascii") - process.stdout.close() if output.startswith("%s: " % filename): output = output[len(filename) + 2 :] mimes = self.magic_map_matches(output, self.magic_mime_map) diff --git a/tests/test-1.23.whl b/tests/test-1.23.whl new file mode 100644 index 0000000..aa0ff55 Binary files /dev/null and b/tests/test-1.23.whl differ diff --git a/tests/tests.yml b/tests/tests.yml index 43b0c28..029e3ad 100644 --- a/tests/tests.yml +++ b/tests/tests.yml @@ -987,3 +987,10 @@ filenames: getting-started.crx baseline: | unzip -q $1 -d getting-started + +- name: whl + filenames: test-1.23.whl + baseline: | + mkdir test-1.23 + cd test-1.23 + unzip -q ../$1