Skip to main content

Automatically restart applications when file changes are detected.

Project description

foremon

foremon is a tool to help develop python projects by executing build tasks when file changes are detected.

foremon intends to have feature parity to nodemon, a similar tool for the NodeJS ecosystem, but provide those features within a python toolchain. To use foremon run your script or module as foremon [script or module] or run foremon --help for advanced usage.

File monitoring is provided by watchdog which provides its own shell-utility, watchmedo.

foremon is currently in beta.

Installation

Clone foremon with git or install it using pip (recommended):

pip install foremon

Usage

foremon will bootstrap your module or script with the arguments you normally pass to it:

foremon [script or module] [args]

If your application uses options which conflict with foremon's options use the -- argument separator.

foremon -- mymodules --version

For CLI options, use -h or --help:

foremon -h

Using foremon is simple. It will guess if you are running a module or python script and adjust the command-line arguments accordingly. To disable this feature, add the -n (--no-guess) option.

# Executes `script.py`
foremon -n -- script.py

# Executes `python3 script.py`
foremon -- script.py

# Does not try to guess command. `test` is ambiguous because it is a
# shell-builtin and python module.
foremon -- test -f script.py

foremon runs python scripts with the python interpreter of the environment it is installed in (sys.executable).

All foremon output is prefixed with [foremon] and written to stderr. Output from your script, errors included, will be echoed out as expected.

If no script is given, foremon will test for a pyproject.toml file and if found, will run scripts specified in the [tool.foremon] section (ref.).

Automatic re-running

When file changes are detected foremon will restart the script. If scripts are still running when the change is detected, foremon will signal the current process with SIGTERM before restarting.

The signal used may be set in by term_signal in the config file.

Manual restart

Scripts may be manually restarted while foremon is running by typing rs and then enter in the terminal where foremon is running.

foremon can also be shutdown gracefully by typing exit followed by enter. Just using ctrl+c will will also stop any scripts if running.

pyproject.toml

support for pyproject.toml is under development

foremon supports pyproject.toml configuration files. If the project contains a pyproject.toml file foremon will automatically load defaults from the [tool.foremon] section. An alternative config file may be specified with the -f (--config-file) option.

All configuration settings are optional but foremon wont run if are no scripts to run.

[tool.foremon]
# Only watch files ending in .py
patterns = ["*.py"]
# Run these scripts in-order on-change
scripts = ["pytest --cov=myproj"]
# Only run if explicitly run with `-a [alias]
skip = true
# Run script like they're in this directory
cwd = "./"
# Key-Value paris of environment variables
environment = { TERM = "MONO" }
# Exit code to expect for a successful exit
returncode = 0
# Signal to send if the process should be terminated
term_signal = "SIGTERM"
# Set to false to turn on case-sensitive pattern matching
ignore_case = true
# List of default ignored paths like .git, or .tox
ignore_defaults = []
# Ignore changes to directories
ignore_dirs = true
# A list of patterns to ignore
ignore = ["*/build/*"]
# Paths to watch for changes
paths = ["src/"]
# Watch paths recursively
recursive = true
# List of events - created, deleted, moved, modified
events = ["created", "modified"]

All subsections contain the same options.

foremon supports multiple sets of files to watch and scripts to run. Sections in the config file matching [tool.foremon.*], where * is the alias for the task, may be defined in addition to the default section.

To run these other sections specify the -a [alias] option. The -a option may be used multiple times or the --all option can be used to turn on all tasks.

[tool.foremon]
patterns = ["*.c", "*.h"]
scripts = ["./configure"]

  # Run me with 'foremon -a make'
  [tool.foremon.make]
  patterns = ["make*"]
  paths = ["src/*"]
  scripts = ["make -C src"]
  events = ["created"]

  [tool.foremon.other]
  scripts = ["echo skipped"]
  skip = true

Any command-line arguments passed to foremon only supersede definitions in default section.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

foremon-1.1.1.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

foremon-1.1.1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file foremon-1.1.1.tar.gz.

File metadata

  • Download URL: foremon-1.1.1.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.7

File hashes

Hashes for foremon-1.1.1.tar.gz
Algorithm Hash digest
SHA256 9d2dd5b3467748cbab018bcfc1416fee545b3e0b35237a464104b782a9b8220a
MD5 901426efd2a42aa9dd1a1403639f9970
BLAKE2b-256 a97062090dd0453cff35192eb0de78c14bd30264fa1595c760860dec15c2e62a

See more details on using hashes here.

File details

Details for the file foremon-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: foremon-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.7

File hashes

Hashes for foremon-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e0d421594f3a13cd0a2398a984bd2483aa8bd98adfa67c7992fbb942798c826
MD5 82b1b8cd58745defcd60de78119693c5
BLAKE2b-256 e05763915453cc3399a7914d785e7319038db4bca5344d4d3d79dd9cac181494

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page