Skip to content

Commit

Permalink
ci(.github): revamp workflows and add hatch
Browse files Browse the repository at this point in the history
Improve the GitHub action workflows and start to use hatch and hatchling as build backend.
  • Loading branch information
yeisonvargasf committed Oct 20, 2023
1 parent 5b9cb59 commit 12fddb6
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 136 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: CI

on:
push:
branches:
- 'main'
pull_request:
types: [opened, synchronize, reopened]

jobs:
test:
name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
python-version: [ "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v4

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

- name: Install Hatch
run: pip install --upgrade hatch

- name: Run tests and track code coverage
run: hatch run test:cov

# TODO: Enforce quality, security and style checks.

bump:
needs: test
name: Bump version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade hatch
- name: Bump version
if: github.ref == 'refs/heads/main'
run: hatch run bump

- name: Local bump
if: github.ref != 'refs/heads/main'
run: hatch run local-bump

build:
needs: bump
name: Build ${{ github.ref }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade hatch
- name: Build package
run: hatch build
- uses: actions/upload-artifact@v3
with:
name: "dist-${{ github.ref }}"
path: dist/*
if-no-files-found: error
retention-days: 7
26 changes: 0 additions & 26 deletions .github/workflows/code_analysis.yml

This file was deleted.

63 changes: 0 additions & 63 deletions .github/workflows/main.yml

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: PyPi Release

on:
workflow_run:
workflows: [CI]
branches: [main]
types:
- completed

jobs:
pypi-publish:
name: upload release to PyPI
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
environment: release
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v3
with:
name: dist-refs/heads/main
path: dist
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
print-hash: true
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repos:
- hooks:
- id: commitizen
- id: commitizen-branch
stages:
- push
repo: https://github.com/commitizen-tools/commitizen
rev: v3.12.0
36 changes: 36 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import sys
import pytest


def pytest_collection_modifyitems(items, config):
for item in items:
if not any(item.iter_markers()):
item.add_marker("basic")

markexpr = config.getoption("markexpr", 'False')

expr = "basic"

if markexpr:
expr += f" or ({markexpr})"

config.option.markexpr = expr


def pytest_deselected(items):
if not items:
return
config = items[0].session.config
config.deselected = items


def pytest_terminal_summary(terminalreporter, exitstatus, config):
reports = terminalreporter.getreports('')
content = os.linesep.join(text for report in reports for secname, text in report.sections)
deselected = getattr(config, "deselected", [])
if deselected:
terminalreporter.ensure_newline()
terminalreporter.section('Deselected tests', sep='-', yellow=True, bold=True)
content = os.linesep.join(item.nodeid for item in deselected)
terminalreporter.line(content)
6 changes: 3 additions & 3 deletions dparse/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Dependency:

def __init__(self, name, specs, line, source="pypi", meta={}, extras=[],
line_numbers=None, index_server=None, hashes=(),
dependency_type=None, section=None):
dependency_type=None, sections=None):
"""
:param name:
Expand All @@ -35,7 +35,7 @@ def __init__(self, name, specs, line, source="pypi", meta={}, extras=[],
self.hashes = hashes
self.dependency_type = dependency_type
self.extras = extras
self.section = section
self.sections = sections

def __str__(self): # pragma: no cover
"""
Expand Down Expand Up @@ -64,7 +64,7 @@ def serialize(self):
"hashes": self.hashes,
"dependency_type": self.dependency_type,
"extras": self.extras,
"section": self.section
"sections": self.sections
}

@classmethod
Expand Down
60 changes: 33 additions & 27 deletions dparse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def parse(self):
name=name, specs=SpecifierSet(specs),
dependency_type=filetypes.pipfile,
line=''.join([name, specs]),
section=package_type
sections=[package_type]
)
)
except (tomllib.TOMLDecodeError, IndexError):
Expand Down Expand Up @@ -401,7 +401,7 @@ def parse(self):
dependency_type=filetypes.pipfile_lock,
hashes=hashes,
line=''.join([name, specs]),
section=package_type
sections=[package_type]
)
)
except ValueError as e:
Expand Down Expand Up @@ -441,36 +441,42 @@ def parse(self):
Parse a poetry.lock
"""
try:
data = tomllib.loads(self.obj.content)
pkg_key = 'package'
if data:
try:
from poetry.packages.locker import Locker
from pathlib import Path

lock_path = Path(self.obj.path)

repository = Locker(lock_path, {}).locked_repository()
for pkg in repository.packages:
self.obj.dependencies.append(
Dependency(
name=pkg.name, specs=SpecifierSet(f"=={pkg.version.text}"),
dependency_type=filetypes.poetry_lock,
line=pkg.to_dependency().to_pep_508(),
sections=list(pkg.dependency_group_names())
)
)
except Exception:
try:
data = tomllib.loads(self.obj.content)
pkg_key = 'package'
if data:
dependencies = data[pkg_key]
except KeyError:
raise KeyError(
"Poetry lock file is missing the package section")

for dep in dependencies:
try:
for dep in dependencies:
name = dep['name']
spec = "=={version}".format(
version=Version(dep['version']))
section = dep.get('category')
except KeyError:
raise KeyError("Malformed poetry lock file")
except InvalidVersion:
continue

self.obj.dependencies.append(
Dependency(
name=name, specs=SpecifierSet(spec),
dependency_type=filetypes.poetry_lock,
line=''.join([name, spec]),
section=section
sections = [dep['category']] if "category" in dep else []
self.obj.dependencies.append(
Dependency(
name=name, specs=SpecifierSet(spec),
dependency_type=filetypes.poetry_lock,
line=''.join([name, spec]),
sections=sections
)
)
)
except (tomllib.TOMLDecodeError, IndexError) as e:
raise MalformedDependencyFileError(info=str(e))
except Exception as e:
raise MalformedDependencyFileError(info=str(e))


class PyprojectTomlParser(Parser):
Expand Down
1 change: 1 addition & 0 deletions dparse/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def update(cls, content, dependency, version, spec="==", hashes=()):
"Updating a Pipfile requires the pipenv extra to be installed."
" Install it with pip install dparse[pipenv]")
pipfile = tempfile.NamedTemporaryFile(delete=False)
pipfile.close()
p = Project(chdir=False)
p.write_toml(data=data, path=pipfile.name)
data = open(pipfile.name).read()
Expand Down

0 comments on commit 12fddb6

Please sign in to comment.