Skip to content

Commit

Permalink
Add instructions for executable packaging
Browse files Browse the repository at this point in the history
For #3
  • Loading branch information
peterstory committed Sep 27, 2023
1 parent fb184b9 commit 8fbc94c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Transfer data through a unidirectional network (i.e., a data diode).

## Installation

Install from PyPI:
To install from PyPI:
```
pip install pydiode
```

To install from source, clone the repo then run:
Or to install from source, clone the repo then run:
```
pip install .
```
Expand All @@ -22,7 +22,7 @@ sudo port install tk -x11 +quartz

## GUI Usage

The `pydiode-gui` command will launch the user interface.
The `pydiode-gui` command will launch the GUI. The GUI can also be run from a frozen executable (see packaging instructions below).

## Command-Line Usage

Expand All @@ -49,9 +49,25 @@ With debug-level logging, you will see details about each packet sent and receiv

## Development

Run unit tests:
### Run Unit Tests

```
python -m unittest tests.tests
```

Since [the unit tests run on the installed code](https://blog.ionelmc.ro/2014/05/25/python-packaging/), remember to install the latest version of the code before running the unit tests.

### Packaging Frozen Executables

First, install PyInstaller:
```
pip install pyinstaller
```

Next, build a frozen executable. On macOS:
```
cd src/pydiode
pyinstaller --windowed --name pydiode gui.py
```

Note that PyInstaller creates a frozen executable for the platform you run it on. For example, when run on macOS, it creates `pydiode.app`.
56 changes: 33 additions & 23 deletions src/pydiode/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from tkinter.filedialog import askdirectory, askopenfilenames
from tkinter.messagebox import showerror

import pydiode.main
import pydiode.tar

# Save the configuration file in the user's home folder
CONFIG = pathlib.Path().home() / ".pydiode.ini"
# Check subprocesses every SLEEP milliseconds
Expand Down Expand Up @@ -85,7 +88,7 @@ def check_subprocesses(widget, *args):
:param args: An array of tuples, each containing a subprocess's name and
its popen object.
"""
# TODO Include a progress bar
# TODO Include a progress bar, and ensure we stop checking eventually.
# Are any of the subprocesses still running?
still_running = False
for name, popen in args:
Expand Down Expand Up @@ -121,21 +124,12 @@ def send_files(root, sources_list, send_ip, receive_ip, port):
:param port: Send data using this port
"""
tar = subprocess.Popen(
[sys.executable, "-m", "pydiode.tar", "create"] + sources_list,
sys.argv + ["tar", "create"] + sources_list,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
pydiode = subprocess.Popen(
[
sys.executable,
"-m",
"pydiode.main",
"send",
receive_ip,
send_ip,
"--port",
port,
],
sys.argv + ["pydiode", "send", receive_ip, send_ip, "--port", port],
stdin=tar.stdout,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand All @@ -159,20 +153,12 @@ def receive_files(root, target_dir, receive_ip, port):
:param port: Receive data using this port
"""
pydiode = subprocess.Popen(
[
sys.executable,
"-m",
"pydiode.main",
"receive",
receive_ip,
"--port",
port,
],
sys.argv + ["pydiode", "receive", receive_ip, "--port", port],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
tar = subprocess.Popen(
[sys.executable, "-m", "pydiode.tar", "extract", target_dir],
sys.argv + ["tar", "extract", target_dir],
stdin=pydiode.stdout,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Expand All @@ -197,7 +183,7 @@ def update_start(start, sources_list):
start.state(["disabled"])


def main():
def gui_main():
# Load configuration
config = configparser.ConfigParser()
config.read(CONFIG)
Expand Down Expand Up @@ -338,5 +324,29 @@ def main():
config.write(configfile)


def main():
"""
Running Python subprocess from a frozen app is complicated, because the
frozen app doesn't have a regular a python interpreter. However, the frozen
app can create a subprocess of its executable, with alternate arguments.
This executable can call the appropriate methods, based on its arguments.
"""
if len(sys.argv) == 1:
# Without arguments, just launch the GUI
gui_main()
else:
# Remove the executable from argv for compatibility with argparse
sys.argv.pop(0)
if sys.argv[0] == "pydiode":
# With pydiode as the first argument, launch pydiode
pydiode.main.main()
elif sys.argv[0] == "tar":
# With tar as the first argument, launch tar
pydiode.tar.main()
else:
print(f"Invalid arguments: {sys.argv}", file=sys.stderr)
sys.exit(1)


if __name__ == "__main__":
main()

0 comments on commit 8fbc94c

Please sign in to comment.