peinjector/installer/install.py

162 lines
4.8 KiB
Python
Raw Normal View History

2024-02-08 19:04:07 +08:00
#####################################
## PEinjector/installer ##
2024-02-14 21:28:53 +08:00
####################################
2024-02-08 19:04:07 +08:00
# This moudle is a part of PEinjector.
# Please comply with the LICENSE.
import os
import shutil
import json
2024-02-13 18:53:43 +08:00
import platform
2024-02-14 21:28:53 +08:00
import getpass
2024-02-08 19:04:07 +08:00
############ Config ############
KEYWORD = "winpe"
PYTHON = "python"
DISK = "{disk}"
################################
2024-02-14 21:28:53 +08:00
is_linux:bool = False
2024-02-08 19:04:07 +08:00
2024-02-13 18:53:43 +08:00
def find_disk():
if platform.system() == 'Linux':
2024-02-14 21:28:53 +08:00
if getpass.getuser() != 'root':
raise OSError("Permission denied")
is_linux = True
mount_point = "/mnt/prinjector_mount_point"
2024-02-14 21:28:53 +08:00
# print(
# "Warning: Linux support is developing.Please umount {mount_point}")
if not os.path.exists(mount_point):
os.mkdir(mount_point)
elif len(os.listdir(mount_point)):
raise OSError("Mount point {} not empty!".format(mount_point))
i: int = 97
while os.path.exists(f"/dev/sd{chr(i)}"):
j: int = 1
2024-02-14 21:28:53 +08:00
while os.path.exists(f"/dev/sd{chr(i)}{str(j)}"):
2024-02-14 12:34:19 +08:00
os.system(
2024-02-14 21:28:53 +08:00
f"mount /dev/sd{chr(i)}{str(j)} {mount_point}")
if os.path.exists(mount_point+"/ventoy/ventoy.json"):
return mount_point
j += 1
os.system(f"umount {mount_point}")
i += 1
i: int = 0
while os.path.exists(f"/dev/nvme{str(i)}"):
j: int = 1
while os.path.exists(f"/dev/nvme{str(i)}n{str(j)}"):
k: int = 1
while os.path.exists(f"/dev/nvme{str(i)}n{str(j)}p{str(k)}"):
os.system(
2024-02-14 12:34:19 +08:00
f"mount /dev/nvme{str(i)}n{str(j)}p{str(k)} {mount_point} --mkdir")
if os.path.exists(mount_point+"/ventoy/ventoy.json"):
return mount_point
k += 1
os.system(f"umount {mount_point}")
j += 1
i += 1
2024-02-13 18:53:43 +08:00
elif platform.system() == 'Windows':
for i in "CDEFGHIJKABLMNOPQRSTUVWYZ":
this_disk = i+":"
if os.path.exists(this_disk + "/ventoy/ventoy.json"):
return this_disk
2024-02-13 18:53:43 +08:00
else:
raise OSError(f"Unsupported operating system:{platform.system()}")
2024-02-08 19:04:07 +08:00
raise OSError(
"Cannot find the Ventoy disk!(try to run ventoyPlugson once)")
print("Update packages")
os.system("git submodule update --init --recursive")
2024-02-08 19:04:07 +08:00
DISK = DISK.replace("{disk}", find_disk())
2024-02-13 18:53:43 +08:00
print("Use disk: "+DISK)
2024-02-08 19:04:07 +08:00
with open(DISK+"/ventoy/ventoy.json", "r") as file:
2024-02-13 18:53:43 +08:00
ventoy_cfg = json.load(file)
2024-02-08 19:04:07 +08:00
searchroot = ""
2024-02-13 18:53:43 +08:00
search_level = 3
if "control" in ventoy_cfg:
for i in ventoy_cfg["control"]:
2024-02-08 19:04:07 +08:00
if "VTOY_DEFAULT_SEARCH_ROOT" in i:
searchroot = i["VTOY_DEFAULT_SEARCH_ROOT"]
if "VTOY_MAX_SEARCH_LEVEL" in i:
if i["VTOY_MAX_SEARCH_LEVEL"] != "max":
2024-02-13 18:53:43 +08:00
search_level = int(i["VTOY_MAX_SEARCH_LEVEL"])
2024-02-08 19:04:07 +08:00
filelist = []
2024-02-13 18:53:43 +08:00
def dfs(deep, dir, search_level):
if deep > search_level:
2024-02-08 19:04:07 +08:00
return
lists = os.listdir(DISK+dir)
for i in lists:
if os.path.isfile(DISK+dir+"/"+i):
if ".iso" in i.lower() and KEYWORD in i.lower():
filelist.append(dir+"/"+i)
else:
2024-02-13 18:53:43 +08:00
dfs(deep+1, dir+"/"+i, search_level)
2024-02-08 19:04:07 +08:00
2024-02-13 18:53:43 +08:00
dfs(0, searchroot, search_level)
2024-02-08 19:04:07 +08:00
2024-02-13 18:53:43 +08:00
if "injection" in ventoy_cfg:
for i in ventoy_cfg["injection"]:
2024-02-08 19:04:07 +08:00
if "image" in i:
if i["image"] in filelist:
filelist.remove(i["image"])
else:
2024-02-13 18:53:43 +08:00
ventoy_cfg["injection"] = []
print("Found iso:", filelist)
2024-02-08 19:04:07 +08:00
for i in filelist:
2024-02-13 18:53:43 +08:00
ventoy_cfg["injection"].append({
2024-02-08 19:04:07 +08:00
"image": i,
"archive": "/PEinjector/PEinjector.7z"
})
2024-02-13 18:53:43 +08:00
print("Writing config")
2024-02-08 19:04:07 +08:00
with open(DISK+"/ventoy/ventoy.json", "w") as file:
2024-02-13 18:53:43 +08:00
file.write(json.dumps(ventoy_cfg))
2024-02-08 19:04:07 +08:00
2024-02-13 18:53:43 +08:00
print("Copying files")
2024-02-08 19:04:07 +08:00
if not os.path.exists(DISK+"/PEinjector"):
os.mkdir(DISK+"/PEinjector")
if os.path.exists(DISK+"/PEinjector/PEinjector.7z"):
os.remove(DISK+"/PEinjector/PEinjector.7z")
shutil.copyfile("dist/PEinjector.7z", DISK+"/PEinjector/PEinjector.7z")
if not os.path.exists(DISK+"/PEinjector/disable.txt"):
with open(DISK+"/PEinjector/disable.txt", "w") as file:
pass
2024-02-13 18:53:43 +08:00
print("Writing version info")
2024-02-08 19:04:07 +08:00
shutil.copyfile("version", DISK+"/PEinjector/VERSION")
2024-02-13 18:53:43 +08:00
print("Creating dir")
2024-02-08 19:04:07 +08:00
if not os.path.exists(DISK+"/PEinjector/install"):
os.mkdir(DISK+"/PEinjector/install")
2024-02-13 18:53:43 +08:00
print("Installing basic package")
2024-02-08 19:04:07 +08:00
if not os.path.exists(DISK+"/PEinjector/package"):
os.mkdir(DISK+"/PEinjector/package")
for i in os.listdir("package"):
if os.path.exists(DISK+"/PEinjector/package/"+i):
shutil.rmtree(DISK+"/PEinjector/package/"+i)
shutil.copytree("package/"+i, DISK+"/PEinjector/package/"+i)
2024-02-14 21:28:53 +08:00
if is_linux:
os.system(f"umount {mount_point}")
2024-02-13 18:53:43 +08:00
print("All done.")