Skip to content

Commit

Permalink
Merge pull request #479 from SPF-OST/command-line
Browse files Browse the repository at this point in the history
Add simple command line interface to `pytrnsys`.
  • Loading branch information
zuckerruebe authored Oct 5, 2023
2 parents 8dcc55a + 053b979 commit 2b16977
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 185 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
echo "file_name_base=pytrnsys-gui-${ref_name}-${pytrnsys_gui_sha}-${pytrnsys_sha}" >> $env:GITHUB_OUTPUT
- name: Create zip file
if: ${{github.event_name == 'push' || (github.event_name == 'repository_dispatch' && github.event.client_payload.type == 'run-tests-and-build-executable')}}
run: Compress-Archive -Path data, release\pyinstaller-venv\pytrnsys_data, release\dist\pytrnsys-gui.exe -DestinationPath ${{steps.fn.outputs.file_name_base}}.zip
run: Compress-Archive -Path data, release\pyinstaller-venv\pytrnsys_data, release\dist -DestinationPath ${{steps.fn.outputs.file_name_base}}.zip
- name: Release artifacts
if: ${{github.event_name == 'push' || (github.event_name == 'repository_dispatch' && github.event.client_payload.type == 'run-tests-and-build-executable')}}
uses: "marvinpinto/action-automatic-releases@latest"
Expand All @@ -92,4 +92,4 @@ jobs:
path: |
data
release\pyinstaller-venv\pytrnsys_data
release\dist\pytrnsys-gui.exe
release\dist
118 changes: 70 additions & 48 deletions dev-tools/devTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
# Run from top-level directory

import argparse as ap
import contextlib as ctx
import os
import pathlib as pl
import shutil as sh
import subprocess as sp
import sysconfig as _sysconfig
import sysconfig as sc
import typing as tp
import venv

import sys
import time

_SCRIPTS_DIR = pl.Path(_sysconfig.get_path("scripts"))
_SCRIPTS_DIR = pl.Path(sc.get_path("scripts"))

_SOURCE_DIRS = ["trnsysGUI", "tests", "dev-tools", "release/src"]


def main():
Expand Down Expand Up @@ -131,73 +134,92 @@ def _prepareTestResultsDirectory(testResultsDirPath: pl.Path, shallKeepResults:


def _maybeRunMypy(arguments):
if arguments.shallRunAll or arguments.shallPerformStaticChecks or arguments.mypyArguments is not None:
cmd = [
_SCRIPTS_DIR / "mypy",
"--show-error-codes",
"--exclude",
# Don't include python scripts which are copied into test
# data directories (from, e.g., `examples`) during tests
"^tests/(.+/)?data/.*",
]
if not (arguments.shallRunAll or arguments.shallPerformStaticChecks or arguments.mypyArguments is not None):
return

additionalArgs = arguments.mypyArguments or ""
args = [*cmd, *additionalArgs, "trnsysGUI", "tests", "dev-tools"]
cmd = [
_SCRIPTS_DIR / "mypy",
"--show-error-codes",
"--exclude",
# Don't include python scripts which are copied into test
# data directories (from, e.g., `examples`) during tests
"^tests/(.+/)?data/.*",
]

_printAndRun(args)
additionalArgs = arguments.mypyArguments or ""
args = [*cmd, *additionalArgs, *_SOURCE_DIRS]

_printAndRun(args)


def _maybeRunPylint(arguments):
if arguments.shallRunAll or arguments.shallPerformStaticChecks or arguments.lintArguments is not None:
cmd = f"{_SCRIPTS_DIR / 'pylint'} trnsysGUI tests dev-tools"
additionalArgs = arguments.lintArguments or ""
if not (arguments.shallRunAll or arguments.shallPerformStaticChecks or arguments.lintArguments is not None):
return

cmd = f"{_SCRIPTS_DIR / 'pylint'} --recursive=yes"
additionalArgs = arguments.lintArguments or ""

_printAndRun([*cmd.split(), *additionalArgs.split()])
_printAndRun([*cmd.split(), *additionalArgs.split(), *_SOURCE_DIRS])


def _maybeRunBlack(arguments):
if arguments.shallRunAll or arguments.shallPerformStaticChecks or arguments.blackArguments is not None:
cmd = f"{_SCRIPTS_DIR / 'black'} -l 120 trnsysGUI tests dev-tools"
additionalArgs = "--check" if arguments.blackArguments is None else arguments.blackArguments
if not (arguments.shallRunAll or arguments.shallPerformStaticChecks or arguments.blackArguments is not None):
return

_printAndRun([*cmd.split(), *additionalArgs.split()])
cmd = f"{_SCRIPTS_DIR / 'black'} -l 120"
additionalArgs = "--check" if arguments.blackArguments is None else arguments.blackArguments

_printAndRun([*cmd.split(), *additionalArgs.split(), *_SOURCE_DIRS])


def _maybeCreateDiagrams(arguments):
if arguments.shallRunAll or arguments.diagramsFormat:
diagramsFormat = arguments.diagramsFormat if arguments.diagramsFormat else "pdf"
cmd = f"{_SCRIPTS_DIR / 'pyreverse'} -k -o {diagramsFormat} -p pytrnsys_gui -d test-results trnsysGUI"
_printAndRun(cmd.split())
if not (arguments.shallRunAll or arguments.diagramsFormat):
return

diagramsFormat = arguments.diagramsFormat if arguments.diagramsFormat else "pdf"
cmd = f"{_SCRIPTS_DIR / 'pyreverse'} -k -o {diagramsFormat} -p pytrnsys_gui -d test-results trnsysGUI"
_printAndRun(cmd.split())


def _maybeCreateExecutable(arguments):
if arguments.shallRunAll or arguments.shallCreateExecutable:
releaseDirPath = pl.Path("release").resolve(strict=True)

sh.rmtree(releaseDirPath / "build", ignore_errors=True)
sh.rmtree(releaseDirPath / "dist", ignore_errors=True)
sh.rmtree(releaseDirPath / "pyinstaller-venv", ignore_errors=True)

venvDirPath = releaseDirPath / "pyinstaller-venv"
venv.create(venvDirPath, with_pip=True)

commands = [
r"release\pyinstaller-venv\Scripts\python.exe -m pip install --upgrade pip",
r"release\pyinstaller-venv\Scripts\python.exe -m pip install wheel",
r"release\pyinstaller-venv\Scripts\python.exe -m pip install -r requirements\release.txt",
r"release\pyinstaller-venv\Scripts\python.exe -m pip uninstall --yes -r requirements\pyinstaller.remove",
r"release\pyinstaller-venv\Scripts\python.exe dev-tools\generateGuiClassesFromQtCreatorStudioUiFiles.py",
]
if not (arguments.shallRunAll or arguments.shallCreateExecutable):
return

releaseDirPath = pl.Path("release").resolve(strict=True)

sh.rmtree(releaseDirPath / "build", ignore_errors=True)
sh.rmtree(releaseDirPath / "dist", ignore_errors=True)
sh.rmtree(releaseDirPath / "pyinstaller-venv", ignore_errors=True)

for cmd in commands:
_printAndRun(cmd.split())
venvDirPath = releaseDirPath / "pyinstaller-venv"
venv.create(venvDirPath, with_pip=True)

os.chdir("release")
commands = [
r"release\pyinstaller-venv\Scripts\python.exe -m pip install --upgrade pip",
r"release\pyinstaller-venv\Scripts\python.exe -m pip install wheel",
r"release\pyinstaller-venv\Scripts\python.exe -m pip install -r requirements\release.txt",
r"release\pyinstaller-venv\Scripts\python.exe -m pip uninstall --yes -r requirements\pyinstaller.remove",
r"release\pyinstaller-venv\Scripts\python.exe dev-tools\generateGuiClassesFromQtCreatorStudioUiFiles.py",
]

cmd = r".\pyinstaller-venv\Scripts\pyinstaller.exe pytrnsys-gui.spec"
for cmd in commands:
_printAndRun(cmd.split())

os.chdir("..")
with _chdir("release"):
cmd = r".\pyinstaller-venv\Scripts\pyinstaller.exe .\src\pytrnsys.spec"
_printAndRun(cmd.split())


@ctx.contextmanager
def _chdir(newWorkingDirPath: tp.Union[pl.Path, str]) -> tp.Iterator[None]:
oldWorkingDirPath = pl.Path()
newWorkingDirPath = pl.Path(newWorkingDirPath)

try:
os.chdir(newWorkingDirPath)
yield
finally:
os.chdir(oldWorkingDirPath)


def _maybeRunPytest(arguments, testResultsDirPath):
Expand Down
49 changes: 0 additions & 49 deletions release/pytrnsys-gui.spec

This file was deleted.

91 changes: 91 additions & 0 deletions release/src/pytrnsys.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_data_files, collect_submodules

datas = []
datas += collect_data_files("pytrnsys")
datas += collect_data_files("trnsysGUI")

block_cipher = None

a = Analysis(
["runPytrnsys.py"],
pathex=[],
binaries=[],
datas=datas,
hiddenimports=["pytrnsys", *collect_submodules("pytrnsys")],
hookspath=[],
hooksconfig={"matplotlib": {"backends": ["AGG", "PDF"]}},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure)

exe = EXE(
pyz,
a.scripts,
[],
name="pytrnsys",
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
hide_console="hide-early",
disable_windowed_traceback=True,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)

ga = Analysis(
["runPytrnsysGui.py"],
pathex=[],
binaries=[],
datas=datas,
hiddenimports=[],
hookspath=[],
hooksconfig={"matplotlib": {"backends": ["AGG", "PDF"]}},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)

gpyz = PYZ(ga.pure)

gexe = EXE(
gpyz,
ga.scripts,
[],
name="pytrnsys-gui",
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)

coll = COLLECT(
exe,
a.binaries,
a.datas,
gexe,
ga.binaries,
ga.datas,
strip=False,
upx=True,
upx_exclude=[],
name="pytrnsys",
)
19 changes: 19 additions & 0 deletions release/src/runPytrnsys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pathlib as _pl

import typer as _typer
import typing_extensions as _te

_ReadableFilePath = _te.Annotated[_pl.Path, _typer.Argument(readable=True, dir_okay=False)]


def main(python_script_path: _ReadableFilePath) -> None: # /NOSONAR # pylint: disable=invalid-name
_runFile(python_script_path)


def _runFile(pythonScriptPath: _pl.Path) -> None:
sourceCode = pythonScriptPath.read_text()
compiledCode = compile(sourceCode, pythonScriptPath, mode="exec")
exec(compiledCode) # pylint: disable=exec-used


_typer.run(main)
File renamed without changes.
4 changes: 2 additions & 2 deletions requirements/dev-3rd-party.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ pkginfo==1.8.3
# via twine
pre-commit==2.20.0
# via -r requirements\dev-3rd-party.in
pyinstaller==5.6.2
pyinstaller==6.0.0
# via -r requirements\dev-3rd-party.in
pyinstaller-hooks-contrib==2022.13
# via pyinstaller
pywin32-ctypes==0.2.0
pywin32-ctypes==0.2.2
# via
# keyring
# pyinstaller
Expand Down
4 changes: 1 addition & 3 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
#
-r dev-3rd-party.txt
-e file:../pytrnsys
# via
# -r requirements\dev.in
# pytrnsys-gui
# via -r requirements\dev.in
-e file:.
# via -r requirements\dev.in

Expand Down
2 changes: 2 additions & 0 deletions requirements/release-3rd-party.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ lark
appdirs
Jinja2
qtconsole
typer
typing_extensions
Loading

0 comments on commit 2b16977

Please sign in to comment.