diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bbcee4c..8ebb41f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -93,7 +93,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v4 with: - python-version: '3.8' + python-version: '3.8.10' - name: Install dependencies run: | python -m pip install --upgrade pip diff --git a/img2texture/_cli.py b/img2texture/_cli.py index 9fa2a45..ce2780f 100644 --- a/img2texture/_cli.py +++ b/img2texture/_cli.py @@ -1,6 +1,7 @@ import argparse import os import sys +import traceback from enum import Enum from pathlib import Path @@ -9,10 +10,16 @@ from ._tiling import tile +def in_pyinstaller() -> bool: + return getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS') + + def print_version(): - print(f'img2texture {constants.__version__} ' - f'({constants.__build_timestamp__.split()[0]})\n' - f'{constants.__copyright__}') + comment = constants.__build_timestamp__.split()[0] \ + if in_pyinstaller() else "pip package" + print(f'img2texture {constants.__version__} ({comment})\n' + f'{constants.__copyright__}\n' + "https://github.com/rtmigo/img2texture") class Mode(Enum): @@ -59,10 +66,13 @@ def __init__(self): choices=[str(m.value) for m in Mode], default=Mode.both, help=argparse.SUPPRESS) + parser.add_argument("--trace", + action='store_true', + help=argparse.SUPPRESS) parser.add_argument('--version', action='store_true', default=False, - help="Show version info and sys.exit") + help="Show version info and exit") self._parsed = parser.parse_args() @@ -89,6 +99,12 @@ def mode(self) -> Mode: def tile(self) -> bool: return self._parsed.tile + @property + def exceptions(self) -> bool: + r = self._parsed.trace + assert isinstance(r, bool) + return r + def tile_filename(texture: Path) -> Path: basename = texture.name @@ -100,20 +116,31 @@ def tile_filename(texture: Path) -> Path: def cli(): args = ParsedArgs() - if args.target.exists(): - if not confirm(f"File '{args.target.name}' exists. Overwrite?"): - sys.exit(3) - os.remove(args.target) - - img2tex(args.source, args.target, pct=args.overlap_pct) - - if args.tile: - tile_src = args.target if args.mode != Mode.none else args.source - tile_fn = tile_filename(tile_src) - if tile_fn.exists() and not confirm( - f"File '{tile_fn}' exists. Overwrite?"): - sys.exit(3) - - if tile_fn.exists(): - os.remove(tile_fn) - tile(tile_src, tile_fn, horizontal=2, vertical=2) + try: + if args.target.exists(): + if not confirm(f"File '{args.target.name}' exists. Overwrite?"): + sys.exit(3) + os.remove(args.target) + + img2tex(args.source, args.target, pct=args.overlap_pct) + + if args.tile: + tile_src = args.target if args.mode != Mode.none else args.source + tile_fn = tile_filename(tile_src) + if tile_fn.exists() and not confirm( + f"File '{tile_fn}' exists. Overwrite?"): + sys.exit(3) + + if tile_fn.exists(): + os.remove(tile_fn) + tile(tile_src, tile_fn, horizontal=2, vertical=2) + + except Exception as e: + if isinstance(e, SystemExit): + raise + if args.exceptions: + print(traceback.format_exc()) + else: + print(f"ERROR: {e}") + print("Run with --trace to see full exception info.") + sys.exit(1) diff --git a/img2texture/_constants.py b/img2texture/_constants.py index 36323b0..185cdee 100644 --- a/img2texture/_constants.py +++ b/img2texture/_constants.py @@ -1,6 +1,6 @@ -__version__ = "1.0.2" +__version__ = "1.0.5" -__copyright__ = "(c) Artsiom iG (rtmigo.github.io)" +__copyright__ = "(c) Artsiom iG " __license__ = "MIT" -__build_timestamp__ = "2022-10-12 01:21:53" +__build_timestamp__ = "2022-10-12 05:30:19"