state
napt.state
State tracking and version management for NAPT.
This module provides state persistence for tracking discovered application versions, ETags, and file metadata between runs. This enables:
- Efficient conditional downloads (HTTP 304 Not Modified)
- Version change detection
- Bandwidth optimization for scheduled workflows
The state file is a JSON file that stores:
- Discovered versions from vendors
- HTTP ETags and Last-Modified headers for conditional requests
- File paths and SHA-256 hashes for cached installers
- Last checked timestamps for monitoring
State tracking is enabled by default and can be disabled with --stateless flag.
Example
Basic usage:
from pathlib import Path
from napt.state import load_state, save_state
state = load_state(Path("state/versions.json"))
app_id = "napt-chrome"
cache = state.get("apps", {}).get(app_id)
state["apps"][app_id] = {
"url": "https://dl.google.com/chrome.msi",
"etag": 'W/"abc123"',
"sha256": "abc123...",
"known_version": "130.0.0"
}
save_state(state, Path("state/versions.json"))
StateTracker
Manages application state tracking with automatic persistence.
This class provides a high-level interface for loading, querying, and updating the state file. It handles file I/O, error recovery, and provides convenience methods for common operations.
Attributes:
| Name | Type | Description |
|---|---|---|
state_file |
Path to the JSON state file. |
|
state |
dict[str, Any]
|
In-memory state dictionary. |
Example
Basic usage:
Source code in napt/state/tracker.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
__init__
Initialize state tracker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_file
|
Path
|
Path to JSON state file. Created if doesn't exist. |
required |
load
Load state from file.
Creates default state structure if file doesn't exist. Handles corrupted files by creating backup and starting fresh.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Loaded state dictionary. |
Raises:
| Type | Description |
|---|---|
OSError
|
If file permissions prevent reading. |
Source code in napt/state/tracker.py
save
Save current state to file.
Updates metadata.last_updated timestamp automatically. Creates parent directories if needed.
Raises:
| Type | Description |
|---|---|
OSError
|
If file permissions prevent writing. |
Source code in napt/state/tracker.py
get_cache
Get cached information for a recipe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recipe_id
|
str
|
Recipe identifier (from recipe's 'id' field). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Cached data if available, None otherwise. |
Example
Retrieve cached information:
Source code in napt/state/tracker.py
update_cache
update_cache(recipe_id: str, url: str, sha256: str, etag: str | None = None, last_modified: str | None = None, known_version: str | None = None, strategy: str | None = None) -> None
Update cached information for a recipe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recipe_id
|
str
|
Recipe identifier. |
required |
url
|
str
|
Download URL for provenance tracking. For version-first strategies (url_pattern, api_github, api_json), this is the actual download URL from version_info. For file-first (url_download), this is source.url. |
required |
sha256
|
str
|
SHA-256 hash of file (for integrity checks). |
required |
etag
|
str | None
|
ETag header from download response. Used by url_download for HTTP 304 conditional requests. Saved but unused by version-first strategies. |
None
|
last_modified
|
str | None
|
Last-Modified header from download response. Used by url_download as fallback for conditional requests. Saved but unused by version-first. |
None
|
known_version
|
str | None
|
Version string. PRIMARY cache key for version-first strategies (compared to skip downloads). Informational only for url_download. |
None
|
strategy
|
str | None
|
Discovery strategy used (for debugging). |
None
|
Example
Update cache entry:
Note
Schema v2: Removed file_path, last_checked, and renamed version -> known_version.
Field usage differs by strategy type:
- Version-first: known_version is PRIMARY cache key, etag/last_modified unused
- File-first: etag/last_modified are PRIMARY cache keys, known_version informational
Filesystem is the source of truth; state is for optimization only.
Source code in napt/state/tracker.py
has_version_changed
Check if discovered version differs from cached known_version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recipe_id
|
str
|
Recipe identifier. |
required |
new_version
|
str
|
Newly discovered version. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if version changed or no cached version exists. |
Example
Check if version has changed:
Note
Uses 'known_version' field which is informational only. Real version should be extracted from filesystem during build.
Source code in napt/state/tracker.py
load_state
Load state from JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_file
|
Path
|
Path to JSON state file. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Loaded state dictionary. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If state file doesn't exist. |
JSONDecodeError
|
If file contains invalid JSON. |
OSError
|
If file cannot be read due to permissions. |
Example
Load state from file:
Source code in napt/state/tracker.py
save_state
Save state to JSON file with pretty-printing.
Creates parent directories if needed. Uses 2-space indentation and sorted keys for consistent diffs in version control.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict[str, Any]
|
State dictionary to save. |
required |
state_file
|
Path
|
Path to JSON state file. |
required |
Raises:
| Type | Description |
|---|---|
OSError
|
If file cannot be written due to permissions. |
Example
Save state to file:
Note
- Uses 2-space indentation for readability
- Sorts keys alphabetically for consistent diffs
- Adds trailing newline for git compatibility