Skip to content
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
69 changes: 69 additions & 0 deletions .github/workflows/update-syntax-description.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Update syntax-description
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably prefer to put the JSON / yaml in as separate commits to avoid doing too many things in one commit.


on:
schedule:
- cron: "0 7 * * 1" # Run every Monday at 7am UTC
# | | | | |
# | | | | day of the week (0-6) (Sunday to Saturday)
# | | | month (1-12)
# | | day of the month (1-31)
# | hour (0-23)
# minute (0-59)
workflow_dispatch: # Enables manual trigger

jobs:
update_syntax_desc:
name: Update syntax-description
runs-on: ubuntu-24.04
permissions:
contents: write
pull-requests: write
steps:
- name: Checks-out repository
uses: actions/checkout@v4
with:
ref: "main"
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install cf-remote
- name: Install cfengine
run: |
cf-remote --version master download --edition community ubuntu24 amd64 hub
sudo dpkg -i ~/.cfengine/cf-remote/packages/cfengine-community*.deb

- name: Extract new syntax-description
run: |
(
sudo cf-promises --syntax-description json
) > new.json
echo "" >> new.json
- name: Set Git user
run: |
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
- name: Update contents of syntax-description
run: |
if ! cmp -s new.json ./src/cfengine_cli/syntax-description.json; then
cat new.json > ./src/cfengine_cli/syntax-description.json
echo "CHANGES_DETECTED=true" >> $GITHUB_ENV
rm new.json
fi
Comment on lines +51 to +55
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ! cmp -s new.json ./src/cfengine_cli/syntax-description.json; then
cat new.json > ./src/cfengine_cli/syntax-description.json
echo "CHANGES_DETECTED=true" >> $GITHUB_ENV
rm new.json
fi
if ! cmp -s new.json ./src/cfengine_cli/syntax-description.json; then
cat new.json > ./src/cfengine_cli/syntax-description.json
echo "CHANGES_DETECTED=true" >> $GITHUB_ENV
rm new.json
fi

- name: Create Pull Request
if: env.CHANGES_DETECTED == 'true'
uses: cfengine/create-pull-request@v6
with:
title: Updated syntax-description.json
body: Automated update to syntax-description.json [the `update-syntax-description` workflow](https://github.com/cfengine/cfengine-cli/tree/main/.github/workflows/update-syntax-description.yml).
reviewers: |
simonthalvorsen
olehermanse
larsewi
nickanderson
craigcomstock
branch: update-syntax-description
branch-suffix: timestamp
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ cfengine = "cfengine_cli.main:main"
[tool.setuptools]
license-files = [] # Workaround bug in setuptools https://github.com/astral-sh/uv/issues/9513

[tool.setuptools.package-data]
cfengine_cli = ["*.json"] # syntax-description.json

[tool.pyright]
include = ["src"]
venvPath = "."
Expand Down
51 changes: 45 additions & 6 deletions src/cfengine_cli/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,56 @@
from cfbs.validate import validate_config
from cfbs.cfbs_config import CFBSConfig
from cfbs.utils import find
from cfengine_cli.policy_language import (
DEPRECATED_PROMISE_TYPES,
ALLOWED_BUNDLE_TYPES,
BUILTIN_PROMISE_TYPES,
BUILTIN_FUNCTIONS,
)
from cfengine_cli.utils import UserError

LINT_EXTENSIONS = (".cf", ".json")


def _load_syntax_description(path: str | None = None) -> dict:
"""Load and return the parsed syntax-description.json file."""
if path is None:
path = os.path.join(os.path.dirname(__file__), "syntax-description.json")
with open(path, "r") as f:
return json.load(f)


def _derive_syntax_sets(data: dict) -> tuple:
"""Derive the four sets used for linting from a loaded syntax-description dict.

Returns: (ALLOWED_BUNDLE_TYPES, BUILTIN_PROMISE_TYPES, BUILTIN_FUNCTIONS, DEPRECATED_PROMISE_TYPES)
"""
builtin_body_types = set(data.get("bodyTypes", {}).keys())

allowed_bundle_types = data.get("bundleTypes", {}).keys()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned below, linting will not work correctly if this one is empty, so it might be appropriate to assert / fail early when the syntax data is invalid / missing.


builtin_promise_types = set(data.get("promiseTypes", {}).keys())

builtin_functions = set(data.get("functions", {}).keys())

deprecated_promise_types = {
"defaults",
"guest_environments",
} # Has to be hardcoded, not tagged in syntax-description.json

return (
builtin_body_types,
allowed_bundle_types,
builtin_promise_types,
builtin_functions,
deprecated_promise_types,
)
Comment on lines +53 to +77
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, we will want to access the syntax data in more complicated ways, not just lists of keys. I don't think this approach will scale nicely, please use a class instead;

class SyntaxData:
  def __init__(self, path):
    self._data = get_json(path)
    # Maybe add some assertions here, linting will not work correctly
    # if for example allowed bundle types is empty.
    [...]
  def get_bundle_types(self):
    [...]
  # In the future we might choose to do something like;
  def is_valid_attribute(self, promise_type, attribute_name, attribute_value):
    [...]



_SYNTAX_DATA = _load_syntax_description()
(
_,
ALLOWED_BUNDLE_TYPES,
BUILTIN_PROMISE_TYPES,
BUILTIN_FUNCTIONS,
DEPRECATED_PROMISE_TYPES,
) = _derive_syntax_sets(_SYNTAX_DATA)


def _qualify(name: str, namespace: str) -> str:
"""If name is already qualified (contains ':'), return as-is. Otherwise prepend namespace."""
assert '"' not in namespace
Expand Down
239 changes: 0 additions & 239 deletions src/cfengine_cli/policy_language.py

This file was deleted.

Loading
Loading