Skip to content

Commit 5656bb9

Browse files
authored
fix: add json to pyproject + run package verification on ci (#1120)
* fix: add nordic regions json to package * ci: run package verification before publishing
1 parent 31b4dac commit 5656bb9

5 files changed

Lines changed: 104 additions & 76 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: package-validation-reusable
2+
3+
on:
4+
workflow_call:
5+
6+
permissions:
7+
contents: read
8+
9+
env:
10+
MAIN_PYTHON_VERSION: 3.12
11+
12+
jobs:
13+
build-package:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
17+
- name: Install uv
18+
uses: astral-sh/setup-uv@caf0cab7a618c569241d31dcd442f54681755d39 # v3.2.4
19+
with:
20+
version: "latest"
21+
- name: Set up Python
22+
run: uv python install ${{ env.MAIN_PYTHON_VERSION }}
23+
- name: Check versions
24+
run: uv run python .github/pyproject_versions.py -c
25+
- name: Build pip package
26+
run: uv build
27+
- name: Archive Pypi artifacts
28+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
29+
with:
30+
name: pypi_dist
31+
path: dist
32+
33+
test-package-from-wheel:
34+
runs-on: ubuntu-24.04
35+
needs: [build-package]
36+
steps:
37+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
38+
with:
39+
sparse-checkout: |
40+
tests/
41+
pytest.ini
42+
sparse-checkout-cone-mode: false
43+
- name: Install uv
44+
uses: astral-sh/setup-uv@caf0cab7a618c569241d31dcd442f54681755d39 # v3.2.4
45+
with:
46+
version: "latest"
47+
- name: Set up Python
48+
run: uv python install ${{ env.MAIN_PYTHON_VERSION }}
49+
- name: Download built package
50+
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
51+
with:
52+
name: pypi_dist
53+
path: dist
54+
- name: Create clean virtual environment
55+
run: |
56+
uv venv .venv --python ${{ env.MAIN_PYTHON_VERSION }}
57+
echo "VIRTUAL_ENV=$PWD/.venv" >> $GITHUB_ENV
58+
echo "$PWD/.venv/bin" >> $GITHUB_PATH
59+
- name: Install package from wheel (without source)
60+
run: |
61+
# Install the wheel and test dependencies without the source code
62+
uv pip install dist/*.whl
63+
uv pip install pytest pytest-mock requests-mock responses pandas logfire
64+
- name: Test package integrity
65+
run: |
66+
# Run the package integrity tests to verify all data files are included
67+
uv run python -m pytest tests/test_package_integrity.py -v
68+
CODECARBON_ALLOW_MULTIPLE_RUNS=True pytest --ignore=tests/test_viz_data.py -vv -m 'not integ_test' tests/
69+
- name: Test CLI functionality
70+
run: |
71+
# Test that the CLI is functional
72+
codecarbon --help
73+
python -c "from codecarbon import EmissionsTracker; print('Package import successful')"

.github/workflows/package.yml

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,16 @@ name: package
22
permissions:
33
contents: read
44
on:
5+
pull_request:
6+
paths:
7+
- "codecarbon/**"
8+
- "pyproject.toml"
9+
branches: [master]
510
push:
611
paths:
712
- "codecarbon/**"
813
- "pyproject.toml"
914
branches: [master]
1015
jobs:
11-
12-
build-package:
13-
runs-on: ubuntu-latest
14-
steps:
15-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
16-
- name: Install uv
17-
uses: astral-sh/setup-uv@caf0cab7a618c569241d31dcd442f54681755d39 # v3.2.4
18-
with:
19-
version: "latest"
20-
- name: Set up Python
21-
run: uv python install 3.12
22-
- name: Check versions
23-
run: uv run python .github/pyproject_versions.py -o
24-
- name: Build pip package
25-
run: uv build
26-
- name: Archive Pypi artifacts
27-
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
28-
with:
29-
name: pypi_dist
30-
path: dist
31-
32-
test-package-from-wheel:
33-
runs-on: ubuntu-24.04
34-
needs: [build-package]
35-
steps:
36-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
37-
with:
38-
sparse-checkout: |
39-
tests/
40-
pytest.ini
41-
sparse-checkout-cone-mode: false
42-
- name: Install uv
43-
uses: astral-sh/setup-uv@caf0cab7a618c569241d31dcd442f54681755d39 # v3.2.4
44-
with:
45-
version: "latest"
46-
- name: Set up Python
47-
run: uv python install 3.12
48-
- name: Download built package
49-
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
50-
with:
51-
name: pypi_dist
52-
path: dist
53-
- name: Create clean virtual environment
54-
run: |
55-
uv venv .venv --python 3.12
56-
echo "VIRTUAL_ENV=$PWD/.venv" >> $GITHUB_ENV
57-
echo "$PWD/.venv/bin" >> $GITHUB_PATH
58-
- name: Install package from wheel (without source)
59-
run: |
60-
# Install the wheel and test dependencies without the source code
61-
uv pip install dist/*.whl
62-
uv pip install pytest pytest-mock requests-mock responses pandas logfire
63-
- name: Test package integrity
64-
run: |
65-
# Run the package integrity tests to verify all data files are included
66-
uv run python -m pytest tests/test_package_integrity.py -v
67-
CODECARBON_ALLOW_MULTIPLE_RUNS=True pytest --ignore=tests/test_viz_data.py -vv -m 'not integ_test' tests/
68-
- name: Test CLI functionality
69-
run: |
70-
# Test that the CLI is functional
71-
codecarbon --help
72-
python -c "from codecarbon import EmissionsTracker; print('✓ Package import successful')"
16+
validate-package:
17+
uses: ./.github/workflows/package-validation-reusable.yml

.github/workflows/python-publish.yml

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,24 @@ on:
1111
permissions:
1212
contents: read
1313
jobs:
14+
validate-package:
15+
uses: ./.github/workflows/package-validation-reusable.yml
16+
1417
deploy-pypi:
1518
runs-on: ubuntu-latest
19+
needs: [validate-package]
1620
# Specifying a GitHub environment is optional, but strongly encouraged
1721
environment: pypi
1822
permissions:
1923
# IMPORTANT: this permission is mandatory for Trusted Publishing
2024
id-token: write
2125
steps:
22-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
23-
- name: Install uv
24-
uses: astral-sh/setup-uv@caf0cab7a618c569241d31dcd442f54681755d39 # v3.2.4
25-
with:
26-
version: "latest"
27-
- name: Set up Python
28-
run: uv python install 3.12
29-
- name: Check versions
30-
run: |
31-
uv run python .github/pyproject_versions.py -c
32-
- name: Build package
33-
run: uv build
34-
- name: Publish package
35-
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # release/v1.13.0
26+
- name: Download validated package
27+
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
28+
with:
29+
name: pypi_dist # This is where validate-package uploads the pypi package
30+
path: dist
31+
- name: Publish package
32+
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # release/v1.13.0
33+
with:
34+
packages-dir: dist

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ codecarbon = [
6161
"data/canada_provinces.geojson",
6262
"data/private_infra/global_energy_mix.json",
6363
"data/private_infra/carbon_intensity_per_source.json",
64+
"data/private_infra/nordic_emissions.json",
6465
"data/private_infra/2016/usa_emissions.json",
6566
"data/private_infra/2023/canada_energy_mix.json",
6667
"viz/assets/car_icon.png",

tests/test_package_integrity.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
This test should be run against the installed package, not the source.
44
"""
55

6+
from importlib import resources as importlib_resources
7+
68
import pandas as pd
79
import pytest
810

@@ -72,6 +74,14 @@ def test_global_energy_mix_data_included():
7274
assert len(global_energy_data) > 0, "Global energy mix data should not be empty"
7375

7476

77+
def test_nordic_emissions_data_included():
78+
"""Test that nordic emissions data file is included in the package."""
79+
nordic_file = importlib_resources.files("codecarbon").joinpath(
80+
"data/private_infra/nordic_emissions.json"
81+
)
82+
assert nordic_file.is_file(), f"Nordic emissions file missing: {nordic_file}"
83+
84+
7585
def test_country_specific_data_access():
7686
"""Test that we can access country-specific data if available."""
7787
ds = DataSource()

0 commit comments

Comments
 (0)