Skip to content

Commit

Permalink
added file large subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
adal-chiriliuc-reef committed Apr 30, 2024
1 parent c554692 commit 429a500
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 50 deletions.
169 changes: 119 additions & 50 deletions b2/_internal/console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,51 +1343,32 @@ def _get_user_requested_realm(cls, args) -> str | None:
return os.environ.get(B2_ENVIRONMENT_ENV_VAR)


class CancelAllUnfinishedLargeFiles(Command):
class FileLargeUnfinishedCancelBase(Command):
"""
Lists all large files that have been started but not
finished and cancels them. Any parts that have been
uploaded will be deleted.
Requires capability:
- **listFiles**
- **writeFiles**
"""

@classmethod
def _setup_parser(cls, parser):
add_bucket_name_argument(parser)
super()._setup_parser(parser)

def _run(self, args):
bucket = self.api.get_bucket_by_name(args.bucketName)
for file_version in bucket.list_unfinished_large_files():
bucket.cancel_large_file(file_version.file_id)
self._print(file_version.file_id, 'canceled')
return 0


class CancelLargeFile(Command):
"""
Cancels a large file upload. Used to undo a ``start-large-file``.
When used with a b2id://fileId, cancels a large file upload.
Cannot be used once the file is finished. After finishing,
using ``delete-file-version`` to delete the large file.
use ``rm`` to delete the large file.
When used with a b2://bucketName, lists all large files that
have been started but not finished and cancels them. Any parts
that have been uploaded will be deleted.
Requires capability:
- **listFiles** (if canceling a bucket)
- **writeFiles**
"""

@classmethod
def _setup_parser(cls, parser):
parser.add_argument('fileId')
super()._setup_parser(parser)

def _run(self, args):
self.api.cancel_large_file(args.fileId)
self._print(args.fileId, 'canceled')
b2_uri = self.get_b2_uri_from_arg(args)
if isinstance(b2_uri, B2FileIdURI):
self.api.cancel_large_file(b2_uri.file_id)
self._print(b2_uri.file_id, 'canceled')
elif isinstance(b2_uri, B2URI):
bucket = self.api.get_bucket_by_name(b2_uri.bucket_name)
for file_version in bucket.list_unfinished_large_files():
bucket.cancel_large_file(file_version.file_id)
self._print(file_version.file_id, 'canceled')
return 0


Expand Down Expand Up @@ -2257,7 +2238,7 @@ def timestamp_display(self, timestamp_or_none):
return dt.strftime('%Y-%m-%d'), dt.strftime('%H:%M:%S')


class ListParts(Command):
class FileLargePartsBase(Command):
"""
Lists all of the parts that have been uploaded for the given
large file, which must be a file that was started but not
Expand All @@ -2268,18 +2249,14 @@ class ListParts(Command):
- **writeFiles**
"""

@classmethod
def _setup_parser(cls, parser):
parser.add_argument('largeFileId')
super()._setup_parser(parser)

def _run(self, args):
for part in self.api.list_parts(args.largeFileId):
b2_uri = self.get_b2_uri_from_arg(args)
for part in self.api.list_parts(b2_uri.file_id):
self._print('%5d %9d %s' % (part.part_number, part.content_length, part.content_sha1))
return 0


class ListUnfinishedLargeFiles(Command):
class FileLargeUnfinishedListBase(Command):
"""
Lists all of the large files in the bucket that were started,
but not finished or canceled.
Expand All @@ -2289,13 +2266,9 @@ class ListUnfinishedLargeFiles(Command):
- **listFiles**
"""

@classmethod
def _setup_parser(cls, parser):
add_bucket_name_argument(parser)
super()._setup_parser(parser)

def _run(self, args):
bucket = self.api.get_bucket_by_name(args.bucketName)
b2_uri = self.get_b2_uri_from_arg(args)
bucket = self.api.get_bucket_by_name(b2_uri.bucket_name)
for unfinished in bucket.list_unfinished_large_files():
file_info_text = ' '.join(
f'{k}={unfinished.file_info[k]}' for k in sorted(unfinished.file_info)
Expand Down Expand Up @@ -5121,6 +5094,102 @@ class DeleteFileVersion(CmdReplacedByMixin, DeleteFileVersionBase):
replaced_by_cmd = Rm


@File.subcommands_registry.register
class FileLarge(Command):
"""
Large file uploads management subcommands.
For more information on each subcommand, use ``{NAME} file large SUBCOMMAND --help``.
Examples:
.. code-block::
{NAME} file large parts
{NAME} file large unfinished
"""
COMMAND_NAME = 'large'
subcommands_registry = ClassRegistry(attr_name='COMMAND_NAME')


@FileLarge.subcommands_registry.register
class FileLargeParts(FileLargePartsBase):
__doc__ = FileLargePartsBase.__doc__
COMMAND_NAME = 'parts'


@FileLarge.subcommands_registry.register
class FileLargeUnfinished(Command):
"""
Large file unfinished uploads management subcommands.
For more information on each subcommand, use ``{NAME} file large unfinished SUBCOMMAND --help``.
Examples:
.. code-block::
{NAME} file large unfinished list
{NAME} file large unfinished cancel
"""
COMMAND_NAME = 'unfinished'
subcommands_registry = ClassRegistry(attr_name='COMMAND_NAME')


@FileLargeUnfinished.subcommands_registry.register
class FileLargeUnfinishedList(FileLargeUnfinishedListBase):
__doc__ = FileLargePartsBase.__doc__
COMMAND_NAME = 'list'


@FileLargeUnfinished.subcommands_registry.register
class FileLargeUnfinishedCancel(FileLargeUnfinishedCancelBase):
__doc__ = FileLargeUnfinishedCancelBase.__doc__
COMMAND_NAME = 'cancel'


class ListParts(CmdReplacedByMixin, B2URIFileIDArgMixin, FileLargePartsBase):
__doc__ = FileLargePartsBase.__doc__
replaced_by_cmd = (File, FileLarge, FileLargeParts)


class ListUnfinishedLargeFiles(
CmdReplacedByMixin, B2URIBucketArgMixin, FileLargeUnfinishedListBase
):
__doc__ = FileLargeUnfinishedListBase.__doc__
replaced_by_cmd = (File, FileLarge, FileLargeUnfinished, FileLargeUnfinishedList)


class CancelAllUnfinishedLargeFiles(
CmdReplacedByMixin, B2URIBucketArgMixin, FileLargeUnfinishedCancelBase
):
"""
Lists all large files that have been started but not
finished and cancels them. Any parts that have been
uploaded will be deleted.
Requires capability:
- **listFiles**
- **writeFiles**
"""
replaced_by_cmd = (File, FileLarge, FileLargeUnfinished, FileLargeUnfinishedCancel)


class CancelLargeFile(CmdReplacedByMixin, B2URIFileIDArgMixin, FileLargeUnfinishedCancelBase):
"""
Cancels a large file upload. Used to undo a ``start-large-file``.
Cannot be used once the file is finished. After finishing,
using ``delete-file-version`` to delete the large file.
Requires capability:
- **writeFiles**
"""
replaced_by_cmd = (File, FileLarge, FileLargeUnfinished, FileLargeUnfinishedCancel)


class ConsoleTool:
"""
Implements the commands available in the B2 command-line tool
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+command-file-large.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `file large {parts|unfinished list|unfinished cancel}` commands.
3 changes: 3 additions & 0 deletions changelog.d/+command-file-large.deprecated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Deprecated `list-parts`, use `file large parts` instead.
Deprecated `list-unfinished-large-files`, use `file large unfinished list` instead.
Deprecated `cancel-large-file` amd `cancel-all-unfinished-large-files`, use `file large unfinished cancel` instead.

0 comments on commit 429a500

Please sign in to comment.