Skip to content

Commit

Permalink
Merge pull request #18 from ministryofjustice/declarative-build
Browse files Browse the repository at this point in the history
Migrate to modern/declarative packaging
  • Loading branch information
ushkarev committed Nov 7, 2023
2 parents 5b7361f + 23bed9f commit 2ba827b
Show file tree
Hide file tree
Showing 18 changed files with 303 additions and 211 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: 3.x
- name: Install linting dependencies
run: pip install -U setuptools pip wheel -r requirements-lint.txt
- name: Update setuptools pip and wheel
run: pip install -U setuptools pip wheel
- name: Install linting requirements
run: pip install -r requirements-lint.txt
- name: Lint code
run: flake8 --verbose
9 changes: 6 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ jobs:
run: sudo apt-get install gettext
- name: Update setuptools pip and wheel
run: pip install -U setuptools pip wheel
- name: Install twine
run: pip install twine
- name: Install release requirements
run: pip install -r requirements-release.txt
- name: Build and release to PYPI
run: |
python setup.py compilemessages sdist bdist_wheel
python scripts/messages.py compile
python -m build
unzip -l dist/*.whl
tar tzf dist/*.tar.gz
twine upload --non-interactive dist/*
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ jobs:
run: sudo apt-get install gettext
- name: Update setuptools pip and wheel
run: pip install -U setuptools pip wheel
- name: Install testing requirements
run: pip install -r requirements-test.txt
- name: Run tests
run: python setup.py compilemessages test
run: |
pip install --editable .
python scripts/messages.py compile
python -m tests
26 changes: 21 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
.PHONY: help clean test test-all coverage release
.PHONY: help init clean test test-all coverage lint release

help:
@echo "Using make is entirely optional; these are simply shortcuts"
@echo "See README.rst for normal usage."
@echo ""
@echo "init - create virtual environment"
@echo "clean - remove all build and test artifacts"
@echo "test - run all tests using current python environment"
@echo "test-all - run all tests in all supported python environments"
@echo "coverage - check code coverage while running all tests using current python environment"
@echo "lint - check code style"
@echo "release - NOT NORMALLY USED; See README.rst for release process"

init:
[ -d venv ] || python -m venv venv
./venv/bin/pip install -U setuptools pip wheel
./venv/bin/pip install --editable .
@echo `./venv/bin/python --version` virtual environment installed. Activate it using '`. ./venv/bin/activate`'

clean:
rm -fr build/ dist/ .eggs/ .tox/ .coverage
find . -name '*.pyc' -exec rm -f {} +
find . -name '__pycache__' -exec rm -fr {} +

test: clean
python setup.py test
test:
pip install --editable .
pip install -r requirements-test.txt
python scripts/messages.py compile
python -m tests

test-all:
pip install --upgrade tox
Expand All @@ -27,7 +38,12 @@ coverage:
coverage run setup.py test
coverage report --show-missing

lint:
pip install -r requirements-lint.txt
flake8 --verbose

release: clean
pip install --upgrade twine
python setup.py compilemessages sdist bdist_wheel
pip install -r requirements-release.txt
python scripts/messages.py compile
python -m build
twine upload dist/*
26 changes: 17 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,33 @@ Development

Please report bugs and open pull requests on `GitHub`_.

Update cached bank holidays from GOV.UK using ``python setup.py updatecachedholidays``.
To work on changes to this library, it’s recommended to install it in editable mode into a virtual environment,
i.e. ``pip install --editable .``

Update translation files using ``python setup.py makemessages``, e.g. when updating the i18n module or when adding new languages.
Compile them using ``python setup.py compilemessages``; this is *required* before testing and distribution.
Update cached bank holidays from GOV.UK using ``python scripts/update-cache.py``.

Use ``python setup.py compilemessages test`` to run all tests locally.
Update translation files using ``python scripts/messages.py update``, e.g. when updating the i18n module or when adding new languages.
Compile them using ``python scripts/messages.py compile``; this is *required* before testing and distribution.
Updating and compiling translation files requires the gettext system package to be installed.

Use ``python -m tests`` to run all tests locally.
Alternatively, you can use ``tox`` if you have multiple python versions.

Distribute a new version to `PyPI`_ by:
[Only for GitHub team members] Distribute a new version to `PyPI`_ by:

- updating the ``VERSION`` tuple in ``govuk_bank_holidays``
- updating the ``VERSION`` tuple in ``govuk_bank_holidays/__init__.py``
- adding a note to the `History`_
- publishing a release on GitHub which triggers an upload to PyPI

Alternatively, run ``python setup.py compilemessages sdist bdist_wheel upload`` locally.
- publishing a release on GitHub which triggers an upload to PyPI;
alternatively, run ``python scripts/messages.py compile; python -m build; twine upload dist/*`` locally

History
-------

Unreleased
Migrated test, build and release processes away from deprecated setuptools commands.
Translation files are updated and compiled through scripts which are not included in distribution.
The ``BankHolidays`` class has a new ``ALL_DIVISIONS`` constant.

0.13
The library does not differ from 0.12.

Expand Down
1 change: 0 additions & 1 deletion govuk_bank_holidays/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
VERSION = (0, 13)
__version__ = '.'.join(map(str, VERSION))
__author__ = 'Ministry of Justice Digital & Technology'
1 change: 1 addition & 0 deletions govuk_bank_holidays/bank_holidays.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BankHolidays:
ENGLAND_AND_WALES = 'england-and-wales'
SCOTLAND = 'scotland'
NORTHERN_IRELAND = 'northern-ireland'
ALL_DIVISIONS = (ENGLAND_AND_WALES, SCOTLAND, NORTHERN_IRELAND)

@classmethod
def load_backup_data(cls):
Expand Down
139 changes: 0 additions & 139 deletions govuk_bank_holidays/setup_extensions.py

This file was deleted.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
2 changes: 2 additions & 0 deletions requirements-release.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
twine
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
responses
81 changes: 81 additions & 0 deletions scripts/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python
import argparse
import logging
import pathlib
import subprocess
import sys


def main():
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')

parser = argparse.ArgumentParser(description='Manage gettext localisation messages')
sub_parsers = parser.add_subparsers()
description = 'Update localisation message files from source code'
sub_parser = sub_parsers.add_parser('update', help=description, description=description)
sub_parser.set_defaults(command='update')
description = 'Compile gettext binary localisation message files'
sub_parser = sub_parsers.add_parser('compile', help=description, description=description)
sub_parser.set_defaults(command='compile')

args = parser.parse_args()
if not hasattr(args, 'command'):
parser.print_help()
sys.exit(1)

domain = 'messages'
source_files = ['govuk_bank_holidays/i18n.py']

root_path = pathlib.Path(__file__).parent.parent
locale_path = root_path / 'govuk_bank_holidays' / 'locale'
if not locale_path.is_dir():
logging.error('This script mst be run from the repository root')
sys.exit(1)

pot_name = f'{domain}.pot'
po_name = f'{domain}.po'
mo_name = f'{domain}.mo'

pot_path = locale_path / pot_name
locales = [
str(path)
for path in locale_path.iterdir()
if (locale_path / path / 'LC_MESSAGES' / po_name).is_file()
]

if args.command == 'update':
logging.info('Writing intermediate POT file')
xgettext = [
'xgettext',
'--default-domain', domain,
'--output', pot_path,
'--language', 'Python',
'--from-code', 'UTF-8',
'--no-wrap',
'--verbose',
]
subprocess.run(xgettext + source_files, check=True)

msgmerge = [
'msgmerge',
'--no-wrap',
'--previous',
'--update',
'--verbose',
]
for locale in locales:
logging.info('Writing PO file for %s locale', locale)
po_path = locale_path / locale / 'LC_MESSAGES' / po_name
subprocess.run(msgmerge + [po_path, pot_path], check=True)

if args.command == 'compile':
msgfmt = ['msgfmt', '--check', '--verbose']
for locale in locales:
logging.info('Compiling PO file for %s locale', locale)
po_path = locale_path / locale / 'LC_MESSAGES' / po_name
mo_path = locale_path / locale / 'LC_MESSAGES' / mo_name
subprocess.run(msgfmt + ['--output-file', mo_path, po_path], check=True)


if __name__ == '__main__':
main()
Loading

0 comments on commit 2ba827b

Please sign in to comment.