lame-project-cli 1.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.
lame/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Lame Python client
2
+
3
+ The `lame` package is a typed, standard-library Python client for the Lame
4
+ project API. It is included in the `lame-project-cli` distribution.
5
+
6
+ Install from a source checkout with:
7
+
8
+ ```console
9
+ python -m pip install .
10
+ ```
11
+
12
+ Once the distribution is published to PyPI, install it with:
13
+
14
+ ```console
15
+ python -m pip install lame-project-cli
16
+ ```
17
+
18
+ Generate a short-lived CLI token from the signed-in Projects page and keep it
19
+ private:
20
+
21
+ ```python
22
+ from lame import LameClient
23
+
24
+ client = LameClient("https://your-lame-server.example", token="YOUR_TOKEN")
25
+ projects = client.list_projects()
26
+ project = client.create_project("research-notes")
27
+ client.rename_project(project.id, "research")
28
+ ```
29
+
30
+ `LameClient` also provides `storage_connections()`, `delete_project(id)`,
31
+ `push_archive(id, bytes_or_file)`, and `pull_archive(id)`. The server validates
32
+ archive paths and size limits and syncs files to the storage connection assigned
33
+ to the project. The client does not execute project code.
34
+
35
+ Use the `lame` command-line entry point for local project sync:
36
+
37
+ ```console
38
+ lame login
39
+ lame projects
40
+ lame push research
41
+ lame pull research --path .\research
42
+ ```
43
+
44
+ The token is a bearer credential. Do not hard-code it or include it in source
45
+ control, and revoke it from Projects when it is no longer needed.
lame/__init__.py ADDED
@@ -0,0 +1,6 @@
1
+ """Python client library for the Lame project workspace."""
2
+
3
+ from .client import LameClient, LameError, Project
4
+
5
+ __all__ = ['LameClient', 'LameError', 'Project']
6
+ __version__ = '1.1.0'
lame/client.py ADDED
@@ -0,0 +1,172 @@
1
+ """Small standard-library client for the Lame project API."""
2
+
3
+ import json
4
+ import urllib.error
5
+ import urllib.request
6
+ from dataclasses import dataclass
7
+ from typing import Any, BinaryIO, Dict, List, Optional, Union
8
+ from urllib.parse import urlsplit
9
+
10
+
11
+ class LameError(RuntimeError):
12
+ """Raised when a Lame API request fails."""
13
+
14
+
15
+ @dataclass(frozen=True)
16
+ class Project:
17
+ """A project visible to the authenticated user."""
18
+
19
+ id: int
20
+ name: str
21
+ storage_provider_id: Optional[int] = None
22
+ storage_name: Optional[str] = None
23
+ created_at: Optional[str] = None
24
+ updated_at: Optional[str] = None
25
+
26
+ @classmethod
27
+ def from_dict(cls, value: Dict[str, Any]) -> 'Project':
28
+ return cls(
29
+ id=value['id'],
30
+ name=value['name'],
31
+ storage_provider_id=value.get('storage_provider_id'),
32
+ storage_name=value.get('storage_name'),
33
+ created_at=value.get('created_at'),
34
+ updated_at=value.get('updated_at'),
35
+ )
36
+
37
+
38
+ class LameClient:
39
+ """Make authenticated requests to a Lame server.
40
+
41
+ Generate a CLI token from the signed-in Projects page and pass it as
42
+ ``token``. Keep that token private and revoke it when it is no longer needed.
43
+ """
44
+
45
+ def __init__(self, server: str, token: str, timeout: Union[int, float] = 600) -> None:
46
+ if not isinstance(server, str) or not server.strip():
47
+ raise ValueError('server must be a non-empty URL')
48
+ if not isinstance(token, str) or not token.strip():
49
+ raise ValueError('token must be a non-empty string')
50
+ parsed_server = urlsplit(server)
51
+ if (
52
+ parsed_server.scheme not in {'http', 'https'}
53
+ or not parsed_server.hostname
54
+ or parsed_server.username
55
+ or parsed_server.password
56
+ or parsed_server.query
57
+ or parsed_server.fragment
58
+ ):
59
+ raise ValueError('server must be an HTTP(S) URL without credentials or query parameters')
60
+ if parsed_server.scheme == 'http' and parsed_server.hostname.lower() not in {
61
+ 'localhost',
62
+ '127.0.0.1',
63
+ '::1',
64
+ }:
65
+ raise ValueError('use HTTPS when connecting to a non-local Lame server')
66
+ if isinstance(timeout, bool) or not isinstance(timeout, (int, float)) or timeout <= 0:
67
+ raise ValueError('timeout must be a positive number')
68
+ self.server = server.rstrip('/')
69
+ self.token = token
70
+ self.timeout = timeout
71
+
72
+ def _request(
73
+ self,
74
+ path: str,
75
+ method: str = 'GET',
76
+ payload: Optional[Dict[str, Any]] = None,
77
+ raw: Optional[bytes] = None,
78
+ content_type: Optional[str] = None,
79
+ download: bool = False,
80
+ ) -> Any:
81
+ headers = {'Authorization': f'Bearer {self.token}'}
82
+ body = raw
83
+ if payload is not None:
84
+ body = json.dumps(payload).encode('utf-8')
85
+ headers['Content-Type'] = 'application/json'
86
+ elif content_type:
87
+ headers['Content-Type'] = content_type
88
+ request = urllib.request.Request(
89
+ f'{self.server}{path}',
90
+ data=body,
91
+ headers=headers,
92
+ method=method,
93
+ )
94
+ try:
95
+ with urllib.request.urlopen(request, timeout=self.timeout) as response:
96
+ result = response.read()
97
+ except urllib.error.HTTPError as error:
98
+ response_body = error.read()
99
+ try:
100
+ details = json.loads(response_body.decode('utf-8'))
101
+ message = details.get('error', f'Request failed with HTTP {error.code}.')
102
+ except (UnicodeDecodeError, json.JSONDecodeError, AttributeError):
103
+ message = f'Request failed with HTTP {error.code}.'
104
+ raise LameError(message) from error
105
+ except (urllib.error.URLError, TimeoutError, OSError) as error:
106
+ reason = getattr(error, 'reason', error)
107
+ raise LameError(f'Could not reach the Lame server: {reason}') from error
108
+ if download:
109
+ return result
110
+ try:
111
+ return json.loads(result.decode('utf-8')) if result else {}
112
+ except (UnicodeDecodeError, json.JSONDecodeError) as error:
113
+ raise LameError('The Lame server returned an invalid response.') from error
114
+
115
+ def list_projects(self) -> List[Project]:
116
+ """Return the user's projects as :class:`Project` instances."""
117
+ response = self._request('/api/projects')
118
+ return [Project.from_dict(item) for item in response.get('projects', [])]
119
+
120
+ def storage_connections(self) -> List[Dict[str, Any]]:
121
+ """Return the user's configured project storage connections."""
122
+ response = self._request('/api/projects/storage')
123
+ return response.get('providers', [])
124
+
125
+ def create_project(self, name: str, storage_provider_id: Optional[int] = None) -> Project:
126
+ """Create a project in the default or explicitly selected connection."""
127
+ payload = {'name': name}
128
+ if storage_provider_id is not None:
129
+ payload['storage_provider_id'] = storage_provider_id
130
+ response = self._request('/api/projects', method='POST', payload=payload)
131
+ return Project.from_dict(response)
132
+
133
+ def rename_project(self, project_id: int, name: str) -> Project:
134
+ """Rename a project without moving its cloud files."""
135
+ response = self._request(
136
+ f'/api/projects/{int(project_id)}',
137
+ method='PATCH',
138
+ payload={'name': name},
139
+ )
140
+ return Project.from_dict(response)
141
+
142
+ def delete_project(self, project_id: int) -> Dict[str, Any]:
143
+ """Delete a project and its files from its configured cloud remote."""
144
+ return self._request(f'/api/projects/{int(project_id)}', method='DELETE')
145
+
146
+ def push_archive(
147
+ self,
148
+ project_id: int,
149
+ archive: Union[bytes, BinaryIO],
150
+ ) -> Dict[str, Any]:
151
+ """Upload a ZIP archive to a project.
152
+
153
+ ``archive`` may be bytes or a binary file object. The archive is sent
154
+ directly to the Lame server; the server applies its configured size and
155
+ path validation before syncing files to cloud storage.
156
+ """
157
+ archive_bytes = archive.read() if hasattr(archive, 'read') else archive
158
+ if not isinstance(archive_bytes, bytes):
159
+ raise TypeError('archive must be bytes or a binary file object')
160
+ return self._request(
161
+ f'/api/projects/{int(project_id)}/archive',
162
+ method='POST',
163
+ raw=archive_bytes,
164
+ content_type='application/zip',
165
+ )
166
+
167
+ def pull_archive(self, project_id: int) -> bytes:
168
+ """Download a project's ZIP archive as bytes."""
169
+ return self._request(
170
+ f'/api/projects/{int(project_id)}/archive',
171
+ download=True,
172
+ )
lame/py.typed ADDED
File without changes
@@ -0,0 +1,55 @@
1
+ Metadata-Version: 2.4
2
+ Name: lame-project-cli
3
+ Version: 1.1.0
4
+ Summary: Python client library and local-first CLI for Lame cloud projects
5
+ Classifier: Programming Language :: Python :: 3
6
+ Classifier: Programming Language :: Python :: 3 :: Only
7
+ Classifier: Operating System :: OS Independent
8
+ Requires-Python: >=3.9
9
+ Description-Content-Type: text/markdown
10
+
11
+ # Lame Python client
12
+
13
+ The `lame` package is a typed, standard-library Python client for the Lame
14
+ project API. It is included in the `lame-project-cli` distribution.
15
+
16
+ Install from a source checkout with:
17
+
18
+ ```console
19
+ python -m pip install .
20
+ ```
21
+
22
+ Once the distribution is published to PyPI, install it with:
23
+
24
+ ```console
25
+ python -m pip install lame-project-cli
26
+ ```
27
+
28
+ Generate a short-lived CLI token from the signed-in Projects page and keep it
29
+ private:
30
+
31
+ ```python
32
+ from lame import LameClient
33
+
34
+ client = LameClient("https://your-lame-server.example", token="YOUR_TOKEN")
35
+ projects = client.list_projects()
36
+ project = client.create_project("research-notes")
37
+ client.rename_project(project.id, "research")
38
+ ```
39
+
40
+ `LameClient` also provides `storage_connections()`, `delete_project(id)`,
41
+ `push_archive(id, bytes_or_file)`, and `pull_archive(id)`. The server validates
42
+ archive paths and size limits and syncs files to the storage connection assigned
43
+ to the project. The client does not execute project code.
44
+
45
+ Use the `lame` command-line entry point for local project sync:
46
+
47
+ ```console
48
+ lame login
49
+ lame projects
50
+ lame push research
51
+ lame pull research --path .\research
52
+ ```
53
+
54
+ The token is a bearer credential. Do not hard-code it or include it in source
55
+ control, and revoke it from Projects when it is no longer needed.
@@ -0,0 +1,11 @@
1
+ lame/README.md,sha256=hhKz1IekU5skaqftP0ONHV6glXvX1ahIaHl6KZk-agM,1344
2
+ lame/__init__.py,sha256=l_fqc0ZXHETX5hZKCnjT0yB1nP6D4eWLNRL9BmYCJXw,188
3
+ lame/client.py,sha256=FpBVRuPlxfCfvAtfNT0OXfU-3XMVupR90wNMUhWmqSg,7025
4
+ lame/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ tools/__init__.py,sha256=BfuxF_hjQY-p0HjFsqbLAESVnQJLYZk_Jj_H28p5fyI,36
6
+ tools/lame_cli.py,sha256=hkiR9eCDBaVrsojrWwFhyL8RZd7FTh7FlLcMgDmyvFE,16868
7
+ lame_project_cli-1.1.0.dist-info/METADATA,sha256=uMby06BLxnKZK74ys26-oEZa-PksRbXMizmZRdp76QY,1704
8
+ lame_project_cli-1.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
9
+ lame_project_cli-1.1.0.dist-info/entry_points.txt,sha256=yKVZ2gwQeIESRZQwbP_qpqe5NtYzP19oI71FPo_Q8ck,45
10
+ lame_project_cli-1.1.0.dist-info/top_level.txt,sha256=yJAJ9L0izQqeMmlUbVJRSvOnUnzPFWwBb-TYLx2HeLk,11
11
+ lame_project_cli-1.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ lame = tools.lame_cli:main
@@ -0,0 +1,2 @@
1
+ lame
2
+ tools
tools/__init__.py ADDED
@@ -0,0 +1 @@
1
+ """Command-line tools for Lame."""
tools/lame_cli.py ADDED
@@ -0,0 +1,429 @@
1
+ #!/usr/bin/env python3
2
+ """Local-first command-line sync for Lame cloud projects."""
3
+
4
+ import argparse
5
+ import fnmatch
6
+ import getpass
7
+ import io
8
+ import json
9
+ import os
10
+ import re
11
+ import shutil
12
+ import stat
13
+ import sys
14
+ import tempfile
15
+ import urllib.error
16
+ import urllib.request
17
+ import zipfile
18
+ from pathlib import Path
19
+
20
+ MAX_ARCHIVE_BYTES = 128 * 1024 * 1024
21
+ MAX_FILE_BYTES = 128 * 1024 * 1024
22
+ DEFAULT_SERVER = 'http://127.0.0.1:5000'
23
+ IGNORED_DIRECTORIES = {
24
+ '.git',
25
+ '.lame',
26
+ '.venv',
27
+ 'venv',
28
+ 'node_modules',
29
+ '__pycache__',
30
+ '.pytest_cache',
31
+ '.mypy_cache',
32
+ '.ruff_cache',
33
+ '.aws',
34
+ '.ssh',
35
+ 'secrets',
36
+ 'credentials',
37
+ }
38
+ IGNORED_FILES = {
39
+ '.ds_store',
40
+ 'thumbs.db',
41
+ 'desktop.ini',
42
+ 'id_rsa',
43
+ 'id_ed25519',
44
+ 'credentials.json',
45
+ }
46
+ VALID_PATH_PART = re.compile(r'^[A-Za-z0-9._ -][A-Za-z0-9_. -]{0,99}$')
47
+
48
+
49
+ class LameCliError(Exception):
50
+ """A user-facing CLI error."""
51
+
52
+
53
+ def config_path():
54
+ if os.name == 'nt':
55
+ base = Path(os.environ.get('APPDATA', Path.home() / 'AppData' / 'Roaming'))
56
+ return base / 'Lame' / 'cli.json'
57
+ return Path(os.environ.get('XDG_CONFIG_HOME', Path.home() / '.config')) / 'lame' / 'cli.json'
58
+
59
+
60
+ def read_config():
61
+ path = config_path()
62
+ try:
63
+ value = json.loads(path.read_text(encoding='utf-8'))
64
+ except FileNotFoundError:
65
+ raise LameCliError('You are not logged in. Run `lame login` first.')
66
+ except (OSError, json.JSONDecodeError) as error:
67
+ raise LameCliError(f'Could not read CLI credentials at {path}: {error}') from error
68
+ if not isinstance(value, dict) or not value.get('server') or not value.get('token'):
69
+ raise LameCliError('The saved CLI credentials are incomplete. Run `lame login` again.')
70
+ return value
71
+
72
+
73
+ def write_config(server, token):
74
+ path = config_path()
75
+ try:
76
+ path.parent.mkdir(parents=True, exist_ok=True)
77
+ path.write_text(
78
+ json.dumps({'server': server.rstrip('/'), 'token': token}, indent=2) + '\n',
79
+ encoding='utf-8',
80
+ )
81
+ try:
82
+ path.chmod(stat.S_IRUSR | stat.S_IWUSR)
83
+ except OSError:
84
+ if os.name != 'nt':
85
+ raise
86
+ except OSError as error:
87
+ raise LameCliError(f'Could not save CLI credentials at {path}: {error}') from error
88
+
89
+
90
+ def remove_temporary_directory(path):
91
+ try:
92
+ shutil.rmtree(path)
93
+ except FileNotFoundError:
94
+ pass
95
+ except OSError as error:
96
+ print(f'Warning: could not remove temporary sync files at {path}: {error}', file=sys.stderr)
97
+
98
+
99
+ def api_request(path, method='GET', payload=None, raw=None, content_type=None, download=False):
100
+ config = read_config()
101
+ headers = {'Authorization': f'Bearer {config["token"]}'}
102
+ body = raw
103
+ if payload is not None:
104
+ body = json.dumps(payload).encode('utf-8')
105
+ headers['Content-Type'] = 'application/json'
106
+ elif content_type:
107
+ headers['Content-Type'] = content_type
108
+ request = urllib.request.Request(
109
+ config['server'] + path,
110
+ data=body,
111
+ headers=headers,
112
+ method=method,
113
+ )
114
+ try:
115
+ with urllib.request.urlopen(request, timeout=600) as response:
116
+ result = response.read()
117
+ except urllib.error.HTTPError as error:
118
+ response_body = error.read()
119
+ try:
120
+ details = json.loads(response_body.decode('utf-8'))
121
+ message = details.get('error', f'Request failed with HTTP {error.code}.')
122
+ except (UnicodeDecodeError, json.JSONDecodeError, AttributeError):
123
+ message = f'Request failed with HTTP {error.code}.'
124
+ if error.code == 401:
125
+ message += ' Your CLI token may have expired; generate a new one in Projects.'
126
+ raise LameCliError(message) from error
127
+ except (urllib.error.URLError, TimeoutError, OSError) as error:
128
+ reason = getattr(error, 'reason', error)
129
+ raise LameCliError(f'Could not reach the Lame server: {reason}') from error
130
+ if download:
131
+ return result
132
+ try:
133
+ return json.loads(result.decode('utf-8')) if result else {}
134
+ except (UnicodeDecodeError, json.JSONDecodeError) as error:
135
+ raise LameCliError('The Lame server returned an invalid response.') from error
136
+
137
+
138
+ def project_for(value):
139
+ result = api_request('/api/projects')
140
+ projects = result.get('projects', [])
141
+ for project in projects:
142
+ if str(project.get('id')) == value or project.get('name', '').casefold() == value.casefold():
143
+ return project
144
+ names = ', '.join(f'{item["name"]} (ID {item["id"]})' for item in projects)
145
+ raise LameCliError(f'Project {value!r} was not found. Available projects: {names or "none"}')
146
+
147
+
148
+ def ignore_rules(root):
149
+ path = root / '.lameignore'
150
+ try:
151
+ lines = path.read_text(encoding='utf-8').splitlines()
152
+ except FileNotFoundError:
153
+ return []
154
+ except OSError as error:
155
+ raise LameCliError(f'Could not read {path}: {error}') from error
156
+ return [
157
+ line.strip()
158
+ for line in lines
159
+ if line.strip() and not line.lstrip().startswith('#')
160
+ ]
161
+
162
+
163
+ def ignored(relative_path, rules):
164
+ parts = relative_path.parts
165
+ if any(part in IGNORED_DIRECTORIES for part in parts):
166
+ return True
167
+ name = parts[-1].lower()
168
+ if (
169
+ name in IGNORED_FILES
170
+ or name.endswith(('.pyc', '.pyo', '.pyd', '.pem', '.key', '.p12'))
171
+ or name == '.env'
172
+ or name.startswith('.env.')
173
+ or name.endswith('.env')
174
+ or 'service-account' in name and name.endswith('.json')
175
+ ):
176
+ return True
177
+ path_text = relative_path.as_posix()
178
+ result = False
179
+ for rule in rules:
180
+ negate = rule.startswith('!')
181
+ pattern = rule[1:] if negate else rule
182
+ pattern = pattern.lstrip('/')
183
+ if not pattern:
184
+ continue
185
+ matches = (
186
+ fnmatch.fnmatch(path_text, pattern)
187
+ or path_text == pattern.rstrip('/')
188
+ or path_text.startswith(pattern.rstrip('/') + '/')
189
+ )
190
+ if matches:
191
+ result = not negate
192
+ return result
193
+
194
+
195
+ def build_archive(root):
196
+ root = root.resolve()
197
+ if not root.is_dir():
198
+ raise LameCliError(f'Project folder does not exist: {root}')
199
+ rules = ignore_rules(root)
200
+ temporary = Path(tempfile.mkdtemp(prefix='lame-sync-'))
201
+ archive_path = temporary / 'project.zip'
202
+ files = 0
203
+ skipped = 0
204
+ succeeded = False
205
+ try:
206
+ with zipfile.ZipFile(archive_path, 'w', compression=zipfile.ZIP_DEFLATED) as archive:
207
+ for directory, directories, filenames in os.walk(root, followlinks=False):
208
+ current = Path(directory)
209
+ relative_directory = current.relative_to(root)
210
+ directories[:] = [
211
+ name for name in directories
212
+ if not ignored(relative_directory / name, rules)
213
+ and not (current / name).is_symlink()
214
+ ]
215
+ for filename in filenames:
216
+ file_path = current / filename
217
+ relative_path = file_path.relative_to(root)
218
+ if ignored(relative_path, rules) or file_path.is_symlink():
219
+ skipped += 1
220
+ continue
221
+ if (
222
+ len(relative_path.parts) > 10
223
+ or any(not VALID_PATH_PART.fullmatch(part) for part in relative_path.parts)
224
+ ):
225
+ raise LameCliError(
226
+ f'{relative_path.as_posix()} has a path component unsupported by project sync.'
227
+ )
228
+ size = file_path.stat().st_size
229
+ if size > MAX_FILE_BYTES:
230
+ skipped += 1
231
+ continue
232
+ archive.write(file_path, relative_path.as_posix())
233
+ files += 1
234
+ size = archive_path.stat().st_size
235
+ if size > MAX_ARCHIVE_BYTES:
236
+ raise LameCliError('The compressed project is larger than the 128 MB CLI sync limit.')
237
+ succeeded = True
238
+ return archive_path, files, skipped, size
239
+ except OSError as error:
240
+ raise LameCliError(f'Could not create the local project archive: {error}') from error
241
+ finally:
242
+ if not succeeded:
243
+ remove_temporary_directory(temporary)
244
+
245
+
246
+ def command_login(args):
247
+ server = args.server.rstrip('/')
248
+ if not server.startswith(('http://', 'https://')):
249
+ raise LameCliError('Enter a server URL beginning with http:// or https://.')
250
+ token = getpass.getpass('Paste the short-lived CLI token from Lame Projects: ').strip()
251
+ if not token:
252
+ raise LameCliError('A CLI token is required.')
253
+ write_config(server, token)
254
+ try:
255
+ response = api_request('/api/projects')
256
+ except LameCliError:
257
+ try:
258
+ config_path().unlink(missing_ok=True)
259
+ except OSError as cleanup_error:
260
+ print(f'Warning: could not remove rejected CLI credentials: {cleanup_error}', file=sys.stderr)
261
+ raise
262
+ print(f'Logged in to {server}. {len(response.get("projects", []))} project(s) available.')
263
+
264
+
265
+ def command_logout(_args):
266
+ path = config_path()
267
+ try:
268
+ read_config()
269
+ api_request('/api/cli/token', method='DELETE')
270
+ except LameCliError as error:
271
+ if 'not logged in' not in str(error):
272
+ print(f'Warning: {error}', file=sys.stderr)
273
+ try:
274
+ path.unlink(missing_ok=True)
275
+ except OSError as error:
276
+ raise LameCliError(f'Could not remove local credentials at {path}: {error}') from error
277
+ print('Logged out and removed local CLI credentials.')
278
+
279
+
280
+ def command_projects(_args):
281
+ projects = api_request('/api/projects').get('projects', [])
282
+ if not projects:
283
+ print('No projects yet. Create one in Lame Projects or run `create <name>`.')
284
+ return
285
+ print(f'{"ID":<8} {"PROJECT":<32} UPDATED')
286
+ for project in projects:
287
+ print(f'{project["id"]:<8} {project["name"]:<32} {project.get("updated_at", "")}')
288
+
289
+
290
+ def command_create(args):
291
+ project = api_request('/api/projects', method='POST', payload={'name': args.name})
292
+ print(f'Created {project["name"]} (ID {project["id"]}).')
293
+
294
+
295
+ def command_push(args):
296
+ project = project_for(args.project)
297
+ archive_path, files, skipped, size = build_archive(Path(args.path))
298
+ try:
299
+ if not files:
300
+ raise LameCliError('No files to upload. Add project files or review your .lameignore rules.')
301
+ with archive_path.open('rb') as archive_file:
302
+ archive = archive_file.read()
303
+ result = api_request(
304
+ f'/api/projects/{project["id"]}/archive',
305
+ method='POST',
306
+ raw=archive,
307
+ content_type='application/zip',
308
+ )
309
+ finally:
310
+ remove_temporary_directory(archive_path.parent)
311
+ print(
312
+ f'Pushed {result.get("files", files)} file(s) to {project["name"]} '
313
+ f'({size / 1024:.1f} KB archive).'
314
+ )
315
+ if skipped:
316
+ print(f'Skipped {skipped} ignored, linked, or oversized file(s).')
317
+
318
+
319
+ def command_pull(args):
320
+ project = project_for(args.project)
321
+ destination = Path(args.path).resolve()
322
+ if destination.exists() and not destination.is_dir():
323
+ raise LameCliError(f'Download destination is not a directory: {destination}')
324
+ if destination.exists() and any(destination.iterdir()) and not args.force:
325
+ raise LameCliError(
326
+ f'{destination} is not empty. Use --force to merge downloaded files into it.'
327
+ )
328
+ archive_bytes = api_request(
329
+ f'/api/projects/{project["id"]}/archive',
330
+ download=True,
331
+ )
332
+ if len(archive_bytes) > MAX_ARCHIVE_BYTES:
333
+ raise LameCliError('The downloaded archive is larger than the 128 MB CLI limit.')
334
+ destination.mkdir(parents=True, exist_ok=True)
335
+ try:
336
+ extracted = set()
337
+ total_size = 0
338
+ with zipfile.ZipFile(io.BytesIO(archive_bytes)) as archive:
339
+ for entry in archive.infolist():
340
+ relative = Path(entry.filename)
341
+ if relative.is_absolute() or '..' in relative.parts or '\\' in entry.filename:
342
+ raise LameCliError('The project archive contains an unsafe file path.')
343
+ name = relative.as_posix().rstrip('/')
344
+ if (
345
+ not name
346
+ or len(relative.parts) > 10
347
+ or any(part in {'.', '..'} or not VALID_PATH_PART.fullmatch(part) for part in relative.parts)
348
+ ):
349
+ raise LameCliError('The project archive contains an unsafe file path.')
350
+ mode = entry.external_attr >> 16
351
+ if stat.S_ISLNK(mode):
352
+ raise LameCliError('The project archive contains a symbolic link.')
353
+ if not entry.is_dir():
354
+ if name.casefold() in extracted:
355
+ raise LameCliError('The project archive contains duplicate file names.')
356
+ extracted.add(name.casefold())
357
+ if entry.file_size > MAX_FILE_BYTES:
358
+ raise LameCliError(f'{entry.filename} exceeds the 128 MB per-file limit.')
359
+ total_size += entry.file_size
360
+ if total_size > 512 * 1024 * 1024:
361
+ raise LameCliError('The project exceeds the 512 MB unpacked size limit.')
362
+ target = (destination / relative).resolve()
363
+ if os.path.commonpath((str(destination), str(target))) != str(destination):
364
+ raise LameCliError('The project archive contains an unsafe file path.')
365
+ if entry.is_dir():
366
+ target.mkdir(parents=True, exist_ok=True)
367
+ continue
368
+ target.parent.mkdir(parents=True, exist_ok=True)
369
+ with archive.open(entry) as source, target.open('wb') as output:
370
+ output.write(source.read())
371
+ except (OSError, zipfile.BadZipFile) as error:
372
+ raise LameCliError(f'Could not extract the project archive: {error}') from error
373
+ print(f'Pulled {project["name"]} into {destination}.')
374
+
375
+
376
+ def command_rename(args):
377
+ project = project_for(args.project)
378
+ result = api_request(
379
+ f'/api/projects/{project["id"]}',
380
+ method='PATCH',
381
+ payload={'name': args.name},
382
+ )
383
+ print(f'Renamed project to {result["name"]}.')
384
+
385
+
386
+ def parser():
387
+ root = argparse.ArgumentParser(
388
+ prog='lame',
389
+ description='Sync local project folders with your Lame cloud workspace.',
390
+ )
391
+ commands = root.add_subparsers(dest='command', required=True)
392
+ login = commands.add_parser('login', help='save a short-lived token from Lame Projects')
393
+ login.add_argument('--server', default=DEFAULT_SERVER, help='Lame server URL')
394
+ login.set_defaults(handler=command_login)
395
+ logout = commands.add_parser('logout', help='revoke the CLI token and log out')
396
+ logout.set_defaults(handler=command_logout)
397
+ projects = commands.add_parser('projects', help='list cloud projects')
398
+ projects.set_defaults(handler=command_projects)
399
+ create = commands.add_parser('create', help='create a cloud project')
400
+ create.add_argument('name')
401
+ create.set_defaults(handler=command_create)
402
+ push = commands.add_parser('push', help='upload a local folder to a project')
403
+ push.add_argument('project', help='project name or ID')
404
+ push.add_argument('--path', default='.', help='local project folder (default: current folder)')
405
+ push.set_defaults(handler=command_push)
406
+ pull = commands.add_parser('pull', help='download a project into a local folder')
407
+ pull.add_argument('project', help='project name or ID')
408
+ pull.add_argument('--path', default='.', help='download folder (default: current folder)')
409
+ pull.add_argument('--force', action='store_true', help='merge into a non-empty folder')
410
+ pull.set_defaults(handler=command_pull)
411
+ rename = commands.add_parser('rename', help='rename a cloud project')
412
+ rename.add_argument('project', help='project name or ID')
413
+ rename.add_argument('name')
414
+ rename.set_defaults(handler=command_rename)
415
+ return root
416
+
417
+
418
+ def main():
419
+ args = parser().parse_args()
420
+ try:
421
+ args.handler(args)
422
+ except LameCliError as error:
423
+ print(f'lame: {error}', file=sys.stderr)
424
+ return 1
425
+ return 0
426
+
427
+
428
+ if __name__ == '__main__':
429
+ raise SystemExit(main())