-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathcli.py
334 lines (296 loc) · 8.72 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import os
import sys
from fnmatch import fnmatch
import click
global_index = 1
EXT_TO_LANG = {
"py": "python",
"c": "c",
"cpp": "cpp",
"java": "java",
"js": "javascript",
"ts": "typescript",
"html": "html",
"css": "css",
"xml": "xml",
"json": "json",
"yaml": "yaml",
"yml": "yaml",
"sh": "bash",
"rb": "ruby",
}
def should_ignore(path, gitignore_rules):
for rule in gitignore_rules:
if fnmatch(os.path.basename(path), rule):
return True
if os.path.isdir(path) and fnmatch(os.path.basename(path) + "/", rule):
return True
return False
def read_gitignore(path):
gitignore_path = os.path.join(path, ".gitignore")
if os.path.isfile(gitignore_path):
with open(gitignore_path, "r") as f:
return [
line.strip() for line in f if line.strip() and not line.startswith("#")
]
return []
def add_line_numbers(content):
lines = content.splitlines()
padding = len(str(len(lines)))
numbered_lines = [f"{i + 1:{padding}} {line}" for i, line in enumerate(lines)]
return "\n".join(numbered_lines)
def print_path(writer, path, content, cxml, markdown, line_numbers):
if cxml:
print_as_xml(writer, path, content, line_numbers)
elif markdown:
print_as_markdown(writer, path, content, line_numbers)
else:
print_default(writer, path, content, line_numbers)
def print_default(writer, path, content, line_numbers):
writer(path)
writer("---")
if line_numbers:
content = add_line_numbers(content)
writer(content)
writer("")
writer("---")
def print_as_xml(writer, path, content, line_numbers):
global global_index
writer(f'<document index="{global_index}">')
writer(f"<source>{path}</source>")
writer("<document_content>")
if line_numbers:
content = add_line_numbers(content)
writer(content)
writer("</document_content>")
writer("</document>")
global_index += 1
def print_as_markdown(writer, path, content, line_numbers):
lang = EXT_TO_LANG.get(path.split(".")[-1], "")
# Figure out how many backticks to use
backticks = "```"
while backticks in content:
backticks += "`"
writer(path)
writer(f"{backticks}{lang}")
if line_numbers:
content = add_line_numbers(content)
writer(content)
writer(f"{backticks}")
def process_path(
path,
extensions,
include_hidden,
ignore_files_only,
ignore_gitignore,
gitignore_rules,
ignore_patterns,
writer,
claude_xml,
markdown,
line_numbers=False,
):
if os.path.isfile(path):
try:
with open(path, "r") as f:
print_path(writer, path, f.read(), claude_xml, markdown, line_numbers)
except UnicodeDecodeError:
warning_message = f"Warning: Skipping file {path} due to UnicodeDecodeError"
click.echo(click.style(warning_message, fg="red"), err=True)
elif os.path.isdir(path):
for root, dirs, files in os.walk(path):
if not include_hidden:
dirs[:] = [d for d in dirs if not d.startswith(".")]
files = [f for f in files if not f.startswith(".")]
if not ignore_gitignore:
gitignore_rules.extend(read_gitignore(root))
dirs[:] = [
d
for d in dirs
if not should_ignore(os.path.join(root, d), gitignore_rules)
]
files = [
f
for f in files
if not should_ignore(os.path.join(root, f), gitignore_rules)
]
if ignore_patterns:
if not ignore_files_only:
dirs[:] = [
d
for d in dirs
if not any(fnmatch(d, pattern) for pattern in ignore_patterns)
]
files = [
f
for f in files
if not any(fnmatch(f, pattern) for pattern in ignore_patterns)
]
if extensions:
files = [f for f in files if f.endswith(extensions)]
for file in sorted(files):
file_path = os.path.join(root, file)
try:
with open(file_path, "r") as f:
print_path(
writer,
file_path,
f.read(),
claude_xml,
markdown,
line_numbers,
)
except UnicodeDecodeError:
warning_message = (
f"Warning: Skipping file {file_path} due to UnicodeDecodeError"
)
click.echo(click.style(warning_message, fg="red"), err=True)
def read_paths_from_stdin(use_null_separator):
if sys.stdin.isatty():
# No ready input from stdin, don't block for input
return []
stdin_content = sys.stdin.read()
if use_null_separator:
paths = stdin_content.split("\0")
else:
paths = stdin_content.split() # split on whitespace
return [p for p in paths if p]
@click.command()
@click.argument("paths", nargs=-1, type=click.Path(exists=True))
@click.option("extensions", "-e", "--extension", multiple=True)
@click.option(
"--include-hidden",
is_flag=True,
help="Include files and folders starting with .",
)
@click.option(
"--ignore-files-only",
is_flag=True,
help="--ignore option only ignores files",
)
@click.option(
"--ignore-gitignore",
is_flag=True,
help="Ignore .gitignore files and include all files",
)
@click.option(
"ignore_patterns",
"--ignore",
multiple=True,
default=[],
help="List of patterns to ignore",
)
@click.option(
"output_file",
"-o",
"--output",
type=click.Path(writable=True),
help="Output to a file instead of stdout",
)
@click.option(
"claude_xml",
"-c",
"--cxml",
is_flag=True,
help="Output in XML-ish format suitable for Claude's long context window.",
)
@click.option(
"markdown",
"-m",
"--markdown",
is_flag=True,
help="Output Markdown with fenced code blocks",
)
@click.option(
"line_numbers",
"-n",
"--line-numbers",
is_flag=True,
help="Add line numbers to the output",
)
@click.option(
"--null",
"-0",
is_flag=True,
help="Use NUL character as separator when reading from stdin",
)
@click.version_option()
def cli(
paths,
extensions,
include_hidden,
ignore_files_only,
ignore_gitignore,
ignore_patterns,
output_file,
claude_xml,
markdown,
line_numbers,
null,
):
"""
Takes one or more paths to files or directories and outputs every file,
recursively, each one preceded with its filename like this:
\b
path/to/file.py
----
Contents of file.py goes here
---
path/to/file2.py
---
...
If the `--cxml` flag is provided, the output will be structured as follows:
\b
<documents>
<document path="path/to/file1.txt">
Contents of file1.txt
</document>
<document path="path/to/file2.txt">
Contents of file2.txt
</document>
...
</documents>
If the `--markdown` flag is provided, the output will be structured as follows:
\b
path/to/file1.py
```python
Contents of file1.py
```
"""
# Reset global_index for pytest
global global_index
global_index = 1
# Read paths from stdin if available
stdin_paths = read_paths_from_stdin(use_null_separator=null)
# Combine paths from arguments and stdin
paths = [*paths, *stdin_paths]
gitignore_rules = []
writer = click.echo
fp = None
if output_file:
fp = open(output_file, "w", encoding="utf-8")
writer = lambda s: print(s, file=fp)
for path in paths:
if not os.path.exists(path):
raise click.BadArgumentUsage(f"Path does not exist: {path}")
if not ignore_gitignore:
gitignore_rules.extend(read_gitignore(os.path.dirname(path)))
if claude_xml and path == paths[0]:
writer("<documents>")
process_path(
path,
extensions,
include_hidden,
ignore_files_only,
ignore_gitignore,
gitignore_rules,
ignore_patterns,
writer,
claude_xml,
markdown,
line_numbers,
)
if claude_xml and len(paths) > 0:
writer("</documents>")
if fp:
fp.close()