lakefs.branch module¶
Module containing lakeFS branch implementation
- class lakefs.branch.Branch(repository_id, branch_id, client=None)[source]¶
Bases:
_BaseBranch
Class representing a branch in lakeFS.
- cherry_pick(reference, parent_number=None)[source]¶
Cherry-pick a given reference onto the branch.
- Parameters:
- Return type:
- Returns:
The cherry-picked commit at the head of the branch.
- Raises:
NotFoundException – If either the repository or target reference do not exist.
NotAuthorizedException – If the user is not authorized to perform this operation.
ServerException – For any other errors.
- commit(message, metadata=None, **kwargs)[source]¶
Commit changes on the current branch
- Parameters:
message (
str
) – Commit messagemetadata (
Optional
[dict
]) – Metadata to attach to the commitkwargs – Additional Keyword Arguments for commit creation
- Return type:
- Returns:
The new reference after the commit
- Raises:
NotFoundException – if branch by this id does not exist
ForbiddenException – if commit is not allowed on this branch
NotAuthorizedException – if user is not authorized to perform this operation
ServerException – for any other errors
- create(source_reference, exist_ok=False)[source]¶
Create a new branch in lakeFS from this object
Example of creating a new branch:
import lakefs branch = lakefs.repository("<repository_name>").branch("<branch_name>").create("<source_reference>")
- Parameters:
- Return type:
- Returns:
The lakeFS SDK object representing the branch
- Raises:
NotFoundException – if repo, branch or source reference id does not exist
ConflictException – if branch already exists and exist_ok is False
NotAuthorizedException – if user is not authorized to perform this operation
ServerException – for any other errors
- delete()[source]¶
Delete branch from lakeFS server
- Raises:
NotFoundException – if branch or repository do not exist
NotAuthorizedException – if user is not authorized to perform this operation
ForbiddenException – for branches that are protected
ServerException – for any other errors
- Return type:
None
- delete_objects(object_paths)¶
Delete objects from lakeFS
This method can be used to delete single/multiple objects from branch. It accepts both str and StoredObject types as well as Iterables of these types. Using this method is more performant than sequentially calling delete on objects as it saves the back and forth from the server.
This can also be used in combination with object listing. For example:
import lakefs branch = lakefs.repository("<repository_name>").branch("<branch_name>") # list objects on a common prefix objs = branch.objects(prefix="my-object-prefix/", max_amount=100) # delete objects which have "foo" in their name branch.delete_objects([o.path for o in objs if "foo" in o.path])
- Parameters:
object_paths (str | StoredObject | Iterable[str | StoredObject]) – a single path or an iterable of paths to delete
- Raises:
NotFoundException – if branch or repository do not exist
NotAuthorizedException – if user is not authorized to perform this operation
ServerException – for any other errors
- Return type:
None
- diff(other_ref, max_amount=None, after=None, prefix=None, delimiter=None, **kwargs)¶
Returns a diff generator of changes between this reference and other_ref
- Parameters:
other_ref – The other ref to diff against
max_amount – Stop showing changes after this amount
after – Return items after this value
prefix – Return items prefixed with this value
delimiter – Group common prefixes by this delimiter
kwargs – Additional Keyword Arguments to send to the server
- Raises:
NotFoundException – if this reference or other_ref does not exist
NotAuthorizedException – if user is not authorized to perform this operation
ServerException – for any other errors
- get_commit()[source]¶
For branches override the default _get_commit method to ensure we always fetch the latest head
- property head: Reference¶
Get the commit reference this branch is pointing to
- Returns:
The commit reference this branch is pointing to
- Raises:
NotFoundException – if branch by this id does not exist
NotAuthorizedException – if user is not authorized to perform this operation
ServerException – for any other errors
- property id: str¶
Returns the reference id
- import_data(commit_message='', metadata=None)[source]¶
Import data to lakeFS
- Parameters:
metadata (
Optional
[dict
]) – metadata to attach to the commitcommit_message (
str
) – once the data is imported, a commit is created with this message. If default (empty) message is provided, uses the default server commit message for imports.
- Return type:
- Returns:
an ImportManager object
- log(max_amount=None, **kwargs)¶
Returns a generator of commits starting with this reference id
- Parameters:
max_amount – (Optional) limits the amount of results to return from the server
kwargs – Additional Keyword Arguments to send to the server
- Raises:
NotFoundException – if reference by this id does not exist
NotAuthorizedException – if user is not authorized to perform this operation
ServerException – for any other errors
- merge_into(destination_branch, **kwargs)¶
Merge this reference into destination branch
- Parameters:
- Return type:
str
- Returns:
The reference id of the merge commit
- Raises:
NotFoundException – if reference by this id does not exist, or branch doesn’t exist
NotAuthorizedException – if user is not authorized to perform this operation
ServerException – for any other errors
- object(path)¶
Returns a writable object using the current repo id, reference and path
- Parameters:
path (
str
) – The object’s path- Return type:
- objects(max_amount=None, after=None, prefix=None, delimiter=None, **kwargs)¶
Returns an object generator for this reference, the generator can yield either a StoredObject or a CommonPrefix object depending on the listing parameters provided.
- Parameters:
max_amount (Optional[int]) – Stop showing changes after this amount
after (Optional[str]) – Return items after this value
prefix (Optional[str]) – Return items prefixed with this value
delimiter (Optional[str]) – Group common prefixes by this delimiter
kwargs – Additional Keyword Arguments to send to the server
- Raises:
NotFoundException – if this reference or other_ref does not exist
NotAuthorizedException – if user is not authorized to perform this operation
ServerException – for any other errors
- Return type:
Generator[StoredObject | CommonPrefix]
- property repo_id: str¶
Return the repository id for this reference
- reset_changes(path_type='reset', path=None)¶
Reset uncommitted changes (if any) on this branch
- Parameters:
path_type (
Literal
['common_prefix'
,'object'
,'reset'
]) – the type of path to reset (‘common_prefix’, ‘object’, ‘reset’ - for all changes)path (
Optional
[str
]) – the path to reset (optional) - if path_type is ‘reset’ this parameter is ignored
- Raises:
ValidationError – if path_type is not one of the allowed values
NotFoundException – if branch or repository do not exist
NotAuthorizedException – if user is not authorized to perform this operation
ServerException – for any other errors
- Return type:
None
- revert(reference, parent_number=0, *, reference_id=None)[source]¶
revert the changes done by the provided reference on the current branch
- Parameters:
reference_id (
Optional
[str
]) –(Optional) The reference ID to revert
Deprecated since version 0.4.0: Use
reference
instead.parent_number (
int
) – when reverting a merge commit, the parent number (starting from 1) relative to which to perform the revert. The default for non merge commits is 0reference (
Union
[str
,Reference
,Commit
,None
]) – the reference to revert
- Return type:
- Returns:
The commit created by the revert
- Raises:
NotFoundException – if branch by this id does not exist
NotAuthorizedException – if user is not authorized to perform this operation
ServerException – for any other errors
- transact(commit_message='', commit_metadata=None, delete_branch_on_error=True)[source]¶
Create a transaction for multiple operations. Transaction allows for multiple modifications to be performed atomically on a branch, similar to a database transaction. It ensures that the branch remains unaffected until the transaction is successfully completed. The process includes:
Creating an ephemeral branch from this branch
Perform object operations on ephemeral branch
Commit changes
Merge back to source branch
Delete ephemeral branch
Using a transaction the code for this flow will look like this:
import lakefs branch = lakefs.repository("<repository_name>").branch("<branch_name>") with branch.transact(commit_message="my transaction") as tx: for obj in tx.objects(prefix="prefix_to_delete/"): # Delete some objects obj.delete() # Create new object tx.object("new_object").upload("new object data")
Note that unlike database transactions, lakeFS transaction does not take a “lock” on the branch, and therefore the transaction might fail due to changes in source branch after the transaction was created.
- Parameters:
commit_message (
str
) – once the transaction is committed, a commit is created with this messagecommit_metadata (
Optional
[Dict
]) – user metadata for the transaction commitdelete_branch_on_error (
bool
) – Defaults to True. Ensures ephemeral branch is deleted on error.
- Return type:
_Transaction
- Returns:
a Transaction object to perform the operations on
- uncommitted(max_amount=None, after=None, prefix=None, **kwargs)¶
Returns a diff generator of uncommitted changes on this branch
- Parameters:
max_amount – Stop showing changes after this amount
after – Return items after this value
prefix – Return items prefixed with this value
kwargs – Additional Keyword Arguments to send to the server
- Raises:
NotFoundException – if branch or repository do not exist
NotAuthorizedException – if user is not authorized to perform this operation
ServerException – for any other errors
- exception lakefs.branch.LakeFSDeprecationWarning[source]¶
Bases:
Warning
Warning about use of a deprecated lakeFS or client feature. Unlike DeprecationWarning, this class is displayed by default. See default warning filter for how to disable it.
- args¶
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- class lakefs.branch.Transaction(repository_id, branch_id, commit_message='', commit_metadata=None, delete_branch_on_error=True, client=None)[source]¶
Bases:
object
Manage a transaction on a given branch
The transaction creates an ephemeral branch from the source branch. The transaction can then be used to perform operations on the branch which will later be merged back into the source branch. Currently, transaction is supported only as a context manager.