Skip to content

Commit

Permalink
Merge pull request #27 from Deric-W/caching-subsystem
Browse files Browse the repository at this point in the history
Caching subsystem
  • Loading branch information
Deric-W authored Mar 14, 2021
2 parents 2d097f8 + ea0ab5e commit 2cc3e2f
Show file tree
Hide file tree
Showing 61 changed files with 3,950 additions and 455 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# disable automatic eol conversions
* text=false
* -text
2 changes: 1 addition & 1 deletion .github/workflows/Tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
run: python setup.py bdist_wheel

- name: Install wheel
run: python -m pip install ./dist/*.whl
run: python -m pip install "dist/pyhp_core-$(python setup.py --version)-py3-none-any.whl[CONFIG]"
shell: bash

- name: run tests
Expand Down
61 changes: 27 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# PyHP-Interpreter ![Tests](https://github.com/Deric-W/PyHP/workflows/Tests/badge.svg) [![codecov](https://codecov.io/gh/Deric-W/PyHP/branch/master/graph/badge.svg?token=SA72E6KGXT)](https://codecov.io/gh/Deric-W/PyHP) [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)

The PyHP Interpreter is a package that allows you to embed Python code like PHP code into HTML and other text files.
The script is called either by the configuration of the web server or a shebang and communicates with the web server via CGI.
PyHP is a package that allows you to embed Python code like PHP code into HTML and other text files.
A script is called either by the configuration of the web server or a shebang and communicates with the web server via CGI.

## Features:

Expand All @@ -14,10 +14,8 @@ The script is called either by the configuration of the web server or a shebang
## How it works:

- Python code is contained within the `<?pyhp` and `?>` tags (like PHP)
- the program is called like a interpreter, with the filepath as cli parameter
- if no filepath is given, the program is reading from stdin
- if the `-c` or `--caching` is given, the cache will be enabled and the file will additionally be preprocessed if needed
and cached in cache_path/absolute/path/of/filename.cache
- the program is called like a interpreter, with a name as cli parameter
- if no name is given, the program is reading from stdin, else it is using the name to load code from the backend configured in pyhp.toml
- python code is allowed to have a starting indentation for better readability inside (for example) HTML files
- the following PHP features are available as methods of the `PyHP` class (available from the outside in pyhp.libpyhp):
- `$_SERVER` as `SERVER`
Expand All @@ -34,21 +32,23 @@ The script is called either by the configuration of the web server or a shebang
- `setcookie` with an additional `samesite` keyword argument
- `setrawcookie` also with an additional `samesite` keyword argument
- `register_shutdown_function`


## Cache Handlers

## Config file

- is valid toml
- is looked for in these locations (no merging takes place, the first file wins):
- the path given by the `-c` or `--config` cli argument
- the path pointed to by the `PYHPCONFIG` environment variable
- `~/.config/pyhp.toml`
- `/etc/pyhp.toml`
- raises a `RuntimeError` if not found

- are responsible for saving/loading/renewing caches
- are python scripts with the following contents:
- the `Handler` class, wich takes the cache path, absolute file path and `caching` section of the config file as
initialization parameters and provides the following methods:
- `is_available`, wich returns a boolean indicating if the handler can be used
- `is_outdated`, wich returns a boolean indicating if the cache needs to be renewed
- `save`, wich takes an code object as an argument and saves it in the cache
- `load`, wich loads an code object from the cache
- `close`, wich does cleanup tasks and gets called when used as a context manager
- note that code objects have to support the pickle protocol
- examples are available in the *cache_handlers* directory
## Backends

- implement code retrieval or decorate other backends to add i.a. caching
- act as containers for CodeSources
- form a hierarchy configured in pyhp.toml
- are contained inside `pyhp.backends`

## Installation

Expand All @@ -58,22 +58,15 @@ The script is called either by the configuration of the web server or a shebang
### Just as python package
1. build the *pyhp-core* python package with `python3 setup.py bdist_wheel`
2. Done! You can now install the wheel contained in the *dist* directory with pip

### As application
If you just installed the python package, then you have to provide `--config` with every call of `python3 -m pyhp`
and can't use the caching feature.
To stop this, you can build a debian package or install PyHP manually.

#### Debian package

### Debian package
1. build the *pyhp-core* python package with `python3 setup.py bdist_wheel`
2. go to the *debian* directory and execute `./build_deb.sh`
3. enter a package version, the path of the *pyhp-core* wheel and the pip command you wish to use
4. Done! You can now install the debian package with `sudo dpkg -i pyhp_<package version>_all.deb`
#### Manually

### Manually
1. install the *pyhp-core* python package
2. copy *pyhp.conf* to */etc*
3. copy *cache_handlers* to */lib/pyhp/*
4. copy *debian/pyhp* to a directoy in your PATH
5. Done! You can now use the `pyhp` command

2. copy *pyhp.toml* to one of the config file locations
3. Done! You can now use the `pyhp` command

58 changes: 0 additions & 58 deletions cache_handlers/files_mtime.py

This file was deleted.

36 changes: 18 additions & 18 deletions debian/build_deb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,33 @@ then read -p "pip executeable: " pip
else pip=$3
fi

package="pyhp_"$version"_all"
package="pyhp_${version}_all"

mkdir "$package"

# place config file, cache handlers and "executable"
mkdir -p "$package/lib/pyhp/cache_handlers"
cp ../cache_handlers/* "$package/lib/pyhp/cache_handlers"

mkdir "$package/etc"
cp ../pyhp.conf "$package/etc"
# place pyhp-core files
mkdir -p "${package}/usr/lib/python3/dist-packages"
$pip install --target "${package}/usr/lib/python3/dist-packages" --ignore-installed --no-compile "$wheel"

mkdir -p "$package/usr/bin"
cp pyhp "$package/usr/bin"
chmod +x "$package/usr/bin/pyhp"
# place config file and "executable"
mkdir "${package}/etc"
cp ../pyhp.toml "${package}/etc"

# place pyhp-core files
mkdir -p "$package/usr/lib/python3/dist-packages"
$pip install --target "$package/usr/lib/python3/dist-packages" --ignore-installed $wheel
mkdir -p "${package}/usr/bin"
mv "${package}/usr/lib/python3/dist-packages/bin/pyhp" "${package}/usr/bin"
rmdir "${package}/usr/lib/python3/dist-packages/bin"
chmod +x "${package}/usr/bin/pyhp"

# place metadata files
mkdir "$package/DEBIAN"
# calculate installed size
cat control | python3 format.py "$version" $(du -sk --apparent-size --exclude "DEBIAN" "$package" 2>/dev/null | cut -f1) > "$package/DEBIAN/control"
cat control | python3 format.py "${version}" $(du -sk --apparent-size --exclude "DEBIAN" "${package}" 2>/dev/null | cut -f1) > "${package}/DEBIAN/control"
cp conffiles "$package/DEBIAN"

mkdir -p "$package/usr/share/doc/pyhp"
cp copyright "$package/usr/share/doc/pyhp"
cp changelog "$package/usr/share/doc/pyhp/changelog.Debian"
gzip -n --best "$package/usr/share/doc/pyhp/changelog.Debian"
mkdir -p "${package}/usr/share/doc/pyhp"
cp copyright "${package}/usr/share/doc/pyhp"
cp changelog "${package}/usr/share/doc/pyhp/changelog.Debian"
gzip -n --best "${package}/usr/share/doc/pyhp/changelog.Debian"

# generate md5sums file
chdir "$package"
Expand All @@ -67,3 +65,5 @@ dpkg-deb --build "$package"
rm -r "$package"

echo "Done"

exit 0
11 changes: 11 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
pyhp (2.2-1) stable; urgency=low

* sixth release
* replace cache handlers with backends submodule
* add unbounded version of memory cache
* add files, zipfiles and memory backend
* fix race condition in file cache
* change config format to TOML

-- Eric Wolf <[email protected]> Sun, 14 Mar 2021 19:00:00 +0100

pyhp (2.1-1) stable; urgency=low

* fifth release
Expand Down
2 changes: 1 addition & 1 deletion debian/conffiles
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/etc/pyhp.conf
/etc/pyhp.toml
28 changes: 0 additions & 28 deletions debian/pyhp

This file was deleted.

51 changes: 0 additions & 51 deletions pyhp.conf

This file was deleted.

59 changes: 59 additions & 0 deletions pyhp.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Configuration for PyHP (https://github.com/Deric-W/PyHP)
# This file uses the TOML syntax

[parser]
# regular expression matching the start of a code section
start = '<\?pyhp\s'

# regular expression matching the end of a code section
end = '\s\?>'


[compiler]
# if python code should be stripped of a starting indentation
dedent = true

# optimization level to be used with the builtin compile() function
# all levels: https://docs.python.org/3/library/functions.html#compile
optimization_level = -1


[backend]
# how container names should be resolved to objects
# module: the name is a module with a dot and the container name appended to it
# module.submodule.name
# path: the name is a file path with a double colon and the container name appended to it
# /path/to/file.py:name
resolve = "module"

# array of containers to build a hierarchy
[[backend.containers]]
# the name of the container
name = "pyhp.backends.files.Directory"

# configuration options for the container
config.path = "."


[request]
# order to fill up REQUEST, values already present in REQUEST get overwritten
# unknown methods are skipped
request_order = [
"GET",
"POST",
"COOKIE"
]

# if arameters with no or blank values should be kept
keep_blank_values = true

# fallback value if a parameter is not in REQUEST / GET / POST / COOKIE
# comment out if a KeyError should be raised instead
fallback_value = ""

# if stdin should not be consumed
# this results in POST not being populated
enable_post_data_reading = false

# fallback value of the Content-Type header
default_mimetype = "text/html"
3 changes: 2 additions & 1 deletion pyhp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# package metadata
# needs to be defined before .main is imported
__version__ = "2.1"
__version__ = "2.2"
__author__ = "Eric Wolf"
__maintainer__ = "Eric Wolf"
__license__ = "GPLv3"
Expand All @@ -22,6 +22,7 @@

__all__ = (
"compiler",
"backends",
"libpyhp",
"main"
)
Loading

0 comments on commit 2cc3e2f

Please sign in to comment.