Skip to content

Commit

Permalink
Merge pull request #2 from sarnold/script-ref
Browse files Browse the repository at this point in the history
Script refactor plus packaging and workflows
  • Loading branch information
sarnold authored Jun 20, 2022
2 parents 64fed2b + 1dd0e2d commit 90ca45d
Show file tree
Hide file tree
Showing 15 changed files with 646 additions and 31 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
- name: Run tests
run: |
tox -e setup
tox
env:
PLATFORM: ${{ matrix.os }}
111 changes: 111 additions & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: Pylint

on:
workflow_dispatch:
pull_request:
push:
branches: [ main ]

jobs:
pylint:

runs-on: ubuntu-20.04
defaults:
run:
shell: bash
outputs:
branch: ${{ steps.extract_branch.outputs.branch }}
rating: ${{ steps.analyze.outputs.rating }}
path: ${{ steps.analyze.outputs.path }}

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Extract base branch name
id: extract_branch
shell: bash
run: |
TMP_PULL_BASE_REF="${{ github.base_ref }}"
TMP_GITHUB_REF="${GITHUB_REF#refs/heads/}"
EXPORT_VALUE=""
if [ "${TMP_PULL_BASE_REF}" != "" ]
then
EXPORT_VALUE="${TMP_PULL_BASE_REF}"
else
EXPORT_VALUE="${TMP_GITHUB_REF}"
fi
echo "##[set-output name=branch;]${EXPORT_VALUE}"
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Install tox
run: |
python -m pip install --upgrade pip wheel
pip install tox tox-gh-actions
- name: Run pylint
id: analyze
env:
BADGE_PATH: badges/pylint-score.svg
run: |
rating=$(bash -c 'tox -e lint' | grep 'Your code has been rated at' | cut -f7 -d " ")
echo "Pylint score: ${rating}"
echo "##[set-output name=rating;]${rating}"
echo "##[set-output name=path;]${BADGE_PATH}"
badge:
# Only generate and publish if these conditions are met:
# - The previous job/analyze step ended successfully
# - At least one of these is true:
# - This is a push event and the push event is on branch 'master' or 'develop'
# Note: if this repo is personal (ie, not an org repo) then you can
# use the following to change the scope of the next 2 jobs
# instead of running on branch push as shown below:
# - This is a pull request event and the pull actor is the same as the repo owner
# if: ${{ ( github.event_name == 'pull_request' && github.actor == github.repository_owner ) || github.ref == 'refs/heads/master' }}
name: Generate badge image with pylint score
runs-on: ubuntu-20.04
needs: [pylint]
if: ${{ github.event_name == 'push' }}

steps:
- uses: actions/checkout@v2
with:
ref: badges
path: badges

# Use the output from the `analyze` step
- name: Create pylint badge
uses: emibcn/badge-action@v1
id: badge
with:
label: 'Pylint score'
status: ${{ needs.pylint.outputs.rating }}
color: 'green'
path: ${{ needs.pylint.outputs.path }}

- name: Commit badge
env:
BRANCH: ${{ needs.pylint.outputs.branch }}
FILE: 'pylint-score.svg'
working-directory: ./badges
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
mkdir -p "${BRANCH}"
mv "${FILE}" "${BRANCH}"
git add "${BRANCH}/${FILE}"
# Will give error if badge has not changed
git commit -m "Add/Update badge" || true
- name: Push badge commit
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: badges
directory: badges
91 changes: 91 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Release

on:
push:
# release on tag push
tags:
- '*'

jobs:
wheels:

runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash
env:
PYTHONIOENCODING: utf-8
strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04, macos-latest, windows-latest]
python-version: [3.7, 3.8, 3.9, '3.10']

steps:
- name: Set git crlf/eol
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- uses: actions/checkout@v3
with:
fetch-depth: 0

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

- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel
pip install tox tox-gh-actions
- name: Build dist pkgs
run: |
tox -e deploy
- name: Upload artifacts
if: matrix.python-version == 3.8 && runner.os != 'Windows'
uses: actions/upload-artifact@v2
with:
name: wheels
path: ./dist/*.whl

create_release:
name: Create Release
needs: [wheels]
runs-on: ubuntu-20.04

steps:
- name: Get version
id: get_version
run: |
echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
echo ${{ env.VERSION }}
- uses: actions/checkout@v2
with:
fetch-depth: 0

# download all artifacts to project dir
- uses: actions/download-artifact@v2

- name: Generate changes file
uses: sarnold/gitchangelog-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN}}

- name: Create release
id: create_release
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ env.VERSION }}
name: Release v${{ env.VERSION }}
body_path: CHANGES.md
draft: false
prerelease: false
files: |
wheels/yml*.whl
53 changes: 53 additions & 0 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Wheels

on:
workflow_dispatch:
pull_request:
push:
branches: [ main ]

jobs:
build:

runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash
env:
PYTHONIOENCODING: utf-8
strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04, macos-latest, windows-latest]
python-version: [3.7, 3.8, 3.9, '3.10']

steps:
- name: Set git crlf/eol
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- uses: actions/checkout@v3
with:
fetch-depth: 0

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

- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel
pip install tox tox-gh-actions
- name: Build dist pkgs
run: |
tox -e deploy,check
- name: Upload artifacts
if: matrix.python-version == 3.8 && runner.os == 'Linux'
uses: actions/upload-artifact@v2
with:
name: wheels
path: ./dist/*.whl
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ __pycache__/
*.py[cod]
*$py.class

# generated test files
# generated/user files
src/ymltoxml/_version.py
in.*
out.*
munch/
.*.yaml
*.xml

# C extensions
*.so
Expand Down
8 changes: 6 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

Some scripts, and eventually a package, to convert real-world XML_ files
to YAML_ and back again, preserving attributes and comments (with minor
corrections).
corrections). The default file encoding for both types is UTF-8 without
a Byte-Order Marker.

Developer workflow
==================
Expand All @@ -17,6 +18,10 @@ Both mavlink and pymavlink require a (host) GCC toolchain for full builds,
however, the basic workflow to generate the library headers requires only
Git, Python, and Tox.

.. _Tox: https://github.com/tox-dev/tox
.. _XML: https://en.wikipedia.org/wiki/Extensible_Markup_Language
.. _YAML: https://en.wikipedia.org/wiki/YAML


In-repo workflow with Tox
-------------------------
Expand All @@ -28,7 +33,6 @@ package manager, eg::
$ sudo apt-get update
$ sudo apt-get install tox

.. _Tox: https://github.com/tox-dev/tox

After cloning the repository, you can run the repo checks with the
``tox`` command. It will build a virtual python environment with
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fail_under = 50
show_missing = true

[tool.black]
line-length = 85
line-length = 90

[tool.pycln]
all = true
Expand All @@ -45,4 +45,4 @@ dirty = "{version}+d{build_date:%Y%m%d}"
distance-dirty = "{next_version}.dev{distance}"

[tool.versioningit.write]
file = "_version.py"
file = "src/ymltoxml/_version.py"
1 change: 1 addition & 0 deletions requirements-dev.txt → requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
xmltodict
munch
ruamel.yaml
PyYAML
26 changes: 22 additions & 4 deletions scripts/genyaml.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
from pathlib import Path

import ruamel.yaml
import xmltodict
from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO


class StrYAML(YAML):
"""
New API likes dumping straight to file/stdout, so we subclass and
create 'inefficient' custom string dumper. <shrug>
"""
def dump(self, data, stream=None, **kw):
inefficient = False
if stream is None:
inefficient = True
stream = StringIO()
YAML.dump(self, data, stream, **kw)
if inefficient:
return stream.getvalue()


with open('in.xml', 'r+b') as xfile:
payload = xmltodict.parse(xfile, process_comments=True)

yaml = ruamel.yaml.YAML()
yaml = StrYAML()
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.preserve_quotes = True # type: ignore

pfile = Path('out.yaml')
yaml.dump(payload, pfile)
res = yaml.dump(payload)

Path('out.yaml').write_text(res)
Loading

0 comments on commit 90ca45d

Please sign in to comment.