cc-costtrack 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cc_costtrack-0.0.1/.flake8 +3 -0
- cc_costtrack-0.0.1/.github/workflows/pr.yml +35 -0
- cc_costtrack-0.0.1/.github/workflows/publish.yml +26 -0
- cc_costtrack-0.0.1/.gitignore +207 -0
- cc_costtrack-0.0.1/.vscode/launch.json +31 -0
- cc_costtrack-0.0.1/.vscode/settings.json +14 -0
- cc_costtrack-0.0.1/LICENSE +21 -0
- cc_costtrack-0.0.1/PKG-INFO +166 -0
- cc_costtrack-0.0.1/README.md +139 -0
- cc_costtrack-0.0.1/pyproject.toml +62 -0
- cc_costtrack-0.0.1/setup.cfg +4 -0
- cc_costtrack-0.0.1/src/cc_costtrack.egg-info/PKG-INFO +166 -0
- cc_costtrack-0.0.1/src/cc_costtrack.egg-info/SOURCES.txt +17 -0
- cc_costtrack-0.0.1/src/cc_costtrack.egg-info/dependency_links.txt +1 -0
- cc_costtrack-0.0.1/src/cc_costtrack.egg-info/entry_points.txt +2 -0
- cc_costtrack-0.0.1/src/cc_costtrack.egg-info/requires.txt +13 -0
- cc_costtrack-0.0.1/src/cc_costtrack.egg-info/top_level.txt +1 -0
- cc_costtrack-0.0.1/src/cc_costtrack.py +125 -0
- cc_costtrack-0.0.1/tests/test_cc_costtrack.py +13 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: PR
|
|
2
|
+
on:
|
|
3
|
+
pull_request:
|
|
4
|
+
types:
|
|
5
|
+
- opened
|
|
6
|
+
- synchronize
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
name: Test
|
|
11
|
+
runs-on: ubuntu-24.04
|
|
12
|
+
|
|
13
|
+
steps:
|
|
14
|
+
- name: Checkout
|
|
15
|
+
uses: actions/checkout@v6
|
|
16
|
+
|
|
17
|
+
- name: Python venv
|
|
18
|
+
run: |
|
|
19
|
+
python3 -m venv .venv
|
|
20
|
+
source .venv/bin/activate \
|
|
21
|
+
&& pip install --upgrade pip \
|
|
22
|
+
&& pip install -e ".[test]"
|
|
23
|
+
|
|
24
|
+
- name: Tests
|
|
25
|
+
run: |
|
|
26
|
+
source .venv/bin/activate \
|
|
27
|
+
&& pytest --cov=cc_costtrack --cov-report=html
|
|
28
|
+
|
|
29
|
+
- name: Store coverage
|
|
30
|
+
uses: actions/upload-artifact@v7
|
|
31
|
+
with:
|
|
32
|
+
name: cov-reports
|
|
33
|
+
path: |
|
|
34
|
+
htmlcov
|
|
35
|
+
retention-days: 7
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
on:
|
|
3
|
+
release:
|
|
4
|
+
types:
|
|
5
|
+
- published
|
|
6
|
+
|
|
7
|
+
env:
|
|
8
|
+
TWINE_USERNAME: ${{ secrets.TWINE_USERNAME }}
|
|
9
|
+
TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD }}
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
package:
|
|
13
|
+
name: Publish
|
|
14
|
+
runs-on: ubuntu-24.04
|
|
15
|
+
steps:
|
|
16
|
+
- name: Checkout
|
|
17
|
+
uses: actions/checkout@v6
|
|
18
|
+
|
|
19
|
+
- name: Build and publish
|
|
20
|
+
run: |
|
|
21
|
+
python3 -m venv .venv
|
|
22
|
+
source .venv/bin/activate \
|
|
23
|
+
&& pip install --upgrade pip \
|
|
24
|
+
&& pip install -e ".[package,publish]" \
|
|
25
|
+
&& python -m build \
|
|
26
|
+
&& twine upload dist/*
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.2.0",
|
|
3
|
+
"configurations": [
|
|
4
|
+
{
|
|
5
|
+
"name": "Debug Tests",
|
|
6
|
+
"type": "debugpy",
|
|
7
|
+
"request": "launch",
|
|
8
|
+
"module": "pytest",
|
|
9
|
+
"args": [
|
|
10
|
+
"tests",
|
|
11
|
+
"-v"
|
|
12
|
+
],
|
|
13
|
+
"cwd": "${workspaceFolder}",
|
|
14
|
+
"python": "${workspaceFolder}/.venv/bin/python",
|
|
15
|
+
"justMyCode": false
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"name": "Debug Current Test File",
|
|
19
|
+
"type": "debugpy",
|
|
20
|
+
"request": "launch",
|
|
21
|
+
"module": "pytest",
|
|
22
|
+
"args": [
|
|
23
|
+
"${file}",
|
|
24
|
+
"-v"
|
|
25
|
+
],
|
|
26
|
+
"cwd": "${workspaceFolder}",
|
|
27
|
+
"python": "${workspaceFolder}/.venv/bin/python",
|
|
28
|
+
"justMyCode": false
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"editor.formatOnSave": true,
|
|
3
|
+
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
|
|
4
|
+
"python.testing.pytestEnabled": true,
|
|
5
|
+
"python.testing.pytestArgs": ["tests"],
|
|
6
|
+
"flake8.interpreter": [".venv/bin/python"],
|
|
7
|
+
"flake8.path": [".venv/bin/flake8"],
|
|
8
|
+
"flake8.args": ["--line-max-length", "88"],
|
|
9
|
+
"black-formatter.interpreter": [".venv/bin/python"],
|
|
10
|
+
"black-formatter.path": [".venv/bin/black"],
|
|
11
|
+
"black-formatter.args": ["--line-length", "88"],
|
|
12
|
+
"isort.path": [".venv/bin/isort"],
|
|
13
|
+
"isort.args": ["--profile", "black"]
|
|
14
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 J Al-Ansari
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cc-costtrack
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Cost tracking hook for ClaudeCode, to run at end of each session
|
|
5
|
+
Author: j
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/jalansari/cc-costtrack
|
|
8
|
+
Project-URL: Issues, https://github.com/jalansari/cc-costtrack/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.12
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Provides-Extra: test
|
|
17
|
+
Requires-Dist: pytest==9.0.3; extra == "test"
|
|
18
|
+
Requires-Dist: pytest-cov==7.1.0; extra == "test"
|
|
19
|
+
Requires-Dist: black==26.3.1; extra == "test"
|
|
20
|
+
Requires-Dist: flake8==7.3.0; extra == "test"
|
|
21
|
+
Requires-Dist: isort==8.0.1; extra == "test"
|
|
22
|
+
Provides-Extra: package
|
|
23
|
+
Requires-Dist: build==1.4.3; extra == "package"
|
|
24
|
+
Provides-Extra: publish
|
|
25
|
+
Requires-Dist: twine==6.2.0; extra == "publish"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# Claude Code Session End Hook for Cost Tracking
|
|
29
|
+
|
|
30
|
+
- [Claude Code Session End Hook for Cost Tracking](#claude-code-session-end-hook-for-cost-tracking)
|
|
31
|
+
- [Introduction](#introduction)
|
|
32
|
+
- [Installation](#installation)
|
|
33
|
+
- [Configuration](#configuration)
|
|
34
|
+
- [Development](#development)
|
|
35
|
+
- [Testing](#testing)
|
|
36
|
+
- [Build and publish](#build-and-publish)
|
|
37
|
+
- [Configure Publishing to Pypi](#configure-publishing-to-pypi)
|
|
38
|
+
- [Publishing to Test Pypi](#publishing-to-test-pypi)
|
|
39
|
+
- [Publishing to Pypi](#publishing-to-pypi)
|
|
40
|
+
|
|
41
|
+
## Introduction
|
|
42
|
+
|
|
43
|
+
Claude Code hook that should run at the end of sessions. The hook will log cost
|
|
44
|
+
and usage in a CSV file, in `~/.claude` folder.
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install cc-costtrack
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Configuration
|
|
53
|
+
|
|
54
|
+
Add to `~/.claude/settings.json`:
|
|
55
|
+
|
|
56
|
+
```json
|
|
57
|
+
{
|
|
58
|
+
"hooks": {
|
|
59
|
+
"SessionEnd": [
|
|
60
|
+
{
|
|
61
|
+
"matcher": "",
|
|
62
|
+
"hooks": [
|
|
63
|
+
{
|
|
64
|
+
"type": "command",
|
|
65
|
+
"command": "cc-costtrack"
|
|
66
|
+
}
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Development
|
|
75
|
+
|
|
76
|
+
Set up a virtual environment:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
python3 -m venv .venv
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Activate the virtual environment before running any of the steps below:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
source .venv/bin/activate
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Testing
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
pip install -e ".[test]"
|
|
92
|
+
pytest
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Build and publish
|
|
96
|
+
|
|
97
|
+
Build the distribution:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
pip install -e ".[package]"
|
|
101
|
+
python -m build
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
This creates `dist/` with `.tar.gz` and `.whl` files.
|
|
105
|
+
|
|
106
|
+
#### Configure Publishing to Pypi
|
|
107
|
+
|
|
108
|
+
Configure PyPI credentials in `~/.pypirc`:
|
|
109
|
+
|
|
110
|
+
```ini
|
|
111
|
+
[distutils]
|
|
112
|
+
index-servers =
|
|
113
|
+
pypi
|
|
114
|
+
testpypi
|
|
115
|
+
|
|
116
|
+
[pypi]
|
|
117
|
+
username = __token__
|
|
118
|
+
password = pypi-<your-api-token>
|
|
119
|
+
|
|
120
|
+
[testpypi]
|
|
121
|
+
repository = https://test.pypi.org/legacy/
|
|
122
|
+
username = __token__
|
|
123
|
+
password = pypi-<your-test-api-token>
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
API tokens can be created at:
|
|
127
|
+
|
|
128
|
+
- PyPI: https://pypi.org/manage/account/token/
|
|
129
|
+
- TestPyPI: https://test.pypi.org/manage/account/token/
|
|
130
|
+
|
|
131
|
+
Alternatively, set credentials via environment variables instead of `~/.pypirc`:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
export TWINE_USERNAME=__token__
|
|
135
|
+
export TWINE_PASSWORD=pypi-<your-api-token>
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
To target TestPyPI, override the repository URL:
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
export TWINE_REPOSITORY_URL=https://test.pypi.org/legacy/
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### Publishing to Test Pypi
|
|
145
|
+
|
|
146
|
+
To publish to TestPyPI first:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
pip install -e ".[package,publish]"
|
|
150
|
+
|
|
151
|
+
# Using ini file:
|
|
152
|
+
twine upload --repository testpypi dist/*
|
|
153
|
+
|
|
154
|
+
# Or, with environment variables:
|
|
155
|
+
export TWINE_REPOSITORY_URL=https://test.pypi.org/legacy/
|
|
156
|
+
twine upload dist/*
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### Publishing to Pypi
|
|
160
|
+
|
|
161
|
+
Publish to PyPI:
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
pip install -e ".[publish]"
|
|
165
|
+
twine upload dist/*
|
|
166
|
+
```
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# Claude Code Session End Hook for Cost Tracking
|
|
2
|
+
|
|
3
|
+
- [Claude Code Session End Hook for Cost Tracking](#claude-code-session-end-hook-for-cost-tracking)
|
|
4
|
+
- [Introduction](#introduction)
|
|
5
|
+
- [Installation](#installation)
|
|
6
|
+
- [Configuration](#configuration)
|
|
7
|
+
- [Development](#development)
|
|
8
|
+
- [Testing](#testing)
|
|
9
|
+
- [Build and publish](#build-and-publish)
|
|
10
|
+
- [Configure Publishing to Pypi](#configure-publishing-to-pypi)
|
|
11
|
+
- [Publishing to Test Pypi](#publishing-to-test-pypi)
|
|
12
|
+
- [Publishing to Pypi](#publishing-to-pypi)
|
|
13
|
+
|
|
14
|
+
## Introduction
|
|
15
|
+
|
|
16
|
+
Claude Code hook that should run at the end of sessions. The hook will log cost
|
|
17
|
+
and usage in a CSV file, in `~/.claude` folder.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install cc-costtrack
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Configuration
|
|
26
|
+
|
|
27
|
+
Add to `~/.claude/settings.json`:
|
|
28
|
+
|
|
29
|
+
```json
|
|
30
|
+
{
|
|
31
|
+
"hooks": {
|
|
32
|
+
"SessionEnd": [
|
|
33
|
+
{
|
|
34
|
+
"matcher": "",
|
|
35
|
+
"hooks": [
|
|
36
|
+
{
|
|
37
|
+
"type": "command",
|
|
38
|
+
"command": "cc-costtrack"
|
|
39
|
+
}
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Development
|
|
48
|
+
|
|
49
|
+
Set up a virtual environment:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
python3 -m venv .venv
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Activate the virtual environment before running any of the steps below:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
source .venv/bin/activate
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Testing
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install -e ".[test]"
|
|
65
|
+
pytest
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Build and publish
|
|
69
|
+
|
|
70
|
+
Build the distribution:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pip install -e ".[package]"
|
|
74
|
+
python -m build
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
This creates `dist/` with `.tar.gz` and `.whl` files.
|
|
78
|
+
|
|
79
|
+
#### Configure Publishing to Pypi
|
|
80
|
+
|
|
81
|
+
Configure PyPI credentials in `~/.pypirc`:
|
|
82
|
+
|
|
83
|
+
```ini
|
|
84
|
+
[distutils]
|
|
85
|
+
index-servers =
|
|
86
|
+
pypi
|
|
87
|
+
testpypi
|
|
88
|
+
|
|
89
|
+
[pypi]
|
|
90
|
+
username = __token__
|
|
91
|
+
password = pypi-<your-api-token>
|
|
92
|
+
|
|
93
|
+
[testpypi]
|
|
94
|
+
repository = https://test.pypi.org/legacy/
|
|
95
|
+
username = __token__
|
|
96
|
+
password = pypi-<your-test-api-token>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
API tokens can be created at:
|
|
100
|
+
|
|
101
|
+
- PyPI: https://pypi.org/manage/account/token/
|
|
102
|
+
- TestPyPI: https://test.pypi.org/manage/account/token/
|
|
103
|
+
|
|
104
|
+
Alternatively, set credentials via environment variables instead of `~/.pypirc`:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
export TWINE_USERNAME=__token__
|
|
108
|
+
export TWINE_PASSWORD=pypi-<your-api-token>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
To target TestPyPI, override the repository URL:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
export TWINE_REPOSITORY_URL=https://test.pypi.org/legacy/
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### Publishing to Test Pypi
|
|
118
|
+
|
|
119
|
+
To publish to TestPyPI first:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
pip install -e ".[package,publish]"
|
|
123
|
+
|
|
124
|
+
# Using ini file:
|
|
125
|
+
twine upload --repository testpypi dist/*
|
|
126
|
+
|
|
127
|
+
# Or, with environment variables:
|
|
128
|
+
export TWINE_REPOSITORY_URL=https://test.pypi.org/legacy/
|
|
129
|
+
twine upload dist/*
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
#### Publishing to Pypi
|
|
133
|
+
|
|
134
|
+
Publish to PyPI:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
pip install -e ".[publish]"
|
|
138
|
+
twine upload dist/*
|
|
139
|
+
```
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
[tool.black]
|
|
2
|
+
line-length = 88
|
|
3
|
+
include = '\.py?$'
|
|
4
|
+
exclude = '(\.git|__pycache__|\.pytest_cache|\.venv)'
|
|
5
|
+
|
|
6
|
+
[tool.isort]
|
|
7
|
+
profile = "black"
|
|
8
|
+
|
|
9
|
+
[build-system]
|
|
10
|
+
requires = [
|
|
11
|
+
"setuptools==82.0.1",
|
|
12
|
+
"setuptools-scm==10.0.5",
|
|
13
|
+
"wheel==0.46.3",
|
|
14
|
+
]
|
|
15
|
+
build-backend = "setuptools.build_meta"
|
|
16
|
+
|
|
17
|
+
[project]
|
|
18
|
+
name = "cc-costtrack"
|
|
19
|
+
authors = [{ name = "j" }]
|
|
20
|
+
description = "Cost tracking hook for ClaudeCode, to run at end of each session"
|
|
21
|
+
readme = "README.md"
|
|
22
|
+
requires-python = ">=3.12"
|
|
23
|
+
license = "MIT"
|
|
24
|
+
license-files = ["LICENSE"]
|
|
25
|
+
classifiers = [
|
|
26
|
+
"Programming Language :: Python :: 3",
|
|
27
|
+
"Programming Language :: Python :: 3.12",
|
|
28
|
+
"Environment :: Console",
|
|
29
|
+
"Operating System :: OS Independent",
|
|
30
|
+
]
|
|
31
|
+
dynamic = ["version"]
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
test = [
|
|
35
|
+
"pytest==9.0.3",
|
|
36
|
+
"pytest-cov==7.1.0",
|
|
37
|
+
"black==26.3.1",
|
|
38
|
+
"flake8==7.3.0",
|
|
39
|
+
"isort==8.0.1",
|
|
40
|
+
]
|
|
41
|
+
package = [
|
|
42
|
+
"build==1.4.3"
|
|
43
|
+
]
|
|
44
|
+
publish = [
|
|
45
|
+
"twine==6.2.0"
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[project.urls]
|
|
49
|
+
Homepage = "https://github.com/jalansari/cc-costtrack"
|
|
50
|
+
Issues = "https://github.com/jalansari/cc-costtrack/issues"
|
|
51
|
+
|
|
52
|
+
[project.scripts]
|
|
53
|
+
cc-costtrack = "cc_costtrack:main"
|
|
54
|
+
|
|
55
|
+
[tool.setuptools]
|
|
56
|
+
package-dir = {"" = "src"}
|
|
57
|
+
py-modules = ["cc_costtrack"]
|
|
58
|
+
|
|
59
|
+
[tool.pytest.ini_options]
|
|
60
|
+
testpaths = ["tests"]
|
|
61
|
+
|
|
62
|
+
[tool.setuptools_scm]
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cc-costtrack
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Cost tracking hook for ClaudeCode, to run at end of each session
|
|
5
|
+
Author: j
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/jalansari/cc-costtrack
|
|
8
|
+
Project-URL: Issues, https://github.com/jalansari/cc-costtrack/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.12
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Provides-Extra: test
|
|
17
|
+
Requires-Dist: pytest==9.0.3; extra == "test"
|
|
18
|
+
Requires-Dist: pytest-cov==7.1.0; extra == "test"
|
|
19
|
+
Requires-Dist: black==26.3.1; extra == "test"
|
|
20
|
+
Requires-Dist: flake8==7.3.0; extra == "test"
|
|
21
|
+
Requires-Dist: isort==8.0.1; extra == "test"
|
|
22
|
+
Provides-Extra: package
|
|
23
|
+
Requires-Dist: build==1.4.3; extra == "package"
|
|
24
|
+
Provides-Extra: publish
|
|
25
|
+
Requires-Dist: twine==6.2.0; extra == "publish"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# Claude Code Session End Hook for Cost Tracking
|
|
29
|
+
|
|
30
|
+
- [Claude Code Session End Hook for Cost Tracking](#claude-code-session-end-hook-for-cost-tracking)
|
|
31
|
+
- [Introduction](#introduction)
|
|
32
|
+
- [Installation](#installation)
|
|
33
|
+
- [Configuration](#configuration)
|
|
34
|
+
- [Development](#development)
|
|
35
|
+
- [Testing](#testing)
|
|
36
|
+
- [Build and publish](#build-and-publish)
|
|
37
|
+
- [Configure Publishing to Pypi](#configure-publishing-to-pypi)
|
|
38
|
+
- [Publishing to Test Pypi](#publishing-to-test-pypi)
|
|
39
|
+
- [Publishing to Pypi](#publishing-to-pypi)
|
|
40
|
+
|
|
41
|
+
## Introduction
|
|
42
|
+
|
|
43
|
+
Claude Code hook that should run at the end of sessions. The hook will log cost
|
|
44
|
+
and usage in a CSV file, in `~/.claude` folder.
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install cc-costtrack
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Configuration
|
|
53
|
+
|
|
54
|
+
Add to `~/.claude/settings.json`:
|
|
55
|
+
|
|
56
|
+
```json
|
|
57
|
+
{
|
|
58
|
+
"hooks": {
|
|
59
|
+
"SessionEnd": [
|
|
60
|
+
{
|
|
61
|
+
"matcher": "",
|
|
62
|
+
"hooks": [
|
|
63
|
+
{
|
|
64
|
+
"type": "command",
|
|
65
|
+
"command": "cc-costtrack"
|
|
66
|
+
}
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Development
|
|
75
|
+
|
|
76
|
+
Set up a virtual environment:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
python3 -m venv .venv
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Activate the virtual environment before running any of the steps below:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
source .venv/bin/activate
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Testing
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
pip install -e ".[test]"
|
|
92
|
+
pytest
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Build and publish
|
|
96
|
+
|
|
97
|
+
Build the distribution:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
pip install -e ".[package]"
|
|
101
|
+
python -m build
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
This creates `dist/` with `.tar.gz` and `.whl` files.
|
|
105
|
+
|
|
106
|
+
#### Configure Publishing to Pypi
|
|
107
|
+
|
|
108
|
+
Configure PyPI credentials in `~/.pypirc`:
|
|
109
|
+
|
|
110
|
+
```ini
|
|
111
|
+
[distutils]
|
|
112
|
+
index-servers =
|
|
113
|
+
pypi
|
|
114
|
+
testpypi
|
|
115
|
+
|
|
116
|
+
[pypi]
|
|
117
|
+
username = __token__
|
|
118
|
+
password = pypi-<your-api-token>
|
|
119
|
+
|
|
120
|
+
[testpypi]
|
|
121
|
+
repository = https://test.pypi.org/legacy/
|
|
122
|
+
username = __token__
|
|
123
|
+
password = pypi-<your-test-api-token>
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
API tokens can be created at:
|
|
127
|
+
|
|
128
|
+
- PyPI: https://pypi.org/manage/account/token/
|
|
129
|
+
- TestPyPI: https://test.pypi.org/manage/account/token/
|
|
130
|
+
|
|
131
|
+
Alternatively, set credentials via environment variables instead of `~/.pypirc`:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
export TWINE_USERNAME=__token__
|
|
135
|
+
export TWINE_PASSWORD=pypi-<your-api-token>
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
To target TestPyPI, override the repository URL:
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
export TWINE_REPOSITORY_URL=https://test.pypi.org/legacy/
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### Publishing to Test Pypi
|
|
145
|
+
|
|
146
|
+
To publish to TestPyPI first:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
pip install -e ".[package,publish]"
|
|
150
|
+
|
|
151
|
+
# Using ini file:
|
|
152
|
+
twine upload --repository testpypi dist/*
|
|
153
|
+
|
|
154
|
+
# Or, with environment variables:
|
|
155
|
+
export TWINE_REPOSITORY_URL=https://test.pypi.org/legacy/
|
|
156
|
+
twine upload dist/*
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### Publishing to Pypi
|
|
160
|
+
|
|
161
|
+
Publish to PyPI:
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
pip install -e ".[publish]"
|
|
165
|
+
twine upload dist/*
|
|
166
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
.flake8
|
|
2
|
+
.gitignore
|
|
3
|
+
LICENSE
|
|
4
|
+
README.md
|
|
5
|
+
pyproject.toml
|
|
6
|
+
.github/workflows/pr.yml
|
|
7
|
+
.github/workflows/publish.yml
|
|
8
|
+
.vscode/launch.json
|
|
9
|
+
.vscode/settings.json
|
|
10
|
+
src/cc_costtrack.py
|
|
11
|
+
src/cc_costtrack.egg-info/PKG-INFO
|
|
12
|
+
src/cc_costtrack.egg-info/SOURCES.txt
|
|
13
|
+
src/cc_costtrack.egg-info/dependency_links.txt
|
|
14
|
+
src/cc_costtrack.egg-info/entry_points.txt
|
|
15
|
+
src/cc_costtrack.egg-info/requires.txt
|
|
16
|
+
src/cc_costtrack.egg-info/top_level.txt
|
|
17
|
+
tests/test_cc_costtrack.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cc_costtrack
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Uses the status line cache (written every second by `cc-statusline`) to get
|
|
4
|
+
the session's total_cost_usd as reported by Claude Code.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import csv
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
import subprocess
|
|
11
|
+
import sys
|
|
12
|
+
import tempfile
|
|
13
|
+
from datetime import datetime, timezone
|
|
14
|
+
|
|
15
|
+
CSV_FILE = os.path.expanduser("~/.claude/claude-token-usage.csv")
|
|
16
|
+
CACHE_DIR = os.path.join(os.environ.get("TMPDIR", "/tmp"), f"claude_status_cache_{os.getuid()}")
|
|
17
|
+
|
|
18
|
+
STATUS_CACHE_FILE_SUFFIX = "_cache_stdin.json"
|
|
19
|
+
|
|
20
|
+
class CostTrackError(Exception):
|
|
21
|
+
def __init__(self, message: str):
|
|
22
|
+
super().__init__(f"cc-costtrack: {message}")
|
|
23
|
+
|
|
24
|
+
class CostTrack:
|
|
25
|
+
def __init__(self, data: dict):
|
|
26
|
+
self.session_id = data.get("session_id", "")
|
|
27
|
+
self.transcript_path = data.get("transcript_path", "")
|
|
28
|
+
self.cwd = data.get("cwd", "")
|
|
29
|
+
|
|
30
|
+
def initialize(self):
|
|
31
|
+
self.time_stamp = datetime.now(timezone.utc).isoformat()
|
|
32
|
+
self.__branch()
|
|
33
|
+
self.__cost()
|
|
34
|
+
self.__token_usage()
|
|
35
|
+
|
|
36
|
+
def __branch(self):
|
|
37
|
+
self.git_branch = ""
|
|
38
|
+
try:
|
|
39
|
+
self.git_branch = subprocess.check_output(
|
|
40
|
+
["git", "rev-parse", "--abbrev-ref", "HEAD"],
|
|
41
|
+
cwd=self.cwd or None, stderr=subprocess.DEVNULL, text=True).strip()
|
|
42
|
+
except Exception:
|
|
43
|
+
# If error, assume we're not in a git repo. Leave branch blank.
|
|
44
|
+
pass
|
|
45
|
+
|
|
46
|
+
def __cost(self):
|
|
47
|
+
self.cost_usd = 0.0
|
|
48
|
+
cache_path = os.path.join(CACHE_DIR, f"{self.session_id}{STATUS_CACHE_FILE_SUFFIX}")
|
|
49
|
+
try:
|
|
50
|
+
with open(cache_path) as fl:
|
|
51
|
+
cache_data = json.load(fl)
|
|
52
|
+
self.cost_usd = cache_data.get("cost", {}).get("total_cost_usd", 0.0)
|
|
53
|
+
except FileNotFoundError:
|
|
54
|
+
raise CostTrackError(f"cc-costtrack: status cache not found: {cache_path}")
|
|
55
|
+
except (json.JSONDecodeError, KeyError) as exc:
|
|
56
|
+
raise CostTrackError(f"cc-costtrack: failed to parse status cache {cache_path}: {exc}")
|
|
57
|
+
|
|
58
|
+
def __token_usage(self):
|
|
59
|
+
self.input_tokens = 0
|
|
60
|
+
self.output_tokens = 0
|
|
61
|
+
self.cache_creation = 0
|
|
62
|
+
self.cache_read = 0
|
|
63
|
+
self.__read_transcript()
|
|
64
|
+
|
|
65
|
+
def __read_transcript(self):
|
|
66
|
+
if not self.transcript_path:
|
|
67
|
+
raise CostTrackError("no transcript_path in hook input")
|
|
68
|
+
if not os.path.isfile(self.transcript_path):
|
|
69
|
+
raise CostTrackError(f"cc-costtrack: transcript not found: {self.transcript_path}")
|
|
70
|
+
with open(self.transcript_path) as fl_transcript:
|
|
71
|
+
for line in fl_transcript:
|
|
72
|
+
stripped = line.strip()
|
|
73
|
+
if not stripped:
|
|
74
|
+
continue
|
|
75
|
+
try:
|
|
76
|
+
entry = json.loads(stripped)
|
|
77
|
+
if entry.get("type") == "assistant":
|
|
78
|
+
usage = entry.get("message", {}).get("usage", {})
|
|
79
|
+
self.input_tokens += usage.get("input_tokens", 0)
|
|
80
|
+
self.output_tokens += usage.get("output_tokens", 0)
|
|
81
|
+
self.cache_creation += usage.get("cache_creation_input_tokens", 0)
|
|
82
|
+
self.cache_read += usage.get("cache_read_input_tokens", 0)
|
|
83
|
+
except json.JSONDecodeError:
|
|
84
|
+
continue
|
|
85
|
+
|
|
86
|
+
def write_csv(cost_track: CostTrack):
|
|
87
|
+
header = ["timestamp", "session_id", "cwd", "git_branch", "input_tokens", "output_tokens",
|
|
88
|
+
"cache_creation_tokens", "cache_read_tokens", "cost_usd"]
|
|
89
|
+
new_row = [cost_track.time_stamp, cost_track.session_id, cost_track.cwd, cost_track.git_branch,
|
|
90
|
+
cost_track.input_tokens, cost_track.output_tokens, cost_track.cache_creation, cost_track.cache_read,
|
|
91
|
+
f"{cost_track.cost_usd:.4f}"]
|
|
92
|
+
|
|
93
|
+
# Read existing rows, filtering out any with the same session_id
|
|
94
|
+
existing_rows = []
|
|
95
|
+
if os.path.isfile(CSV_FILE):
|
|
96
|
+
with open(CSV_FILE, "r", newline="") as fl_r:
|
|
97
|
+
reader = csv.reader(fl_r)
|
|
98
|
+
next(reader, None) # skip header
|
|
99
|
+
existing_rows = [row for row in reader if row[1] != cost_track.session_id]
|
|
100
|
+
|
|
101
|
+
csv_dir = os.path.dirname(CSV_FILE)
|
|
102
|
+
fd, tmp_path = tempfile.mkstemp(dir=csv_dir, suffix=".csv")
|
|
103
|
+
try:
|
|
104
|
+
with os.fdopen(fd, "w", newline="") as fl_w:
|
|
105
|
+
writer = csv.writer(fl_w)
|
|
106
|
+
writer.writerow(header)
|
|
107
|
+
writer.writerows(existing_rows)
|
|
108
|
+
writer.writerow(new_row)
|
|
109
|
+
os.replace(tmp_path, CSV_FILE)
|
|
110
|
+
except Exception:
|
|
111
|
+
os.unlink(tmp_path)
|
|
112
|
+
raise
|
|
113
|
+
|
|
114
|
+
def cost_log():
|
|
115
|
+
hook_input = json.load(sys.stdin)
|
|
116
|
+
cost_track = CostTrack(hook_input)
|
|
117
|
+
cost_track.initialize()
|
|
118
|
+
|
|
119
|
+
write_csv(cost_track)
|
|
120
|
+
|
|
121
|
+
def main():
|
|
122
|
+
cost_log()
|
|
123
|
+
|
|
124
|
+
if __name__ == "__main__":
|
|
125
|
+
main()
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from cc_costtrack import CostTrackError
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class TestCostTrackError:
|
|
5
|
+
def test_prefix_applied(self):
|
|
6
|
+
err = CostTrackError("something went wrong")
|
|
7
|
+
assert str(err) == "cc-costtrack: something went wrong"
|
|
8
|
+
|
|
9
|
+
def test_is_exception(self):
|
|
10
|
+
assert issubclass(CostTrackError, Exception)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# TODO - complete tests
|