-
Notifications
You must be signed in to change notification settings - Fork 4
/
autoupdate.py
84 lines (66 loc) · 2.84 KB
/
autoupdate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import requests
import zipfile
from io import BytesIO
class MyGitReleaseDownloader:
"""
Download the last repo release
"""
def __init__(self, repo_url: str, destination_path: str):
self.repo_url = repo_url
self.api_url = f"https://api.github.com/repos/{self._extract_repo_owner_and_name()}/releases/latest"
self.destination_path = destination_path
def _extract_repo_owner_and_name(self):
"""
Extract my name
"""
return "/".join(self.repo_url.rstrip("/").split("/")[-2:])
def download_latest_release(self):
"""
Download the lastest release ( zip file) or update if necessary
"""
response = requests.get(self.api_url)
if response.status_code != 200:
print(f"Error : {response.status_code} Please report it")
return False
# Get the url of the zip release
latest_release = response.json()
zip_url = latest_release['zipball_url']
# Downloading..
print(f"Download from {zip_url}...")
zip_response = requests.get(zip_url)
if zip_response.status_code == 200:
# Decompress or update the repo
self._unzip_and_update(zip_response.content)
else:
print(f"Download Error. Please report it {zip_response.status_code}")
return False
return True
def _unzip_and_update(self, zip_content):
"""
Zip file extraction
"""
if not os.path.exists(self.destination_path):
print(f"Create the Folder {self.destination_path}...")
os.makedirs(self.destination_path)
# Unzip...
print(f"Estrazione e aggiornamento nella cartella {self.destination_path}...")
with zipfile.ZipFile(BytesIO(zip_content)) as zip_file:
# For each file or folder in zip file
for file_info in zip_file.infolist():
extracted_file_path = os.path.join(self.destination_path, file_info.filename)
# Make folder if it does not exist
if file_info.is_dir():
if not os.path.exists(extracted_file_path):
os.makedirs(extracted_file_path)
else:
# Overwite the file only if it is different from the previous version
print(f"Aggiornamento del file: {extracted_file_path}")
with zip_file.open(file_info) as source, open(extracted_file_path, "wb") as target:
target.write(source.read())
print(f"Finish {self.destination_path}")
if __name__ == "__main__":
repo_url = "https://github.com/31December99/Unit3Dup"
destination_path = os.path.join(os.getcwd(), "Unit3Dup")
downloader = MyGitReleaseDownloader(repo_url, destination_path)
downloader.download_latest_release()