From c3e63361dcdfe7c6d7603f2bc9c53718752b86af Mon Sep 17 00:00:00 2001 From: shubh Date: Mon, 25 Nov 2024 14:24:26 +0100 Subject: [PATCH 1/4] added pipeline to publish package on pypi --- .github/workflows/workflow.yml | 52 +++++++++++++++++++++++++ .gitignore | 5 ++- codegreen_core/tools/carbon_emission.py | 34 ++++++++-------- pyproject.toml | 2 +- 4 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/workflow.yml diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml new file mode 100644 index 0000000..c777bcb --- /dev/null +++ b/.github/workflows/workflow.yml @@ -0,0 +1,52 @@ +name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI + +on: + push: + branches: + - release + +jobs: + build: + name: Build distribution 📦 + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11.9" + - name: Install Poetry + run: | + python3 -m pip install poetry --user + - name: Install dependencies and build the project + run: | + poetry install --no-dev + poetry build + - name: Store the distribution packages + uses: actions/upload-artifact@v4 + with: + name: python-package-distributions + path: dist/ + + publish-to-pypi: + name: >- + Publish Python 🐍 distribution 📦 to PyPI + if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes + needs: + - build + runs-on: ubuntu-latest + environment: + name: pypi + url: https://pypi.org/p/codegreen-core + permissions: + id-token: write # IMPORTANT: mandatory for trusted publishing + + steps: + - name: Download all the dists + uses: actions/download-artifact@v4 + with: + name: python-package-distributions + path: dist/ + - name: Publish distribution 📦 to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 671e9cd..c864c2a 100644 --- a/.gitignore +++ b/.gitignore @@ -173,4 +173,7 @@ tests/data1 codegreen_core/models/files Dockerfile .vscode -poetry.lock \ No newline at end of file +poetry.lock + +codegreen_core/tools/test.py +codegreen_core/data/test.py diff --git a/codegreen_core/tools/carbon_emission.py b/codegreen_core/tools/carbon_emission.py index 859fd91..875328e 100644 --- a/codegreen_core/tools/carbon_emission.py +++ b/codegreen_core/tools/carbon_emission.py @@ -169,14 +169,9 @@ def compute_ce_from_energy(server, ci_data: pd.DataFrame): """ date_format = "%Y%m%d%H%M" # Year, Month, Day, Hour, Minute - server_defaults = { - "power_draw_core": 15.8, - "usage_factor_core": 1, - "power_draw_mem": 0.3725, - "power_usage_efficiency": 1.6, - } - server = server_defaults | server # set defaults if not provided + server = _add_server_defaults(server) # set defaults if not provided + # print(server) # to make sure startTimeUTC is in date format if not pd.api.types.is_datetime64_any_dtype(ci_data["startTimeUTC"]): ci_data["startTimeUTC"] = pd.to_datetime(ci_data["startTimeUTC"]) @@ -208,7 +203,7 @@ def compute_ce_from_energy(server, ci_data: pd.DataFrame): def _compute_ce_bulk(server, jobs): for job in jobs: - job.end_time = job["start_time"] + timedelta(minutes=job["runtime_minutes"]) + job['end_time'] = job["start_time"] + timedelta(minutes=job["runtime_minutes"]) min_start_date = min(job["start_time"] for job in jobs) max_end_date = max(job["end_time"] for job in jobs) @@ -222,19 +217,23 @@ def _compute_ce_bulk(server, jobs): & (energy_data["startTimeUTC"] <= job["end_time"]) ] job["emissions"], temp = compute_ce_from_energy( - filtered_energy, - server["number_core"], - server["memory_gb"], - server["power_draw_core"], - server["usage_factor_core"], - server["power_draw_mem"], - server["power_usage_efficiency"], + server, + filtered_energy ) return energy_data, jobs, min_start_date, max_end_date +def _add_server_defaults(server): + server_defaults = { + "power_draw_core": 15.8, + "usage_factor_core": 1, + "power_draw_mem": 0.3725, + "power_usage_efficiency": 1.6, + } + server = server_defaults | server # set defaults if not provided + return server def plot_ce_jobs(server, jobs): - energy_data, jobs, min_start_date, max_end_date = _compute_ce_bulk(server, jobs) + energy_data, jobs, min_start_date, max_end_date = _compute_ce_bulk(_add_server_defaults(server), jobs) Color = { "red": "#D6A99A", "green": "#99D19C", @@ -294,4 +293,5 @@ def plot_ce_jobs(server, jobs): # Add legend and show the plot fig.tight_layout() # plt.legend(loc='lower right') - plt.show() + plt.show(block=True) + # plt.pyplot.show() diff --git a/pyproject.toml b/pyproject.toml index 5f435da..0a1f0cb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "codegreen_core" -version = "0.5.0" +version = "0.0.1" description = "This package helps you become aware of the carbon footprint of your computation" authors = ["Anne Hartebrodt ","Shubh Vardhan Jain "] readme = "README.md" From bf95390477aeb4fcd5af580c1f5318734e845db1 Mon Sep 17 00:00:00 2001 From: shubh Date: Mon, 25 Nov 2024 14:30:56 +0100 Subject: [PATCH 2/4] workflow fix --- .github/workflows/workflow.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index c777bcb..ea329cf 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -1,4 +1,4 @@ -name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI +name: Publish Python 🐍 distribution 📦 to PyPI on: push: @@ -30,9 +30,7 @@ jobs: path: dist/ publish-to-pypi: - name: >- - Publish Python 🐍 distribution 📦 to PyPI - if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes + name: Publish Python 🐍 distribution 📦 to PyPI needs: - build runs-on: ubuntu-latest From 6170e3cdd086b0ae424277eb21d08d6ca805dac5 Mon Sep 17 00:00:00 2001 From: shubh Date: Mon, 25 Nov 2024 14:50:40 +0100 Subject: [PATCH 3/4] testing gh release --- .github/workflows/workflow.yml | 45 +++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index ea329cf..350b3e7 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -47,4 +47,47 @@ jobs: name: python-package-distributions path: dist/ - name: Publish distribution 📦 to PyPI - uses: pypa/gh-action-pypi-publish@release/v1 \ No newline at end of file + uses: pypa/gh-action-pypi-publish@release/v1 + + github-release: + name: >- + Sign the Python 🐍 distribution 📦 with Sigstore + and upload them to GitHub Release + needs: + - publish-to-pypi + runs-on: ubuntu-latest + + permissions: + contents: write # IMPORTANT: mandatory for making GitHub Releases + id-token: write # IMPORTANT: mandatory for sigstore + + steps: + - name: Download all the dists + uses: actions/download-artifact@v4 + with: + name: python-package-distributions + path: dist/ + - name: Sign the dists with Sigstore + uses: sigstore/gh-action-sigstore-python@v3.0.0 + with: + inputs: >- + ./dist/*.tar.gz + ./dist/*.whl + - name: Create GitHub Release + env: + GITHUB_TOKEN: ${{ github.token }} + run: >- + gh release create + '${{ github.ref_name }}' + --repo '${{ github.repository }}' + --notes "" + - name: Upload artifact signatures to GitHub Release + env: + GITHUB_TOKEN: ${{ github.token }} + # Upload to GitHub Release using the `gh` CLI. + # `dist/` contains the built packages, and the + # sigstore-produced signatures and certificates. + run: >- + gh release upload + '${{ github.ref_name }}' dist/** + --repo '${{ github.repository }}' \ No newline at end of file From 8672d52820e4d2b57c13e8ffe3223a804f9e5873 Mon Sep 17 00:00:00 2001 From: shubh Date: Mon, 25 Nov 2024 14:54:44 +0100 Subject: [PATCH 4/4] new version release for testing github release and pypi release --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 0a1f0cb..be6f930 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "codegreen_core" -version = "0.0.1" +version = "0.0.2" description = "This package helps you become aware of the carbon footprint of your computation" authors = ["Anne Hartebrodt ","Shubh Vardhan Jain "] readme = "README.md"