b2c_tooling_sdk.operations.code
Code deployment operations for B2C Commerce.
Mirrors src/operations/code/index.ts. Provides functions for managing cartridge code versions on B2C Commerce instances via WebDAV and OCAPI/SCAPI.
Cartridge discovery:
find_cartridges- Find cartridges by.projectfiles.
Code versions:
list_code_versions- List all code versions on an instance.get_active_code_version- Get the currently active code version.activate_code_version- Activate a code version.reload_code_version- Reload (re-activate) a code version.delete_code_version- Delete a code version.create_code_version- Create a new code version.
Deployment:
find_and_deploy_cartridges- Find and deploy cartridges to an instance.upload_cartridges- Low-level cartridge upload.delete_cartridges- Low-level cartridge deletion.watch_cartridges- Watch and sync file changes.
Download:
download_cartridges- Download cartridges from an instance.
Classes
CartridgeMapping
class CartridgeMappingA discovered cartridge in the local filesystem.
Fields
| Name | Type |
|---|---|
name | str |
src | str |
dest | str |
CodeVersion
class CodeVersion(BaseModel)<p>Document representing a code version</p>
Fields
| Name | Type | Default |
|---|---|---|
activation_time | AwareDatetime | None | Field(None, description='The code version activation time.') |
active | bool | None | |
cartridges | list[str] | None | |
compatibility_mode | str | None | |
id | str | None | Field(None, description='The code version id.') |
last_modification_time | AwareDatetime | None | |
rollback | bool | None | |
total_size | int | None | |
web_dav_url | str | None |
CodeVersionActivationResult
class CodeVersionActivationResultResult of requesting activation of a code version.
Fields
| Name | Type |
|---|---|
already_active | bool |
CodeVersionInfo
class CodeVersionInfoA code version, normalized across the OCAPI and SCAPI representations.
activation_time/last_modification_time are kept as ISO-8601 strings (matching the TypeScript string fields) even though the underlying generated Pydantic models parse them as datetime — each backend converts via .isoformat() at the mapping boundary.
Fields
| Name | Type | Default |
|---|---|---|
id | str | |
active | bool | None | None |
cartridges | list[str] | None | None |
compatibility_mode | str | None | None |
activation_time | str | None | None |
last_modification_time | str | None | None |
rollback | bool | None | None |
total_size | int | None | None |
web_dav_url | str | None | None |
raw | Any | None |
CodeVersionResult
class CodeVersionResult(BaseModel)<p>Result document containing an array of code versions.</p>
Fields
| Name | Type | Default |
|---|---|---|
count | int | None | Field(None, description='The number of returned documents.') |
data | list[CodeVersion] | None | Field(None, description='The array of code versions') |
next | str | None | Field(None, description='The URL of the next result page.') |
previous | str | None | |
select | str | None | |
start | int | None | |
total | int | None | Field(None, description='The total number of documents.') |
DeployOptions
class DeployOptions(FindCartridgesOptions)Options for deploying cartridges.
Fields
| Name | Type | Default |
|---|---|---|
scripts_backend | ScriptsBackend | None | None |
activate | bool | False |
reload | bool | False |
delete | bool | False |
on_progress | Callable[[UploadProgressInfo], None] | None | None |
DeployResult
class DeployResultResult of a cartridge deployment.
Fields
| Name | Type |
|---|---|
cartridges | list[CartridgeMapping] |
code_version | str |
activated | bool |
reloaded | bool |
DownloadOptions
class DownloadOptionsOptions for downloading cartridges.
Fields
| Name | Type | Default |
|---|---|---|
scripts_backend | ScriptsBackend | None | None |
include | list[str] | field(default_factory=list) |
exclude | list[str] | field(default_factory=list) |
mirror | dict[str, str] | None | None |
on_progress | Callable[[DownloadProgressInfo], None] | None | None |
DownloadProgressInfo
class DownloadProgressInfoProgress info passed to the download on_progress callback.
Fields
| Name | Type |
|---|---|
phase | DownloadPhase |
elapsed_seconds | int |
DownloadResult
class DownloadResultResult of a cartridge download.
Fields
| Name | Type |
|---|---|
cartridges | list[str] |
code_version | str |
output_directory | str |
FileChange
class FileChangeA file to upload or delete, with source and destination paths.
Fields
| Name | Type |
|---|---|
src | str |
dest | str |
FindCartridgesOptions
class FindCartridgesOptionsOptions for find_cartridges.
Fields
| Name | Type | Default |
|---|---|---|
include | list[str] | field(default_factory=list) |
exclude | list[str] | field(default_factory=list) |
max_depth | int | None | None |
first_match_only | bool | False |
OcapiScriptsBackend
class OcapiScriptsBackendCode-version operations against the legacy OCAPI Data API.
Fields
| Name | Type |
|---|---|
name | Literal['ocapi'] |
ScapiScriptsBackend
class ScapiScriptsBackendCode-version operations against the SCAPI Scripts DX API.
Fields
| Name | Type |
|---|---|
name | Literal['scapi'] |
ScriptsBackend
class ScriptsBackend(BackendBase, Protocol)Backend-agnostic interface for code-version operations.
Implemented by OcapiScriptsBackend and ScapiScriptsBackend.
UploadFilesOptions
class UploadFilesOptionsCallbacks for file upload/delete operations.
Fields
| Name | Type | Default |
|---|---|---|
on_upload | Callable[[list[str]], None] | None | None |
on_delete | Callable[[list[str]], None] | None | None |
on_error | Callable[[Exception], None] | None | None |
UploadOptions
class UploadOptionsOptions for upload progress reporting.
Fields
| Name | Type | Default |
|---|---|---|
on_progress | Callable[[UploadProgressInfo], None] | None | None |
UploadProgressInfo
class UploadProgressInfoProgress info passed to the upload on_progress callback.
Fields
| Name | Type |
|---|---|
phase | UploadPhase |
elapsed_seconds | int |
WatchOptions
class WatchOptions(FindCartridgesOptions)Options for watching cartridges.
Fields
| Name | Type | Default |
|---|---|---|
scripts_backend | ScriptsBackend | None | None |
debounce_time_ms | int | _DEFAULT_DEBOUNCE_MS |
on_upload | Callable[[list[str]], None] | None | None |
on_delete | Callable[[list[str]], None] | None | None |
on_error | Callable[[Exception], None] | None | None |
poll_interval_seconds | float | _DEFAULT_POLL_INTERVAL_SECONDS |
WatchResult
class WatchResultResult of starting a watcher.
Fields
| Name | Type |
|---|---|
cartridges | list[CartridgeMapping] |
code_version | str |
stop | Callable[[], Awaitable[None]] |
Functions
activate_code_version
async def activate_code_version(instance: B2CInstance, code_version_id: str) -> CodeVersionActivationResultActivate a code version.
Idempotent: if OCAPI reports the code version is already active, this returns already_active=True rather than raising.
create_code_version
async def create_code_version(instance: B2CInstance, code_version_id: str) -> NoneCreate a new (empty) code version.
create_scripts_backend
def create_scripts_backend(config: ScriptsBackendConfig) -> ScriptsBackendCreate a code-version backend, choosing SCAPI or OCAPI per config.
Delegates to create_dual_backend.
delete_cartridges
async def delete_cartridges(instance: B2CInstance, cartridges: list[CartridgeMapping]) -> NoneDelete cartridges from an instance via WebDAV.
Low-level function that deletes cartridge directories from the specified code version. Errors are silently ignored for cartridges that don't exist.
Requires instance.config.code_version to be set.
Raises
RuntimeError— if the instance has no code version configured.
delete_code_version
async def delete_code_version(instance: B2CInstance, code_version_id: str) -> NoneDelete a code version.
download_cartridges
async def download_cartridges(instance: B2CInstance, output_directory: str, options: DownloadOptions | None = None) -> DownloadResultDownload cartridges from an instance via WebDAV.
When include specifies cartridges, each is downloaded individually using per-cartridge server-side zipping for efficiency. When downloading all cartridges (no include filter), the entire code version is zipped at once.
If instance.config.code_version is not set, attempts to discover the active code version via the scripts backend. If that also fails, raises.
download_single_cartridge
async def download_single_cartridge(instance: B2CInstance, code_version: str, cartridge_name: str, output_path: str, on_progress: Callable[[DownloadProgressInfo], None] | None = None) -> NoneDownload a single cartridge from an instance via WebDAV.
More efficient than downloading the entire code version when only one cartridge is needed, since it ZIPs only the cartridge subdirectory server-side.
file_to_cartridge_path
def file_to_cartridge_path(absolute_path: str, cartridges: list[CartridgeMapping]) -> FileChange | NoneMap an absolute file path to its cartridge-relative destination.
Parameters
| Name | Type | Description |
|---|---|---|
absolute_path | str | The absolute path to a file. |
cartridges | list[CartridgeMapping] | The list of discovered cartridge mappings. |
Returns: The file change with src/dest, or None if the path is not inside any cartridge.
find_and_deploy_cartridges
async def find_and_deploy_cartridges(instance: B2CInstance, directory: str, options: DeployOptions | None = None) -> DeployResultFind and deploy cartridges from a directory to an instance.
High-level function that orchestrates the deployment process:
- Finds cartridges in the specified directory (by
.projectfiles). - Applies include/exclude filters.
- Optionally deletes existing cartridges first.
- Creates a zip archive and uploads via WebDAV.
- Optionally activates or reloads the code version.
Requires instance.config.code_version to be set.
Raises
RuntimeError— if code version is not set, no cartridges are found, or deployment fails.
find_cartridges
def find_cartridges(directory: str | None = None, options: FindCartridgesOptions | None = None) -> list[CartridgeMapping]Find cartridges recursively in a directory.
Cartridges are identified by the presence of a .project file (Eclipse project marker commonly used in SFCC development).
Parameters
| Name | Type | Description |
|---|---|---|
directory | str | None | Directory to search for cartridges (defaults to the current working directory). |
options | FindCartridgesOptions | None | Filter options for including/excluding cartridges. |
Returns: List of discovered cartridge mappings.
get_active_code_version
async def get_active_code_version(instance: B2CInstance) -> CodeVersion | NoneReturn the currently active code version, or None if none is active.
list_code_versions
async def list_code_versions(instance: B2CInstance) -> list[CodeVersion]List all code versions on the instance.
reload_code_version
async def reload_code_version(backend: ScriptsBackend, code_version_id: str | None = None) -> NoneReload (re-activate) a code version using a toggle-activate technique.
Activates an alternate version, then re-activates the target. This forces the instance to reload the code (rebuild caches, re-register custom APIs, etc.). Works on top of any ScriptsBackend since it only uses list/activate primitives.
Parameters
| Name | Type | Description |
|---|---|---|
code_version_id | str | None | Code version to reload (defaults to the current active version). |
Raises
RuntimeError— if no code version is specified and none is active, or if the target is already active and no alternate code version exists to toggle through.
upload_cartridges
async def upload_cartridges(instance: B2CInstance, cartridges: list[CartridgeMapping], options: UploadOptions | None = None) -> NoneUpload cartridges to an instance via WebDAV.
Low-level upload function that:
- Creates a zip archive of the cartridges.
- Uploads it to WebDAV.
- Unzips on the server.
- Cleans up the temporary zip file.
Requires instance.config.code_version to be set.
Raises
RuntimeError— if code version is not set or no cartridges are given.
watch_cartridges
async def watch_cartridges(instance: B2CInstance, directory: str, options: WatchOptions | None = None) -> WatchResultWatch cartridge directories and sync changes to an instance.
- Finds cartridges in the specified directory.
- Polls those directories for changes (see module docstring for why polling rather than native OS watching is used).
- Batches file changes (debounced) and uploads them via WebDAV.
- Handles file deletions.
Raises
RuntimeError— if no code version is configured and none is active, or if no cartridges are found indirectory.
Attributes
ScapiScriptsBackendConfig
ScapiScriptsBackendConfig = ScapiBackendCtorConfigScriptsBackendConfig
ScriptsBackendConfig = DualBackendConfig