Skip to content

Commit

Permalink
CI: Package bids-examples and bids-error-examples into Python distrib…
Browse files Browse the repository at this point in the history
…utions on schema-* tags (#1252)

Co-authored-by: Christopher J. Markiewicz <markiewicz@stanford.edu>
Co-authored-by: Anthony Galassi <28850131+bendhouseart@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 25, 2022
1 parent 8106ee8 commit e610cbf
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 74 deletions.
158 changes: 106 additions & 52 deletions .github/workflows/schemacode_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,81 @@ on:
push:
branches:
- "master"
paths:
- "tools/schemacode/**"
- "src/schema/**"
tags:
- "schema-*"
pull_request:
branches:
- "*"
paths:
- "tools/schemacode/**"
- "src/schema/**"

jobs:
defaults:
run:
shell: bash

run_tests:
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: ["ubuntu-latest"]
python-version: ["3.10"]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: "Install build dependencies"
run: pip install --upgrade build twine
- name: "Install test dependencies on tag"
run: pip install --upgrade pytest pyyaml pandas tabulate
if: ${{ startsWith(github.ref, 'refs/tags/schema-') }}
- name: "Build archive on tag"
run: pytest tools/schemacode/bidsschematools -k make_archive
env:
BIDSSCHEMATOOLS_RELEASE: 1
if: ${{ startsWith(github.ref, 'refs/tags/schema-') }}
- name: "Build source distribution and wheel"
run: python -m build tools/schemacode
- name: "Check distribution metadata"
run: twine check tools/schemacode/dist/*
- uses: actions/upload-artifact@v3
with:
name: dist
path: tools/schemacode/dist/

test:
runs-on: ${{ matrix.os }}
needs: [build]
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
python-version: ["3.7", "3.8", "3.9", "3.10"]
fail-fast: false
matrix:
os: ["ubuntu-latest"]
python-version: ["3.7", "3.8", "3.9", "3.10"]
name: ${{ matrix.os }} with Python ${{ matrix.python-version }}
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v3

- name: "Set up Python"
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
python-version: ${{ matrix.python-version }}

- name: "Display Python version"
shell: bash {0}
run: python -c "import sys; print(sys.version)"

- name: "Install the schemacode package"
shell: bash {0}
- name: "Fetch packages"
uses: actions/download-artifact@v3
with:
name: dist
path: dist/

- name: "Install package"
run: |
python -m pip install --progress-bar off --upgrade pip
python -m pip install ./tools/schemacode[all]
pip install $( ls dist/*.whl )[all]
- name: "Run tests"
shell: bash {0}
run: |
python -m pytest --pyargs bidsschematools -m "not validate_schema" --cov-append --cov-report=xml --cov=bidsschematools
python -m pytest -vs --pyargs bidsschematools -m "not validate_schema" \
--cov-append --cov-report=xml --cov=bidsschematools
- name: Upload artifacts
uses: actions/upload-artifact@v3
Expand All @@ -58,6 +87,34 @@ jobs:
path: coverage.xml
if: success()

publish:
runs-on: ${{ matrix.os }}
name: Publish Python Package
needs: [test]
strategy:
matrix:
os: ["ubuntu-latest"]
python-version: ["3.10"]
steps:
- name: "Fetch packages"
uses: actions/download-artifact@v3
with:
name: dist
path: dist/
- name: "Test PyPI upload"
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
skip_existing: true
- name: "Upload to PyPI (on tags)"
if: ${{ startsWith(github.ref, 'refs/tags/schema-') }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}

flake8-lint:
runs-on: ubuntu-latest
name: Lint schemacode
Expand All @@ -71,56 +128,53 @@ jobs:
python-version: "3.7"

- name: Install the schemacode package
shell: bash {0}
run: |
python -m pip install --progress-bar off --upgrade pip setuptools wheel
python -m pip install -e ./tools/schemacode[all]
- name: Run flake8
working-directory: ./tools/schemacode/
shell: bash {0}
run: |
flake8 .
validate_schema:
runs-on: ubuntu-latest
name: Validate schema
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3

- name: Set Up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.9"
- name: Set Up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.9"

- name: Install the schemacode package
shell: bash {0}
run: |
python -m pip install --progress-bar off --upgrade pip setuptools wheel
python -m pip install -e ./tools/schemacode[all]
- name: Install the schemacode package
run: |
python -m pip install --progress-bar off --upgrade pip setuptools wheel
python -m pip install -e ./tools/schemacode[all]
- name: Run schema validation tests
run: python -m pytest --pyargs bidsschematools -m "validate_schema" --cov-append --cov-report=xml --cov=bidsschematools
- name: Run schema validation tests
run: python -m pytest --pyargs bidsschematools -m "validate_schema" --cov-append --cov-report=xml --cov=bidsschematools

- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: schema_validation
path: coverage.xml
if: success()
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: schema_validation
path: coverage.xml
if: success()

upload_to_codecov:
runs-on: "ubuntu-latest"
name: Upload coverage
needs: [run_tests,validate_schema]
needs: [test, validate_schema]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v2

- name: Download artifacts
uses: actions/download-artifact@v3
- name: Download artifacts
uses: actions/download-artifact@v3

- name: Upload to CodeCov
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
- name: Upload to CodeCov
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
23 changes: 23 additions & 0 deletions scripts/test_tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

# Execute this script from anywhere in the repository to push to a new unique testing tag.
# This is a script which automates creation and pushing of unique tags for github actions.
# It is particularly useful when debugging the upload hooks for pypi.
# The workflow for that is to edit `.github/workflows/schemacode_ci.yml` and add a secondary branch name to the `on` section right after master.
# This will allow you to keep pushing to the branch as if it were master and unix-time tagged archives being uploaded to test.pypi.

EPOCH="$(date +%s)"

GIT_DIR=$( git rev-parse --git-dir )
REPO_ROOT=$( ls -d ${GIT_DIR%.git} )

VERSION_FILE="$REPO_ROOT/src/schema/SCHEMA_VERSION"

REAL_VERSION=$(cat "${VERSION_FILE}")
DUMMY_VERSION="0.0.${EPOCH}-dev"

echo "${DUMMY_VERSION}" > "${VERSION_FILE}"
git commit -a -m "Test release $DUMMY_VERSION"
git tag -a "schema-$DUMMY_VERSION" -m "Test release $DUMMY_VERSION"

echo "${REAL_VERSION}" > "${VERSION_FILE}"
31 changes: 18 additions & 13 deletions tools/schemacode/bidsschematools/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
import os
import tempfile
from subprocess import run

import pytest
try:
from importlib.resources import as_file, files
except ImportError: # PY<3.9
from importlib_resources import as_file, files

from bidsschematools import schema, utils
import pytest

lgr = logging.getLogger()

Expand Down Expand Up @@ -35,22 +37,21 @@ def get_gitrepo_fixture(url, whitelist):
@pytest.fixture(scope="session")
def fixture():
archive_name = url.rsplit("/", 1)[-1]
testdata_archive = os.path.join(os.getcwd(), "testdata", archive_name)
if os.path.isdir(testdata_archive):
testdata_dir = files("bidsschematools.tests.data") / archive_name
if testdata_dir.is_dir():
lgr.info(
"Found static testdata archive under `%s`. "
"Not downloading latest data from version control.",
testdata_archive,
f"Found static testdata archive under `{testdata_dir}`. "
"Not downloading latest data from version control."
)
yield testdata_archive
with as_file(testdata_dir) as path:
yield path
else:
lgr.info(
"No static testdata available under `%s`. "
"Attempting to fetch live data from version control.",
testdata_archive,
testdata_dir,
)
with tempfile.TemporaryDirectory() as path:
assert os.path.exists(path)
lgr.debug("Cloning %r into %r", url, path)
runout = run(
[
Expand Down Expand Up @@ -79,14 +80,18 @@ def fixture():
@pytest.fixture(scope="session")
def schema_dir():
"""Path to the schema housed in the bids-specification repo."""
from bidsschematools import utils

bids_schema = utils.get_schema_path()
return bids_schema


@pytest.fixture(scope="session")
def schema_obj(schema_dir):
def schema_obj():
"""Schema object."""
return schema.load_schema(schema_dir)
from bidsschematools import schema

return schema.load_schema()


bids_examples = get_gitrepo_fixture(
Expand Down
Empty file.
25 changes: 17 additions & 8 deletions tools/schemacode/bidsschematools/tests/test_make_testdata.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
import tarfile
import shutil

import pytest
try:
from importlib.resources import files
except ImportError: # PY<3.9
from importlib_resources import files

from .. import __version__
import pytest


def require_env(var):
Expand All @@ -30,9 +33,15 @@ def test_make_archive(bids_examples, bids_error_examples):
* Archives will be generated under `/tmp/bidsschematools-testdata-SCHEMA_VERSION.tar.gz`
"""

archive_name = f"bidsschematools-testdata-{__version__}"
archive_path = f"/tmp/{archive_name}.tar.gz"
testdata_dir = files("bidsschematools.tests.data")
ignore_git = shutil.ignore_patterns(".git*")
shutil.copytree(bids_examples, testdata_dir / "bids-examples", ignore=ignore_git)
shutil.copytree(bids_error_examples, testdata_dir / "bids-error-examples", ignore=ignore_git)

# Keeping this for now, it would be really nice to have a separate archive someday.
# archive_name = f"bidsschematools-testdata-{__version__}"
# archive_path = f"/tmp/{archive_name}.tar.gz"

with tarfile.open(archive_path, "w:gz") as tar:
tar.add(bids_examples, arcname=f"{archive_name}/bids-examples")
tar.add(bids_error_examples, arcname=f"{archive_name}/bids-error-examples")
# with tarfile.open(archive_path, "w:gz") as tar:
# tar.add(bids_examples, arcname=f"{archive_name}/bids-examples")
# tar.add(bids_error_examples, arcname=f"{archive_name}/bids-error-examples")
2 changes: 1 addition & 1 deletion tools/schemacode/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bidsschematools =
data/schema/BIDS_VERSION
data/schema/SCHEMA_VERSION
data/schema/**/*.yaml
tests/data/*
tests/data/**/*

[options.entry_points]
console_scripts =
Expand Down

0 comments on commit e610cbf

Please sign in to comment.