From 17d14919766c66401f87c066cee0ea147b65064a Mon Sep 17 00:00:00 2001 From: Ari Archer Date: Fri, 11 Mar 2022 17:47:54 +0200 Subject: [PATCH] update @ Fri 11 Mar 17:47:54 EET 2022 Signed-off-by: Ari Archer --- .github/workflows/codeql-analysis.yml | 71 +++++++++++++++++++++++++++ .github/workflows/spt.yml | 30 +++++++++++ blog.json | 2 +- netlify.toml | 2 +- scripts/blog | 66 +++++++++++++++++++++---- 5 files changed, 160 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/codeql-analysis.yml create mode 100644 .github/workflows/spt.yml diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000..ac1576d --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,71 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ main ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ main ] + schedule: + - cron: '16 2 * * 4' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'python' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] + # Learn more: + # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v1 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 diff --git a/.github/workflows/spt.yml b/.github/workflows/spt.yml new file mode 100644 index 0000000..6d936ee --- /dev/null +++ b/.github/workflows/spt.yml @@ -0,0 +1,30 @@ +name: Static python type-checking + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.8] + + steps: + - uses: actions/checkout@v2 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v1 + with: + python-version: ${{ matrix.python-version }} + + - name: Install mypy + run: | + pip install mypy + + - name: Run mypy + run: | + mypy scripts/blog diff --git a/blog.json b/blog.json index e4798de..1f4c4f9 100644 --- a/blog.json +++ b/blog.json @@ -49,4 +49,4 @@ "keywords": "management linux http-redirect new website blog opinion article ari-web ari" } } -} +} \ No newline at end of file diff --git a/netlify.toml b/netlify.toml index 5bb6546..8263f33 100644 --- a/netlify.toml +++ b/netlify.toml @@ -1,5 +1,5 @@ [build] - command = "python3 ./scripts/blog build" + command = "python3 ./scripts/blog clean && python3 ./scripts/blog build" [[redirects]] from = "/git/*" diff --git a/scripts/blog b/scripts/blog index 5d97bd9..197946f 100755 --- a/scripts/blog +++ b/scripts/blog @@ -11,15 +11,16 @@ import sys from atexit import register as fn_register from base64 import b64decode, b64encode from datetime import datetime +from glob import iglob from html import escape as html_escape from shutil import rmtree from threading import Thread from timeit import default_timer as code_timer -from typing import Dict, List, Tuple +from typing import Dict, List, Set, Tuple from warnings import filterwarnings as filter_warnings -from css_html_js_minify import (html_minify, # type: ignore - process_single_css_file) +from css_html_js_minify import html_minify # type: ignore +from css_html_js_minify import process_single_css_file from markdown import markdown # type: ignore from plumbum.commands.processes import ProcessExecutionError # type: ignore from pyfzf import FzfPrompt # type: ignore @@ -259,6 +260,12 @@ def build(config: Dict) -> Tuple[int, Dict]: log("Building blogs...", "INFO") def thread(blog_name: str, blog_meta: Dict): + if blog_meta["version"] != BLOG_VERSION: + log( + f"{blog_name}: unmatching version between {blog_meta['version']} and {BLOG_VERSION}", + "WARNING", + ) + blog_dir: str = os.path.join(config["blog-dir"], blog_name) os.makedirs(blog_dir, exist_ok=True) @@ -348,6 +355,9 @@ def build(config: Dict) -> Tuple[int, Dict]: def list_blogs(config: Dict) -> Tuple[int, Dict]: """List blogs""" + if not config["blogs"]: + return log("No blogs to list"), config + for blog_id, blog_meta in config["blogs"].items(): print( f"""ID: {blog_id} @@ -364,6 +374,9 @@ Keywords: {blog_meta['keywords'].replace(' ', ', ')} def remove_blog(config: Dict) -> Tuple[int, Dict]: """Remove a blog page""" + if not config["blogs"]: + return log("No blogs to remove"), config + blog_id: str = pick_blog(config) if not blog_id: @@ -386,6 +399,9 @@ def edit_title(blog: str, config: Dict) -> int: return log("New title cannot be empty") config["blogs"][blog]["title"] = b64encode(new_title.encode()).decode() + config["blogs"][sanitise_title(new_title, config["blogs"])] = config["blogs"][blog] + + del config["blogs"][blog] return EXIT_OK @@ -431,6 +447,9 @@ EDIT_HOOKS: Dict = { def edit(config: Dict) -> Tuple[int, Dict]: """Edit a blog""" + if not config["blogs"]: + return log("No blogs to edit"), config + blog_id: str = pick_blog(config) if not blog_id: @@ -466,6 +485,33 @@ def gen_new_config(config: Dict) -> Tuple[int, Dict]: return EXIT_OK, config +def clean(config: Dict) -> Tuple[int, Dict]: + """Clean up current directory""" + + TRASH: Set[str] = { + HISTORY_FILE, + config["blog-dir"], + "index.html", + "content/*.min.*", + } + + def remove(file: str) -> None: + log(f"Removing {file!r}", "REMOVE") + + try: + os.remove(file) + except IsADirectoryError: + rmtree(file) + + for glob_ex in TRASH: + for file in iglob(glob_ex, recursive=True): + remove(file) + + open(HISTORY_FILE, "w").close() + + return EXIT_OK, config + + SUBCOMMANDS: Dict = { "help": dummy, "new": new_blog, @@ -474,6 +520,7 @@ SUBCOMMANDS: Dict = { "rm": remove_blog, "edit": edit, "new-config": gen_new_config, + "clean": clean, } @@ -514,13 +561,14 @@ def main() -> int: with open(DEFAULT_CONFIG_FILE, "r") as lcfg: code, config = SUBCOMMANDS[sys.argv[1]](config=json.load(lcfg)) - log("Sorting blogs by creation time...", "CLEANUP") - config["blogs"] = dict( - map( - lambda k: (k, config["blogs"][k]), - sorted(config["blogs"], key=lambda k: config["blogs"][k]["time"]), + if config["blogs"]: + log("Sorting blogs by creation time...", "CLEANUP") + config["blogs"] = dict( + map( + lambda k: (k, config["blogs"][k]), + sorted(config["blogs"], key=lambda k: config["blogs"][k]["time"]), + ) ) - ) with open(DEFAULT_CONFIG_FILE, "w") as dcfg: json.dump(config, dcfg, indent=4)