pywa-cloud-cli 0.1.0__py3-none-any.whl
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.
- pywa_cloud_cli/__init__.py +13 -0
- pywa_cloud_cli/archive.py +115 -0
- pywa_cloud_cli/cli.py +2070 -0
- pywa_cloud_cli/client.py +497 -0
- pywa_cloud_cli/config.py +72 -0
- pywa_cloud_cli/link.py +55 -0
- pywa_cloud_cli/py.typed +0 -0
- pywa_cloud_cli/tunnel.py +287 -0
- pywa_cloud_cli-0.1.0.dist-info/METADATA +83 -0
- pywa_cloud_cli-0.1.0.dist-info/RECORD +13 -0
- pywa_cloud_cli-0.1.0.dist-info/WHEEL +4 -0
- pywa_cloud_cli-0.1.0.dist-info/entry_points.txt +3 -0
- pywa_cloud_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
2
|
+
|
|
3
|
+
# Re-exported so pywa's own CLI (`pywa/cli.py`) only needs `import pywa_cloud_cli` - it never
|
|
4
|
+
# imports `pywa_cloud_cli.cli` directly, since that submodule is an implementation detail.
|
|
5
|
+
from pywa_cloud_cli.cli import (
|
|
6
|
+
dispatch,
|
|
7
|
+
main,
|
|
8
|
+
register,
|
|
9
|
+
register_shallow,
|
|
10
|
+
start_dev_tunnel,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
__all__ = ["dispatch", "main", "register", "register_shallow", "start_dev_tunnel"]
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""Packs a project directory into an in-memory tar.gz for `deploy`.
|
|
2
|
+
|
|
3
|
+
Two layers decide what goes in:
|
|
4
|
+
|
|
5
|
+
1. `HARD_EXCLUDES` always applies (`.git`, virtual environments, secret-looking dotfiles). `.pywacloudignore`
|
|
6
|
+
cannot un-ignore these.
|
|
7
|
+
2. `.gitignore` and `.pywacloudignore` are combined into one pattern set (via `pathspec`). The latter is appended
|
|
8
|
+
last, so under gitignore's last-match-wins rule it takes precedence.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import io
|
|
14
|
+
import os
|
|
15
|
+
import pathlib
|
|
16
|
+
import re
|
|
17
|
+
import tarfile
|
|
18
|
+
|
|
19
|
+
import pathspec
|
|
20
|
+
|
|
21
|
+
HARD_EXCLUDES = {
|
|
22
|
+
".venv",
|
|
23
|
+
"venv",
|
|
24
|
+
".git",
|
|
25
|
+
"__pycache__",
|
|
26
|
+
".pywacloud",
|
|
27
|
+
# Tool caches, editor state and JS dependencies: never part of a Python bot, and `node_modules` alone can
|
|
28
|
+
# push an upload past the size limit.
|
|
29
|
+
"node_modules",
|
|
30
|
+
".mypy_cache",
|
|
31
|
+
".pytest_cache",
|
|
32
|
+
".ruff_cache",
|
|
33
|
+
".tox",
|
|
34
|
+
".idea",
|
|
35
|
+
".DS_Store",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
# The server refuses a larger upload; checking here saves sending it first.
|
|
39
|
+
MAX_ARCHIVE_BYTES = 50 * 1024 * 1024
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _hard_excluded(rel_parts: tuple[str, ...]) -> bool:
|
|
43
|
+
if any(part in HARD_EXCLUDES for part in rel_parts):
|
|
44
|
+
return True
|
|
45
|
+
name = rel_parts[-1]
|
|
46
|
+
if name.startswith(".env"):
|
|
47
|
+
return True
|
|
48
|
+
return bool(name.endswith(".pyc"))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _walk_files(project_dir: pathlib.Path):
|
|
52
|
+
"""Every file under `project_dir`, in a stable order, without descending into the always-excluded directories:
|
|
53
|
+
a `.venv`, `node_modules` or `.git` can hold tens of thousands of files that would only be filtered out one by one."""
|
|
54
|
+
for root, dirnames, filenames in os.walk(project_dir):
|
|
55
|
+
dirnames[:] = sorted(d for d in dirnames if d not in HARD_EXCLUDES)
|
|
56
|
+
for name in sorted(filenames):
|
|
57
|
+
yield pathlib.Path(root) / name
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _load_ignore_spec(project_dir: pathlib.Path) -> pathspec.PathSpec | None:
|
|
61
|
+
patterns: list[str] = []
|
|
62
|
+
for ignore_file in (".gitignore", ".pywacloudignore"):
|
|
63
|
+
path = project_dir / ignore_file
|
|
64
|
+
if path.is_file():
|
|
65
|
+
patterns += path.read_text().splitlines()
|
|
66
|
+
if not patterns:
|
|
67
|
+
return None
|
|
68
|
+
return pathspec.PathSpec.from_lines("gitignore", patterns)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def build_archive(project_dir: pathlib.Path) -> bytes:
|
|
72
|
+
spec = _load_ignore_spec(project_dir)
|
|
73
|
+
buf = io.BytesIO()
|
|
74
|
+
# dereference: a symlinked file is uploaded as the file it points to. As a link it either pointed outside the
|
|
75
|
+
# project (the server refuses such an archive) or to a file that was excluded, and the bot started without it.
|
|
76
|
+
with tarfile.open(fileobj=buf, mode="w:gz", dereference=True) as tar:
|
|
77
|
+
for path in _walk_files(project_dir):
|
|
78
|
+
if not path.is_file():
|
|
79
|
+
continue # e.g. a broken symlink
|
|
80
|
+
rel = path.relative_to(project_dir)
|
|
81
|
+
if _hard_excluded(rel.parts):
|
|
82
|
+
continue
|
|
83
|
+
if spec is not None and spec.match_file(str(rel)):
|
|
84
|
+
continue
|
|
85
|
+
tar.add(path, arcname=str(rel))
|
|
86
|
+
return buf.getvalue()
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
_SECRET_NAMES = re.compile(
|
|
90
|
+
r"(\.(pem|key|p12|pfx|jks|keystore)|^id_(rsa|dsa|ecdsa|ed25519)(\.pub)?|^\.netrc|^\.htpasswd|^credentials\.json"
|
|
91
|
+
r"|^service[-_]?account.*\.json|^client_secret.*\.json)\Z",
|
|
92
|
+
re.IGNORECASE,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def secret_like(archive_bytes: bytes) -> list[str]:
|
|
97
|
+
"""Paths in a built archive whose names look like keys or credentials. Not removed (a bot may really need a
|
|
98
|
+
certificate, and silently dropping a file would start it broken); the deploy summary names them so they are chosen."""
|
|
99
|
+
with tarfile.open(fileobj=io.BytesIO(archive_bytes), mode="r:gz") as tar:
|
|
100
|
+
return sorted(
|
|
101
|
+
m.name
|
|
102
|
+
for m in tar.getmembers()
|
|
103
|
+
if m.isfile() and _SECRET_NAMES.search(pathlib.PurePosixPath(m.name).name)
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def describe(
|
|
108
|
+
archive_bytes: bytes, largest: int = 5
|
|
109
|
+
) -> tuple[int, list[tuple[str, int]]]:
|
|
110
|
+
"""(number of files, the `largest` files as (path, bytes)) of a built archive: what the deploy summary
|
|
111
|
+
prints, and what tells the user what to put in `.pywacloudignore` when the upload is too big."""
|
|
112
|
+
with tarfile.open(fileobj=io.BytesIO(archive_bytes), mode="r:gz") as tar:
|
|
113
|
+
members = [m for m in tar.getmembers() if m.isfile()]
|
|
114
|
+
members.sort(key=lambda m: m.size, reverse=True)
|
|
115
|
+
return len(members), [(m.name, m.size) for m in members[:largest]]
|