Skip to content

Commit 4bc2b36

Browse files
committed
feat(updater): test extract
Signed-off-by: Me0wo <[email protected]>
1 parent d4b657f commit 4bc2b36

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed

Updater/data/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
path:
2-
workPath: ../
2+
workPath: ./work
33
processName: QuickNote.exe
44
tagPath: ./data/tags
55

Updater/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from Updater.config.yml_loader import get_config
66
from Updater.updater.assets.name import get_package_name_from_current_machine
77
from Updater.updater.downloader.temp import download_to_temp
8+
from Updater.updater.extract.extract import extract_and_replace
89
from Updater.updater.runner.process import find_processes_by_path, try_terminate
910
from Updater.updater.tag.reader import read
1011
from fetch.github_release_api import fetch_latest_release
@@ -47,6 +48,10 @@ def main():
4748

4849
downloaded_path = download_to_temp(download_url)
4950

51+
extract_and_replace(downloaded_path, Path(get_config().path.workPath))
52+
53+
print(Path(get_config().path.workPath))
54+
5055
print(downloaded_path)
5156

5257
print("=" * 80)

Updater/updater/extract/__init__.py

Whitespace-only changes.

Updater/updater/extract/extract.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from pathlib import Path
2+
3+
from Updater.updater.extract.tar_gz import _extract_tar
4+
from Updater.updater.extract.zip import _extract_zip
5+
6+
7+
def extract_and_replace(archive_path: Path, extract_to: Path):
8+
if not archive_path.exists():
9+
raise FileNotFoundError(f"Archive not found: {archive_path}")
10+
11+
if not extract_to.exists():
12+
extract_to.mkdir(parents=True)
13+
14+
if archive_path.suffix == '.zip':
15+
_extract_zip(archive_path, extract_to)
16+
elif archive_path.suffixes[-2:] == ['.tar', '.gz'] or archive_path.suffix == '.tgz':
17+
_extract_tar(archive_path, extract_to)
18+
else:
19+
raise ValueError(f"Unsupported archive format: {archive_path.suffix}")

Updater/updater/extract/tar_gz.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import shutil
2+
import tarfile
3+
from pathlib import Path
4+
5+
6+
def _extract_tar(tar_path: Path, extract_to: Path):
7+
with tarfile.open(tar_path, 'r:*') as tar:
8+
for member in tar.getmembers():
9+
target_path = extract_to / member.name
10+
11+
if member.isdir():
12+
if target_path.exists():
13+
shutil.rmtree(target_path)
14+
target_path.mkdir(parents=True, exist_ok=True)
15+
else:
16+
if target_path.exists():
17+
target_path.unlink()
18+
target_path.parent.mkdir(parents=True, exist_ok=True)
19+
with tar.extractfile(member) as source, target_path.open('wb') as target:
20+
shutil.copyfileobj(source, target)

Updater/updater/extract/zip.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import shutil
2+
import zipfile
3+
from pathlib import Path
4+
5+
6+
def _extract_zip(zip_path: Path, extract_to: Path):
7+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
8+
for member in zip_ref.namelist():
9+
target_path = extract_to / member
10+
11+
if member.endswith('/'):
12+
if target_path.exists():
13+
shutil.rmtree(target_path)
14+
target_path.mkdir(parents=True, exist_ok=True)
15+
else:
16+
if target_path.exists():
17+
target_path.unlink()
18+
target_path.parent.mkdir(parents=True, exist_ok=True)
19+
with zip_ref.open(member) as source, target_path.open('wb') as target:
20+
shutil.copyfileobj(source, target)

0 commit comments

Comments
 (0)