cloudsh 0.3.10__tar.gz → 0.3.12__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cloudsh-0.3.10 → cloudsh-0.3.12}/.gitignore +3 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/PKG-INFO +28 -12
- {cloudsh-0.3.10 → cloudsh-0.3.12}/README.md +6 -6
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/cp.toml +6 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/less.toml +2 -2
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/mv.toml +1 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/sink.toml +1 -1
- cloudsh-0.3.12/cloudsh/commands/_common.py +83 -0
- cloudsh-0.3.12/cloudsh/commands/_pager_buffer.py +158 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/commands/cat.py +70 -20
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/commands/complete.py +19 -10
- cloudsh-0.3.12/cloudsh/commands/cp.py +198 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/commands/head.py +50 -17
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/commands/less.py +26 -38
- cloudsh-0.3.12/cloudsh/commands/ls.py +372 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/commands/mkdir.py +5 -4
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/commands/more.py +52 -53
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/commands/mv.py +50 -36
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/commands/rm.py +5 -4
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/commands/sink.py +2 -1
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/commands/tail.py +193 -28
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/commands/touch.py +4 -7
- cloudsh-0.3.12/cloudsh/main.py +177 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/utils.py +1 -1
- cloudsh-0.3.12/junit/test-results.xml +1 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/pyproject.toml +34 -17
- cloudsh-0.3.10/.github/copilot-instructions.md +0 -115
- cloudsh-0.3.10/.github/workflows/build.yml +0 -43
- cloudsh-0.3.10/.github/workflows/release.yml +0 -28
- cloudsh-0.3.10/cloudsh/commands/cp.py +0 -159
- cloudsh-0.3.10/cloudsh/commands/ls.py +0 -264
- cloudsh-0.3.10/cloudsh/main.py +0 -66
- cloudsh-0.3.10/tests/__init__.py +0 -0
- cloudsh-0.3.10/tests/conftest.py +0 -37
- cloudsh-0.3.10/tests/test_cat.py +0 -342
- cloudsh-0.3.10/tests/test_complete.py +0 -397
- cloudsh-0.3.10/tests/test_cp.py +0 -553
- cloudsh-0.3.10/tests/test_head.py +0 -626
- cloudsh-0.3.10/tests/test_less.py +0 -646
- cloudsh-0.3.10/tests/test_ls.py +0 -371
- cloudsh-0.3.10/tests/test_main_module.py +0 -80
- cloudsh-0.3.10/tests/test_mkdir.py +0 -148
- cloudsh-0.3.10/tests/test_more.py +0 -380
- cloudsh-0.3.10/tests/test_mv.py +0 -877
- cloudsh-0.3.10/tests/test_rm.py +0 -466
- cloudsh-0.3.10/tests/test_sink.py +0 -166
- cloudsh-0.3.10/tests/test_tail.py +0 -801
- cloudsh-0.3.10/tests/test_touch.py +0 -230
- cloudsh-0.3.10/uv.lock +0 -2977
- {cloudsh-0.3.10 → cloudsh-0.3.12}/.coveragerc +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/LICENSE +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/__init__.py +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/__main__.py +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/cat.toml +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/complete.toml +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/head.toml +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/ls.toml +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/mkdir.toml +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/more.toml +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/rm.toml +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/tail.toml +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/cloudsh/args/touch.toml +0 -0
- {cloudsh-0.3.10 → cloudsh-0.3.12}/tox.ini +0 -0
|
@@ -1,44 +1,60 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: cloudsh
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.12
|
|
4
4
|
Summary: A Python CLI wrapping common Linux commands for local/cloud files.
|
|
5
|
-
Author-email:
|
|
5
|
+
Author-email: pwwang <pwwang@pwwang.com>
|
|
6
6
|
License-Expression: MIT
|
|
7
7
|
License-File: LICENSE
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
8
18
|
Requires-Python: >=3.9
|
|
9
19
|
Requires-Dist: aiofiles>=23.0.0
|
|
10
20
|
Requires-Dist: argcomplete<3.7,>=3.5.3; python_version < '3.10'
|
|
11
21
|
Requires-Dist: argcomplete<4,>=3.5.3; python_version >= '3.10'
|
|
12
22
|
Requires-Dist: argx<0.5,>=0.4
|
|
13
|
-
Requires-Dist: panpath<0.
|
|
23
|
+
Requires-Dist: panpath<0.6,>=0.5.0
|
|
14
24
|
Requires-Dist: python-dateutil<3,>=2.9.0.post0
|
|
15
25
|
Requires-Dist: python-simpleconf[toml]<0.10,>=0.9
|
|
16
26
|
Provides-Extra: all
|
|
17
27
|
Requires-Dist: aioboto3>=11.0.0; extra == 'all'
|
|
18
|
-
Requires-Dist: azure-storage-blob<13,>=12; extra == 'all'
|
|
28
|
+
Requires-Dist: azure-storage-blob[aio]<13,>=12; extra == 'all'
|
|
29
|
+
Requires-Dist: boto3; extra == 'all'
|
|
19
30
|
Requires-Dist: gcloud-aio-storage>=9.5; extra == 'all'
|
|
31
|
+
Requires-Dist: google-cloud-storage>=2; extra == 'all'
|
|
20
32
|
Provides-Extra: aws
|
|
21
33
|
Requires-Dist: aioboto3>=11.0.0; extra == 'aws'
|
|
34
|
+
Requires-Dist: boto3; extra == 'aws'
|
|
22
35
|
Provides-Extra: azure
|
|
23
|
-
Requires-Dist: azure-storage-blob<13,>=12; extra == 'azure'
|
|
36
|
+
Requires-Dist: azure-storage-blob[aio]<13,>=12; extra == 'azure'
|
|
24
37
|
Provides-Extra: gcs
|
|
25
38
|
Requires-Dist: gcloud-aio-storage>=9.5; extra == 'gcs'
|
|
39
|
+
Requires-Dist: google-cloud-storage>=2; extra == 'gcs'
|
|
26
40
|
Provides-Extra: gs
|
|
27
41
|
Requires-Dist: gcloud-aio-storage>=9.5; extra == 'gs'
|
|
42
|
+
Requires-Dist: google-cloud-storage>=2; extra == 'gs'
|
|
28
43
|
Provides-Extra: s3
|
|
29
44
|
Requires-Dist: aioboto3>=11.0.0; extra == 's3'
|
|
45
|
+
Requires-Dist: boto3; extra == 's3'
|
|
30
46
|
Description-Content-Type: text/markdown
|
|
31
47
|
|
|
32
48
|
# cloudsh
|
|
33
49
|
|
|
34
|
-
A Python CLI that wraps common Linux commands for both local and cloud files using [
|
|
50
|
+
A Python CLI that wraps common Linux commands for both local and cloud files using [panpath](https://github.com/pwwang/panpath).
|
|
35
51
|
|
|
36
52
|
[![Pypi][1]][2] [![Github][3]][4] [![PythonVers][5]][6] ![Building][7] [![Codacy coverage][8]][9]
|
|
37
53
|
|
|
38
54
|
Cloud Storage Provider dependencies:
|
|
39
55
|
|
|
40
|
-
[![
|
|
41
|
-
[![
|
|
56
|
+
[![gcloud-aio-storage][10]][11] \
|
|
57
|
+
[![aioboto3][12]][13] \
|
|
42
58
|
[![azure-storage-blob][14]][15]
|
|
43
59
|
|
|
44
60
|
## Installation
|
|
@@ -78,7 +94,7 @@ And two additional commands:
|
|
|
78
94
|
|
|
79
95
|
### Authentication
|
|
80
96
|
|
|
81
|
-
See
|
|
97
|
+
See https://github.com/pwwang/panpath for cloud backend configuration (ADC / `GOOGLE_APPLICATION_CREDENTIALS` for GCS, `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` for S3, and `AZURE_STORAGE_CONNECTION_STRING` for Azure).
|
|
82
98
|
|
|
83
99
|
### The commands works on local files as the GNU/Linux commands do
|
|
84
100
|
|
|
@@ -196,8 +212,8 @@ cloudsh complete --update-cache --depth 2 gs://my-bucket
|
|
|
196
212
|
[8]: https://img.shields.io/codacy/coverage/bb84185297244aefa6de4d675206a1cf?style=flat-square
|
|
197
213
|
[9]: https://app.codacy.com/gh/pwwang/cloudsh/dashboard
|
|
198
214
|
[10]: https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fpwwang%2Fcloudsh%2Fmaster%2Fpyproject.toml&query=%24.%22project.optional-dependencies%22.gcs%5B0%5D&prefix=requires%3A&style=flat-square&label=gcloud-aio-storage
|
|
199
|
-
[11]: https://
|
|
215
|
+
[11]: https://pypi.org/project/gcloud-aio-storage/
|
|
200
216
|
[12]: https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fpwwang%2Fcloudsh%2Fmaster%2Fpyproject.toml&query=%24.%22project.optional-dependencies%22.aws%5B0%5D&prefix=requires%3A&style=flat-square&label=aioboto3
|
|
201
|
-
[13]: https://pypi.org/project/
|
|
217
|
+
[13]: https://pypi.org/project/aioboto3/
|
|
202
218
|
[14]: https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fpwwang%2Fcloudsh%2Fmaster%2Fpyproject.toml&query=%24.%22project.optional-dependencies%22.azure%5B0%5D&prefix=requires%3A&style=flat-square&label=azure-storage-blob
|
|
203
219
|
[15]: https://pypi.org/project/azure-storage-blob/
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# cloudsh
|
|
2
2
|
|
|
3
|
-
A Python CLI that wraps common Linux commands for both local and cloud files using [
|
|
3
|
+
A Python CLI that wraps common Linux commands for both local and cloud files using [panpath](https://github.com/pwwang/panpath).
|
|
4
4
|
|
|
5
5
|
[![Pypi][1]][2] [![Github][3]][4] [![PythonVers][5]][6] ![Building][7] [![Codacy coverage][8]][9]
|
|
6
6
|
|
|
7
7
|
Cloud Storage Provider dependencies:
|
|
8
8
|
|
|
9
|
-
[![
|
|
10
|
-
[![
|
|
9
|
+
[![gcloud-aio-storage][10]][11] \
|
|
10
|
+
[![aioboto3][12]][13] \
|
|
11
11
|
[![azure-storage-blob][14]][15]
|
|
12
12
|
|
|
13
13
|
## Installation
|
|
@@ -47,7 +47,7 @@ And two additional commands:
|
|
|
47
47
|
|
|
48
48
|
### Authentication
|
|
49
49
|
|
|
50
|
-
See
|
|
50
|
+
See https://github.com/pwwang/panpath for cloud backend configuration (ADC / `GOOGLE_APPLICATION_CREDENTIALS` for GCS, `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` for S3, and `AZURE_STORAGE_CONNECTION_STRING` for Azure).
|
|
51
51
|
|
|
52
52
|
### The commands works on local files as the GNU/Linux commands do
|
|
53
53
|
|
|
@@ -165,8 +165,8 @@ cloudsh complete --update-cache --depth 2 gs://my-bucket
|
|
|
165
165
|
[8]: https://img.shields.io/codacy/coverage/bb84185297244aefa6de4d675206a1cf?style=flat-square
|
|
166
166
|
[9]: https://app.codacy.com/gh/pwwang/cloudsh/dashboard
|
|
167
167
|
[10]: https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fpwwang%2Fcloudsh%2Fmaster%2Fpyproject.toml&query=%24.%22project.optional-dependencies%22.gcs%5B0%5D&prefix=requires%3A&style=flat-square&label=gcloud-aio-storage
|
|
168
|
-
[11]: https://
|
|
168
|
+
[11]: https://pypi.org/project/gcloud-aio-storage/
|
|
169
169
|
[12]: https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fpwwang%2Fcloudsh%2Fmaster%2Fpyproject.toml&query=%24.%22project.optional-dependencies%22.aws%5B0%5D&prefix=requires%3A&style=flat-square&label=aioboto3
|
|
170
|
-
[13]: https://pypi.org/project/
|
|
170
|
+
[13]: https://pypi.org/project/aioboto3/
|
|
171
171
|
[14]: https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fpwwang%2Fcloudsh%2Fmaster%2Fpyproject.toml&query=%24.%22project.optional-dependencies%22.azure%5B0%5D&prefix=requires%3A&style=flat-square&label=azure-storage-blob
|
|
172
172
|
[15]: https://pypi.org/project/azure-storage-blob/
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
name = "cp"
|
|
2
|
+
usage = """
|
|
3
|
+
cp [OPTION]... [-T] SOURCE DEST
|
|
4
|
+
cp [OPTION]... SOURCE... DIRECTORY
|
|
5
|
+
cp [OPTION]... -t DIRECTORY SOURCE...
|
|
6
|
+
"""
|
|
2
7
|
help = "Copy files and directories"
|
|
3
8
|
description = """
|
|
4
9
|
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
|
|
@@ -14,6 +19,7 @@ nargs = "+"
|
|
|
14
19
|
[[arguments]]
|
|
15
20
|
flags = ["DEST"]
|
|
16
21
|
metavar = "DEST"
|
|
22
|
+
nargs = "?"
|
|
17
23
|
help = "Destination file or directory"
|
|
18
24
|
|
|
19
25
|
[[arguments]]
|
|
@@ -5,7 +5,7 @@ Display FILE(s) with the ability to scroll forward and backward through the cont
|
|
|
5
5
|
Less is more powerful than more, providing features like search, backward scrolling,
|
|
6
6
|
and pattern matching.
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
Reading from standard input is not supported; pass one or more FILE paths.
|
|
9
9
|
"""
|
|
10
10
|
path_options = "file"
|
|
11
11
|
|
|
@@ -13,7 +13,7 @@ path_options = "file"
|
|
|
13
13
|
flags = ["file"]
|
|
14
14
|
metavar = "FILE"
|
|
15
15
|
nargs = "*"
|
|
16
|
-
help = "Files to display
|
|
16
|
+
help = "Files to display"
|
|
17
17
|
|
|
18
18
|
[[arguments]]
|
|
19
19
|
flags = ["-E", "--QUIT-AT-EOF"]
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""Shared helpers for cloudsh file and directory commands."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
import sys
|
|
7
|
+
from typing import TYPE_CHECKING, List, Optional, Tuple
|
|
8
|
+
|
|
9
|
+
from panpath import CloudPath, PanPath
|
|
10
|
+
|
|
11
|
+
from ..utils import PACKAGE
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from argx import Namespace
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
async def path_exists(path: PanPath) -> bool:
|
|
18
|
+
"""Return True if path exists or is a markerless cloud directory."""
|
|
19
|
+
if isinstance(path, CloudPath):
|
|
20
|
+
exists, _ = await path_info(path)
|
|
21
|
+
return exists
|
|
22
|
+
return await path.a_exists()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
async def path_info(path: PanPath) -> Tuple[bool, bool]:
|
|
26
|
+
"""Return ``(exists, is_dir)`` for ``path`` with as few cloud probes as possible.
|
|
27
|
+
|
|
28
|
+
panpath backends can answer both questions from one capped prefix listing
|
|
29
|
+
through ``a_path_info``; use it when available. Older panpath releases lack
|
|
30
|
+
it, so the fallback below probes ``is_dir`` first and treats a markerless
|
|
31
|
+
cloud directory as existing.
|
|
32
|
+
"""
|
|
33
|
+
probe = getattr(path, "a_path_info", None)
|
|
34
|
+
if probe is not None:
|
|
35
|
+
return await probe()
|
|
36
|
+
if isinstance(path, CloudPath):
|
|
37
|
+
is_dir = await path.a_is_dir()
|
|
38
|
+
if is_dir:
|
|
39
|
+
return True, True
|
|
40
|
+
return await path.a_exists(), False
|
|
41
|
+
exists = await path.a_exists()
|
|
42
|
+
return exists, exists and await path.a_is_dir()
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def to_timestamp(value: object) -> Optional[float]:
|
|
46
|
+
"""Normalize float/int or datetime stat timestamps to POSIX seconds."""
|
|
47
|
+
if value is None:
|
|
48
|
+
return None
|
|
49
|
+
if isinstance(value, datetime):
|
|
50
|
+
return value.timestamp()
|
|
51
|
+
if isinstance(value, (int, float)):
|
|
52
|
+
return float(value)
|
|
53
|
+
return None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def resolve_sources_and_dest(
|
|
57
|
+
args: Namespace,
|
|
58
|
+
command: str,
|
|
59
|
+
*,
|
|
60
|
+
strip_sources: bool = False,
|
|
61
|
+
) -> Tuple[List[str], str]:
|
|
62
|
+
"""Resolve SOURCE(s) and destination honoring optional DEST with -t."""
|
|
63
|
+
|
|
64
|
+
def _norm(items: List[str]) -> List[str]:
|
|
65
|
+
if strip_sources:
|
|
66
|
+
return [item.rstrip("/") for item in items]
|
|
67
|
+
return list(items)
|
|
68
|
+
|
|
69
|
+
if args.target_directory:
|
|
70
|
+
return _norm(args.SOURCE), args.target_directory.rstrip("/")
|
|
71
|
+
if getattr(args, "DEST", None) is not None:
|
|
72
|
+
return _norm(args.SOURCE), args.DEST.rstrip("/")
|
|
73
|
+
if len(args.SOURCE) < 2:
|
|
74
|
+
operand = args.SOURCE[0] if args.SOURCE else ""
|
|
75
|
+
msg = (
|
|
76
|
+
f"{PACKAGE} {command}: missing destination file operand "
|
|
77
|
+
f"after '{operand}'"
|
|
78
|
+
if operand
|
|
79
|
+
else f"{PACKAGE} {command}: missing file operand"
|
|
80
|
+
)
|
|
81
|
+
print(msg, file=sys.stderr)
|
|
82
|
+
sys.exit(1)
|
|
83
|
+
return _norm(args.SOURCE[:-1]), args.SOURCE[-1].rstrip("/")
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"""Bounded-memory line buffering shared by the less and more pagers.
|
|
2
|
+
|
|
3
|
+
The pagers need random access to every line (search, jumps, status), but
|
|
4
|
+
materialising a whole object as a ``list`` of bytes costs the object's size in
|
|
5
|
+
RAM. :class:`LineBuffer` instead spools the bytes to a
|
|
6
|
+
``SpooledTemporaryFile`` (kept in memory up to 8 MiB, then on disk) and stores
|
|
7
|
+
only one byte offset per line, so memory stays near one read chunk plus the
|
|
8
|
+
offset table regardless of the object size.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import inspect
|
|
14
|
+
import tempfile
|
|
15
|
+
from array import array
|
|
16
|
+
from typing import Any, Callable, Optional
|
|
17
|
+
|
|
18
|
+
_SPOOL_MAX_SIZE = 8 * 1024 * 1024
|
|
19
|
+
_DEFAULT_CHUNK_SIZE = 1024 * 1024
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class LineBuffer:
|
|
23
|
+
"""Random-access, line-oriented view over buffered bytes."""
|
|
24
|
+
|
|
25
|
+
def __init__(self) -> None:
|
|
26
|
+
self._spool = tempfile.SpooledTemporaryFile(
|
|
27
|
+
max_size=_SPOOL_MAX_SIZE, mode="w+b"
|
|
28
|
+
)
|
|
29
|
+
self._offsets = array("Q")
|
|
30
|
+
|
|
31
|
+
def __len__(self) -> int:
|
|
32
|
+
return len(self._offsets)
|
|
33
|
+
|
|
34
|
+
def __getitem__(self, index: int) -> bytes:
|
|
35
|
+
"""Return line ``index`` including its trailing newline, if any."""
|
|
36
|
+
offset = self._offsets[index]
|
|
37
|
+
self._spool.seek(offset)
|
|
38
|
+
return self._spool.readline()
|
|
39
|
+
|
|
40
|
+
def close(self) -> None:
|
|
41
|
+
"""Close (and delete) the underlying spool. Safe to call twice."""
|
|
42
|
+
spool = getattr(self, "_spool", None)
|
|
43
|
+
if spool is not None:
|
|
44
|
+
self._spool = None
|
|
45
|
+
spool.close()
|
|
46
|
+
|
|
47
|
+
def __enter__(self) -> "LineBuffer":
|
|
48
|
+
return self
|
|
49
|
+
|
|
50
|
+
def __exit__(self, *exc_info: Any) -> None:
|
|
51
|
+
self.close()
|
|
52
|
+
|
|
53
|
+
def __del__(self) -> None:
|
|
54
|
+
try:
|
|
55
|
+
self.close()
|
|
56
|
+
except Exception:
|
|
57
|
+
pass
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _is_blank(piece: bytes) -> bool:
|
|
61
|
+
return not piece.strip()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
async def build_line_buffer(
|
|
65
|
+
fh: Any,
|
|
66
|
+
*,
|
|
67
|
+
chunk_size: int = _DEFAULT_CHUNK_SIZE,
|
|
68
|
+
squeeze: bool = False,
|
|
69
|
+
transform: Optional[Callable[[bytes], bytes]] = None,
|
|
70
|
+
) -> LineBuffer:
|
|
71
|
+
"""Stream ``fh`` into a :class:`LineBuffer`.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
fh: Sync or async file handle opened in binary mode.
|
|
75
|
+
chunk_size: Number of bytes requested per read.
|
|
76
|
+
squeeze: Collapse runs of blank lines (a blank line whose previously
|
|
77
|
+
emitted line was also blank is dropped).
|
|
78
|
+
transform: Optional per-line bytes transform applied before the
|
|
79
|
+
squeeze rule and before the line is stored.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
The populated buffer. Peak Python memory is about one chunk plus the
|
|
83
|
+
offset table, independent of the object size.
|
|
84
|
+
"""
|
|
85
|
+
buf = LineBuffer()
|
|
86
|
+
spool = buf._spool
|
|
87
|
+
offsets = buf._offsets
|
|
88
|
+
|
|
89
|
+
write_pos = 0
|
|
90
|
+
line_start = 0
|
|
91
|
+
line_blank = True
|
|
92
|
+
prev_blank = False
|
|
93
|
+
pending = bytearray() if transform is not None else None
|
|
94
|
+
|
|
95
|
+
def finish_line(is_blank: bool) -> None:
|
|
96
|
+
nonlocal line_start, write_pos, prev_blank
|
|
97
|
+
if squeeze and prev_blank and is_blank:
|
|
98
|
+
# Drop the line just written; nothing after it is in the spool yet.
|
|
99
|
+
spool.seek(line_start)
|
|
100
|
+
spool.truncate(line_start)
|
|
101
|
+
write_pos = line_start
|
|
102
|
+
return
|
|
103
|
+
offsets.append(line_start)
|
|
104
|
+
prev_blank = is_blank
|
|
105
|
+
line_start = write_pos
|
|
106
|
+
|
|
107
|
+
try:
|
|
108
|
+
while True:
|
|
109
|
+
chunk = fh.read(chunk_size)
|
|
110
|
+
if inspect.isawaitable(chunk):
|
|
111
|
+
chunk = await chunk
|
|
112
|
+
if not chunk:
|
|
113
|
+
break
|
|
114
|
+
|
|
115
|
+
start = 0
|
|
116
|
+
while start < len(chunk):
|
|
117
|
+
newline = chunk.find(b"\n", start)
|
|
118
|
+
if newline < 0:
|
|
119
|
+
piece = chunk[start:]
|
|
120
|
+
if pending is None:
|
|
121
|
+
spool.write(piece)
|
|
122
|
+
write_pos += len(piece)
|
|
123
|
+
line_blank = line_blank and _is_blank(piece)
|
|
124
|
+
else:
|
|
125
|
+
pending += piece
|
|
126
|
+
break
|
|
127
|
+
|
|
128
|
+
piece = chunk[start : newline + 1]
|
|
129
|
+
if pending is None:
|
|
130
|
+
spool.write(piece)
|
|
131
|
+
write_pos += len(piece)
|
|
132
|
+
line_blank = line_blank and _is_blank(piece)
|
|
133
|
+
else:
|
|
134
|
+
pending += piece
|
|
135
|
+
line = transform(bytes(pending))
|
|
136
|
+
pending.clear()
|
|
137
|
+
spool.write(line)
|
|
138
|
+
write_pos += len(line)
|
|
139
|
+
line_blank = _is_blank(line)
|
|
140
|
+
finish_line(line_blank)
|
|
141
|
+
line_blank = True
|
|
142
|
+
start = newline + 1
|
|
143
|
+
|
|
144
|
+
if pending is not None:
|
|
145
|
+
if pending:
|
|
146
|
+
line = transform(bytes(pending))
|
|
147
|
+
spool.write(line)
|
|
148
|
+
write_pos += len(line)
|
|
149
|
+
finish_line(_is_blank(line))
|
|
150
|
+
elif write_pos > line_start:
|
|
151
|
+
# Final line without a trailing newline.
|
|
152
|
+
finish_line(line_blank)
|
|
153
|
+
except BaseException:
|
|
154
|
+
# Ctrl-C or a backend error mid-read: drop the spool before re-raising.
|
|
155
|
+
buf.close()
|
|
156
|
+
raise
|
|
157
|
+
|
|
158
|
+
return buf
|
|
@@ -6,7 +6,8 @@ import sys
|
|
|
6
6
|
import asyncio
|
|
7
7
|
from typing import TYPE_CHECKING, AsyncGenerator
|
|
8
8
|
|
|
9
|
-
from panpath import PanPath, LocalPath
|
|
9
|
+
from panpath import PanPath, CloudPath, LocalPath
|
|
10
|
+
from panpath.exceptions import NoStatError
|
|
10
11
|
from panpath.clients import AsyncFileHandle
|
|
11
12
|
|
|
12
13
|
from ..utils import PACKAGE
|
|
@@ -15,6 +16,33 @@ if TYPE_CHECKING:
|
|
|
15
16
|
from argx import Namespace
|
|
16
17
|
|
|
17
18
|
|
|
19
|
+
def _escape_nonprinting(line: bytes) -> bytes:
|
|
20
|
+
"""Render control and high bytes in caret/M- notation, as GNU cat -v does.
|
|
21
|
+
|
|
22
|
+
Pure and allocation-light: one pass over the input, one output buffer.
|
|
23
|
+
"""
|
|
24
|
+
out = bytearray()
|
|
25
|
+
for char in line:
|
|
26
|
+
if char in (9, 10): # GNU cat -v leaves TAB and NL untouched
|
|
27
|
+
out.append(char)
|
|
28
|
+
elif char < 32:
|
|
29
|
+
out.extend((ord("^"), char + 64))
|
|
30
|
+
elif char == 127:
|
|
31
|
+
out.extend((ord("^"), ord("?")))
|
|
32
|
+
elif char >= 128:
|
|
33
|
+
out.extend((ord("M"), ord("-")))
|
|
34
|
+
high = char - 128
|
|
35
|
+
if high == 127:
|
|
36
|
+
out.extend((ord("^"), ord("?")))
|
|
37
|
+
elif high < 32:
|
|
38
|
+
out.extend((ord("^"), high + 64))
|
|
39
|
+
else:
|
|
40
|
+
out.append(high)
|
|
41
|
+
else:
|
|
42
|
+
out.append(char)
|
|
43
|
+
return bytes(out)
|
|
44
|
+
|
|
45
|
+
|
|
18
46
|
async def _process_stdin(args) -> None:
|
|
19
47
|
"""Process stdin line by line, echoing each line immediately like GNU cat.
|
|
20
48
|
|
|
@@ -99,6 +127,8 @@ async def _process_stdin(args) -> None:
|
|
|
99
127
|
# Wait for all tasks
|
|
100
128
|
await asyncio.gather(stdin_task, stdout_task, stderr_task)
|
|
101
129
|
await process.wait()
|
|
130
|
+
if process.returncode:
|
|
131
|
+
sys.exit(process.returncode)
|
|
102
132
|
|
|
103
133
|
except (asyncio.CancelledError, KeyboardInterrupt):
|
|
104
134
|
# Cancel all tasks
|
|
@@ -158,6 +188,8 @@ async def _process_local_file(
|
|
|
158
188
|
sys.stdout.buffer.write(stdout)
|
|
159
189
|
if stderr:
|
|
160
190
|
sys.stderr.buffer.write(stderr)
|
|
191
|
+
if process.returncode:
|
|
192
|
+
sys.exit(process.returncode)
|
|
161
193
|
|
|
162
194
|
|
|
163
195
|
async def _process_cloud_file(
|
|
@@ -173,6 +205,27 @@ async def _process_cloud_file(
|
|
|
173
205
|
Yields:
|
|
174
206
|
Processed lines of output
|
|
175
207
|
"""
|
|
208
|
+
has_formatting = any(
|
|
209
|
+
[
|
|
210
|
+
args.number,
|
|
211
|
+
args.number_nonblank,
|
|
212
|
+
args.squeeze_blank,
|
|
213
|
+
args.show_ends,
|
|
214
|
+
args.show_tabs,
|
|
215
|
+
args.show_nonprinting,
|
|
216
|
+
args.show_all,
|
|
217
|
+
args.e,
|
|
218
|
+
args.t,
|
|
219
|
+
]
|
|
220
|
+
)
|
|
221
|
+
if not has_formatting:
|
|
222
|
+
while True:
|
|
223
|
+
chunk = await fh.read(1024 * 1024)
|
|
224
|
+
if not chunk:
|
|
225
|
+
break
|
|
226
|
+
yield chunk
|
|
227
|
+
return
|
|
228
|
+
|
|
176
229
|
line_num = 0
|
|
177
230
|
last_empty = False
|
|
178
231
|
|
|
@@ -188,10 +241,12 @@ async def _process_cloud_file(
|
|
|
188
241
|
last_empty = is_empty
|
|
189
242
|
|
|
190
243
|
# Line numbering
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
244
|
+
if args.number_nonblank:
|
|
245
|
+
if not is_empty:
|
|
246
|
+
line_num += 1
|
|
247
|
+
yield f"{line_num:6}\t".encode()
|
|
248
|
+
elif args.number:
|
|
249
|
+
line_num += 1
|
|
195
250
|
yield f"{line_num:6}\t".encode()
|
|
196
251
|
|
|
197
252
|
# Handle special characters
|
|
@@ -200,17 +255,7 @@ async def _process_cloud_file(
|
|
|
200
255
|
|
|
201
256
|
if args.show_nonprinting or args.show_all or args.t or args.e:
|
|
202
257
|
# Convert non-printing characters to ^ notation
|
|
203
|
-
|
|
204
|
-
for char in line:
|
|
205
|
-
if char < 32 and char != 10: # Not newline
|
|
206
|
-
chars.append(b"^" + bytes([char + 64]))
|
|
207
|
-
elif char == 127:
|
|
208
|
-
chars.append(b"^?")
|
|
209
|
-
elif char >= 128:
|
|
210
|
-
chars.append(b"M-" + bytes([char - 128]))
|
|
211
|
-
else:
|
|
212
|
-
chars.append(bytes([char]))
|
|
213
|
-
line = b"".join(chars)
|
|
258
|
+
line = _escape_nonprinting(line)
|
|
214
259
|
|
|
215
260
|
# Add $ at end of line
|
|
216
261
|
if args.show_ends or args.show_all or args.show_ends or args.e:
|
|
@@ -264,16 +309,21 @@ async def run(args: Namespace) -> None:
|
|
|
264
309
|
# Use cat command for local files or stdin
|
|
265
310
|
await _process_local_file(file, args)
|
|
266
311
|
else:
|
|
267
|
-
#
|
|
268
|
-
|
|
269
|
-
|
|
312
|
+
# Cloud file: use 1 MiB reads even for formatted
|
|
313
|
+
# output rather than panpath's 4 KiB default.
|
|
314
|
+
open_kwargs = (
|
|
315
|
+
{"chunk_size": 1048576}
|
|
316
|
+
if isinstance(path, CloudPath)
|
|
317
|
+
else {}
|
|
318
|
+
)
|
|
319
|
+
async with path.a_open("rb", **open_kwargs) as fh:
|
|
270
320
|
async for chunk in _process_cloud_file(fh, args):
|
|
271
321
|
sys.stdout.buffer.write(chunk)
|
|
272
322
|
sys.stdout.buffer.flush()
|
|
273
323
|
except BrokenPipeError:
|
|
274
324
|
sys.stderr.close() # Prevent additional errors
|
|
275
325
|
sys.exit(141) # Standard Unix practice
|
|
276
|
-
except (OSError, IOError) as e:
|
|
326
|
+
except (OSError, IOError, NoStatError) as e:
|
|
277
327
|
print(f"cat: {file}: {str(e)}", file=sys.stderr)
|
|
278
328
|
sys.exit(1)
|
|
279
329
|
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
import asyncio
|
|
5
6
|
import os
|
|
6
7
|
import sys
|
|
7
8
|
import glob
|
|
@@ -25,26 +26,26 @@ async def _scan_path(path: str, depth: int = -1) -> AsyncGenerator[str, None, No
|
|
|
25
26
|
print(f"{PACKAGE} complete: only cloud paths are supported", file=sys.stderr)
|
|
26
27
|
sys.exit(1)
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
is_dir = await apath.a_is_dir()
|
|
30
|
+
if not await apath.a_exists() and not is_dir:
|
|
29
31
|
print(f"{PACKAGE} complete: path does not exist: {path}", file=sys.stderr)
|
|
30
32
|
sys.exit(1)
|
|
31
33
|
|
|
32
|
-
if not
|
|
33
|
-
print(apath)
|
|
34
|
-
print(repr(apath))
|
|
34
|
+
if not is_dir:
|
|
35
35
|
print(f"{PACKAGE} complete: path is not a directory: {path}", file=sys.stderr)
|
|
36
36
|
yield path
|
|
37
|
+
return
|
|
37
38
|
|
|
38
39
|
if depth == 0:
|
|
39
40
|
yield path.rstrip("/") + "/"
|
|
40
41
|
return
|
|
41
42
|
|
|
42
|
-
dep = 0
|
|
43
43
|
async for p in apath.a_iterdir():
|
|
44
44
|
if await p.a_is_dir():
|
|
45
45
|
yield str(p).rstrip("/") + "/"
|
|
46
|
-
if depth == -1 or
|
|
47
|
-
|
|
46
|
+
if depth == -1 or depth > 1:
|
|
47
|
+
next_depth = -1 if depth == -1 else depth - 1
|
|
48
|
+
async for r in _scan_path(str(p), next_depth):
|
|
48
49
|
yield r
|
|
49
50
|
else:
|
|
50
51
|
yield str(p)
|
|
@@ -55,7 +56,9 @@ async def _read_cache() -> AsyncGenerator[str, None, None]:
|
|
|
55
56
|
if await COMPLETE_CACHE.a_exists():
|
|
56
57
|
async with COMPLETE_CACHE.a_open() as f:
|
|
57
58
|
async for path in f:
|
|
58
|
-
|
|
59
|
+
line = path.strip()
|
|
60
|
+
if line:
|
|
61
|
+
yield line
|
|
59
62
|
|
|
60
63
|
|
|
61
64
|
async def _update_cache(prefix: str, paths: Iterable[str] | None = None) -> None:
|
|
@@ -70,6 +73,7 @@ async def _update_cache(prefix: str, paths: Iterable[str] | None = None) -> None
|
|
|
70
73
|
else:
|
|
71
74
|
other_cache.add(path)
|
|
72
75
|
|
|
76
|
+
await COMPLETE_CACHE.parent.a_mkdir(parents=True, exist_ok=True)
|
|
73
77
|
if paths is None:
|
|
74
78
|
await COMPLETE_CACHE.a_write_text("\n".join(other_cache))
|
|
75
79
|
return
|
|
@@ -113,7 +117,7 @@ async def path_completer(prefix: str, **kwargs) -> list[str]:
|
|
|
113
117
|
path = PanPath(prefix)
|
|
114
118
|
return [
|
|
115
119
|
str(p).rstrip("/") + "/" if await p.a_is_dir() else str(p)
|
|
116
|
-
for p in
|
|
120
|
+
async for p in path.parent.a_glob(path.name + "*")
|
|
117
121
|
]
|
|
118
122
|
except Exception as e:
|
|
119
123
|
warn(f"Error listing cloud path: {e}")
|
|
@@ -139,6 +143,11 @@ async def path_completer(prefix: str, **kwargs) -> list[str]:
|
|
|
139
143
|
] + [p for p in ("-", "gs://", "s3://", "az://") if p.startswith(prefix)]
|
|
140
144
|
|
|
141
145
|
|
|
146
|
+
def sync_path_completer(prefix: str, **kwargs) -> list[str]:
|
|
147
|
+
"""Synchronous argcomplete adapter for async path_completer."""
|
|
148
|
+
return asyncio.run(path_completer(prefix, **kwargs))
|
|
149
|
+
|
|
150
|
+
|
|
142
151
|
async def run(args: Namespace) -> None:
|
|
143
152
|
"""Execute the complete command with given arguments."""
|
|
144
153
|
if args.clear_cache:
|
|
@@ -175,7 +184,7 @@ async def run(args: Namespace) -> None:
|
|
|
175
184
|
[PACKAGE],
|
|
176
185
|
shell=shell,
|
|
177
186
|
complete_arguments={
|
|
178
|
-
"file":
|
|
187
|
+
"file": sync_path_completer,
|
|
179
188
|
},
|
|
180
189
|
)
|
|
181
190
|
sys.stdout.write(script)
|