File tree Expand file tree Collapse file tree 6 files changed +65
-1
lines changed Expand file tree Collapse file tree 6 files changed +65
-1
lines changed Original file line number Diff line number Diff line change 11path :
2- workPath : ../
2+ workPath : ./work
33 processName : QuickNote.exe
44 tagPath : ./data/tags
55
Original file line number Diff line number Diff line change 55from Updater .config .yml_loader import get_config
66from Updater .updater .assets .name import get_package_name_from_current_machine
77from Updater .updater .downloader .temp import download_to_temp
8+ from Updater .updater .extract .extract import extract_and_replace
89from Updater .updater .runner .process import find_processes_by_path , try_terminate
910from Updater .updater .tag .reader import read
1011from 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 )
Original file line number Diff line number Diff line change 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 } " )
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments