Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

✨ fix packaging #28

Merged
merged 7 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/onpush.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install hatch
hatch run sync
- name: Download CSS dependencies
run: hatch run python src/python/schorle/scripts/load_css_deps.py
- name: Lint
run: hatch run lint:style .
- name: Test
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/onrelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install hatch
hatch run sync
- name: Download CSS dependencies
run: hatch run python src/python/schorle/scripts/load_css_deps.py
- name: Build package
run: hatch build
- name: Publish package distributions to PyPI
Expand Down
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,20 @@ docs-serve: docs-build
docs-restart-app:
@echo "Restarting docs app..."
az webapp restart --name schorle-webapp --resource-group schorle-rg
@echo "Done."

docs-build-local:
rm -rf src/python/schorle/assets/js
rm -rf src/python/schorle/assets/dist/*.br
python src/python/schorle/scripts/load_css_deps.py
yarn --cwd src/typescript build
@echo "Building package..."
hatch build -t wheel -c
@echo "Package built, contents:"
unzip -l dist/*.whl
docker build --no-cache -t schorle-docs-local -f docs/Dockerfile.docs.local .

docs-serve-local: docs-build-local
@echo "Serving docs..."
docker run -p 4444:4444 -it schorle-docs-local
@echo "Done."
14 changes: 14 additions & 0 deletions docs/Dockerfile.docs.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM python:3.10.6-slim as runtime
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_DISABLE_PIP_VERSION_CHECK=on


COPY ../dist/ /app/dist
RUN pip install /app/dist/*.whl
RUN mkdir -p /app/docs
RUN mkdir -p /app/raw
WORKDIR /app
COPY docs/landing.py /app/docs/landing.py
COPY raw /app/raw
ENTRYPOINT ["uvicorn", "docs.landing:app", "--host", "0.0.0.0", "--port", "4444"]
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ packages = ["src/python/schorle"]

[tool.hatch.build.targets.wheel.force-include]
"src/python/schorle/assets/js" = "schorle/assets/js"
"src/python/schorle/assets/dist" = "schorle/assets/dist"

[tool.hatch.build.hooks.vcs]
version-file = "src/python/schorle/_version.py"
Expand All @@ -57,7 +58,9 @@ dependencies = [
"coverage[toml]>=6.5", "pytest",
"betterproto[compiler]",
"hatch-vcs",
"pytest-cov"
"pytest-cov",
"brotli",
"requests"
]
[tool.hatch.envs.default.scripts]
test = "pytest {args:tests}"
Expand Down
7 changes: 4 additions & 3 deletions src/python/schorle/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from uuid import uuid4

from fastapi import FastAPI
from loguru import logger

Check warning on line 8 in src/python/schorle/app.py

View check run for this annotation

Codecov / codecov/patch

src/python/schorle/app.py#L8

Added line #L8 was not covered by tests
from starlette.responses import FileResponse, HTMLResponse
from starlette.types import Receive, Scope, Send
from starlette.websockets import WebSocket

from schorle.component import Component
from schorle.document import Document
from schorle.events import EventsEndpoint
from schorle.headers import DEV_HEADER, SESSION_ID_HEADER
from schorle.headers import SESSION_ID_HEADER

Check warning on line 16 in src/python/schorle/app.py

View check run for this annotation

Codecov / codecov/patch

src/python/schorle/app.py#L16

Added line #L16 was not covered by tests
from schorle.session import Session
from schorle.theme import Theme
from schorle.utils import ASSETS_PATH, RunningMode, get_running_mode
Expand All @@ -30,8 +31,10 @@
mime_type, _ = mimetypes.guess_type(file_path)

response = FileResponse(file_path, media_type=mime_type)
logger.info(f"Sending file: {file_path} with suffixes: {file_path.suffixes}")

Check warning on line 34 in src/python/schorle/app.py

View check run for this annotation

Codecov / codecov/patch

src/python/schorle/app.py#L34

Added line #L34 was not covered by tests
if ".br" in file_path.suffixes:
response.headers["Content-Encoding"] = "br"
logger.info(f"Using brotli compression for file {file_path}")

Check warning on line 37 in src/python/schorle/app.py

View check run for this annotation

Codecov / codecov/patch

src/python/schorle/app.py#L37

Added line #L37 was not covered by tests
return response
else:
return HTMLResponse(status_code=404)
Expand Down Expand Up @@ -108,8 +111,6 @@
new_session = self.session_manager.create_session()
response = doc.to_response(new_session)
response.set_cookie(SESSION_ID_HEADER, new_session.uuid)
if get_running_mode() == RunningMode.DEV:
response.set_cookie(DEV_HEADER, "true")
return response

return decorator
Empty file.
51 changes: 48 additions & 3 deletions src/python/schorle/document.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import base64
import hashlib
from typing import Callable

from fastapi.responses import HTMLResponse
Expand All @@ -11,6 +13,7 @@
from schorle.tags import HTMLTag
from schorle.text import text
from schorle.theme import Theme
from schorle.utils import ASSETS_PATH


class DevLoader(Component):
Expand All @@ -21,6 +24,30 @@
span(classes="loading loading-md loading-bars text-primary")


def get_integrity(raw_file_path: str) -> str:
file_path = ASSETS_PATH / raw_file_path
if not file_path.exists():
msg = f"File '{file_path}' not found."
raise FileNotFoundError(msg)

Check warning on line 31 in src/python/schorle/document.py

View check run for this annotation

Codecov / codecov/patch

src/python/schorle/document.py#L30-L31

Added lines #L30 - L31 were not covered by tests

# Open the file in binary mode for reading
with open(file_path, "rb") as f:
# Calculate the SHA384 hash of the file
hash_obj = hashlib.sha256()
while True:
# Read the file in chunks to avoid loading large files into memory
chunk = f.read(4096)
if not chunk:
break
hash_obj.update(chunk)

# Encode the hash using base64 with URL and filename safe characters
integrity_hash = base64.b64encode(hash_obj.digest()).decode()

# Prepend "sha384-" to the hash
return f"sha256-{integrity_hash}"


class Document(Component):
page: Component
title: str = "Schorle"
Expand All @@ -44,11 +71,29 @@
with title():
text(self.title)

if self.with_dev_tools:
meta(name="schorle-dev-mode", content="true")

Check warning on line 75 in src/python/schorle/document.py

View check run for this annotation

Codecov / codecov/patch

src/python/schorle/document.py#L75

Added line #L75 was not covered by tests

link(href="/favicon.svg", rel="icon", type="image/svg+xml")

script(src="/_schorle/dist/tailwind.min.js.br")
link(href="/_schorle/dist/daisyui.min.css.br", rel="stylesheet")
script(src="/_schorle/js/index.min.js.br", crossorigin="anonymous", defer="", **{"type": "module"})
script(
src="/_schorle/dist/tailwind.min.js.br",
integrity=get_integrity("dist/tailwind.min.js"),
crossorigin="anonymous",
)
link(
href="/_schorle/dist/daisyui.min.css.br",
integrity=get_integrity("dist/daisyui.min.css"),
crossorigin="anonymous",
rel="stylesheet",
)
script(
src="/_schorle/js/index.min.js.br",
integrity=get_integrity("js/index.min.js"),
crossorigin="anonymous",
defer="",
**{"type": "module"},
)
if self.extra_assets:
self.extra_assets()

Expand Down
16 changes: 12 additions & 4 deletions src/python/schorle/scripts/load_css_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@ def replace_tw_message(text):


cdns = [
CdnInfo("https://cdn.tailwindcss.com", "tailwind", "tailwind.min.js.br"),
CdnInfo("https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css", "daisyui", "daisyui.min.css.br"),
CdnInfo("https://cdn.tailwindcss.com", "tailwind", "tailwind.min.js"),
CdnInfo("https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css", "daisyui", "daisyui.min.css"),
]

dist_path = ASSETS_PATH / "dist"


def load_deps():
print("Loading CSS dependencies...") # noqa T201
if not dist_path.exists():
dist_path.mkdir()

for cdn in cdns:

print(f"Fetching {cdn.name}...") # noqa T201
_text = cdn.fetch()

if cdn.name == "tailwind":
Expand All @@ -57,9 +58,16 @@ def load_deps():
if output_path.exists():
output_path.unlink()

with open(output_path, "wb") as f:
with open(output_path, "w") as f:
f.write(_text)

with open(output_path.with_suffix(f"{output_path.suffix}.br"), "wb") as f:
f.write(brotli.compress(_text.encode("utf-8")))

print(f"Saved {cdn.name} to {output_path}") # noqa T201

print("All CSS dependencies loaded!") # noqa T201


if __name__ == "__main__":
load_deps()
6 changes: 5 additions & 1 deletion src/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ let getCookieByName = (name: string): string | undefined => {
};

let getSessionId = () => getCookieByName('X-Schorle-Session-Id');
let getDevMode = () => getCookieByName('X-Schorle-Dev-Mode');
let getDevMode = () => {
// check if meta with name schorle-dev-mode exists and return its content
let meta = document.querySelector('meta[name="schorle-dev-mode"]');
return meta ? meta.getAttribute('content') : 'false';
};

let getWsUrl = (subPath: string): string => {
let protocol = window.location.protocol.replace('http', 'ws');
Expand Down
2 changes: 1 addition & 1 deletion src/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"license": "MIT",
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"esbuild": "^0.20.2",
"rollup": "^4.13.0",
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-gzip": "^3.1.2",
"rollup-plugin-uglify": "^6.0.4",
"typescript": "^5.4.3"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/typescript/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import esbuild from 'rollup-plugin-esbuild';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { uglify } from 'rollup-plugin-uglify';
import { brotliCompress } from 'zlib';
import { promisify } from 'util';
import gzipPlugin from 'rollup-plugin-gzip';
import terser from '@rollup/plugin-terser';

const brotliPromise = promisify(brotliCompress);

Expand All @@ -19,7 +19,7 @@ export default [
},
{
plugins: [
uglify(),
terser(),
gzipPlugin({
customCompression: content => brotliPromise(Buffer.from(content)),
fileName: '.br'
Expand Down
Loading