Skip to content

Commit 5f5dd86

Browse files
authored
Merge pull request #122 from victormlg/linting
CFE-4632: Added python linting in github actions
2 parents 9e03119 + 5d054f1 commit 5f5dd86

15 files changed

Lines changed: 238 additions & 173 deletions

File tree

.github/workflows/ci.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
name: Continuous Integration
22

33
on:
4+
push:
5+
branches: [master]
46
pull_request:
5-
types: [opened, reopened]
7+
branches: [master]
68

79
jobs:
810
unit_tests:
911
name: Run Unit Tests
1012
runs-on: ubuntu-24.04
13+
permissions:
14+
contents: read
1115
steps:
1216
- uses: actions/checkout@v4
1317
with:
1418
submodules: recursive
1519
- name: Install dependencies
16-
run: sudo apt-get update -y && sudo apt-get install -y python3
20+
run: |
21+
sudo apt-get update -y && sudo apt-get install -y python3
22+
pip install flake8 pyright black pyflakes
23+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
1724
- name: Install cfbs
1825
run: pip install cfbs
1926
- name: Check the status with cfbs
@@ -22,3 +29,5 @@ jobs:
2229
run: cfbs validate
2330
- name: Check the formatting
2431
run: cfbs --check pretty ./cfbs.json
32+
- name: Linting
33+
run: ./ci/linting.sh

ci/linting.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
echo "Running flake8"
6+
flake8 . --ignore=E203,W503,E722,E731 --max-complexity=100 --max-line-length=160
7+
8+
echo "Running pyright"
9+
pyright .
10+
11+
shopt -s globstar
12+
echo "Running black"
13+
black --check .
14+
15+
echo "Running pyflakes"
16+
pyflakes .

examples/git-using-lib/git_using_lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def validate_promise(self, promiser, attributes, metadata):
1010
if not promiser.startswith("/"):
1111
raise ValidationError(f"File path '{promiser}' must be absolute")
1212
if "repository" not in attributes:
13-
raise ValidationError(f"Attribute 'repository' is required")
13+
raise ValidationError("Attribute 'repository' is required")
1414

1515
def evaluate_promise(self, promiser, attributes, metadata):
1616
url = attributes["repository"]

examples/gpg/gpg.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
"""
4343

4444
import json
45-
from subprocess import Popen, PIPE
46-
import sys
45+
from subprocess import Popen, PIPE, TimeoutExpired
4746
from cfengine_module_library import PromiseModule, ValidationError, Result
4847

4948

@@ -109,9 +108,9 @@ def validate_promise(self, promiser, attributes, metadata):
109108
raise ValidationError(
110109
f"Promiser '{promiser}' for 'gpg_keys' promise must be an absolute path"
111110
)
112-
if not "keylist" in attributes:
111+
if "keylist" not in attributes:
113112
raise ValidationError(
114-
f"Required attribute 'keylist' missing for 'gpg_keys' promise"
113+
"Required attribute 'keylist' missing for 'gpg_keys' promise"
115114
)
116115

117116
def evaluate_promise(self, promiser, attributes, metadata):

examples/rss/rss.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import requests, html, re, os, random
1+
import requests
2+
import re
3+
import os
4+
import random
25
import xml.etree.ElementTree as ET
36
from cfengine_module_library import PromiseModule, ValidationError, Result
47

@@ -42,7 +45,7 @@ def validate_promise(self, promiser, attributes, metadata):
4245
# check that attribute select has a valid type
4346
if type(select) is not str:
4447
raise ValidationError(
45-
f"Invalid type for attribute select: expected string"
48+
"Invalid type for attribute select: expected string"
4649
)
4750

4851
# check that attribute select has a valid value
@@ -159,18 +162,18 @@ def _write_promiser(self, item, promiser):
159162
return Result.NOT_KEPT
160163

161164
def _is_win_file(self, path):
162-
return re.search(r"^[a-zA-Z]:\\[\\\S|*\S]?.*$", path) != None
165+
return re.search(r"^[a-zA-Z]:\\[\\\S|*\S]?.*$", path) is not None
163166

164167
def _is_unix_file(self, path):
165-
return re.search(r"^(/[^/ ]*)+/?$", path) != None
168+
return re.search(r"^(/[^/ ]*)+/?$", path) is not None
166169

167170
def _is_url(self, path):
168171
return (
169172
re.search(
170173
r"^http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+",
171174
path,
172175
)
173-
!= None
176+
is not None
174177
)
175178

176179

examples/site-up/site_up.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def evaluate_promise(self, promiser, attributes, metadata):
3131

3232
error = None
3333
try:
34-
code = urllib.request.urlopen(url, context=ssl_ctx).getcode()
34+
urllib.request.urlopen(url, context=ssl_ctx).getcode()
3535
self.log_verbose(f"Site '{url}' is UP!")
3636
return Result.KEPT
3737
except urllib.error.HTTPError as e:

libraries/python/cfengine_module_library.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ def _handle_request(self, request):
226226
"debug",
227227
]
228228

229+
promiser = None
230+
attributes = {}
229231
if operation in ["validate_promise", "evaluate_promise"]:
230232
promiser = request["promiser"]
231233
attributes = request.get("attributes", {})
@@ -404,7 +406,7 @@ def _handle_evaluate(self, promiser, attributes, request):
404406
assert results is not None # Most likely someone forgot to return something
405407

406408
# evaluate_promise should return either a result or a (result, result_classes) pair
407-
if type(results) == str:
409+
if isinstance(results, str):
408410
self._result = results
409411
else:
410412
assert len(results) == 2

0 commit comments

Comments
 (0)