Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Improve method naming and consistency (#934)

Mish Ushakovedfcc63076ff
diff --git a/.changeset/seven-fans-tickle.md b/.changeset/seven-fans-tickle.md
new file mode 100644
index 000000000..2359d164f
--- /dev/null
+++ b/.changeset/seven-fans-tickle.md
@@ -0,0 +1,6 @@
+---
+'@e2b/python-sdk': patch
+'e2b': patch
+---
+
+updated template methods signature, naming, consistency
diff --git a/packages/cli/tests/commands/template/fixtures/custom-commands/expected/typescript/template.ts b/packages/cli/tests/commands/template/fixtures/custom-commands/expected/typescript/template.ts
index f5beb2bd3..cccc51f8b 100644
--- a/packages/cli/tests/commands/template/fixtures/custom-commands/expected/typescript/template.ts
+++ b/packages/cli/tests/commands/template/fixtures/custom-commands/expected/typescript/template.ts
@@ -6,4 +6,4 @@ export const template = Template()
.copy('server.js', '.')
.setUser('root')
.setWorkdir('/home/user')
- .setStartCmd('node server.js', 'curl -f http://localhost:3000/health || exit 1')
\ No newline at end of file
+ .setStartCmd('node server.js', 'curl -f http://localhost:3000/health || exit 1')
diff --git a/packages/cli/tests/commands/template/fixtures/multiple-env/expected/typescript/template.ts b/packages/cli/tests/commands/template/fixtures/multiple-env/expected/typescript/template.ts
index 6f7e08e41..225074837 100644
--- a/packages/cli/tests/commands/template/fixtures/multiple-env/expected/typescript/template.ts
+++ b/packages/cli/tests/commands/template/fixtures/multiple-env/expected/typescript/template.ts
@@ -16,4 +16,4 @@ export const template = Template()
.setEnvs({
'SINGLE_VAR': 'single_value',
})
- .setWorkdir('/app')
\ No newline at end of file
+ .setWorkdir('/app')
diff --git a/packages/cli/tests/commands/template/fixtures/start-cmd/expected/typescript/template.ts b/packages/cli/tests/commands/template/fixtures/start-cmd/expected/typescript/template.ts
index 957225750..9e2dc7c90 100644
--- a/packages/cli/tests/commands/template/fixtures/start-cmd/expected/typescript/template.ts
+++ b/packages/cli/tests/commands/template/fixtures/start-cmd/expected/typescript/template.ts
@@ -10,4 +10,4 @@ export const template = Template()
})
.setUser('root')
.setWorkdir('/home/user')
- .setStartCmd('node server.js', 'sleep 20')
\ No newline at end of file
+ .setStartCmd('node server.js', 'sleep 20')
diff --git a/packages/js-sdk/src/index.ts b/packages/js-sdk/src/index.ts
index 0ef1b3963..1a0d9dac8 100644
--- a/packages/js-sdk/src/index.ts
+++ b/packages/js-sdk/src/index.ts
@@ -62,6 +62,8 @@ export default Sandbox
export { BuildError, FileUploadError } from './template/errors'
+export type { CopyItem } from './template/types'
+
export {
waitForPort,
waitForURL,
diff --git a/packages/js-sdk/src/template/dockerfileParser.ts b/packages/js-sdk/src/template/dockerfileParser.ts
index dffd0fe3c..551f5e3ae 100644
--- a/packages/js-sdk/src/template/dockerfileParser.ts
+++ b/packages/js-sdk/src/template/dockerfileParser.ts
@@ -27,7 +27,7 @@ export interface DockerfileParserInterface {
dest: string,
options?: { forceUpload?: true; user?: string; mode?: number }
): DockerfileParserInterface
- copy(
+ copyItems(
items: CopyItem[],
options?: { forceUpload?: true; user?: string; mode?: number }
): DockerfileParserInterface
diff --git a/packages/js-sdk/src/template/index.ts b/packages/js-sdk/src/template/index.ts
index 6a9026807..224f8c916 100644
--- a/packages/js-sdk/src/template/index.ts
+++ b/packages/js-sdk/src/template/index.ts
@@ -30,12 +30,13 @@ import {
readDockerignore,
readGCPServiceAccountJSON,
} from './utils'
+import type { PathLike } from 'node:fs'
export { type TemplateBuilder } from './types'
type TemplateOptions = {
- fileContextPath?: string
- ignoreFilePaths?: string[]
+ fileContextPath?: PathLike
+ fileIgnorePatterns?: string[]
}
type BasicBuildOptions = {
@@ -65,16 +66,17 @@ export class TemplateBase
// Force the next layer to be rebuilt
private forceNextLayer: boolean = false
private instructions: Instruction[] = []
- private fileContextPath: string =
+ private fileContextPath: PathLike =
runtime === 'browser' ? '.' : getCallerDirectory(STACK_TRACE_DEPTH) ?? '.'
- private ignoreFilePaths: string[] = []
+ private fileIgnorePatterns: string[] = []
private logsRefreshFrequency: number = 200
private stackTraces: (string | undefined)[] = []
private stackTracesEnabled: boolean = true
constructor(options?: TemplateOptions) {
this.fileContextPath = options?.fileContextPath ?? this.fileContextPath
- this.ignoreFilePaths = options?.ignoreFilePaths ?? this.ignoreFilePaths
+ this.fileIgnorePatterns =
+ options?.fileIgnorePatterns ?? this.fileIgnorePatterns
}
static toJSON(
@@ -115,14 +117,18 @@ export class TemplateBase
fromImage(
baseImage: string,
- options?: { registryConfig?: RegistryConfig }
+ credentials?: { username: string; password: string }
): TemplateBuilder {
this.baseImage = baseImage
this.baseTemplate = undefined
// Set the registry config if provided
- if (options?.registryConfig) {
- this.registryConfig = options.registryConfig
+ if (credentials) {
+ this.registryConfig = {
+ type: 'registry',
+ username: credentials.username,
+ password: credentials.password,
+ }
}
// If we should force the next layer and it's a FROM command, invalidate whole template
@@ -168,84 +174,64 @@ export class TemplateBase
return this
}
- fromRegistry(
- image: string,
- options: {
- username: string
- password: string
- }
- ): TemplateBuilder {
- return this.runInNewStackTraceContext(() =>
- this.fromImage(image, {
- registryConfig: {
- type: 'registry',
- username: options.username,
- password: options.password,
- },
- })
- )
- }
-
fromAWSRegistry(
image: string,
- options: {
+ credentials: {
accessKeyId: string
secretAccessKey: string
region: string
}
): TemplateBuilder {
- return this.runInNewStackTraceContext(() =>
- this.fromImage(image, {
- registryConfig: {
- type: 'aws',
- awsAccessKeyId: options.accessKeyId,
- awsSecretAccessKey: options.secretAccessKey,
- awsRegion: options.region,
- },
- })
- )
+ this.baseImage = image
+ this.baseTemplate = undefined
+
+ // Set the registry config if provided
+ this.registryConfig = {
+ type: 'aws',
+ awsAccessKeyId: credentials.accessKeyId,
+ awsSecretAccessKey: credentials.secretAccessKey,
+ awsRegion: credentials.region,
+ }
+
+ // If we should force the next layer and it's a FROM command, invalidate whole template
+ if (this.forceNextLayer) {
+ this.force = true
+ }
+
+ this.collectStackTrace()
+ return this
}
fromGCPRegistry(
image: string,
- options: {
+ credentials: {
serviceAccountJSON: string | object
}
): TemplateBuilder {
- return this.runInNewStackTraceContext(() =>
- this.fromImage(image, {
- registryConfig: {
- type: 'gcp',
- serviceAccountJson: readGCPServiceAccountJSON(
- this.fileContextPath,
- options.serviceAccountJSON
- ),
- },
- })
- )
- }
+ this.baseImage = image
+ this.baseTemplate = undefined
- copy(
- src: string,
- dest: string,
- options?: {
- forceUpload?: true
- user?: string
- mode?: number
- resolveSymlinks?: boolean
+ // Set the registry config if provided
+ this.registryConfig = {
+ type: 'gcp',
+ serviceAccountJson: readGCPServiceAccountJSON(
+ this.fileContextPath.toString(),
+ credentials.serviceAccountJSON
+ ),
}
- ): TemplateBuilder
- copy(items: CopyItem[]): TemplateBuilder
+
+ // If we should force the next layer and it's a FROM command, invalidate whole template
+ if (this.forceNextLayer) {
+ this.force = true
+ }
+
+ this.collectStackTrace()
+ return this
+ }
+
copy(
- srcOrItems: string | CopyItem[],
- destOrOptions?:
- | string
- | {
- forceUpload?: true
- user?: string
- mode?: number
- resolveSymlinks?: boolean
- },
+ src: PathLike | PathLike[],
+ dest: PathLike,
options?: {
forceUpload?: true
user?: string
@@ -257,32 +243,22 @@ export class TemplateBase
throw new Error('Browser runtime is not supported for copy')
}
- const items = Array.isArray(srcOrItems)
- ? srcOrItems
- : [
- {
- src: srcOrItems,
- dest: destOrOptions as string,
- mode: options?.mode,
- user: options?.user,
- forceUpload: options?.forceUpload,
- resolveSymlinks: options?.resolveSymlinks,
- },
- ]
- for (const item of items) {
+ const srcs = Array.isArray(src) ? src : [src]
+
+ for (const src of srcs) {
const args = [
- item.src,
- item.dest,
- item.user ?? '',
- item.mode ? padOctal(item.mode) : '',
+ src.toString(),
+ dest.toString(),
+ options?.user ?? '',
+ options?.mode ? padOctal(options.mode) : '',
]
this.instructions.push({
type: InstructionType.COPY,
args,
- force: item.forceUpload ?? this.forceNextLayer,
- forceUpload: item.forceUpload,
- resolveSymlinks: item.resolveSymlinks,
+ force: options?.forceUpload || this.forceNextLayer,
+ forceUpload: options?.forceUpload,
+ resolveSymlinks: options?.resolveSymlinks,
})
}
@@ -290,26 +266,47 @@ export class TemplateBase
return this
}
+ copyItems(items: CopyItem[]): TemplateBuilder {
+ if (runtime === 'browser') {
+ throw new Error('Browser runtime is not supported for copyItems')
+ }
+
+ this.runInNewStackTraceContext(() => {
+ for (const item of items) {
+ this.copy(item.src, item.dest, {
+ forceUpload: item.forceUpload,
+ user: item.user,
+ mode: item.mode,
+ resolveSymlinks: item.resolveSymlinks,
+ })
+ }
+ })
+
+ return this
+ }
+
remove(
- path: string,
+ path: PathLike | PathLike[],
options?: { force?: boolean; recursive?: boolean }
): TemplateBuilder {
- const args = ['rm', path]
+ const paths = Array.isArray(path) ? path : [path]
+ const args = ['rm']
if (options?.recursive) {
args.push('-r')
}
if (options?.force) {
args.push('-f')
}
+ args.push(...paths.map((p) => p.toString()))
return this.runInNewStackTraceContext(() => this.runCmd(args.join(' ')))
}
rename(
- src: string,
- dest: string,
+ src: PathLike,
+ dest: PathLike,
options?: { force?: boolean }
): TemplateBuilder {
- const args = ['mv', src, dest]
+ const args = ['mv', src.toString(), dest.toString()]
if (options?.force) {
args.push('-f')
}
@@ -317,18 +314,20 @@ export class TemplateBase
}
makeDir(
- paths: string | string[],
+ path: PathLike | PathLike[],
options?: { mode?: number }
): TemplateBuilder {
- const args = ['mkdir', '-p', ...(Array.isArray(paths) ? paths : [paths])]
+ const paths = Array.isArray(path) ? path : [path]
+ const args = ['mkdir', '-p']
if (options?.mode) {
args.push(`-m ${padOctal(options.mode)}`)
}
+ args.push(...paths.map((p) => p.toString()))
return this.runInNewStackTraceContext(() => this.runCmd(args.join(' ')))
}
- makeSymlink(src: string, dest: string): TemplateBuilder {
- const args = ['ln', '-s', src, dest]
+ makeSymlink(src: PathLike, dest: PathLike): TemplateBuilder {
+ const args = ['ln', '-s', src.toString(), dest.toString()]
return this.runInNewStackTraceContext(() => this.runCmd(args.join(' ')))
}
@@ -357,10 +356,10 @@ export class TemplateBase
return this
}
- setWorkdir(workdir: string): TemplateBuilder {
+ setWorkdir(workdir: PathLike): TemplateBuilder {
this.instructions.push({
type: InstructionType.WORKDIR,
- args: [workdir],
+ args: [workdir.toString()],
force: this.forceNextLayer,
})
@@ -432,10 +431,10 @@ export class TemplateBase
gitClone(
url: string,
- path?: string,
+ path?: PathLike,
options?: { branch?: string; depth?: number }
): TemplateBuilder {
- const args = ['git', 'clone', url, path]
+ const args = ['git', 'clone', url]
if (options?.branch) {
args.push(`--branch ${options.branch}`)
args.push('--single-branch')
@@ -443,6 +442,9 @@ export class TemplateBase
if (options?.depth) {
args.push(`--depth ${options.depth}`)
}
+ if (path) {
+ args.push(path.toString())
+ }
return this.runInNewStackTraceContext(() => this.runCmd(args.join(' ')))
}
@@ -622,7 +624,7 @@ export class TemplateBase
await uploadFile(
{
fileName: src,
- fileContextPath: this.fileContextPath,
+ fileContextPath: this.fileContextPath.toString(),
url,
resolveSymlinks: instruction.resolveSymlinks ?? RESOLVE_SYMLINKS,
},
@@ -697,12 +699,12 @@ export class TemplateBase
filesHash: await calculateFilesHash(
src,
dest,
- this.fileContextPath,
+ this.fileContextPath.toString(),
[
- ...this.ignoreFilePaths,
+ ...this.fileIgnorePatterns,
...(runtime === 'browser'
? []
- : readDockerignore(this.fileContextPath)),
+ : readDockerignore(this.fileContextPath.toString())),
],
instruction.resolveSymlinks ?? RESOLVE_SYMLINKS,
stackTrace
diff --git a/packages/js-sdk/src/template/types.ts b/packages/js-sdk/src/template/types.ts
index 7c1f29e57..e68e15744 100644
--- a/packages/js-sdk/src/template/types.ts
+++ b/packages/js-sdk/src/template/types.ts
@@ -1,5 +1,6 @@
import { stripAnsi } from '../utils'
import { ReadyCmd } from './readycmd'
+import type { PathLike } from 'node:fs'
export enum InstructionType {
COPY = 'COPY',
@@ -13,15 +14,15 @@ export type Instruction = {
type: InstructionType
args: string[]
force: boolean
- forceUpload?: boolean
+ forceUpload?: true
filesHash?: string
resolveSymlinks?: boolean
}
export type CopyItem = {
- src: string
- dest: string
- forceUpload?: boolean
+ src: PathLike | PathLike[]
+ dest: PathLike
+ forceUpload?: true
user?: string
mode?: number
resolveSymlinks?: boolean
@@ -39,23 +40,18 @@ export interface TemplateFromImage {
fromBaseImage(): TemplateBuilder
- fromImage(baseImage: string): TemplateBuilder
+ fromImage(
+ baseImage: string,
+ credentials?: { username: string; password: string }
+ ): TemplateBuilder
fromTemplate(template: string): TemplateBuilder
fromDockerfile(dockerfileContent: string): TemplateBuilder
- fromRegistry(
- image: string,
- options: {
- username: string
- password: string
- }
- ): TemplateBuilder
-
fromAWSRegistry(
image: string,
- options: {
+ credentials: {
accessKeyId: string
secretAccessKey: string
region: string
@@ -64,7 +60,7 @@ export interface TemplateFromImage {
fromGCPRegistry(
image: string,
- options: {
+ credentials: {
serviceAccountJSON: object | string
}
): TemplateBuilder
@@ -75,8 +71,8 @@ export interface TemplateFromImage {
// Interface for the main builder state
export interface TemplateBuilder {
copy(
- src: string,
- dest: string,
+ src: PathLike | PathLike[],
+ dest: PathLike,
options?: {
forceUpload?: true
user?: string
@@ -85,25 +81,25 @@ export interface TemplateBuilder {
}
): TemplateBuilder
- copy(items: CopyItem[]): TemplateBuilder
+ copyItems(items: CopyItem[]): TemplateBuilder
remove(
- path: string,
+ path: PathLike | PathLike[],
options?: { force?: boolean; recursive?: boolean }
): TemplateBuilder
rename(
- src: string,
- dest: string,
+ src: PathLike,
+ dest: PathLike,
options?: { force?: boolean }
): TemplateBuilder
makeDir(
- paths: string | string[],
+ path: PathLike | PathLike[],
options?: { mode?: number }
): TemplateBuilder
- makeSymlink(src: string, dest: string): TemplateBuilder
+ makeSymlink(src: PathLike, dest: PathLike): TemplateBuilder
runCmd(command: string, options?: { user?: string }): TemplateBuilder
@@ -114,7 +110,7 @@ export interface TemplateBuilder {
options?: { user?: string }
): TemplateBuilder
- setWorkdir(workdir: string): TemplateBuilder
+ setWorkdir(workdir: PathLike): TemplateBuilder
setUser(user: string): TemplateBuilder
@@ -129,7 +125,7 @@ export interface TemplateBuilder {
gitClone(
url: string,
- path?: string,
+ path?: PathLike,
options?: { branch?: string; depth?: number }
): TemplateBuilder
diff --git a/packages/js-sdk/tests/setup.ts b/packages/js-sdk/tests/setup.ts
index 3fade5e0f..a5d119bec 100644
--- a/packages/js-sdk/tests/setup.ts
+++ b/packages/js-sdk/tests/setup.ts
@@ -10,7 +10,7 @@ interface SandboxFixture {
}
interface BuildTemplateFixture {
- buildTemplate: (template: TemplateClass) => Promise<void>
+ buildTemplate: (template: TemplateClass, skipCache?: boolean) => Promise<void>
}
function buildTemplate(template: TemplateClass, skipCache?: boolean) {
diff --git a/packages/js-sdk/tests/template/stacktrace.test.ts b/packages/js-sdk/tests/template/stacktrace.test.ts
index a5560d3db..07cb08988 100644
--- a/packages/js-sdk/tests/template/stacktrace.test.ts
+++ b/packages/js-sdk/tests/template/stacktrace.test.ts
@@ -80,9 +80,9 @@ buildTemplateTest('traces on fromDockerfile', async ({ buildTemplate }) => {
}, 'fromDockerfile')
})
-buildTemplateTest('traces on fromRegistry', async ({ buildTemplate }) => {
+buildTemplateTest('traces on fromImage registry', async ({ buildTemplate }) => {
const templateFrom = Template()
- const template = templateFrom.fromRegistry(
+ const template = templateFrom.fromImage(
'registry.example.com/nonexistent:latest',
{
username: 'test',
@@ -91,7 +91,7 @@ buildTemplateTest('traces on fromRegistry', async ({ buildTemplate }) => {
)
await expectToThrowAndCheckTrace(async () => {
await buildTemplate(template, true)
- }, 'fromRegistry')
+ }, 'fromImage')
})
buildTemplateTest('traces on fromAWSRegistry', async ({ buildTemplate }) => {
@@ -130,6 +130,16 @@ buildTemplateTest('traces on copy', async ({ buildTemplate }) => {
}, 'copy')
})
+buildTemplateTest('traces on copyItems', async ({ buildTemplate }) => {
+ let template = Template().fromBaseImage()
+ template = template
+ .skipCache()
+ .copyItems([{ src: nonExistentPath, dest: nonExistentPath }])
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'copyItems')
+})
+
buildTemplateTest('traces on remove', async ({ buildTemplate }) => {
let template = Template().fromBaseImage()
template = template.skipCache().remove(nonExistentPath)
diff --git a/packages/python-sdk/e2b/__init__.py b/packages/python-sdk/e2b/__init__.py
index 5ea7ca785..ff4a95d6c 100644
--- a/packages/python-sdk/e2b/__init__.py
+++ b/packages/python-sdk/e2b/__init__.py
@@ -71,6 +71,8 @@ from .sandbox_sync.paginator import SandboxPaginator
from .template.main import TemplateBase, TemplateClass
+from .template.types import CopyItem
+
from .template_sync.main import Template
from .template_async.main import AsyncTemplate
@@ -136,6 +138,7 @@ __all__ = [
"AsyncTemplate",
"TemplateBase",
"TemplateClass",
+ "CopyItem",
"wait_for_file",
"wait_for_url",
"wait_for_port",
diff --git a/packages/python-sdk/e2b/template/dockerfile_parser.py b/packages/python-sdk/e2b/template/dockerfile_parser.py
index b17f6e108..558bdadbf 100644
--- a/packages/python-sdk/e2b/template/dockerfile_parser.py
+++ b/packages/python-sdk/e2b/template/dockerfile_parser.py
@@ -1,7 +1,7 @@
import os
import re
import tempfile
-from typing import Dict, List, Optional, Protocol, Union
+from typing import Dict, List, Optional, Protocol, Union, Literal
from dockerfile_parse import DockerfileParser
from e2b.template.types import CopyItem
@@ -24,7 +24,8 @@ class DockerfileParserInterface(Protocol):
self,
src: Union[str, List[CopyItem]],
dest: Optional[str] = None,
- force_upload: Optional[bool] = None,
+ force_upload: Optional[Literal[True]] = None,
+ resolve_symlinks: Optional[bool] = None,
user: Optional[str] = None,
mode: Optional[int] = None,
) -> "DockerfileParserInterface":
@@ -239,7 +240,7 @@ def _handle_env_instruction(
def _handle_cmd_entrypoint_instruction(
value: str, template_builder: DockerfileParserInterface
) -> None:
- """Handle CMD/ENTRYPOINT instruction - convert to setStartCmd with 20s timeout"""
+ """Handle CMD/ENTRYPOINT instruction - convert to set_start_cmd with 20s timeout"""
if not value.strip():
return
command = value.strip()
diff --git a/packages/python-sdk/e2b/template/main.py b/packages/python-sdk/e2b/template/main.py
index 5d5a86240..3c8d62ca6 100644
--- a/packages/python-sdk/e2b/template/main.py
+++ b/packages/python-sdk/e2b/template/main.py
@@ -1,5 +1,6 @@
import json
-from typing import Dict, List, Optional, Union
+from typing import Dict, List, Optional, Union, Literal
+from pathlib import Path
from httpx import Limits
@@ -30,65 +31,74 @@ class TemplateBuilder:
def copy(
self,
- src: Union[str, List[CopyItem]],
- dest: Optional[str] = None,
- force_upload: Optional[bool] = None,
+ src: Union[Union[str, Path], List[Union[str, Path]]],
+ dest: Union[str, Path],
+ force_upload: Optional[Literal[True]] = None,
user: Optional[str] = None,
mode: Optional[int] = None,
resolve_symlinks: Optional[bool] = None,
) -> "TemplateBuilder":
- if isinstance(src, str):
- # Single copy operation
- if dest is None:
- raise ValueError("dest parameter is required when src is a string")
- copy_items: List[CopyItem] = [
- CopyItem(
- src=src,
- dest=dest,
- forceUpload=force_upload,
- user=user,
- mode=mode,
- resolveSymlinks=resolve_symlinks,
- )
- ]
- else:
- # Multiple copy operations
- copy_items = src
+ srcs = [src] if isinstance(src, (str, Path)) else src
- for copy_item in copy_items:
+ for src_item in srcs:
args = [
- copy_item["src"],
- copy_item["dest"],
+ str(src_item),
+ str(dest),
user or "",
pad_octal(mode) if mode else "",
]
- instruction: Instruction = Instruction(
- type=InstructionType.COPY,
- args=args,
- force=force_upload or self._template._force_next_layer,
- forceUpload=force_upload,
- resolveSymlinks=resolve_symlinks,
- )
+ instruction: Instruction = {
+ "type": InstructionType.COPY,
+ "args": args,
+ "force": force_upload or self._template._force_next_layer,
+ "forceUpload": force_upload,
+ "resolveSymlinks": resolve_symlinks,
+ }
+
self._template._instructions.append(instruction)
+
self._template._collect_stack_trace()
return self
+ def copy_items(self, items: List[CopyItem]) -> "TemplateBuilder":
+ self._template._run_in_new_stack_trace_context(
+ lambda: [
+ self.copy(
+ item["src"],
+ item["dest"],
+ item.get("forceUpload"),
+ item.get("user"),
+ item.get("mode"),
+ item.get("resolveSymlinks"),
+ )
+ for item in items
+ ]
+ )
+ return self
+
def remove(
- self, path: str, force: bool = False, recursive: bool = False
+ self,
+ path: Union[Union[str, Path], List[Union[str, Path]]],
+ force: bool = False,
+ recursive: bool = False,
) -> "TemplateBuilder":
- args = ["rm", path]
+ paths = [path] if isinstance(path, (str, Path)) else path
+ args = ["rm"]
if recursive:
args.append("-r")
if force:
args.append("-f")
+ args.extend([str(p) for p in paths])
return self._template._run_in_new_stack_trace_context(
lambda: self.run_cmd(" ".join(args))
)
- def rename(self, src: str, dest: str, force: bool = False) -> "TemplateBuilder":
- args = ["mv", src, dest]
+ def rename(
+ self, src: Union[str, Path], dest: Union[str, Path], force: bool = False
+ ) -> "TemplateBuilder":
+ args = ["mv", str(src), str(dest)]
if force:
args.append("-f")
@@ -97,21 +107,24 @@ class TemplateBuilder:
)
def make_dir(
- self, paths: Union[str, List[str]], mode: Optional[int] = None
+ self,
+ path: Union[Union[str, Path], List[Union[str, Path]]],
+ mode: Optional[int] = None,
) -> "TemplateBuilder":
- if isinstance(paths, str):
- paths = [paths]
-
- args = ["mkdir", "-p", *paths]
+ path_list = [path] if isinstance(path, (str, Path)) else path
+ args = ["mkdir", "-p"]
if mode:
args.append(f"-m {pad_octal(mode)}")
+ args.extend([str(p) for p in path_list])
return self._template._run_in_new_stack_trace_context(
lambda: self.run_cmd(" ".join(args))
)
- def make_symlink(self, src: str, dest: str) -> "TemplateBuilder":
- args = ["ln", "-s", src, dest]
+ def make_symlink(
+ self, src: Union[str, Path], dest: Union[str, Path]
+ ) -> "TemplateBuilder":
+ args = ["ln", "-s", str(src), str(dest)]
return self._template._run_in_new_stack_trace_context(
lambda: self.run_cmd(" ".join(args))
)
@@ -125,34 +138,34 @@ class TemplateBuilder:
if user:
args.append(user)
- instruction: Instruction = Instruction(
- type=InstructionType.RUN,
- args=args,
- force=self._template._force_next_layer,
- forceUpload=None,
- )
+ instruction: Instruction = {
+ "type": InstructionType.RUN,
+ "args": args,
+ "force": self._template._force_next_layer,
+ "forceUpload": None,
+ }
self._template._instructions.append(instruction)
self._template._collect_stack_trace()
return self
- def set_workdir(self, workdir: str) -> "TemplateBuilder":
- instruction: Instruction = Instruction(
- type=InstructionType.WORKDIR,
- args=[workdir],
- force=self._template._force_next_layer,
- forceUpload=None,
- )
+ def set_workdir(self, workdir: Union[str, Path]) -> "TemplateBuilder":
+ instruction: Instruction = {
+ "type": InstructionType.WORKDIR,
+ "args": [str(workdir)],
+ "force": self._template._force_next_layer,
+ "forceUpload": None,
+ }
self._template._instructions.append(instruction)
self._template._collect_stack_trace()
return self
def set_user(self, user: str) -> "TemplateBuilder":
- instruction: Instruction = Instruction(
- type=InstructionType.USER,
- args=[user],
- force=self._template._force_next_layer,
- forceUpload=None,
- )
+ instruction: Instruction = {
+ "type": InstructionType.USER,
+ "args": [user],
+ "force": self._template._force_next_layer,
+ "forceUpload": None,
+ }
self._template._instructions.append(instruction)
self._template._collect_stack_trace()
return self
@@ -208,18 +221,18 @@ class TemplateBuilder:
def git_clone(
self,
url: str,
- path: Optional[str] = None,
+ path: Optional[Union[str, Path]] = None,
branch: Optional[str] = None,
depth: Optional[int] = None,
) -> "TemplateBuilder":
args = ["git", "clone", url]
- if path:
- args.append(path)
if branch:
args.append(f"--branch {branch}")
args.append("--single-branch")
if depth:
args.append(f"--depth {depth}")
+ if path:
+ args.append(str(path))
return self._template._run_in_new_stack_trace_context(
lambda: self.run_cmd(" ".join(args))
)
@@ -228,12 +241,12 @@ class TemplateBuilder:
if len(envs) == 0:
return self
- instruction: Instruction = Instruction(
- type=InstructionType.ENV,
- args=[item for key, value in envs.items() for item in [key, value]],
- force=self._template._force_next_layer,
- forceUpload=None,
- )
+ instruction: Instruction = {
+ "type": InstructionType.ENV,
+ "args": [item for key, value in envs.items() for item in [key, value]],
+ "force": self._template._force_next_layer,
+ "forceUpload": None,
+ }
self._template._instructions.append(instruction)
self._template._collect_stack_trace()
return self
@@ -278,8 +291,8 @@ class TemplateBase:
def __init__(
self,
- file_context_path: Optional[str] = None,
- ignore_file_paths: Optional[List[str]] = None,
+ file_context_path: Optional[Union[str, Path]] = None,
+ file_ignore_patterns: Optional[List[str]] = None,
):
self._default_base_image: str = "e2bdev/base"
self._base_image: Optional[str] = self._default_base_image
@@ -293,11 +306,13 @@ class TemplateBase:
self._force_next_layer: bool = False
self._instructions: List[Instruction] = []
# If no file_context_path is provided, use the caller's directory
- self._file_context_path: str = (
- file_context_path or get_caller_directory(STACK_TRACE_DEPTH) or "."
+ self._file_context_path = (
+ file_context_path.as_posix()
+ if isinstance(file_context_path, Path)
+ else (file_context_path or get_caller_directory(STACK_TRACE_DEPTH) or ".")
)
- self._ignore_file_paths: List[str] = ignore_file_paths or []
- self._stack_traces: List[TracebackType] = []
+ self._file_ignore_patterns: List[str] = file_ignore_patterns or []
+ self._stack_traces: List[Union[TracebackType, None]] = []
self._stack_traces_enabled: bool = True
def skip_cache(self) -> "TemplateBase":
@@ -306,7 +321,7 @@ class TemplateBase:
return self
def _collect_stack_trace(
- self, stack_traces_depth: Optional[int] = STACK_TRACE_DEPTH
+ self, stack_traces_depth: int = STACK_TRACE_DEPTH
) -> "TemplateBase":
"""Collect stack trace if enabled"""
if not self._stack_traces_enabled:
@@ -373,15 +388,22 @@ class TemplateBase:
)
def from_image(
- self, base_image: str, registry_config: Optional[RegistryConfig] = None
+ self,
+ image: str,
+ username: Optional[str] = None,
+ password: Optional[str] = None,
) -> TemplateBuilder:
"""Private method to set base image without adding stack trace"""
- self._base_image = base_image
+ self._base_image = image
self._base_template = None
# Set the registry config if provided
- if registry_config is not None:
- self._registry_config = registry_config
+ if username and password:
+ self._registry_config = {
+ "type": "registry",
+ "username": username,
+ "password": password,
+ }
# If we should force the next layer and it's a FROM command, invalidate whole template
if self._force_next_layer:
@@ -422,20 +444,6 @@ class TemplateBase:
self._collect_stack_trace()
return builder
- def from_registry(
- self, image: str, username: str, password: str
- ) -> TemplateBuilder:
- return self._run_in_new_stack_trace_context(
- lambda: self.from_image(
- image,
- registry_config={
- "type": "registry",
- "username": username,
- "password": password,
- },
- )
- )
-
def from_aws_registry(
self,
image: str,
@@ -443,32 +451,44 @@ class TemplateBase:
secret_access_key: str,
region: str,
) -> TemplateBuilder:
- return self._run_in_new_stack_trace_context(
- lambda: self.from_image(
- image,
- registry_config={
- "type": "aws",
- "awsAccessKeyId": access_key_id,
- "awsSecretAccessKey": secret_access_key,
- "awsRegion": region,
- },
- )
- )
+ self._base_image = image
+ self._base_template = None
+
+ # Set the registry config if provided
+ self._registry_config = {
+ "type": "aws",
+ "awsAccessKeyId": access_key_id,
+ "awsSecretAccessKey": secret_access_key,
+ "awsRegion": region,
+ }
+
+ # If we should force the next layer and it's a FROM command, invalidate whole template
+ if self._force_next_layer:
+ self._force = True
+
+ self._collect_stack_trace()
+ return TemplateBuilder(self)
def from_gcp_registry(
self, image: str, service_account_json: Union[str, dict]
) -> TemplateBuilder:
- return self._run_in_new_stack_trace_context(
- lambda: self.from_image(
- image,
- registry_config={
- "type": "gcp",
- "serviceAccountJson": read_gcp_service_account_json(
- self._file_context_path, service_account_json
- ),
- },
- )
- )
+ self._base_image = image
+ self._base_template = None
+
+ # Set the registry config if provided
+ self._registry_config = {
+ "type": "gcp",
+ "serviceAccountJson": read_gcp_service_account_json(
+ self._file_context_path, service_account_json
+ ),
+ }
+
+ # If we should force the next layer and it's a FROM command, invalidate whole template
+ if self._force_next_layer:
+ self._force = True
+
+ self._collect_stack_trace()
+ return TemplateBuilder(self)
@staticmethod
def to_json(template: "TemplateClass") -> str:
@@ -508,13 +528,13 @@ class TemplateBase:
steps: List[Instruction] = []
for index, instruction in enumerate(self._instructions):
- step: Instruction = Instruction(
- type=instruction["type"],
- args=instruction["args"],
- force=instruction["force"],
- forceUpload=instruction.get("forceUpload"),
- resolveSymlinks=instruction.get("resolveSymlinks"),
- )
+ step: Instruction = {
+ "type": instruction["type"],
+ "args": instruction["args"],
+ "force": instruction["force"],
+ "forceUpload": instruction.get("forceUpload"),
+ "resolveSymlinks": instruction.get("resolveSymlinks"),
+ }
if instruction["type"] == InstructionType.COPY:
stack_trace = None
@@ -532,7 +552,7 @@ class TemplateBase:
dest,
self._file_context_path,
[
- *self._ignore_file_paths,
+ *self._file_ignore_patterns,
*read_dockerignore(self._file_context_path),
],
instruction.get("resolveSymlinks", RESOLVE_SYMLINKS),
@@ -546,12 +566,11 @@ class TemplateBase:
def _serialize(self, steps: List[Instruction]) -> TemplateType:
_steps: List[Instruction] = []
- for index, instruction in enumerate(steps):
- step = {
- # Serialize enum to string value
- "type": instruction["type"].value,
- "args": instruction["args"],
- "force": instruction["force"],
+ for _, instruction in enumerate(steps):
+ step: Instruction = {
+ "type": instruction.get("type"),
+ "args": instruction.get("args"),
+ "force": instruction.get("force"),
}
if instruction.get("filesHash") is not None:
diff --git a/packages/python-sdk/e2b/template/types.py b/packages/python-sdk/e2b/template/types.py
index 64fbd6e61..80a046fa5 100644
--- a/packages/python-sdk/e2b/template/types.py
+++ b/packages/python-sdk/e2b/template/types.py
@@ -4,10 +4,11 @@ from dataclasses import dataclass
from datetime import datetime
from typing import Literal
from enum import Enum
+from pathlib import Path
from e2b.template.utils import strip_ansi_escape_codes
-class InstructionType(Enum):
+class InstructionType(str, Enum):
COPY = "COPY"
ENV = "ENV"
RUN = "RUN"
@@ -16,9 +17,9 @@ class InstructionType(Enum):
class CopyItem(TypedDict):
- src: str
- dest: str
- forceUpload: NotRequired[Optional[bool]]
+ src: Union[Union[str, Path], List[Union[str, Path]]]
+ dest: Union[str, Path]
+ forceUpload: NotRequired[Optional[Literal[True]]]
user: NotRequired[Optional[str]]
mode: NotRequired[Optional[int]]
resolveSymlinks: NotRequired[Optional[bool]]
@@ -28,7 +29,7 @@ class Instruction(TypedDict):
type: InstructionType
args: List[str]
force: bool
- forceUpload: NotRequired[Optional[bool]]
+ forceUpload: NotRequired[Optional[Literal[True]]]
filesHash: NotRequired[Optional[str]]
resolveSymlinks: NotRequired[Optional[bool]]
diff --git a/packages/python-sdk/tests/async/template_async/test_stacktrace.py b/packages/python-sdk/tests/async/template_async/test_stacktrace.py
index d26c33a9b..509c7b8cb 100644
--- a/packages/python-sdk/tests/async/template_async/test_stacktrace.py
+++ b/packages/python-sdk/tests/async/template_async/test_stacktrace.py
@@ -1,7 +1,7 @@
import pytest
import linecache
-from e2b import AsyncTemplate, wait_for_timeout
+from e2b import AsyncTemplate, CopyItem, wait_for_timeout
non_existent_path = "/nonexistent/path"
@@ -50,16 +50,14 @@ async def test_traces_on_from_dockerfile(async_build):
@pytest.mark.skip_debug()
-async def test_traces_on_from_registry(async_build):
+async def test_traces_on_from_image_registry(async_build):
template = AsyncTemplate()
- template = template.skip_cache().from_registry(
+ template = template.skip_cache().from_image(
"registry.example.com/nonexistent:latest",
username="test",
password="test",
)
- await _expect_to_throw_and_check_trace(
- lambda: async_build(template), "from_registry"
- )
+ await _expect_to_throw_and_check_trace(lambda: async_build(template), "from_image")
@pytest.mark.skip_debug()
@@ -98,6 +96,16 @@ async def test_traces_on_copy(async_build):
await _expect_to_throw_and_check_trace(lambda: async_build(template), "copy")
+async def test_traces_on_copyItems(async_build):
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.skip_cache().copy_items(
+ [CopyItem(src=non_existent_path, dest=non_existent_path)]
+ )
+ await _expect_to_throw_and_check_trace(lambda: async_build(template), "copy_items")
+
+
@pytest.mark.skip_debug()
async def test_traces_on_remove(async_build):
template = AsyncTemplate()
@@ -189,7 +197,7 @@ async def test_traces_on_git_clone(async_build):
@pytest.mark.skip_debug()
-async def test_traces_on_set_start_cmd(async_build):
+async def test_traces_on_start_cmd(async_build):
template = AsyncTemplate()
template = template.from_base_image()
template = template.set_start_cmd(
diff --git a/packages/python-sdk/tests/sync/template_sync/test_stacktrace.py b/packages/python-sdk/tests/sync/template_sync/test_stacktrace.py
index 4a53165cf..6ffd6a7be 100644
--- a/packages/python-sdk/tests/sync/template_sync/test_stacktrace.py
+++ b/packages/python-sdk/tests/sync/template_sync/test_stacktrace.py
@@ -1,7 +1,7 @@
import pytest
import linecache
-from e2b import Template, wait_for_timeout
+from e2b import Template, CopyItem, wait_for_timeout
non_existent_path = "/nonexistent/path"
@@ -48,14 +48,14 @@ def test_traces_on_from_dockerfile(build):
@pytest.mark.skip_debug()
-def test_traces_on_from_registry(build):
+def test_traces_on_from_image_registry(build):
template = Template()
- template = template.skip_cache().from_registry(
+ template = template.skip_cache().from_image(
"registry.example.com/nonexistent:latest",
username="test",
password="test",
)
- _expect_to_throw_and_check_trace(lambda: build(template), "from_registry")
+ _expect_to_throw_and_check_trace(lambda: build(template), "from_image")
@pytest.mark.skip_debug()
@@ -90,6 +90,16 @@ def test_traces_on_copy(build):
_expect_to_throw_and_check_trace(lambda: build(template), "copy")
+def test_traces_on_copyItems(build):
+ template = Template()
+ template = template.from_base_image()
+ template = template.skip_cache().copy_items(
+ [CopyItem(src=non_existent_path, dest=non_existent_path)]
+ )
+ _expect_to_throw_and_check_trace(lambda: build(template), "copy_items")
+
+
@pytest.mark.skip_debug()
def test_traces_on_remove(build):
template = Template()