2019-10-19 22:47:25 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from invoke import task
|
|
|
|
from invoke.util import cd
|
|
|
|
from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer
|
|
|
|
from pelican.settings import DEFAULT_CONFIG, get_settings_from_file
|
|
|
|
|
2020-09-19 22:04:09 +02:00
|
|
|
SETTINGS_FILE_BASE = "pelicanconf.py"
|
2019-10-19 22:47:25 +02:00
|
|
|
SETTINGS = {}
|
|
|
|
SETTINGS.update(DEFAULT_CONFIG)
|
|
|
|
LOCAL_SETTINGS = get_settings_from_file(SETTINGS_FILE_BASE)
|
|
|
|
SETTINGS.update(LOCAL_SETTINGS)
|
|
|
|
|
|
|
|
CONFIG = {
|
2020-09-19 22:04:09 +02:00
|
|
|
"settings_base": SETTINGS_FILE_BASE,
|
|
|
|
"settings_publish": "publishconf.py",
|
2019-10-19 22:47:25 +02:00
|
|
|
# Output path. Can be absolute or relative to tasks.py. Default: 'output'
|
2020-09-19 22:04:09 +02:00
|
|
|
"deploy_path": SETTINGS["OUTPUT_PATH"],
|
2019-10-19 22:47:25 +02:00
|
|
|
# Github Pages configuration
|
2020-09-19 22:04:09 +02:00
|
|
|
"github_pages_branch": "gh-pages",
|
|
|
|
"commit_message": "'Publish site on {}'".format(datetime.date.today().isoformat()),
|
2019-10-19 22:47:25 +02:00
|
|
|
# Port for `serve`
|
2020-09-19 22:04:09 +02:00
|
|
|
"port": 8000,
|
2019-10-19 22:47:25 +02:00
|
|
|
}
|
|
|
|
|
2020-09-19 22:04:09 +02:00
|
|
|
|
2019-10-19 22:47:25 +02:00
|
|
|
@task
|
|
|
|
def clean(c):
|
|
|
|
"""Remove generated files"""
|
2020-09-19 22:04:09 +02:00
|
|
|
if os.path.isdir(CONFIG["deploy_path"]):
|
|
|
|
shutil.rmtree(CONFIG["deploy_path"])
|
|
|
|
os.makedirs(CONFIG["deploy_path"])
|
|
|
|
|
2019-10-19 22:47:25 +02:00
|
|
|
|
|
|
|
@task
|
|
|
|
def build(c):
|
|
|
|
"""Build local version of site"""
|
2020-09-19 22:04:09 +02:00
|
|
|
c.run("pelican -s {settings_base}".format(**CONFIG))
|
|
|
|
|
2019-10-19 22:47:25 +02:00
|
|
|
|
|
|
|
@task
|
|
|
|
def rebuild(c):
|
|
|
|
"""`build` with the delete switch"""
|
2020-09-19 22:04:09 +02:00
|
|
|
c.run("pelican -d -s {settings_base}".format(**CONFIG))
|
|
|
|
|
2019-10-19 22:47:25 +02:00
|
|
|
|
|
|
|
@task
|
|
|
|
def regenerate(c):
|
|
|
|
"""Automatically regenerate site upon file modification"""
|
2020-09-19 22:04:09 +02:00
|
|
|
c.run("pelican -r -s {settings_base}".format(**CONFIG))
|
|
|
|
|
2019-10-19 22:47:25 +02:00
|
|
|
|
|
|
|
@task
|
|
|
|
def serve(c):
|
|
|
|
"""Serve site at http://localhost:$PORT/ (default port is 8000)"""
|
|
|
|
|
|
|
|
class AddressReuseTCPServer(RootedHTTPServer):
|
|
|
|
allow_reuse_address = True
|
|
|
|
|
|
|
|
server = AddressReuseTCPServer(
|
2020-09-19 22:04:09 +02:00
|
|
|
CONFIG["deploy_path"], ("", CONFIG["port"]), ComplexHTTPRequestHandler
|
|
|
|
)
|
2019-10-19 22:47:25 +02:00
|
|
|
|
2020-09-19 22:04:09 +02:00
|
|
|
sys.stderr.write("Serving on port {port} ...\n".format(**CONFIG))
|
2019-10-19 22:47:25 +02:00
|
|
|
server.serve_forever()
|
|
|
|
|
2020-09-19 22:04:09 +02:00
|
|
|
|
2019-10-19 22:47:25 +02:00
|
|
|
@task
|
|
|
|
def reserve(c):
|
|
|
|
"""`build`, then `serve`"""
|
|
|
|
build(c)
|
|
|
|
serve(c)
|
|
|
|
|
2020-09-19 22:04:09 +02:00
|
|
|
|
2019-10-19 22:47:25 +02:00
|
|
|
@task
|
|
|
|
def preview(c):
|
|
|
|
"""Build production version of site"""
|
2020-09-19 22:04:09 +02:00
|
|
|
c.run("pelican -s {settings_publish}".format(**CONFIG))
|
|
|
|
|
2019-10-19 22:47:25 +02:00
|
|
|
|
|
|
|
@task
|
|
|
|
def livereload(c):
|
|
|
|
"""Automatically reload browser tab upon file modification."""
|
|
|
|
from livereload import Server
|
2020-09-19 22:04:09 +02:00
|
|
|
|
2019-10-19 22:47:25 +02:00
|
|
|
build(c)
|
|
|
|
server = Server()
|
|
|
|
# Watch the base settings file
|
2020-09-19 22:04:09 +02:00
|
|
|
server.watch(CONFIG["settings_base"], lambda: build(c))
|
2019-10-19 22:47:25 +02:00
|
|
|
# Watch content source files
|
2020-09-19 22:04:09 +02:00
|
|
|
content_file_extensions = [".md", ".rst"]
|
2019-10-19 22:47:25 +02:00
|
|
|
for extension in content_file_extensions:
|
2020-09-19 22:04:09 +02:00
|
|
|
content_blob = "{0}/**/*{1}".format(SETTINGS["PATH"], extension)
|
2019-10-19 22:47:25 +02:00
|
|
|
server.watch(content_blob, lambda: build(c))
|
|
|
|
# Watch the theme's templates and static assets
|
2020-09-19 22:04:09 +02:00
|
|
|
theme_path = SETTINGS["THEME"]
|
|
|
|
server.watch("{}/templates/*.html".format(theme_path), lambda: build(c))
|
|
|
|
static_file_extensions = [".css", ".js"]
|
2019-10-19 22:47:25 +02:00
|
|
|
for extension in static_file_extensions:
|
2020-09-19 22:04:09 +02:00
|
|
|
static_file = "{0}/static/**/*{1}".format(theme_path, extension)
|
2019-10-19 22:47:25 +02:00
|
|
|
server.watch(static_file, lambda: build(c))
|
|
|
|
# Serve output path on configured port
|
2020-09-19 22:04:09 +02:00
|
|
|
server.serve(port=CONFIG["port"], root=CONFIG["deploy_path"])
|
2019-10-19 22:47:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
def publish(c):
|
|
|
|
"""Publish to production via rsync"""
|
2020-09-19 22:04:09 +02:00
|
|
|
c.run("pelican -s {settings_publish}".format(**CONFIG))
|
2019-10-19 22:47:25 +02:00
|
|
|
c.run(
|
|
|
|
'rsync --delete --exclude ".DS_Store" -pthrvz -c '
|
|
|
|
'-e "ssh -p {ssh_port}" '
|
2020-09-19 22:04:09 +02:00
|
|
|
"{} {ssh_user}@{ssh_host}:{ssh_path}".format(
|
|
|
|
CONFIG["deploy_path"].rstrip("/") + "/", **CONFIG
|
|
|
|
)
|
|
|
|
)
|