Skip to content

Commit 370d04a

Browse files
authored
Fixes to CI/CD (#100)
* format: add task to format code * gha: use new formatting task and actions checkout * fix: remove hardcoded paths in workon file * fix: update venv to remove hardcoded path * gha: fix failing test * set safe directory * gha: add step to cancel previously running workflows for the same pr
1 parent f411932 commit 370d04a

File tree

6 files changed

+94
-39
lines changed

6 files changed

+94
-39
lines changed

.github/workflows/tests.yml

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,27 @@ on:
88
types: [opened, synchronize, reopened, ready_for_review]
99

1010
jobs:
11+
# Cancel previous running actions for the same PR
12+
cancel_previous:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Cancel Workflow Action
16+
uses: styfle/[email protected]
17+
1118
checks:
1219
if: github.event.pull_request.draft == false
1320
runs-on: ubuntu-20.04
1421
container:
1522
image: faasm/cpp-sysroot:0.1.8
16-
defaults:
17-
run:
18-
working-directory: /code/cpp
1923
steps:
2024
# --- Update code ---
21-
- name: "Fetch ref"
22-
run: git fetch origin ${GITHUB_REF}:ci-branch
23-
- name: "Check out branch"
24-
run: git checkout --force ci-branch
25-
- name: "Update Faabric submodule"
26-
run: git submodule update --init -f third-party/faabric
27-
- name: "Update FFI submodule"
28-
run: git submodule update --init -f third-party/libffi
29-
- name: "Update FFmpeg submodule"
30-
run: git submodule update --init -f third-party/FFmpeg
31-
- name: "Update ImageMagick submodule"
32-
run: git submodule update --init -f third-party/ImageMagick
33-
- name: "Update libpng submodule"
34-
run: git submodule update --init -f third-party/libpng
35-
- name: "Update zlib submodule"
36-
run: git submodule update --init -f third-party/zlib
37-
- name: "Update wasi-libc submodule"
38-
run: git submodule update --init -f third-party/wasi-libc
25+
- name: "Checkout code"
26+
uses: actions/checkout@v3
27+
with:
28+
submodules: true
29+
# See actions/checkout#766
30+
- name: "Set the GH workspace as a safe git directory"
31+
run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
3932
# --- Build libraries to wasm ---
4033
- name: "Build libc"
4134
run: ./bin/inv_wrapper.sh libc
@@ -70,10 +63,5 @@ jobs:
7063
- name: "Build libfaasmpi native"
7164
run: ./bin/inv_wrapper.sh libfaasmpi --native --clean
7265
# --- Formatting ---
73-
- name: "Check python"
74-
run: source venv/bin/activate && ./bin/check_python.sh
75-
shell: bash
76-
- name: "Run C/C++ formatting"
77-
run: ./bin/run_clang_format.sh
78-
- name: "Check no formatting changes"
79-
run: git diff --exit-code -- :^third-party
66+
- name: "Format code"
67+
run: ./bin/inv_wrapper.sh format-code --check

bin/create_venv.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ VENV_PATH="undetected"
1111
if [[ -z "$CPP_DOCKER" ]]; then
1212
VENV_PATH="${PROJ_ROOT}/venv-bm"
1313
else
14-
VENV_PATH="/code/cpp/venv"
14+
VENV_PATH="${PROJ_ROOT}/venv"
1515
fi
1616

1717
PIP=${VENV_PATH}/bin/pip3

bin/workon.sh

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,17 @@
44
# Container-specific settings
55
# ----------------------------
66

7+
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" >/dev/null 2>&1 && pwd )"
8+
PROJ_ROOT="${THIS_DIR}/.."
79
MODE="undetected"
810
if [[ -z "$CPP_DOCKER" ]]; then
9-
10-
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" >/dev/null 2>&1 && pwd )"
11-
PROJ_ROOT="${THIS_DIR}/.."
12-
VENV_PATH="${PROJ_ROOT}/venv-bm"
13-
1411
# Normal terminal
12+
VENV_PATH="${PROJ_ROOT}/venv-bm"
1513
MODE="terminal"
1614
else
17-
# Running inside the container, we know the project root
18-
PROJ_ROOT="/code/cpp"
19-
VENV_PATH="/code/cpp/venv"
20-
15+
VENV_PATH="${PROJ_ROOT}/venv"
2116
# Use containerised redis
2217
alias redis-cli="redis-cli -h redis"
23-
2418
MODE="container"
2519
fi
2620

tasks/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from . import clapack
44
from . import docker
55
from . import ffmpeg
6+
from . import format_code
67
from . import func
78
from . import git
89
from . import imagemagick
@@ -20,6 +21,7 @@
2021
ns = Collection(
2122
clapack,
2223
docker,
24+
format_code,
2325
ffmpeg,
2426
func,
2527
git,

tasks/format_code.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from invoke import task
2+
from tasks.util.env import PROJ_ROOT
3+
from subprocess import run
4+
5+
6+
@task(default=True)
7+
def format(ctx, check=False):
8+
"""
9+
Format Python and C++ code
10+
"""
11+
# ---- Python formatting ----
12+
13+
files_to_check = (
14+
run(
15+
'git ls-files -- "*.py"',
16+
shell=True,
17+
check=True,
18+
cwd=PROJ_ROOT,
19+
capture_output=True,
20+
)
21+
.stdout.decode("utf-8")
22+
.split("\n")[:-1]
23+
)
24+
black_cmd = [
25+
"python3 -m black",
26+
"{}".format("--check" if check else ""),
27+
" ".join(files_to_check),
28+
]
29+
black_cmd = " ".join(black_cmd)
30+
run(black_cmd, shell=True, check=True, cwd=PROJ_ROOT)
31+
32+
flake8_cmd = [
33+
"python3 -m flake8",
34+
"{}".format("--format" if not check else ""),
35+
" ".join(files_to_check),
36+
]
37+
flake8_cmd = " ".join(flake8_cmd)
38+
run(flake8_cmd, shell=True, check=True, cwd=PROJ_ROOT)
39+
40+
# ---- C/C++ formatting ----
41+
42+
files_to_check = (
43+
run(
44+
'git ls-files -- "*.h" "*.cpp" "*.c"',
45+
shell=True,
46+
check=True,
47+
cwd=PROJ_ROOT,
48+
capture_output=True,
49+
)
50+
.stdout.decode("utf-8")
51+
.split("\n")[:-1]
52+
)
53+
54+
clang_cmd = "clang-format-10 -i {}".format(" ".join(files_to_check))
55+
run(clang_cmd, shell=True, check=True, cwd=PROJ_ROOT)
56+
57+
# ---- Append newlines to C/C++ files if not there ----
58+
59+
for f in files_to_check:
60+
# Append opens the file from the end, but there is no easy way to read
61+
# just one character backwards unless you open the file as a byte
62+
# stream, which then makes it very involved to compare against the
63+
# newline character
64+
with open(f, "a+") as fh:
65+
fh.seek(0)
66+
read = fh.read()
67+
if read[-1] != "\n":
68+
fh.write("\n")

tasks/util/env.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from os.path import dirname, realpath
2+
3+
PROJ_ROOT = dirname(dirname(dirname(realpath(__file__))))

0 commit comments

Comments
 (0)