Skip to content

Commit 456da2c

Browse files
authored
Update packaging structure and workflows (#68)
1 parent 7035c2a commit 456da2c

17 files changed

+318
-153
lines changed

.github/workflows/main.yaml

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,46 @@ name: CI
22

33
on:
44
push:
5-
branches: "*"
5+
branches:
6+
- main
67
pull_request:
7-
branches: "*"
88

9-
jobs:
10-
lint:
11-
runs-on: ubuntu-latest
12-
steps:
13-
- uses: actions/[email protected]
14-
- uses: actions/[email protected]
15-
- uses: pre-commit/[email protected]
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
1612

13+
jobs:
1714
test:
1815
name: ${{ matrix.python-version }}-build
1916
runs-on: ubuntu-latest
17+
defaults:
18+
run:
19+
shell: bash -l {0}
2020
strategy:
21+
fail-fast: false
2122
matrix:
22-
python-version: [3.7]
23+
python-version: ["3.9", "3.10", "3.11", "3.12"]
2324
steps:
24-
- uses: actions/checkout@v2.4.0
25-
- name: Setup Python
26-
uses: actions/setup-[email protected]
25+
- uses: actions/checkout@v4
26+
- name: Install Conda environment from environment.yml
27+
uses: mamba-org/setup-micromamba@v1
2728
with:
28-
python-version: ${{ matrix.python-version }}
29-
architecture: x64
30-
- uses: actions/[email protected]
31-
with:
32-
path: ~/.cache/pip
33-
key: ${{ runner.os }}-pip-${{ hashFiles('**/dev-requirements.txt') }}
34-
restore-keys: |
35-
${{ runner.os }}-pip-
36-
- run: |
37-
python -m pip install -r dev-requirements.txt
38-
python -m pip list
39-
- name: Running Tests
29+
environment-name: carbonplan
30+
condarc: |
31+
channels:
32+
- conda-forge
33+
- nodefaults
34+
cache-downloads: false
35+
cache-environment: true
36+
create-args: >-
37+
python=${{ matrix.python-version }}
38+
- name: Install package
39+
run: |
40+
python -m pip install -e .[dev]
41+
- name: Conda list information
42+
run: |
43+
conda env list
44+
conda list
45+
- name: Run tests
4046
run: |
4147
python -m pytest --verbose

.github/workflows/pypi-release.yaml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
name: Build and Upload carbonplan to PyPI
2+
on:
3+
release:
4+
types:
5+
- published
6+
# Runs for pull requests should be disabled other than for testing purposes
7+
# pull_request:
8+
# branches:
9+
# - main
10+
11+
permissions:
12+
contents: read
13+
14+
jobs:
15+
build-artifacts:
16+
runs-on: ubuntu-latest
17+
if: github.repository == 'carbonplan/carbonplan-python'
18+
steps:
19+
- uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
- uses: actions/[email protected]
23+
name: Install Python
24+
with:
25+
python-version: "3.11"
26+
27+
- name: Install dependencies
28+
run: |
29+
python -m pip install --upgrade pip
30+
python -m pip install build twine
31+
git clean -xdf
32+
git restore -SW .
33+
34+
# This step is only necessary for testing purposes and for TestPyPI
35+
- name: Fix up version string for TestPyPI
36+
if: ${{ !startsWith(github.ref, 'refs/tags') }}
37+
run: |
38+
# Change setuptools-scm local_scheme to "no-local-version" so the
39+
# local part of the version isn't included, making the version string
40+
# compatible with PyPI.
41+
sed --in-place "s/node-and-date/no-local-version/g" pyproject.toml
42+
43+
- name: Build tarball and wheels
44+
run: |
45+
python -m build
46+
- name: Check built artifacts
47+
run: |
48+
python -m twine check --strict dist/*
49+
pwd
50+
if [ -f dist/carbonplan-0.0.0.tar.gz ]; then
51+
echo "❌ INVALID VERSION NUMBER"
52+
exit 1
53+
else
54+
echo "✅ Looks good"
55+
fi
56+
- uses: actions/upload-artifact@v4
57+
with:
58+
name: releases
59+
path: dist
60+
61+
test-built-dist:
62+
needs: build-artifacts
63+
runs-on: ubuntu-latest
64+
environment:
65+
name: release
66+
url: https://test.pypi.org/p/carbonplan
67+
permissions:
68+
id-token: write
69+
steps:
70+
- uses: actions/[email protected]
71+
name: Install Python
72+
with:
73+
python-version: "3.11"
74+
- uses: actions/download-artifact@v4
75+
with:
76+
name: releases
77+
path: dist
78+
- name: List contents of built dist
79+
run: |
80+
ls -ltrh
81+
ls -ltrh dist
82+
- name: Verify the built dist/wheel is valid
83+
run: |
84+
python -m pip install --upgrade pip
85+
python -m pip install dist/carbonplan*.whl
86+
python -c "import carbonplan; print(carbonplan.__version__)"
87+
- name: Publish package to TestPyPI
88+
uses: pypa/[email protected]
89+
with:
90+
repository-url: https://test.pypi.org/legacy/
91+
# verbose: true
92+
93+
upload-to-pypi:
94+
needs: test-built-dist
95+
if: github.event_name == 'release'
96+
runs-on: ubuntu-latest
97+
environment:
98+
name: release
99+
url: https://pypi.org/p/carbonplan
100+
permissions:
101+
id-token: write
102+
steps:
103+
- uses: actions/download-artifact@v4
104+
with:
105+
name: releases
106+
path: dist
107+
- name: Publish package to PyPI
108+
uses: pypa/[email protected]

.pre-commit-config.yaml

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,58 @@
11
exclude: ^(secrets/)
22

33
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v4.6.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-docstring-first
10+
- id: check-json
11+
- id: check-yaml
12+
- id: debug-statements
13+
- id: mixed-line-ending
14+
15+
- repo: https://github.com/asottile/pyupgrade
16+
rev: v3.15.2
17+
hooks:
18+
- id: pyupgrade
19+
args:
20+
- "--py38-plus"
421

5-
- repo: https://github.com/pre-commit/pre-commit-hooks
6-
rev: v4.4.0
22+
- repo: https://github.com/psf/black
23+
rev: 24.3.0
724
hooks:
8-
- id: trailing-whitespace
9-
- id: end-of-file-fixer
10-
- id: check-docstring-first
11-
- id: check-json
12-
- id: check-yaml
13-
- id: pretty-format-json
14-
args: ["--autofix", "--indent=2", "--no-sort-keys"]
25+
- id: black
26+
- id: black-jupyter
1527

16-
- repo: https://github.com/psf/black
17-
rev: 23.3.0
28+
- repo: https://github.com/keewis/blackdoc
29+
rev: v0.3.9
1830
hooks:
19-
- id: black
20-
args: ["--line-length", "100", "--skip-string-normalization"]
31+
- id: blackdoc
2132

22-
- repo: https://github.com/PyCQA/flake8
23-
rev: 6.0.0
33+
- repo: https://github.com/astral-sh/ruff-pre-commit
34+
rev: "v0.3.5"
2435
hooks:
25-
- id: flake8
36+
- id: ruff
37+
args: ["--fix"]
2638

27-
- repo: https://github.com/asottile/seed-isort-config
28-
rev: v2.2.0
39+
- repo: https://github.com/pre-commit/mirrors-prettier
40+
rev: v4.0.0-alpha.8
2941
hooks:
30-
- id: seed-isort-config
31-
- repo: https://github.com/PyCQA/isort
32-
rev: 5.12.0
42+
- id: prettier
43+
44+
- repo: https://github.com/kynan/nbstripout
45+
rev: 0.7.1
3346
hooks:
34-
- id: isort
47+
- id: nbstripout
3548

36-
- repo: https://github.com/deathbeds/prenotebook
37-
rev: f5bdb72a400f1a56fe88109936c83aa12cc349fa
49+
- repo: https://github.com/pre-commit/mirrors-prettier
50+
rev: "v4.0.0-alpha.8"
3851
hooks:
39-
- id: prenotebook
40-
args: ["--line-width", "100", "--no-verify-order"]
52+
- id: prettier
53+
name: prettier-markdown
54+
entry: prettier --write --parser mdx
55+
files: "\\.(\
56+
|md|markdown|mdown|mkdn\
57+
|mdx\
58+
)$"

carbonplan/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from pkg_resources import DistributionNotFound, get_distribution
1+
from importlib.metadata import PackageNotFoundError as _PackageNotFoundError
2+
from importlib.metadata import version as _version
23

34
try:
4-
version = get_distribution(__name__).version
5-
except DistributionNotFound: # pragma: no cover
6-
version = '0.0.0' # pragma: no cover
5+
version = _version(__name__)
6+
except _PackageNotFoundError:
7+
# package is not installed
8+
version = "unknown"
79
__version__ = version

carbonplan/data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
try:
55
with warnings.catch_warnings():
6-
warnings.filterwarnings('ignore', category=DeprecationWarning, module='intake')
7-
warnings.filterwarnings('ignore', category=PendingDeprecationWarning, module='intake')
6+
warnings.filterwarnings("ignore", category=DeprecationWarning, module="intake")
7+
warnings.filterwarnings("ignore", category=PendingDeprecationWarning, module="intake")
88
from carbonplan_data import *
99
except ImportError as e:
1010
msg = (
1111
"CarbonPlan's Data package is not installed.\n\n"
12-
'Please install the carbonplan-data package:\n\n'
12+
"Please install the carbonplan-data package:\n\n"
1313
' python -m pip install "carbonplan-data"'
1414
)
1515
raise ImportError(msg) from e

carbonplan/forests_offsets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
except ImportError as e:
55
msg = (
66
"CarbonPlan's Forests Offsets package is not installed.\n\n"
7-
'Please install the carbonplan-forest-offsets package:\n\n'
7+
"Please install the carbonplan-forest-offsets package:\n\n"
88
' python -m pip install "carbonplan-forest-risks"'
99
)
1010
raise ImportError(msg) from e

carbonplan/forests_risks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
except ImportError as e:
55
msg = (
66
"CarbonPlan's Forests Risks package is not installed.\n\n"
7-
'Please install the carbonplan-forests package:\n\n'
7+
"Please install the carbonplan-forests package:\n\n"
88
' python -m pip install "carbonplan-forest-risks"'
99
)
1010
raise ImportError(msg) from e

carbonplan/styles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
except ImportError as e:
55
msg = (
66
"CarbonPlan's Styles package is not installed.\n\n"
7-
'Please install the carbonplan-styles package:\n\n'
7+
"Please install the carbonplan-styles package:\n\n"
88
' python -m pip install "carbonplan-styles"'
99
)
1010
raise ImportError(msg) from e

carbonplan/tests/test_metapackage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55

66
@pytest.mark.parametrize(
7-
'mod',
7+
"mod",
88
[
9-
'carbonplan',
10-
'carbonplan.data',
11-
'carbonplan.styles',
9+
"carbonplan",
10+
"carbonplan.data",
11+
"carbonplan.styles",
1212
],
1313
)
1414
def test_import_submodules(mod):

carbonplan/trace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
except ImportError as e:
55
msg = (
66
"CarbonPlan's Trace package is not installed.\n\n"
7-
'Please install the carbonplan-trace package:\n\n'
7+
"Please install the carbonplan-trace package:\n\n"
88
' python -m pip install "carbonplan-trace"'
99
)
1010
raise ImportError(msg) from e

0 commit comments

Comments
 (0)