aiar 0.1.4__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.
- aiar-0.1.4/.github/workflows/publish.yml +70 -0
- aiar-0.1.4/.gitignore +155 -0
- aiar-0.1.4/PKG-INFO +393 -0
- aiar-0.1.4/README.md +384 -0
- aiar-0.1.4/pyproject.toml +21 -0
- aiar-0.1.4/src/aiar/__init__.py +11 -0
- aiar-0.1.4/src/aiar/aiar_base.py +1125 -0
- aiar-0.1.4/tests/__init__.py +2 -0
- aiar-0.1.4/tests/aiar_base_test.py +210 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# This workflow will upload a Python Package to PyPI when a release is created
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
|
|
3
|
+
|
|
4
|
+
# This workflow uses actions that are not certified by GitHub.
|
|
5
|
+
# They are provided by a third-party and are governed by
|
|
6
|
+
# separate terms of service, privacy policy, and support
|
|
7
|
+
# documentation.
|
|
8
|
+
|
|
9
|
+
name: Upload Python Package
|
|
10
|
+
|
|
11
|
+
on:
|
|
12
|
+
release:
|
|
13
|
+
types: [published]
|
|
14
|
+
|
|
15
|
+
permissions:
|
|
16
|
+
contents: read
|
|
17
|
+
|
|
18
|
+
jobs:
|
|
19
|
+
release-build:
|
|
20
|
+
runs-on: ubuntu-latest
|
|
21
|
+
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v4
|
|
24
|
+
|
|
25
|
+
- uses: actions/setup-python@v5
|
|
26
|
+
with:
|
|
27
|
+
python-version: "3.x"
|
|
28
|
+
|
|
29
|
+
- name: Build release distributions
|
|
30
|
+
run: |
|
|
31
|
+
# NOTE: put your own distribution build steps here.
|
|
32
|
+
python -m pip install build
|
|
33
|
+
python -m build
|
|
34
|
+
|
|
35
|
+
- name: Upload distributions
|
|
36
|
+
uses: actions/upload-artifact@v4
|
|
37
|
+
with:
|
|
38
|
+
name: release-dists
|
|
39
|
+
path: dist/
|
|
40
|
+
|
|
41
|
+
pypi-publish:
|
|
42
|
+
runs-on: ubuntu-latest
|
|
43
|
+
needs:
|
|
44
|
+
- release-build
|
|
45
|
+
permissions:
|
|
46
|
+
# IMPORTANT: this permission is mandatory for trusted publishing
|
|
47
|
+
id-token: write
|
|
48
|
+
|
|
49
|
+
# Dedicated environments with protections for publishing are strongly recommended.
|
|
50
|
+
# For more information, see: https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment#deployment-protection-rules
|
|
51
|
+
environment:
|
|
52
|
+
name: pypi
|
|
53
|
+
# OPTIONAL: uncomment and update to include your PyPI project URL in the deployment status:
|
|
54
|
+
url: https://pypi.org/p/pythonopenscad
|
|
55
|
+
#
|
|
56
|
+
# ALTERNATIVE: if your GitHub Release name is the PyPI project version string
|
|
57
|
+
# ALTERNATIVE: exactly, uncomment the following line instead:
|
|
58
|
+
# url: https://pypi.org/project/pythonopenscad/${{ github.event.release.name }}
|
|
59
|
+
|
|
60
|
+
steps:
|
|
61
|
+
- name: Retrieve release distributions
|
|
62
|
+
uses: actions/download-artifact@v4
|
|
63
|
+
with:
|
|
64
|
+
name: release-dists
|
|
65
|
+
path: dist/
|
|
66
|
+
|
|
67
|
+
- name: Publish release distributions to PyPI
|
|
68
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
69
|
+
with:
|
|
70
|
+
packages-dir: dist/
|
aiar-0.1.4/.gitignore
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
examples_out/
|
|
24
|
+
share/python-wheels/
|
|
25
|
+
*.egg-info/
|
|
26
|
+
.installed.cfg
|
|
27
|
+
*.egg
|
|
28
|
+
MANIFEST
|
|
29
|
+
generated*/
|
|
30
|
+
|
|
31
|
+
# PyInstaller
|
|
32
|
+
# Usually these files are written by a python script from a template
|
|
33
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
34
|
+
*.manifest
|
|
35
|
+
*.spec
|
|
36
|
+
|
|
37
|
+
# Installer logs
|
|
38
|
+
pip-log.txt
|
|
39
|
+
pip-delete-this-directory.txt
|
|
40
|
+
|
|
41
|
+
# Unit test / coverage reports
|
|
42
|
+
htmlcov/
|
|
43
|
+
.tox/
|
|
44
|
+
.nox/
|
|
45
|
+
.coverage
|
|
46
|
+
.coverage.*
|
|
47
|
+
.cache
|
|
48
|
+
nosetests.xml
|
|
49
|
+
coverage.xml
|
|
50
|
+
*.cover
|
|
51
|
+
*.py,cover
|
|
52
|
+
.hypothesis/
|
|
53
|
+
.pytest_cache/
|
|
54
|
+
cover/
|
|
55
|
+
|
|
56
|
+
# Translations
|
|
57
|
+
*.mo
|
|
58
|
+
*.pot
|
|
59
|
+
|
|
60
|
+
# Django stuff:
|
|
61
|
+
*.log
|
|
62
|
+
local_settings.py
|
|
63
|
+
db.sqlite3
|
|
64
|
+
db.sqlite3-journal
|
|
65
|
+
|
|
66
|
+
# Flask stuff:
|
|
67
|
+
instance/
|
|
68
|
+
.webassets-cache
|
|
69
|
+
|
|
70
|
+
# Scrapy stuff:
|
|
71
|
+
.scrapy
|
|
72
|
+
|
|
73
|
+
# Sphinx documentation
|
|
74
|
+
docs/_build/
|
|
75
|
+
|
|
76
|
+
# PyBuilder
|
|
77
|
+
.pybuilder/
|
|
78
|
+
target/
|
|
79
|
+
|
|
80
|
+
# Jupyter Notebook
|
|
81
|
+
.ipynb_checkpoints
|
|
82
|
+
|
|
83
|
+
# IPython
|
|
84
|
+
profile_default/
|
|
85
|
+
ipython_config.py
|
|
86
|
+
|
|
87
|
+
# pyenv
|
|
88
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
89
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
90
|
+
# .python-version
|
|
91
|
+
|
|
92
|
+
# pipenv
|
|
93
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
94
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
95
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
96
|
+
# install all needed dependencies.
|
|
97
|
+
#Pipfile.lock
|
|
98
|
+
|
|
99
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
100
|
+
__pypackages__/
|
|
101
|
+
|
|
102
|
+
# Celery stuff
|
|
103
|
+
celerybeat-schedule
|
|
104
|
+
celerybeat.pid
|
|
105
|
+
|
|
106
|
+
# SageMath parsed files
|
|
107
|
+
*.sage.py
|
|
108
|
+
|
|
109
|
+
# Environments
|
|
110
|
+
.env
|
|
111
|
+
.venv
|
|
112
|
+
env/
|
|
113
|
+
venv/
|
|
114
|
+
ENV/
|
|
115
|
+
env.bak/
|
|
116
|
+
venv.bak/
|
|
117
|
+
|
|
118
|
+
# Spyder project settings
|
|
119
|
+
.spyderproject
|
|
120
|
+
.spyproject
|
|
121
|
+
|
|
122
|
+
# Rope project settings
|
|
123
|
+
.ropeproject
|
|
124
|
+
|
|
125
|
+
# mkdocs documentation
|
|
126
|
+
/site
|
|
127
|
+
|
|
128
|
+
# mypy
|
|
129
|
+
.mypy_cache/
|
|
130
|
+
.dmypy.json
|
|
131
|
+
dmypy.json
|
|
132
|
+
|
|
133
|
+
# Pyre type checker
|
|
134
|
+
.pyre/
|
|
135
|
+
|
|
136
|
+
# pytype static type analyzer
|
|
137
|
+
.pytype/
|
|
138
|
+
|
|
139
|
+
# Cython debug symbols
|
|
140
|
+
cython_debug/
|
|
141
|
+
|
|
142
|
+
# BAK files
|
|
143
|
+
*.BAK
|
|
144
|
+
|
|
145
|
+
# Test artifacts
|
|
146
|
+
test*.scad
|
|
147
|
+
test*.dot
|
|
148
|
+
test*.svg
|
|
149
|
+
test*.stl
|
|
150
|
+
test*.png
|
|
151
|
+
test*.3mf
|
|
152
|
+
test*.txt
|
|
153
|
+
test*.html
|
|
154
|
+
test*.zip
|
|
155
|
+
test*.csq
|
aiar-0.1.4/PKG-INFO
ADDED
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: aiar
|
|
3
|
+
Version: 0.1.4
|
|
4
|
+
Summary: AI Archive is a format and a tool for bundling a directory structure and its files into a single, executable bash/zsh script. It's designed to make sending and receiving file collections in a chat-based or text-only environment—like interacting with an LLM—as simple as copying and pasting a single block of text.
|
|
5
|
+
Author-email: Gianni Mariani <gianni@mariani.ws>
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Requires-Dist: pathspec>=0.12.1
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# **aiar (AI Archive)**
|
|
11
|
+
|
|
12
|
+
**A simple LLM-friendly archive format and utility for creating self-extracting shell archives.**
|
|
13
|
+
|
|
14
|
+
Inspired by the classic Unix shar (shell archive), aiar is a format and a tool for bundling a directory structure and its files into a single, executable bash/zsh script. It's designed to make sending and receiving file collections in a chat-based or text-only environment—like interacting with an LLM—as simple as copying and pasting a single block of text.
|
|
15
|
+
|
|
16
|
+
## **Purpose**
|
|
17
|
+
|
|
18
|
+
The primary purpose of the aiar format is to package a project's files into a single text block for use with a Large Language Model. This allows an LLM to receive or transmit a collection of files within a text-only interface, bypassing the need for binary archive formats like .zip. (Yes, I once had an LLM try to send me a base 64 encoded zip file, I kid you not. And, no, it wasn’t a valid zip file.)
|
|
19
|
+
|
|
20
|
+
## **Key Features**
|
|
21
|
+
|
|
22
|
+
* **Single File:** The entire archive is one text file. Easy to copy, paste, and save.
|
|
23
|
+
* **LLM-Friendly:** The format is simple for an LLM to generate or consume. Because the file content is never executed, the LLM doesn't need to worry about shell-escaping special characters.
|
|
24
|
+
* **Self-Contained:** The extraction logic is bundled with the data. No external tools like zip or tar are needed to unpack it.
|
|
25
|
+
|
|
26
|
+
## **The aiar Format**
|
|
27
|
+
|
|
28
|
+
An aiar script has two main parts, separated by an exit 0 command.
|
|
29
|
+
|
|
30
|
+
1. **The Unpacker Logic:** A bash script that reads its own file, line by line. It looks for a unique separator line that denotes the start of a new file. This part is optional if you use the "aiar" tool and might
|
|
31
|
+
even be desirable if you don't want to run code directly from an LLM.
|
|
32
|
+
2. **The exit 0 Guard:** This command prevents the shell from ever trying to execute the data section below it.
|
|
33
|
+
3. **The Data Payload:** The raw, unescaped contents of your files, each preceded by the unique separator line.
|
|
34
|
+
|
|
35
|
+
## **Supported Formats**
|
|
36
|
+
|
|
37
|
+
The `aiar` tool supports multiple output formats:
|
|
38
|
+
|
|
39
|
+
- **bare** (`.aiar`) - Data-only format without self-extraction script. Most LLM-friendly for creation.
|
|
40
|
+
- **bash** (`.sh`) - Self-extracting Bash script (default). Compatible with Linux/macOS/WSL.
|
|
41
|
+
- **python** (`.py`) - Self-extracting Python script. Requires Python 3.6+.
|
|
42
|
+
- **nodejs** (`.js`) - Self-extracting Node.js script. Requires Node.js.
|
|
43
|
+
- **powershell** (`.ps1`) - Self-extracting PowerShell script. Requires PowerShell 5.1+.
|
|
44
|
+
|
|
45
|
+
All formats support both text and binary files and can be extracted using the `aiar extract` command.
|
|
46
|
+
|
|
47
|
+
### **Example Archives**
|
|
48
|
+
|
|
49
|
+
Below are examples of each format. Each demonstrates the same two-file archive structure.
|
|
50
|
+
|
|
51
|
+
## **Bare Format (.aiar)**
|
|
52
|
+
|
|
53
|
+
The bare format is the simplest and most LLM-friendly. It contains only the data with a separator definition, no extraction logic. This format can be extracted using `aiar extract archive.aiar`.
|
|
54
|
+
|
|
55
|
+
**Note for LLMs:** To create a bare format archive:
|
|
56
|
+
1. Start with `SEPARATOR="..."` (use a unique string not found in file contents)
|
|
57
|
+
2. Mark each file with `:t:` for text or `:b:` for binary
|
|
58
|
+
3. Use format: `SEPARATOR:t:filepath` followed by file content
|
|
59
|
+
4. The contents of the binary blobs are base64 encoded.
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
SEPARATOR="++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:"
|
|
63
|
+
|
|
64
|
+
++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:t:example/README.md
|
|
65
|
+
# Example Project
|
|
66
|
+
|
|
67
|
+
This is a sample file in the bare aiar format.
|
|
68
|
+
|
|
69
|
+
++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:t:example/hello.txt
|
|
70
|
+
Hello from aiar!
|
|
71
|
+
This file can contain any text content.
|
|
72
|
+
Special characters like $, #, ', ", `, $(cmd) are preserved literally.
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## **Bash Format (.sh)**
|
|
76
|
+
|
|
77
|
+
The Bash format is a self-extracting shell script. Save as `archive.sh` and run with `bash archive.sh`.
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
#!/bin/bash
|
|
81
|
+
|
|
82
|
+
SEPARATOR="++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:"
|
|
83
|
+
|
|
84
|
+
writing=false
|
|
85
|
+
# Function to report errors and exit cleanly
|
|
86
|
+
handle_error() {
|
|
87
|
+
echo "Error: $1" >&2
|
|
88
|
+
exit 1
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
# Function to close the previous file descriptor and wait for bg processes
|
|
92
|
+
close_previous_fd() {
|
|
93
|
+
if [ "$writing" = true ]; then
|
|
94
|
+
exec 3>&-
|
|
95
|
+
wait 2>/dev/null || true
|
|
96
|
+
fi
|
|
97
|
+
writing=false
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
while read -r line; do
|
|
101
|
+
if [[ "$line" == "$SEPARATOR"* ]]; then
|
|
102
|
+
close_previous_fd
|
|
103
|
+
payload=$(echo "$line" | cut -d ':' -f 2-)
|
|
104
|
+
type=$(echo "$payload" | cut -d ':' -f 1)
|
|
105
|
+
filepath=$(echo "$payload" | cut -d ':' -f 2-)
|
|
106
|
+
if [ -n "$filepath" ]; then
|
|
107
|
+
echo "Creating: $filepath"
|
|
108
|
+
mkdir -p "$(dirname "$filepath")" || handle_error "Cannot create directory for '$filepath'."
|
|
109
|
+
if [ "$type" == "b" ]; then
|
|
110
|
+
exec 3> >(base64 -d > "$filepath") || handle_error "Cannot start base64 process for '$filepath'."
|
|
111
|
+
writing=true
|
|
112
|
+
elif [ "$type" == "t" ]; then
|
|
113
|
+
exec 3>"$filepath" || handle_error "Cannot open '$filepath' for writing."
|
|
114
|
+
writing=true
|
|
115
|
+
else
|
|
116
|
+
handle_error "Invalid file type '$type' in separator."
|
|
117
|
+
fi
|
|
118
|
+
fi
|
|
119
|
+
elif [ "$writing" = true ]; then
|
|
120
|
+
echo "$line" >&3
|
|
121
|
+
fi
|
|
122
|
+
done < "$0"
|
|
123
|
+
|
|
124
|
+
close_previous_fd
|
|
125
|
+
echo "Extraction complete."
|
|
126
|
+
exit 0
|
|
127
|
+
|
|
128
|
+
# --- DATA ---
|
|
129
|
+
|
|
130
|
+
++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:t:example/README.md
|
|
131
|
+
# Example Project
|
|
132
|
+
|
|
133
|
+
This file includes special characters: $PATH, #comment, 'quotes', "quotes", `backticks`, $(cmd)
|
|
134
|
+
All are preserved literally.
|
|
135
|
+
|
|
136
|
+
++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:t:example/hello.txt
|
|
137
|
+
Hello from aiar!
|
|
138
|
+
She said, "He's going to the store for $5."
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## **Python Format (.py)**
|
|
142
|
+
|
|
143
|
+
The Python format is a self-extracting Python script. Save as `archive.py` and run with `python archive.py`. Files are embedded as commented lines with `# ` prefix.
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
import sys, os, re, base64
|
|
147
|
+
from pathlib import Path
|
|
148
|
+
|
|
149
|
+
SEPARATOR="++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:"
|
|
150
|
+
SEP = re.escape(SEPARATOR)
|
|
151
|
+
|
|
152
|
+
def _safe_dest(rel: str) -> Path:
|
|
153
|
+
p = Path(rel)
|
|
154
|
+
if p.is_absolute():
|
|
155
|
+
raise ValueError(f"Absolute path not allowed: {rel}")
|
|
156
|
+
dest = (Path(".") / p).resolve()
|
|
157
|
+
if Path(".").resolve() not in (set(dest.parents) | {dest}):
|
|
158
|
+
raise ValueError(f"Path escapes output root: {rel}")
|
|
159
|
+
return dest
|
|
160
|
+
|
|
161
|
+
def extract_all():
|
|
162
|
+
with open(__file__, "r", encoding="utf-8") as f:
|
|
163
|
+
script_content = f.read()
|
|
164
|
+
|
|
165
|
+
pat = re.compile(
|
|
166
|
+
rf"^# ?{SEP}([tb]):([^\n]+)\n(.*?)(?=^# ?{SEP}[tb]:|\Z)",
|
|
167
|
+
re.DOTALL | re.MULTILINE,)
|
|
168
|
+
|
|
169
|
+
any_found = False
|
|
170
|
+
for ftype, path, body in pat.findall(script_content):
|
|
171
|
+
any_found = True
|
|
172
|
+
path = path.strip()
|
|
173
|
+
try:
|
|
174
|
+
dest = _safe_dest(path)
|
|
175
|
+
except ValueError as e:
|
|
176
|
+
print(f"Warning: {e}. Skipping.")
|
|
177
|
+
continue
|
|
178
|
+
|
|
179
|
+
if dest.exists():
|
|
180
|
+
print(f"Skipping already existing file: '{dest}'")
|
|
181
|
+
continue
|
|
182
|
+
|
|
183
|
+
print(f"Creating: {dest}")
|
|
184
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
185
|
+
|
|
186
|
+
uncommented_body = re.sub(r"^# ?", "", body, flags=re.MULTILINE)
|
|
187
|
+
|
|
188
|
+
if ftype == "t":
|
|
189
|
+
with open(dest, "w", encoding="utf-8", newline="\n") as out:
|
|
190
|
+
out.write(uncommented_body)
|
|
191
|
+
else: # binary
|
|
192
|
+
with open(dest, "wb") as out:
|
|
193
|
+
out.write(base64.b64decode(uncommented_body.strip().encode("ascii"), validate=False))
|
|
194
|
+
|
|
195
|
+
if not any_found:
|
|
196
|
+
print("Error: No payload sections found in data block.")
|
|
197
|
+
sys.exit(1)
|
|
198
|
+
|
|
199
|
+
extract_all()
|
|
200
|
+
print("Extraction complete.")
|
|
201
|
+
sys.exit(0)
|
|
202
|
+
|
|
203
|
+
# ++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:t:example/README.md
|
|
204
|
+
# # Example Project
|
|
205
|
+
#
|
|
206
|
+
# This file includes special characters: $PATH, #comment, 'quotes', "quotes", `backticks`, $(cmd)
|
|
207
|
+
# All are preserved literally.
|
|
208
|
+
#
|
|
209
|
+
# ++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:t:example/hello.txt
|
|
210
|
+
# Hello from aiar!
|
|
211
|
+
# She said, "He's going to the store for $5."
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## **Node.js Format (.js)**
|
|
215
|
+
|
|
216
|
+
The Node.js format is a self-extracting Node.js script. Save as `archive.js` and run with `node archive.js`. Files are embedded as commented lines with `// ` prefix.
|
|
217
|
+
|
|
218
|
+
```javascript
|
|
219
|
+
#!/usr/bin/env node
|
|
220
|
+
|
|
221
|
+
const fs = require('fs');
|
|
222
|
+
const path = require('path');
|
|
223
|
+
|
|
224
|
+
function escapeRegex(str) {
|
|
225
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const SEPARATOR = "++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:";
|
|
229
|
+
const SEP = escapeRegex(SEPARATOR);
|
|
230
|
+
|
|
231
|
+
function safeDest(rel) {
|
|
232
|
+
if (path.isAbsolute(rel)) {
|
|
233
|
+
throw new Error(`Absolute path not allowed: ${rel}`);
|
|
234
|
+
}
|
|
235
|
+
const dest = path.resolve(process.cwd(), rel);
|
|
236
|
+
if (!dest.startsWith(process.cwd())) {
|
|
237
|
+
throw new Error(`Path escapes output root: ${rel}`);
|
|
238
|
+
}
|
|
239
|
+
return dest;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function extractAll() {
|
|
243
|
+
const scriptContent = fs.readFileSync(__filename, 'utf8');
|
|
244
|
+
|
|
245
|
+
const pat = new RegExp(
|
|
246
|
+
`^// ?${SEP}([tb]):([^\\n]+)\\n(.*?)(?=(^// ?${SEP}[tb]:|\\Z))`,
|
|
247
|
+
'gms'
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
const matches = [...scriptContent.matchAll(pat)];
|
|
251
|
+
|
|
252
|
+
if (matches.length === 0) {
|
|
253
|
+
console.error("Error: No payload sections found in data block.");
|
|
254
|
+
process.exit(1);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
for (const match of matches) {
|
|
258
|
+
const [, ftype, relPath, body] = match;
|
|
259
|
+
const cleanPath = relPath.trim();
|
|
260
|
+
|
|
261
|
+
let dest;
|
|
262
|
+
try {
|
|
263
|
+
dest = safeDest(cleanPath);
|
|
264
|
+
} catch (e) {
|
|
265
|
+
console.warn(`Warning: ${e.message}. Skipping.`);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (fs.existsSync(dest)) {
|
|
270
|
+
console.log(`Skipping already existing file: '${dest}'`);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
console.log(`Creating: ${dest}`);
|
|
275
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
276
|
+
|
|
277
|
+
const uncommentedBody = body.replace(/^\/\/ ?/gm, '');
|
|
278
|
+
|
|
279
|
+
if (ftype === 't') {
|
|
280
|
+
fs.writeFileSync(dest, uncommentedBody, { encoding: 'utf8' });
|
|
281
|
+
} else {
|
|
282
|
+
const buffer = Buffer.from(uncommentedBody.trim(), 'base64');
|
|
283
|
+
fs.writeFileSync(dest, buffer);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
extractAll();
|
|
289
|
+
console.log("Extraction complete.");
|
|
290
|
+
process.exit(0);
|
|
291
|
+
|
|
292
|
+
// ++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:t:example/README.md
|
|
293
|
+
// # Example Project
|
|
294
|
+
//
|
|
295
|
+
// This file includes special characters: $PATH, #comment, 'quotes', "quotes", `backticks`, $(cmd)
|
|
296
|
+
// All are preserved literally.
|
|
297
|
+
//
|
|
298
|
+
// ++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:t:example/hello.txt
|
|
299
|
+
// Hello from aiar!
|
|
300
|
+
// She said, "He's going to the store for $5."
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## **PowerShell Format (.ps1)**
|
|
304
|
+
|
|
305
|
+
The PowerShell format is a self-extracting PowerShell script. Save as `archive.ps1` and run with `powershell -ExecutionPolicy Bypass -File archive.ps1`. Files are embedded as commented lines with `# ` prefix.
|
|
306
|
+
|
|
307
|
+
```powershell
|
|
308
|
+
#Requires -Version 5.1
|
|
309
|
+
|
|
310
|
+
$SEPARATOR="++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:"
|
|
311
|
+
|
|
312
|
+
function Escape-Regex {
|
|
313
|
+
param([string]$String)
|
|
314
|
+
return [System.Text.RegularExpressions.Regex]::Escape($String)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function Safe-Dest {
|
|
318
|
+
param([string]$RelativePath)
|
|
319
|
+
if ([System.IO.Path]::IsPathRooted($RelativePath)) {
|
|
320
|
+
throw "Absolute path not allowed: $RelativePath"
|
|
321
|
+
}
|
|
322
|
+
$resolvedPath = [System.IO.Path]::GetFullPath((Join-Path -Path $PWD.Path -ChildPath $RelativePath))
|
|
323
|
+
if (-not $resolvedPath.StartsWith($PWD.Path)) {
|
|
324
|
+
throw "Path escapes output root: $RelativePath"
|
|
325
|
+
}
|
|
326
|
+
return $resolvedPath
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function Extract-All {
|
|
330
|
+
$scriptPath = $PSCommandPath
|
|
331
|
+
$scriptContent = Get-Content -Path $scriptPath -Raw
|
|
332
|
+
$sep = Escape-Regex "$SEPARATOR"
|
|
333
|
+
$pattern = "(?ms)^#\s?$sep([tb]):([^\n]+)\n(.*?)(?=(^#\s?$sep[tb]:|\Z))"
|
|
334
|
+
$matches = [System.Text.RegularExpressions.Regex]::Matches($scriptContent, $pattern)
|
|
335
|
+
|
|
336
|
+
if ($matches.Count -eq 0) {
|
|
337
|
+
Write-Error "No payload sections found in data block."
|
|
338
|
+
exit 1
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
foreach ($match in $matches) {
|
|
342
|
+
$ftype = $match.Groups[1].Value
|
|
343
|
+
$relPath = $match.Groups[2].Value.Trim()
|
|
344
|
+
$body = $match.Groups[3].Value
|
|
345
|
+
|
|
346
|
+
try {
|
|
347
|
+
$dest = Safe-Dest -RelativePath $relPath
|
|
348
|
+
} catch {
|
|
349
|
+
Write-Warning "Warning: $_. Skipping."
|
|
350
|
+
continue
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (Test-Path -LiteralPath $dest) {
|
|
354
|
+
Write-Output "Skipping already existing file: '$dest'"
|
|
355
|
+
continue
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
Write-Output "Creating: $dest"
|
|
359
|
+
$null = New-Item -ItemType Directory -Force -Path (Split-Path -Path $dest -Parent)
|
|
360
|
+
$uncommentedBody = $body -replace '(?m)^#\s?' , ''
|
|
361
|
+
|
|
362
|
+
if ($ftype -eq 't') {
|
|
363
|
+
Set-Content -Path $dest -Value $uncommentedBody -NoNewline -Encoding utf8
|
|
364
|
+
} elseif ($ftype -eq 'b') {
|
|
365
|
+
$cleanBase64String = $uncommentedBody -replace '\s'
|
|
366
|
+
$bytes = [System.Convert]::FromBase64String($cleanBase64String)
|
|
367
|
+
[System.IO.File]::WriteAllBytes($dest, $bytes)
|
|
368
|
+
} else {
|
|
369
|
+
Write-Warning "Unknown file type '$ftype' for '$relPath'. Skipping."
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
Extract-All
|
|
375
|
+
Write-Output "Extraction complete."
|
|
376
|
+
exit 0
|
|
377
|
+
|
|
378
|
+
# --- PAYLOAD ---
|
|
379
|
+
# ++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:t:example/README.md
|
|
380
|
+
# # Example Project
|
|
381
|
+
#
|
|
382
|
+
# This file includes special characters: $PATH, #comment, 'quotes', "quotes", `backticks`, $(cmd)
|
|
383
|
+
# All are preserved literally.
|
|
384
|
+
#
|
|
385
|
+
# ++++++++++--------:a1b2c3d4-5678-90ab-cdef-1234567890ab:t:example/hello.txt
|
|
386
|
+
# Hello from aiar!
|
|
387
|
+
# She said, "He's going to the store for $5."
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
## **License**
|
|
392
|
+
|
|
393
|
+
This project is licensed under the MIT License. See the LICENSE file for details.
|