Skip to content

Commit

Permalink
Merge pull request #26 from Deric-W/compiler-subsystem
Browse files Browse the repository at this point in the history
Compiler subsystem
  • Loading branch information
Deric-W authored Jan 27, 2021
2 parents 4abb7ac + efdee0b commit fa5275f
Show file tree
Hide file tree
Showing 30 changed files with 1,447 additions and 343 deletions.
55 changes: 25 additions & 30 deletions .github/workflows/Tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
Test:
strategy:
matrix:
python-version: ["3.5", "3.6", "3.7","3.8", "3.9", "3.10.0-alpha.4", "pypy3"]
python-version: ["3.7", "3.8", "3.9", "3.10.0-alpha.4", "pypy-3.7"]
os: [ubuntu-latest, windows-latest]

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -50,34 +50,29 @@ jobs:
uses: codecov/codecov-action@v1
with:
flags: ${{ runner.os }}

Typecheck:
runs-on: ubuntu-latest
steps:
# setup
- name: Checkout Repository
uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: "3.9"

# dependencies
- name: Restore pip cache
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('setup.cfg') }}

- name: Install test dependencies
run: python -m pip --cache-dir ~/.cache/pip install --quiet mypy

# Typecheck: # disabled until we have type hints
# runs-on: ubuntu-latest
# strategy:
# matrix:
# python-version: ["3.5", "3.6", "3.7", "3.8", "3.9", "3.10.0-alpha.4", "pypy3"]
#
# steps:
# # setup
# - name: Checkout Repository
# uses: actions/checkout@v2
#
# - name: Set up Python ${{ matrix.python-version }}
# uses: actions/setup-python@v2
# with:
# python-version: ${{ matrix.python-version }}
#
# # dependencies
# - name: Restore pip cache
# uses: actions/cache@v2
# with:
# path: ~/.cache/pip
# key: ${{ runner.os }}-pip-${{ hashFiles('setup.cfg') }}
#
# - name: Install lint dependencies
# run: python -m pip --cache-dir ~/.cache/pip install --quiet mypy
#
# # lint
# - name: run MyPy
# run: python -m mypy --config-file setupg.cfg .
# check
- name: run MyPy
run: python -m mypy --config-file setup.cfg -p pyhp
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*/__pycache__
*.pyc
.coverage
.htmlcov
dist
notes.txt
Test.html

**/.vscode/**
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ The script is called either by the configuration of the web server or a shebang
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 iterator as argument and saves it in the cache
- `load`, wich loads an iterator from the cache
- `close`, wich does cleanup tasks
- note that the iterator may contain code objects which can't be pickled
- `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

## Installation
Expand Down
13 changes: 9 additions & 4 deletions cache_handlers/files_mtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"""PyHP cache handler (files with modification time)"""

import marshal # not pickle because only marshal supports code objects
import pickle
import os.path
from os import makedirs
from time import time
Expand All @@ -16,6 +16,12 @@ def __init__(self, cache_path, file_path, config):
self.ttl = config.getint("ttl")
self.max_size = config.getint("max_size")

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
self.close()

def get_cachedir_size(self): # get size of cache directory (with all sub directories) in Mbytes
size = 0
for dirpath, dirnames, filenames in os.walk(self.cache_prefix, followlinks=False):
Expand All @@ -40,14 +46,13 @@ def is_outdated(self): # return True if cache is not created or needs refre

def load(self): # load sections
with open(self.cache_path, "rb") as cache:
code = marshal.load(cache)
return code
return pickle.load(cache)

def save(self, code): # save sections
if not os.path.isdir(os.path.dirname(self.cache_path)): # directories not already created
makedirs(os.path.dirname(self.cache_path), exist_ok=True) # ignore already created directories
with open(self.cache_path, "wb") as cache:
marshal.dump(code, cache)
pickle.dump(code, cache)

def close(self):
pass # nothing to do
12 changes: 12 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
pyhp (2.1-1) stable; urgency=low

* fifth release
* add bytecode code object implementation
* change CacheHandlers to support the context manager protocol
* change CacheHandlers to work with code objects directly
* replace pyhp.embed with pyhp.compiler
* fix errors during compilation and execution having wrong line numbers
* fix code objects lacking proper module constants

-- Eric Wolf <[email protected]> Wed, 27 Jan 2021 19:17:00 +0100

pyhp (2.0-1) stable; urgency=low

* fourth release
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Version: {0}
Architecture: all
Maintainer: Eric Wolf <[email protected]>
Installed-Size: {1}
Depends: python3:any (>= 3.5)
Depends: python3:any (>= 3.7)
Suggests: apache2
Section: web
Priority: optional
Expand Down
8 changes: 4 additions & 4 deletions debian/copyright
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: pyhp
Upstream-Contact: Eric Wolf <[email protected]>
Source: https://github.com/Deric-W/PyHP
Copyright: 2019 Eric Wolf
License: Expat
Copyright: 2021 Eric Wolf
License: GPLv3

Files: *
Copyright: 2019 Eric Wolf
License: Expat
Copyright: 2021 Eric Wolf
License: GPLv3
26 changes: 21 additions & 5 deletions debian/pyhp
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
#!/usr/bin/python3

# script to support the pyhp command
"""Script to support the pyhp command"""
# This script is part of PyHP (https://github.com/Deric-W/PyHP)
# Copyright (C) 2021 Eric Wolf

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# SPDX-License-Identifier: GPL-3.0-only

import sys
from pyhp.main import main, get_args
from pyhp.main import main, argparser

# get cli arguments
args = get_args()
args = argparser.parse_args()

# execute main with file_path as normal argument and the rest as keyword arguments
sys.exit(main(args.pop("file_path"), **args))
# execute main
sys.exit(
main(
args.file,
args.caching,
args.config
)
)
51 changes: 12 additions & 39 deletions pyhp.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
# This file uses the INI syntax

[parser]
# regex to isolate the code
# escape sequences are processed
regex = \\<\\?pyhp[\\s](.*?)[\\s]\\?\\>
# regex matching the start and end of a code section
# escape sequences are processed (at the cost of missing unicode support)
start = <\\?pyhp\\s
end = \\s\\?>

# if python code should be stripped of a starting indentation
dedent = True

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

[request]
# order to fill up REQUEST, starting left and missing methods are not filled in
Expand Down Expand Up @@ -40,39 +49,3 @@ path = ~/.cache/pyhp

# path to handler
handler_path = /lib/pyhp/cache_handlers/files_mtime.py

[sessions]
enable = True
auto_start = False

# path argument for handler
path = ~/.pyhp/sessions

# session handler + directory containing the session handler
handler = files
handler_path = /lib/pyhp/session_handlers

# lenght of the session id
sid_length = 32

# how to serialize/unserialize session data, pickle or json
serialize_handler = pickle

# config for session cookie
name = PyHPSESSID
cookie_lifetime = 0
cookie_path = /
cookie_domain =
cookie_secure = True
cookie_httponly = False
cookie_samesite =

# probability/divisor = probability for carrying out a garbage collection at startup
gc_probability = 1
gc_divisor = 100

# max lifetime of session since last use
gc_maxlifetime = 1440

# write only if data has changed
lazy_write = True
13 changes: 7 additions & 6 deletions pyhp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

# package metadata
# needs to be defined before .main is imported
__version__ = "2.0"
__version__ = "2.1"
__author__ = "Eric Wolf"
__maintainer__ = "Eric Wolf"
__license__ = "MIT"
__license__ = "GPLv3"
__email__ = "[email protected]" # please dont use for spam :(
__contact__ = "https://github.com/Deric-W/PyHP"

# import all submodules
from . import embed
from . import libpyhp
from . import main
__all__ = (
"compiler",
"libpyhp",
"main"
)
14 changes: 10 additions & 4 deletions pyhp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
# SPDX-License-Identifier: GPL-3.0-only

import sys
from .main import main, get_args
from .main import main, argparser

# get cli arguments
args = get_args()
args = argparser.parse_args()

# execute main with file_path as normal argument and the rest as keyword arguments
sys.exit(main(args.pop("file_path"), **args))
# execute main
sys.exit(
main(
args.file,
args.caching,
args.config
)
)
Loading

0 comments on commit fa5275f

Please sign in to comment.