Skip to content

Commit

Permalink
Using PyInstall now!
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaid committed Nov 29, 2019
1 parent 2de3c3b commit 15b1b68
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ venv.bak/
.mypy_cache/
LPHK.lnk
user_layouts/*.*
install/__setup__/*
__setup__/*
31 changes: 26 additions & 5 deletions LPHK.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import sys, os
from datetime import datetime

# Test if this is a PyInstaller EXE or a .py file
if getattr(sys, 'frozen', False):
is_exe = True
PATH = sys._MEIPASS
else:
is_exe = False
PATH = os.path.dirname(os.path.abspath(__file__))

# Setup dual logging/printing
class Tee(object):
def __init__(self, *files):
self.files = files
def write(self, obj):
for f in self.files:
f.write(obj)
f = open('last_session.log', 'w')
backup = sys.stdout
sys.stdout = Tee(sys.stdout, f)

# Start printing output
def datetime_str():
now = datetime.now()
return now.strftime("%d/%m/%Y %H:%M:%S")
Expand All @@ -8,10 +29,9 @@ def datetime_str():
print("!!!!!!!! DO NOT CLOSE THIS WINDOW WITHOUT SAVING !!!!!!!!")
print("-------- BEGIN LOG", datetime_str(), "--------\n")

import sys, os

EXIT_ON_WINDOW_CLOSE = True

# Try to import launchpad.py
try:
import launchpad_py as launchpad
except ImportError:
Expand All @@ -22,8 +42,6 @@ def datetime_str():

import lp_events, scripts, kb, files, sound, window

PATH = sys.path[0]

lp = launchpad.Launchpad()

def init():
Expand Down Expand Up @@ -54,7 +72,10 @@ def shutdown():
lp.Close()
window.lp_connected = False
if window.restart:
os.execv(sys.executable, ["\"" + sys.executable + "\""] + sys.argv)
if is_exe:
os.startfile(sys.argv[0])
else:
os.execv(sys.executable, ["\"" + sys.executable + "\""] + sys.argv)
sys.exit("[LPHK] Shutting down...")

def main():
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ This is still a work-in-progress, so things will be changing often. [See below f
### Windows Install/Run Instructions [[Table of Contents]](https://github.com/nimaid/LPHK#table-of-contents)
*THE FOLLOWING INSTRUCTIONS ARE FOR THE BETA INSTALLER, WHICH AIMS TO BE PAINLESS TO USE. IF THIS FAILS, PLEASE SHARE IT ON THE DISCORD (OR AS A GITHUB ISSUE), AND IN THE MEANTIME TRY MANUALLY INSTALLING CONDA AND THE CONDA ENVIRONMENT AND USING `run.bat`.*

* Download the latest `LPHK_setup_VERSION.exe` file from [https://github.com/nimaid/LPHK/releases/latest](https://github.com/nimaid/LPHK/releases/latest)
* Run LPHK_setup_VERSION.exe` and follow the on-screen setup instructions.
* It is recommend to use the default install location
* The setup wizard will install MiniConda3 if no conda is found
* It will also create various shortcuts
* To run LPHK, use one of the shortcuts, either the `Start Menu` or `Desktop` one.
* You can uninstall LPHK like any other program, with the Windows `Add or remove programs` utility.
* There will also be an uninstaller named `unins000.exe` in the program folder.
* If you want the installed version (recomended)
* Download the latest `LPHK_setup_VERSION.exe` file from [https://github.com/nimaid/LPHK/releases/latest](https://github.com/nimaid/LPHK/releases/latest)
* Run LPHK_setup_VERSION.exe` and follow the on-screen setup instructions.
* To run LPHK, use one of the shortcuts, either the `Start Menu` or `Desktop` one (if you selected that option).
* You can uninstall LPHK like any other program, with the Windows `Add or remove programs` utility.
* There will also be an uninstaller named `unins000.exe` in the program folder. You can find the program folder by clicking the menu option `Help > Program folder...`.
* If you want to run LPHK portably (advanced)
* This method lets you use LPHK by simply downloading `LPHK_portable_win_VERSION.zip` and extracting it to a folder of your choice. You can then move the folder to a flash drive, between computers, etc.
* Run `LPHK.exe`

### Linux Install/Run Instructions [[Table of Contents]](https://github.com/nimaid/LPHK#table-of-contents)
*THE FOLLOWING INSTRUCTIONS ARE FOR THE BETA INSTALLER, WHICH AIMS TO BE PAINLESS TO USE. IF THIS FAILS, PLEASE SHARE IT ON THE DISCORD (OR AS A GITHUB ISSUE), AND IN THE MEANTIME TRY MANUALLY INSTALLING CONDA AND THE CONDA ENVIRONMENT.*
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.8
0.2.0
19 changes: 19 additions & 0 deletions install/windows/build_pyinstall.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@echo off
set "ORIGDIR=%~dp0"
cd %~dp0\..\..

echo Building EXE...
call conda run -n LPHK-build pyinstaller --add-data VERSION;. --add-data resources\;resources\ --onefile --windowed --icon=resources\LPHK.ico LPHK.py
if errorlevel 1 goto ERROR

goto DONE

:ERROR
cd %ORIGDIR%
echo Build failed!
exit /B 1

:DONE
cd %ORIGDIR%
echo Build done!
exit /B 0
15 changes: 15 additions & 0 deletions install/windows/environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: LPHK-build
channels:
- defaults
dependencies:
- python=3.7*
- pip
- tk
- pip:
- keyboard
- launchpad-py
- pillow
- pygame
- pynput
- tkcolorpicker
- pyinstaller
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,41 @@ REM DO NOT USE THIS SCRIPT. It is for the new Inno Setup installer.
set "LPHKENV="
set "STARTPATH="

set "ENVNAME=LPHK-build"

where conda >nul 2>nul
if %ERRORLEVEL% NEQ 0 goto CONDAMISSING

:INSTALLENV
REM TODO: Use environments.txt
FOR /F "tokens=*" %%g IN ('conda env list ^| findstr /R /C:"LPHK"') do (set LPHKENV="%%g")
FOR /F "tokens=*" %%g IN ('conda env list ^| findstr /R /C:"%ENVNAME%"') do (set LPHKENV="%%g")
if defined LPHKENV goto ALREADYINSTALLED

echo Installing LPHK Conda environment...
echo Installing LPHK build environment...
set STARTPATH="%CD%"
cd "%~dp0"
call conda env create -f environment.yml
if errorlevel 1 goto INSTALLENVFAIL

cd %STARTPATH%
echo LPHK Conda environment installed!
echo LPHK build environment installed!
goto END

:CONDAMISSING
echo "Conda is not installed!"
goto ERROREND

:INSTALLENVFAIL
call conda env remove -n LPHK
REM TODO: Use environments.txt
rmdir %USERPROFILE%\Miniconda3\envs\LPHK /s /q 2> nul
rmdir %USERPROFILE%\Miniconda3\envs\%ENVNAME% /s /q 2> nul
goto ERROREND

:ALREADYINSTALLED
echo LPHK is already installed!
echo LPHK build environment is already installed!
goto END

:ERROREND
echo The LPHK Conda environment could not be installed!
echo The LPHK build environment could not be installed!
exit /B 1

:END
Expand Down
31 changes: 0 additions & 31 deletions install/windows/uninstall_env_windows.bat

This file was deleted.

55 changes: 5 additions & 50 deletions install/windows/windows.iss
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define MyAppName "LPHK"
#define MyAppPublisher "Ella Jameson (nimaid)"
#define MyAppURL "https://github.com/nimaid/LPHK"
#define MyAppExeName "run.bat"
#define MyAppExeName "LPHK.exe"

[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
Expand All @@ -34,7 +34,7 @@ AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
DisableProgramGroupPage=yes
PrivilegesRequired=lowest
OutputDir=..\__setup__
OutputDir=..\..\__setup__
OutputBaseFilename=LPHK_setup_{#MyAppVersion}
SetupIconFile=..\..\resources\LPHK.ico
Compression=lzma
Expand All @@ -48,22 +48,15 @@ Name: "english"; MessagesFile: "compiler:Default.isl"
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "..\..\run.bat"; DestDir: "{app}"; Flags: ignoreversion
Source: "..\..\*.py"; DestDir: "{app}"; Flags: ignoreversion
Source: "..\..\VERSION"; DestDir: "{app}"; Flags: ignoreversion
Source: "..\..\resources\*"; DestDir: "{app}\resources"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "..\..\dist\LPHK.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "..\..\user_layouts\*"; DestDir: "{app}\user_layouts"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "..\..\user_scripts\*"; DestDir: "{app}\user_scripts"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "..\..\user_sounds\*"; DestDir: "{app}\user_sounds"; Flags: ignoreversion recursesubdirs createallsubdirs
Source: "..\environment.yml"; DestDir: "{tmp}"; Flags: ignoreversion
Source: ".\uninstall_env_windows.bat"; DestDir: "{app}"; Flags: ignoreversion
Source: ".\install_conda_windows.bat"; DestDir: "{tmp}"; Flags: ignoreversion
Source: ".\install_env_windows.bat"; DestDir: "{tmp}"; Flags: ignoreversion; AfterInstall: MyAfterInstall
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; IconFilename: "{app}\resources\LPHK.ico"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon; IconFilename: "{app}\resources\LPHK.ico"
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; IconFilename: "{app}\LPHK.exe"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon; IconFilename: "{app}\LPHK.exe"

[Code]
var CancelWithoutPrompt: boolean;
Expand All @@ -74,44 +67,6 @@ begin
result := true;
end;
procedure MyAfterInstall();
var ResultCode: integer;
begin
WizardForm.StatusLabel.Caption := 'Installing Conda...'
Exec(ExpandConstant('{tmp}\install_conda_windows.bat'), '', '', SW_HIDE, ewWaitUntilTerminated, ResultCode)
if ResultCode <> 0 then begin
MsgBox('Conda could not be installed!',mbError,MB_OK)
CancelWithoutPrompt := true;
WizardForm.Close;
end;
WizardForm.StatusLabel.Caption := 'Installing LPHK Conda environment...'
Exec(ExpandConstant('{tmp}\install_env_windows.bat'), '', '', SW_HIDE, ewWaitUntilTerminated, ResultCode)
if ResultCode <> 0 then begin
MsgBox('LPHK Conda environment could not be installed!',mbError,MB_OK)
CancelWithoutPrompt := true;
WizardForm.Close;
end;
end;
procedure CancelButtonClick(CurPageID: Integer; var Cancel, Confirm: Boolean);
begin
if CurPageID=wpInstalling then
Confirm := not CancelWithoutPrompt;
end;
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var ResultCode : Integer;
begin
if CurUninstallStep = usUninstall then
begin
Exec(ExpandConstant('{app}\uninstall_env_windows.bat'), '', '', SW_HIDE, ewWaitUntilTerminated, ResultCode)
if ResultCode <> 0 then begin
MsgBox('Could not uninstall Conda environment. You can manually remove it with "conda env remove -n LPHK".',mbError,MB_OK)
end;
end;
end;
[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: shellexec postinstall skipifsilent

Expand Down
3 changes: 0 additions & 3 deletions run.bat

This file was deleted.

9 changes: 8 additions & 1 deletion window.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
DICER_NAME = "dicer"

PATH = None
MAIN_ICON = None

launchpad = None

Expand All @@ -48,9 +49,11 @@ def init(lp_object_in, launchpad_in, path_in):
global lp_object
global launchpad
global PATH
global MAIN_ICON
lp_object = lp_object_in
launchpad = launchpad_in
PATH = path_in
MAIN_ICON = os.path.join(PATH, "resources", "LPHK.ico")

make()

Expand Down Expand Up @@ -384,6 +387,7 @@ def script_entry_window(self, x, y, text_override=None, color_override=None):
w = tk.Toplevel(self)
w.winfo_toplevel().title("Editing Script for Button (" + str(x) + ", " + str(y) + ")")
w.resizable(False, False)
w.iconbitmap(MAIN_ICON)

def validate_func():
nonlocal x, y, t
Expand Down Expand Up @@ -459,6 +463,7 @@ def classic_askcolor(self, color=(255, 0, 0), title="Color Chooser"):
w = tk.Toplevel(self)
w.winfo_toplevel().title(title)
w.resizable(False, False)
w.iconbitmap(MAIN_ICON)

w.protocol("WM_DELETE_WINDOW", w.destroy)

Expand Down Expand Up @@ -607,6 +612,7 @@ def export_script(self, textbox, window):
def popup(self, window, title, image, text, button_text, end_command=None):
popup = tk.Toplevel(window)
popup.resizable(False, False)
popup.iconbitmap(MAIN_ICON)
popup.wm_title(title)
popup.tkraise(window)

Expand All @@ -627,6 +633,7 @@ def run_end():
def popup_choice(self, window, title, image, text, choices):
popup = tk.Toplevel(window)
popup.resizable(False, False)
popup.iconbitmap(MAIN_ICON)
popup.wm_title(title)
popup.tkraise(window)

Expand Down Expand Up @@ -666,7 +673,7 @@ def make():
root_destroyed = False
root.protocol("WM_DELETE_WINDOW", close)
root.resizable(False, False)
root.iconbitmap(os.path.join(PATH, "resources", "LPHK.ico"))
root.iconbitmap(MAIN_ICON)
app = Main_Window(root)
app.raise_above_all()
app.mainloop()
Expand Down

0 comments on commit 15b1b68

Please sign in to comment.