-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathupdate.py
94 lines (75 loc) · 2.39 KB
/
update.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
85
86
87
88
89
90
91
92
93
94
#
# Voxel Builder Updater
#
import os
import sys
import shutil
from io import BytesIO
from urllib.request import urlopen
from zipfile import ZipFile
VBUILDER = 'https://github.com/nimadez/voxel-builder/archive/refs/heads/main.zip'
EXCLUDE = [ "voxel-builder-main", "electron", ".user_backup" ]
run_bat = """@echo off
title Voxel Builder
start "" electron\electron .
"""
cwd = os.getcwd()
def main():
DIR_SRC = cwd + '/voxel-builder-main'
DIR_DST = cwd
DIR_USR = cwd + '/src/user'
DIR_BKP = cwd + '/.user_backup'
remove_directory(DIR_SRC)
try:
print("Connecting to GitHub...")
downloadZip(VBUILDER, DIR_DST)
except:
input("Error: Unable to fetch GitHub repository, check your internet connection.")
sys.exit(0)
print(' --------------------------------')
print(' Voxel Builder Updater')
print(' --------------------------------')
print(' A backup copy of the "user"')
print(' directory will be created.\n')
if input(" Begin Update (Y/N)? ").upper() != "Y":
remove_directory(DIR_SRC)
sys.exit(0)
# clear previous installation
if os.path.exists(DIR_DST):
if os.path.exists(DIR_USR):
if os.path.exists(DIR_BKP):
remove_directory(DIR_BKP)
os.rename(DIR_USR, DIR_BKP)
os.chdir(DIR_DST)
for item in os.listdir(os.getcwd()):
if item not in EXCLUDE:
if os.path.isfile(item):
os.remove(item)
elif os.path.isdir(item):
shutil.rmtree(item, ignore_errors=True)
# extract repository
print('\nSetting up voxel-builder...')
for f in os.listdir(DIR_SRC):
shutil.move(os.path.join(DIR_SRC, f), DIR_DST)
os.rmdir(DIR_SRC)
with open(DIR_DST + "/run.bat", "w") as f:
f.write(run_bat)
print('Done')
def downloadZip(url, destdir):
with urlopen(url) as zip:
with ZipFile(BytesIO(zip.read())) as zf:
zf.extractall(destdir)
def remove_directory(dir):
if os.path.exists(dir):
os.chdir(dir)
for item in os.listdir(os.getcwd()):
if os.path.isfile(item):
os.remove(item)
elif os.path.isdir(item):
shutil.rmtree(item, ignore_errors=True)
os.chdir(cwd)
os.rmdir(dir)
if __name__== "__main__":
main()
print("\nUpdate complete.")
input()