Skip to content

Commit

Permalink
Fix zipped IOS support after zipfile refactor (fixes tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas FitzRoy-Dale committed Jun 24, 2024
1 parent df02d0e commit c47423b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
22 changes: 10 additions & 12 deletions rime/filesystem/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import tempfile
from typing import Optional
import zipfile
import posixpath

from iphone_backup_decrypt import EncryptedBackup # pyright: ignore[reportMissingImports]

Expand Down Expand Up @@ -68,7 +69,7 @@ def get_hashed_pathname(self, path):
else:
file_id = self._get_ios_hash(domain, relative_path)

return os.path.join(file_id[:2], file_id)
return posixpath.join(file_id[:2], file_id)

def add_file(self, path):
"""
Expand Down Expand Up @@ -317,10 +318,10 @@ def __init__(self, id_: str, root: str, metadata_db_path: str):
# get the main directory contained in the .zip container file
main_dir = zipsupport.get_zipfile_main_dir(zp)

with zp.open(main_dir / 'Manifest.db') as zf:
with (main_dir / 'Manifest.db').open('rb') as zf:
self.temp_manifest.write(zf.read())

with zp.open(main_dir / '_rime_settings.db') as zf:
with (main_dir / '_rime_settings.db').open('rb') as zf:
self.temp_settings.write(zf.read())

self.manifest = sqlite3_connect_with_regex_support(self.temp_manifest.name, read_only=True)
Expand All @@ -340,10 +341,7 @@ def is_device_filesystem(cls, path) -> bool:
with zipfile.ZipFile(path) as zp:
# get the main directory contained in the .zip container file
main_dir = zipsupport.get_zipfile_main_dir(zp)
return (
zipfile.Path(zp, main_dir / 'Manifest.db').exists()
and zipfile.Path(zp, main_dir / 'Info.plist').exists()
)
return (main_dir / 'Manifest.db').exists() and (main_dir / 'Info.plist').exists()

@classmethod
def create(cls, id_: str, root: str, metadata_db_path, template: Optional['DeviceFilesystem'] = None)\
Expand All @@ -368,21 +366,21 @@ def exists(self, path) -> bool:
with zipfile.ZipFile(self.root) as zp:
# get the main directory contained in the .zip container file
main_dir = zipsupport.get_zipfile_main_dir(zp)
return zipfile.Path(zp, main_dir.name / real_path).exists()
return (main_dir / real_path).exists()

def getsize(self, path) -> int:
with zipfile.ZipFile(self.root) as zp:
# get the main directory contained in the .zip container file
main_dir = zipsupport.get_zipfile_main_dir(zp)
return zp.getinfo(main_dir / self._converter.get_hashed_pathname(path)).file_size
return zipsupport.path_to_info(zp, main_dir / self._converter.get_hashed_pathname(path)).file_size

def ios_open_raw(self, path, mode):
# TODO: mode
tmp_copy = tempfile.NamedTemporaryFile(mode='w+b')
with zipfile.ZipFile(self.root) as zp:
# get the main directory contained in the .zip container file
main_dir = zipsupport.get_zipfile_main_dir(zp)
with zp.open(main_dir / path) as zf:
with (main_dir / path).open('rb') as zf:
tmp_copy.write(zf.read())
return tmp_copy

Expand All @@ -398,7 +396,7 @@ def sqlite3_connect(self, path, read_only=True):
with zipfile.ZipFile(self.root) as zp:
# get the main directory contained in the .zip container file
main_dir = zipsupport.get_zipfile_main_dir(zp)
with zp.open(main_dir / self._converter.get_hashed_pathname(path)) as zf:
with (main_dir / self._converter.get_hashed_pathname(path)).open('rb') as zf:
tmp_copy.write(zf.read())

log.debug(f"iOS connecting to {tmp_copy.name}")
Expand All @@ -415,7 +413,7 @@ def lock(self, locked: bool):
with zipfile.ZipFile(self.root, 'w') as zp:
# get the main directory contained in the .zip container file
main_dir = zipsupport.get_zipfile_main_dir(zp)
with zp.open(main_dir / '_rime_settings.db', 'w') as zf:
with zp.open(str(main_dir / '_rime_settings.db'), 'w') as zf:
zf.write(self.temp_settings.read())

def is_locked(self) -> bool:
Expand Down
8 changes: 7 additions & 1 deletion rime/filesystem/zipsupport.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from zipfile import ZipFile, Path
from zipfile import ZipFile, Path, ZipInfo

def get_zipfile_main_dir(zf: ZipFile) -> Path:
"""
Expand All @@ -19,3 +19,9 @@ def get_zipfile_main_dir(zf: ZipFile) -> Path:
return element

raise ValueError("The zipfile does not contain a single directory.")

def path_to_info(zf: ZipFile, path: Path) -> ZipInfo:
"""
Convert a Path to a PathInfo object.
"""
return zf.getinfo(str(path))
11 changes: 8 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

VENV = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, '.venv'))

RIMESERVER_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'frontend'))
RIMESERVER_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
RIMESERVER_CMD = [os.path.join(VENV, 'bin', 'uvicorn'), '--interface', 'asgi3', '--port', str(RIME_PORT),
'--factory', 'rimeserver:create_app']
'--factory', 'rime:rimeserver_create_app']
RIMESERVER_ENV = {
'RIME_CONFIG': os.path.join(RIMESERVER_DIR, 'frontend', 'rime_settings.yaml'),
}


def _rime_server_running(timeout=0):
Expand Down Expand Up @@ -48,7 +51,9 @@ def _install_dependencies():

def _start_rime_server():
# Start the RIME server in the background.
return subprocess.Popen(RIMESERVER_CMD, cwd=RIMESERVER_DIR)
env = os.environ.copy()
env.update(RIMESERVER_ENV)
return subprocess.Popen(RIMESERVER_CMD, cwd=RIMESERVER_DIR, env=env)


@pytest.fixture(scope="session", autouse=True)
Expand Down

0 comments on commit c47423b

Please sign in to comment.