Skip to content

Commit 3712b4c

Browse files
authored
Merge pull request #21 from openaleph/bug/password-7z
Handle password protected 7z
2 parents 511f516 + dd5bf24 commit 3712b4c

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

ingestors/packages/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from pathlib import PurePath
66

77
import py7zr
8-
from py7zr.exceptions import ArchiveError
8+
from py7zr.exceptions import ArchiveError, PasswordRequired
99

10-
from ingestors.exc import ProcessingException
10+
from ingestors.exc import ENCRYPTED_MSG, ProcessingException
1111
from ingestors.ingestor import Ingestor
1212
from ingestors.support.package import PackageSupport
1313
from ingestors.support.shell import ShellSupport
@@ -29,7 +29,10 @@ def unpack(self, file_path, entity, temp_dir):
2929

3030
try:
3131
with py7zr.SevenZipFile(str(pure_file_path), mode="r") as extractor:
32-
extractor.extractall(path=temp_dir)
32+
try:
33+
extractor.extractall(path=temp_dir)
34+
except PasswordRequired:
35+
raise ProcessingException(ENCRYPTED_MSG)
3336
except ArchiveError as e:
3437
raise ProcessingException(f"Error: {e}")
3538

ingestors/support/package.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import shutil
21
import logging
2+
import shutil
3+
34
from followthemoney import model
45

5-
from ingestors.support.temp import TempFileSupport
6-
from ingestors.support.encoding import EncodingSupport
76
from ingestors.directory import DirectoryIngestor
87
from ingestors.exc import ProcessingException
8+
from ingestors.support.encoding import EncodingSupport
9+
from ingestors.support.temp import TempFileSupport
910

1011
log = logging.getLogger(__name__)
1112

@@ -36,7 +37,7 @@ def ingest(self, file_path, entity):
3637
self.manager.delegate(DirectoryIngestor, temp_dir, entity)
3738
except ProcessingException as e:
3839
raise ProcessingException(
39-
"Could not unpack the contents of this file."
40+
f"Could not unpack the contents of this file. {e}"
4041
) from e
4142

4243
def unpack(self, file_path, entity, temp_dir):
11.4 KB
Binary file not shown.

tests/test_packages.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from pprint import pprint # noqa
33

4-
from .support import TestCase
4+
from tests.support import TestCase
55

66

77
class PackagesTest(TestCase):
@@ -22,3 +22,12 @@ def test_tar(self):
2222
self.manager.ingest(fixture_path, entity)
2323
self.assertEqual(entity.first("processingStatus"), self.manager.STATUS_SUCCESS)
2424
self.assertEqual(entity.schema.name, "Package")
25+
26+
def test_password_protected_7z(self):
27+
fixture_path, entity = self.fixture("500_pages_password.7z")
28+
self.manager.ingest(fixture_path, entity)
29+
self.assertEqual(entity.first("processingStatus"), self.manager.STATUS_FAILURE)
30+
self.assertEqual(
31+
entity.get("processingError")[0],
32+
"Could not unpack the contents of this file. The document might be protected with a password. Try removing the password protection and re-uploading the documents.",
33+
)

0 commit comments

Comments
 (0)