Skip to content

Commit

Permalink
future-proof command line argument parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tobi committed Nov 21, 2019
1 parent 76d902a commit 63b2c82
Showing 1 changed file with 51 additions and 58 deletions.
109 changes: 51 additions & 58 deletions BlenderUpdaterCLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def stop(self):
parser.add_argument(
"-b",
"--blender",
help="Desired Blender version, either '-b 281' or '-b 282'",
help="Desired Blender version - for example '-b 2.82'",
required=True,
type=str,
)
Expand Down Expand Up @@ -156,18 +156,7 @@ def stop(self):
failed = True

# check for desired blender version
if args.blender == "281":
blender = "2.81"
print("Blender version: " + Fore.GREEN + "2.81")
elif args.blender == "282":
blender = "2.82"
print("Blender version: " + Fore.GREEN + "2.82")
else:
print(
Fore.RED
+ "Syntax error - use '-b 281' for Blender 2.81 or '-b 282' for Blender 2.82"
)
failed = True
blender = args.blender

# check for desired operating system or autodetect when empty
if args.operatingsystem == "windows":
Expand Down Expand Up @@ -229,56 +218,62 @@ def stop(self):
if failed is True:
print(f"{Fore.RED}Input errors detected, aborted (check above for details)")
else:
print(f"{Fore.GREEN}All settings valid, proceeding...")
try:
req = requests.get(url)
except Exception:
print(f"{Fore.RED}Error connecting to {url}, check your internet connection")

filename = re.findall(
r"blender-"
+ blender
+ r"-\w+-"
+ opsys
+ r"[0-9a-zA-Z-._]*"
+ arch
+ r"\."
+ extension,
req.text,
)
try:
filename = re.findall(
r"blender-"
+ blender
+ r"-\w+-"
+ opsys
+ r"[0-9a-zA-Z-._]*"
+ arch
+ r"\."
+ extension,
req.text,
)
except Exception:
print(
f"{Fore.RED}No valid Blender version specified ({args.blender} not found)"
)
sys.exit()

if os.path.isfile("./config.ini"):
config.read("config.ini")
if "2.81" in str(filename[0]):
try:
lastversion = config.get("main", "version281")
except Exception: # TODO: Handle errors a bit more gracefully
lastversion = ""
elif "2.82" in str(filename[0]):
try:
lastversion = config.get("main", "version282")
except Exception:
lastversion = ""
if lastversion == filename[0]:
while True:
if args.yes:
break
elif args.no:
print(
"This version is already installed. -n option present, exiting..."
)
sys.exit()
else:
anyway = str(
input(
"This version is already installed. Continue anyways? [Y]es or [N]o: "
config.read("./config.ini")
try:
lastversion = config.get("main", "version")
except Exception: # TODO: Handle errors a bit more gracefully
lastversion = ""

try:
if lastversion == filename[0]:
while True:
if args.yes:
break
elif args.no:
print(
"This version is already installed. -n option present, exiting..."
)
).lower()
if anyway == "n":
sys.exit()
elif anyway == "y":
break
print("Invalid choice, try again!")
else:
anyway = str(
input(
"This version is already installed. Continue anyways? [Y]es or [N]o: "
)
).lower()
if anyway == "n":
sys.exit()
elif anyway == "y":
break
print("Invalid choice, try again!")
except Exception:
print(
f"{Fore.RED}No valid Blender version specified ({args.blender} not found)"
)
sys.exit()

else:
config.read("config.ini")
Expand All @@ -291,6 +286,7 @@ def stop(self):
shutil.rmtree("./blendertemp")
os.makedirs("./blendertemp", exist_ok=True)
dir_ = os.path.join(args.path, "")
print(f"{Fore.GREEN}All settings valid, proceeding...")
print(f"Downloading {filename[0]}")
chunkSize = 10240
try:
Expand Down Expand Up @@ -355,10 +351,7 @@ def stop(self):

# write configuration file
config.read("config.ini")
if "2.81" in str(filename[0]):
config.set("main", "version281", filename[0])
elif "2.82" in str(filename[0]):
config.set("main", "version282", filename[0])
config.set("main", "version", filename[0])
with open("config.ini", "w") as f:
config.write(f)

Expand Down

0 comments on commit 63b2c82

Please sign in to comment.