Skip to content

Commit

Permalink
Merge pull request #20 from gabryelreyes/feature/autopytoexe
Browse files Browse the repository at this point in the history
Configured project for compiling into executable
  • Loading branch information
nhjschulz authored Jun 20, 2024
2 parents 921bd04 + b74ac0c commit 9cc6c72
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ written to stdout id no output file option is given. This allows to "pipe" the d
--output filename, -o filename
file name of generated Pargen xml file (default: <stdout>)

## PyInstaller

Using the PyInstaller package, it is possible to pack `flashcontainer` into an executable tha includes all its dependencies. This makes the tool portable (between Windows machines), and removes the requirement of having Python and the dependencies installed in every machine.

To create the executable, PyInstaller must be installed via pip: `pip install pyinstaller`
Then, you can call the generation script: `.\tools\create_executable.bat`
The resulting executable will be found in `.\dist\pargen.exe`

## Issues, Ideas And Bugs

If you have further ideas or you found some bugs, great! Create an
Expand Down
2 changes: 1 addition & 1 deletion src/flashcontainer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import sys

from .pargen import pargen_cli
from flashcontainer.pargen import pargen_cli


def main():
Expand Down
19 changes: 15 additions & 4 deletions src/flashcontainer/packageinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#

import importlib.metadata as meta
import pathlib

import os
import sys
import toml

__version__ = "???"
Expand All @@ -41,6 +41,18 @@
__repository__ = "???"
__license__ = "???"

def resource_path(relative_path):
""" Get the absolute path to the resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
# pylint: disable=protected-access
# pylint: disable=no-member
base_path = sys._MEIPASS
except Exception: # pylint: disable=broad-except
base_path = os.path.abspath(".")

return os.path.join(base_path, relative_path)

def init_from_metadata() -> None:
"""Initialize dunders from importlib.metadata
Expand All @@ -63,8 +75,7 @@ def init_from_toml() -> None:
Tried if package wasn't installed
"""

dist_dir = pathlib.Path(__file__).resolve().parents[2]
toml_file = pathlib.Path.joinpath(dist_dir, "pyproject.toml")
toml_file = resource_path("pyproject.toml")
data = toml.load(toml_file)

return \
Expand Down
2 changes: 2 additions & 0 deletions tools/create_executable.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rem Please run this script from the root path. ".\tools\create_executable.bat"
pyinstaller --noconfirm --onefile --console --name "pargen" --add-data "./pyproject.toml;." "./src/flashcontainer/__main__.py"

0 comments on commit 9cc6c72

Please sign in to comment.