Skip to content

Commit

Permalink
handle exceptions before crashing gui, add symlink python3 to python3X
Browse files Browse the repository at this point in the history
  • Loading branch information
csm10495 committed Sep 11, 2021
1 parent ed7b034 commit 7b72a30
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
22 changes: 15 additions & 7 deletions cappinstaller/gui.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'''
Home to the GUI for cAppInstaller
'''
from inspect import trace
import queue
import time
import traceback
from concurrent.futures import ThreadPoolExecutor, TimeoutError

import numpy as np
Expand Down Expand Up @@ -73,19 +75,25 @@ def install_single_thing(self, tag: str, key: str) -> int:
output = self.window['Output']

def _inner():
if tag == 'choco':
return Choco(key, output_queue).install()
elif tag == 'cmd':
obj = MISC_COMMANDS[key]
return obj.install(output_queue)
else:
raise ValueError(f"{tag} is not supported")
try:
if tag == 'choco':
return Choco(key, output_queue).install()
elif tag == 'cmd':
obj = MISC_COMMANDS[key]
return obj.install(output_queue)
else:
raise ValueError(f"{tag} is not supported")
except Exception as ex:
output_queue.put_nowait([f"Unhandled Exception: {ex}"])
output_queue.put_nowait([traceback.format_exc()])
return -1

def xfer_from_queue_to_output():
lines = output_queue.get_nowait()
for line in lines:
output.print(line)

output.print(f"About to run: {tag} - {key}")
with ThreadPoolExecutor(max_workers=1) as pool:
result = pool.submit(_inner)
try:
Expand Down
32 changes: 28 additions & 4 deletions cappinstaller/misc_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, cmd):
''' cmd is a shell command to run '''
self.cmd = cmd

def install(self, output_queue: queue.Queue):
def install(self, output_queue: queue.Queue) -> int:
'''
This is the function called by the GUI to install/run.
'''
Expand All @@ -39,11 +39,35 @@ def __init__(self, reg_file_text: str):
pathlib.Path(temp_file).write_text(self.reg_file_text)
MiscCommand.__init__(self, 'reg import "%s"' % temp_file)

class SymlinkPython3ToPython3X(MiscCommand):
'''
A command to Symlink Python3 to Python3X'''
def __init__(self):
self.python_path = pathlib.Path('C:/Python3')

def get_latest_python_folder(self):
'''
Gets the latest version of Python 3 in C:/
'''
return sorted(list(pathlib.Path('C:/').glob("Python3*")))[-1]

def install(self, output_queue: queue.Queue) -> int:
'''
This is the function called by the GUI to symlink
C:/Python3 to C:/Python3X
'''
if self.python_path.is_symlink():
self.python_path.unlink()

latest_py = self.get_latest_python_folder()
output_queue.put_nowait([f'Symlinking: {self.python_path} -> {latest_py}'])
self.python_path.symlink_to(latest_py)
return 0

# todo:
# Symlink C:/Python to C:/Python3X
# Add C:/Python/bin to PATH
# Enable Windows Sandbox: powershell Enable-WindowsOptionalFeature -FeatureName "Containers-DisposableClientVM" -All -Online
MISC_COMMANDS = {
'Add Admin Cmd Prompt via Shift/Right Click' : RegistryInstallCommand(ADD_ADMIN_CMD_PROMPT_VIA_SHIFT_RIGHT_CLICK_REG)
'Add Admin Cmd Prompt via Shift/Right Click' : RegistryInstallCommand(ADD_ADMIN_CMD_PROMPT_VIA_SHIFT_RIGHT_CLICK_REG),
'Symlink Python3 -> Python3X' : SymlinkPython3ToPython3X(),
'Add C:/Python3 to System PATH' : MiscCommand(f'setx /m PATH "C:\Python3;C:\Python3\Scripts;%PATH%"'),
}

0 comments on commit 7b72a30

Please sign in to comment.