Skip to content

Commit fae3057

Browse files
authored
Merge pull request #59 from SimonThalvorsen/main
ENT-13814: Added syntax-description and GH Action to update it weekly to replace policy_language.py
2 parents 2b0d9a3 + af3a6fd commit fae3057

6 files changed

Lines changed: 10786 additions & 246 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: Update syntax-description
2+
3+
on:
4+
schedule:
5+
- cron: "0 7 * * 1" # Run every Monday at 7am UTC
6+
# | | | | |
7+
# | | | | day of the week (0-6) (Sunday to Saturday)
8+
# | | | month (1-12)
9+
# | | day of the month (1-31)
10+
# | hour (0-23)
11+
# minute (0-59)
12+
workflow_dispatch: # Enables manual trigger
13+
14+
jobs:
15+
update_syntax_desc:
16+
name: Update syntax-description
17+
runs-on: ubuntu-24.04
18+
permissions:
19+
contents: write
20+
pull-requests: write
21+
steps:
22+
- name: Checks-out repository
23+
uses: actions/checkout@v4
24+
with:
25+
ref: "main"
26+
- name: Set up Python 3.12
27+
uses: actions/setup-python@v5
28+
with:
29+
python-version: "3.12"
30+
- name: Install dependencies
31+
run: |
32+
python -m pip install --upgrade pip
33+
python -m pip install cf-remote
34+
- name: Install cfengine
35+
run: |
36+
cf-remote --version master download --edition community ubuntu24 amd64 hub
37+
sudo dpkg -i ~/.cfengine/cf-remote/packages/cfengine-community*.deb
38+
39+
- name: Extract new syntax-description
40+
run: |
41+
(
42+
sudo cf-promises --syntax-description json
43+
) > new.json
44+
echo "" >> new.json
45+
- name: Set Git user
46+
run: |
47+
git config user.name 'github-actions[bot]'
48+
git config user.email 'github-actions[bot]@users.noreply.github.com'
49+
- name: Update contents of syntax-description
50+
run: |
51+
if ! cmp -s new.json ./src/cfengine_cli/syntax-description.json; then
52+
cat new.json > ./src/cfengine_cli/syntax-description.json
53+
echo "CHANGES_DETECTED=true" >> $GITHUB_ENV
54+
rm new.json
55+
fi
56+
- name: Create Pull Request
57+
if: env.CHANGES_DETECTED == 'true'
58+
uses: cfengine/create-pull-request@v6
59+
with:
60+
title: Updated syntax-description.json
61+
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).
62+
reviewers: |
63+
simonthalvorsen
64+
olehermanse
65+
larsewi
66+
nickanderson
67+
craigcomstock
68+
branch: update-syntax-description
69+
branch-suffix: timestamp

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ cfengine = "cfengine_cli.main:main"
4040
[tool.setuptools]
4141
license-files = [] # Workaround bug in setuptools https://github.com/astral-sh/uv/issues/9513
4242

43+
[tool.setuptools.package-data]
44+
cfengine_cli = ["*.json"] # syntax-description.json
45+
4346
[tool.pyright]
4447
include = ["src"]
4548
venvPath = "."

src/cfengine_cli/lint.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,56 @@
3737
from cfbs.validate import validate_config
3838
from cfbs.cfbs_config import CFBSConfig
3939
from cfbs.utils import find
40-
from cfengine_cli.policy_language import (
41-
DEPRECATED_PROMISE_TYPES,
42-
ALLOWED_BUNDLE_TYPES,
43-
BUILTIN_PROMISE_TYPES,
44-
BUILTIN_FUNCTIONS,
45-
)
4640
from cfengine_cli.utils import UserError
4741

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

5044

45+
def _load_syntax_description(path: str | None = None) -> dict:
46+
"""Load and return the parsed syntax-description.json file."""
47+
if path is None:
48+
path = os.path.join(os.path.dirname(__file__), "syntax-description.json")
49+
with open(path, "r") as f:
50+
return json.load(f)
51+
52+
53+
def _derive_syntax_sets(data: dict) -> tuple:
54+
"""Derive the four sets used for linting from a loaded syntax-description dict.
55+
56+
Returns: (ALLOWED_BUNDLE_TYPES, BUILTIN_PROMISE_TYPES, BUILTIN_FUNCTIONS, DEPRECATED_PROMISE_TYPES)
57+
"""
58+
builtin_body_types = set(data.get("bodyTypes", {}).keys())
59+
60+
allowed_bundle_types = data.get("bundleTypes", {}).keys()
61+
62+
builtin_promise_types = set(data.get("promiseTypes", {}).keys())
63+
64+
builtin_functions = set(data.get("functions", {}).keys())
65+
66+
deprecated_promise_types = {
67+
"defaults",
68+
"guest_environments",
69+
} # Has to be hardcoded, not tagged in syntax-description.json
70+
71+
return (
72+
builtin_body_types,
73+
allowed_bundle_types,
74+
builtin_promise_types,
75+
builtin_functions,
76+
deprecated_promise_types,
77+
)
78+
79+
80+
_SYNTAX_DATA = _load_syntax_description()
81+
(
82+
_,
83+
ALLOWED_BUNDLE_TYPES,
84+
BUILTIN_PROMISE_TYPES,
85+
BUILTIN_FUNCTIONS,
86+
DEPRECATED_PROMISE_TYPES,
87+
) = _derive_syntax_sets(_SYNTAX_DATA)
88+
89+
5190
def _qualify(name: str, namespace: str) -> str:
5291
"""If name is already qualified (contains ':'), return as-is. Otherwise prepend namespace."""
5392
assert '"' not in namespace

src/cfengine_cli/policy_language.py

Lines changed: 0 additions & 239 deletions
This file was deleted.

0 commit comments

Comments
 (0)