aimancli 0.2.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.
@@ -0,0 +1,690 @@
1
+ #!/usr/bin/env python3
2
+ """Generate an intent -> `find` command dataset for `aiman find "<intent>"`.
3
+
4
+ Coverage is derived from the GNU find man page: tests (-name/-iname/-path/-regex,
5
+ -type, -size, -empty, -mtime/-mmin/-atime/-ctime/-newer, -perm, -user/-group,
6
+ -nouser/-nogroup, -executable/-readable/-writable, -links, -samefile), global
7
+ options (-maxdepth/-mindepth/-xdev), operators (! -o \\( \\)), -prune, and actions
8
+ (-print0, -delete, -exec ... {} + / \\;, -execdir, -ok, -ls, -printf, -quit, -fprint).
9
+
10
+ Used by aiman.tools.find.FindTool.generate().
11
+ """
12
+ import random
13
+ import re
14
+
15
+ SEED = 42
16
+ rng = random.Random(SEED)
17
+
18
+ # ---------------------------------------------------------------- vocab
19
+ PATHS = [
20
+ (".", ["in the current directory", "here", "in this folder", "under the current dir", "in cwd", ""]),
21
+ ("/var/log", ["in /var/log", "under /var/log", "inside /var/log"]),
22
+ ("/tmp", ["in /tmp", "under /tmp", "in the temp directory"]),
23
+ ("~", ["in my home directory", "under my home folder", "in ~", "in home"]),
24
+ ("/etc", ["in /etc", "under /etc", "in the config directory /etc"]),
25
+ ("/home", ["under /home", "in all home directories", "in /home"]),
26
+ ("/usr/bin", ["in /usr/bin", "under /usr/bin"]),
27
+ ("src", ["in the src directory", "under src", "inside src/"]),
28
+ ("/", ["on the whole system", "across the entire filesystem", "everywhere", "from root"]),
29
+ ("/srv/www", ["in /srv/www", "under the web root /srv/www"]),
30
+ ("/opt", ["in /opt", "under /opt"]),
31
+ ]
32
+
33
+ EXTS = {
34
+ "py": ["Python files", "python scripts", ".py files"],
35
+ "log": ["log files", ".log files", "logs"],
36
+ "txt": ["text files", ".txt files"],
37
+ "conf": ["config files", ".conf files"],
38
+ "jpg": ["JPG images", "jpg files", ".jpg photos"],
39
+ "png": ["PNG images", ".png files"],
40
+ "c": ["C source files", ".c files"],
41
+ "sh": ["shell scripts", ".sh files"],
42
+ "md": ["markdown files", ".md files"],
43
+ "json": ["JSON files", ".json files"],
44
+ "yaml": ["YAML files", ".yaml files"],
45
+ "tmp": ["temp files", ".tmp files"],
46
+ "bak": ["backup files", ".bak files"],
47
+ "mp4": ["MP4 videos", ".mp4 files"],
48
+ "pdf": ["PDFs", "PDF documents", ".pdf files"],
49
+ "csv": ["CSV files", ".csv files"],
50
+ "js": ["JavaScript files", ".js files"],
51
+ "gz": ["gzip archives", ".gz files", "compressed .gz files"],
52
+ "iso": ["ISO images", ".iso files"],
53
+ "html": ["HTML files", ".html pages"],
54
+ }
55
+
56
+ NAMES = ["core", "needle", "config.yaml", "README.md", "Makefile", ".env",
57
+ "id_rsa", "docker-compose.yml", "package.json", "nginx.conf", "hosts",
58
+ "settings.py", "Dockerfile", ".gitignore", "notes.txt"]
59
+
60
+ SUBSTRS = ["backup", "test", "report", "2024", "draft", "cache", "old", "invoice", "temp", "log"]
61
+
62
+ SIZES = [ # (find arg, phrases)
63
+ ("+100M", ["larger than 100MB", "bigger than 100 MB", "over 100M", "more than 100 megabytes"]),
64
+ ("+1G", ["larger than 1GB", "bigger than 1 gigabyte", "over 1G"]),
65
+ ("+500M", ["larger than 500MB", "over 500 MB"]),
66
+ ("+10M", ["larger than 10MB", "over 10 megs", "bigger than 10M"]),
67
+ ("+50k", ["larger than 50KB", "over 50k"]),
68
+ ("-10k", ["smaller than 10KB", "under 10k", "less than 10 kilobytes"]),
69
+ ("-1M", ["smaller than 1MB", "under a megabyte"]),
70
+ ("+2G", ["bigger than 2GB", "over 2 gigabytes"]),
71
+ ]
72
+
73
+ MTIMES = [ # (expr, phrases)
74
+ ("-mtime -1", ["modified in the last day", "changed within the last 24 hours", "edited since yesterday"]),
75
+ ("-mtime 0", ["modified today", "modified in the last 24 hours"]),
76
+ ("-mtime -7", ["modified in the last week", "changed within 7 days", "edited this week"]),
77
+ ("-mtime +30", ["older than 30 days", "not modified in 30 days", "last modified over a month ago"]),
78
+ ("-mtime +7", ["older than a week", "not touched in 7 days", "modified more than 7 days ago"]),
79
+ ("-mtime +90", ["older than 90 days", "not modified in 3 months"]),
80
+ ("-mtime +365", ["older than a year", "not modified for a year"]),
81
+ ("-mmin -60", ["modified in the last hour", "changed within 60 minutes"]),
82
+ ("-mmin -10", ["modified in the last 10 minutes", "changed in the past 10 min"]),
83
+ ("-mmin -30", ["modified in the last half hour", "changed in the past 30 minutes"]),
84
+ ("-atime +90", ["not accessed in 90 days", "unused for 3 months"]),
85
+ ("-atime -1", ["accessed in the last day", "opened within 24 hours"]),
86
+ ("-amin -5", ["accessed in the last 5 minutes"]),
87
+ ("-ctime -2", ["whose status changed in the last 2 days", "with metadata changed in the past 2 days"]),
88
+ ("-cmin -15", ["whose status changed in the last 15 minutes"]),
89
+ ]
90
+
91
+ TYPES = {
92
+ "f": ["files", "regular files", "plain files"],
93
+ "d": ["directories", "folders", "dirs", "subdirectories"],
94
+ "l": ["symlinks", "symbolic links", "soft links"],
95
+ "s": ["sockets", "unix sockets"],
96
+ "p": ["named pipes", "FIFOs"],
97
+ "b": ["block devices", "block special files"],
98
+ "c": ["character devices", "char special files"],
99
+ }
100
+
101
+ USERS = ["root", "www-data", "alice", "bob", "postgres", "nobody", "deploy"]
102
+ GROUPS = ["staff", "www-data", "docker", "adm", "developers", "wheel"]
103
+
104
+ PERMS = [
105
+ ("-perm 777", ["with permissions 777", "with mode 777", "that are chmod 777"]),
106
+ ("-perm 644", ["with exactly 644 permissions", "with mode exactly 644"]),
107
+ ("-perm 600", ["with exactly 600 permissions"]),
108
+ ("-perm -4000", ["with the SUID bit set", "that are setuid", "with setuid permission"]),
109
+ ("-perm -2000", ["with the SGID bit set", "that are setgid"]),
110
+ ("-perm -1000", ["with the sticky bit set"]),
111
+ ("-perm -o+w", ["that are world-writable", "writable by everyone", "writable by others"]),
112
+ ("-perm /222", ["writable by somebody", "writable by anyone (owner, group or other)"]),
113
+ ("-perm /u+x", ["executable by the owner"]),
114
+ ("-perm -664", ["readable and writable by owner and group and readable by others"]),
115
+ ("-perm -444", ["readable by everybody", "world-readable"]),
116
+ ("! -perm /111", ["not executable by anyone", "without any execute bit"]),
117
+ ("-perm -u+w,g+w", ["writable by both owner and group"]),
118
+ ]
119
+
120
+ EXCLUDES = ["node_modules", ".git", "venv", "build", "__pycache__", ".cache", "vendor", "dist"]
121
+
122
+ GREP_WORDS = ["TODO", "password", "error", "FIXME", "localhost", "api_key"]
123
+
124
+ FIND_VERBS = ["find", "list", "show", "search for", "locate", "get", "look for", "show me", "find all",
125
+ "list all", "give me", "which are the"]
126
+
127
+
128
+ def v():
129
+ return rng.choice(FIND_VERBS)
130
+
131
+
132
+ def path():
133
+ p, phrases = rng.choice(PATHS)
134
+ return p, rng.choice(phrases)
135
+
136
+
137
+ def q(s):
138
+ """single-quote a glob for the shell"""
139
+ return "'" + s + "'"
140
+
141
+
142
+ def join_intent(*parts):
143
+ s = " ".join(p for p in parts if p).strip()
144
+ s = re.sub(r"\s+", " ", s)
145
+ # random casing / punctuation noise for robustness
146
+ r = rng.random()
147
+ if r < 0.15:
148
+ s = s.capitalize()
149
+ elif r < 0.2:
150
+ s = s + "?"
151
+ return s
152
+
153
+
154
+ def cmd(p, *expr, pipe=None):
155
+ c = " ".join(["find", p] + [e for e in expr if e])
156
+ if pipe:
157
+ c += " | " + pipe
158
+ return c
159
+
160
+
161
+ pairs = []
162
+
163
+
164
+ def add(intent, command):
165
+ pairs.append((intent, command))
166
+
167
+
168
+ # ---------------------------------------------------------------- generators
169
+ def g_name_ext():
170
+ ext, phr = rng.choice(list(EXTS.items()))
171
+ p, pp = path()
172
+ add(join_intent(v(), rng.choice(phr), pp), cmd(p, "-type f", "-name", q("*." + ext)))
173
+
174
+
175
+ def g_exact_name():
176
+ n = rng.choice(NAMES)
177
+ p, pp = path()
178
+ t = rng.choice([f"a file named {n}", f"the file {n}", f"where is {n}", f"{n}", f"files called {n}"])
179
+ if t.startswith("where"):
180
+ add(join_intent(t, pp), cmd(p, "-name", q(n)))
181
+ else:
182
+ add(join_intent(v(), t, pp), cmd(p, "-name", q(n)))
183
+
184
+
185
+ def g_iname():
186
+ w = rng.choice(SUBSTRS)
187
+ p, pp = path()
188
+ t = rng.choice([f"files with '{w}' in the name, ignoring case",
189
+ f"case-insensitive search for names containing {w}",
190
+ f"files whose name contains {w} regardless of case"])
191
+ add(join_intent(v(), t, pp), cmd(p, "-iname", q(f"*{w}*")))
192
+
193
+
194
+ def g_name_contains():
195
+ w = rng.choice(SUBSTRS)
196
+ p, pp = path()
197
+ t = rng.choice([f"files with {w} in the name", f"files whose name contains {w}",
198
+ f"anything named like *{w}*"])
199
+ add(join_intent(v(), t, pp), cmd(p, "-name", q(f"*{w}*")))
200
+
201
+
202
+ def g_name_starts():
203
+ w = rng.choice(SUBSTRS)
204
+ p, pp = path()
205
+ add(join_intent(v(), f"files starting with {w}", pp), cmd(p, "-type f", "-name", q(f"{w}*")))
206
+
207
+
208
+ def g_type():
209
+ t, phr = rng.choice(list(TYPES.items()))
210
+ p, pp = path()
211
+ add(join_intent(v(), "all", rng.choice(phr), pp), cmd(p, "-type", t))
212
+
213
+
214
+ def g_multi_ext():
215
+ a, b = rng.sample(list(EXTS), 2)
216
+ p, pp = path()
217
+ add(join_intent(v(), f"all .{a} and .{b} files", pp),
218
+ cmd(p, "-type f", "\\(", "-name", q("*." + a), "-o", "-name", q("*." + b), "\\)"))
219
+
220
+
221
+ def g_not_name():
222
+ ext, phr = rng.choice(list(EXTS.items()))
223
+ p, pp = path()
224
+ add(join_intent(v(), f"files that are not {rng.choice(phr)}", pp),
225
+ cmd(p, "-type f", "! -name", q("*." + ext)))
226
+
227
+
228
+ def g_size():
229
+ s, phr = rng.choice(SIZES)
230
+ p, pp = path()
231
+ add(join_intent(v(), "files", rng.choice(phr), pp), cmd(p, "-type f", "-size", s))
232
+
233
+
234
+ def g_size_range():
235
+ p, pp = path()
236
+ rr =rng.choice([("+10M", "-100M", "10MB", "100MB"), ("+1M", "-10M", "1MB", "10MB"),
237
+ ("+100M", "-1G", "100MB", "1GB")])
238
+ add(join_intent(v(), f"files between {rr[2]} and {rr[3]}", pp),
239
+ cmd(p, "-type f", "-size", rr[0], "-size", rr[1]))
240
+
241
+
242
+ def g_empty():
243
+ p, pp = path()
244
+ kind = rng.choice([("f", ["empty files", "zero-byte files", "files with size 0"]),
245
+ ("d", ["empty directories", "empty folders"]),
246
+ ("", ["empty files and directories", "anything empty"])])
247
+ add(join_intent(v(), rng.choice(kind[1]), pp),
248
+ cmd(p, f"-type {kind[0]}" if kind[0] else "", "-empty"))
249
+
250
+
251
+ def g_time():
252
+ e, phr = rng.choice(MTIMES)
253
+ p, pp = path()
254
+ t, tphr = rng.choice([("f", "files"), ("", "files and folders"), ("f", "")])
255
+ obj = tphr or "files"
256
+ add(join_intent(v(), obj, rng.choice(phr), pp), cmd(p, f"-type {t}" if t else "", e))
257
+
258
+
259
+ def g_ext_time():
260
+ ext, phr = rng.choice(list(EXTS.items()))
261
+ e, tp = rng.choice(MTIMES)
262
+ p, pp = path()
263
+ add(join_intent(v(), rng.choice(phr), rng.choice(tp), pp), cmd(p, "-type f", "-name", q("*." + ext), e))
264
+
265
+
266
+ def g_ext_size():
267
+ ext, phr = rng.choice(list(EXTS.items()))
268
+ s, sp = rng.choice(SIZES)
269
+ p, pp = path()
270
+ add(join_intent(v(), rng.choice(phr), rng.choice(sp), pp), cmd(p, "-type f", "-name", q("*." + ext), "-size", s))
271
+
272
+
273
+ def g_newer():
274
+ ref = rng.choice(["main.c", "/etc/passwd", "build.stamp", "last_backup", "config.yaml"])
275
+ p, pp = path()
276
+ if rng.random() < 0.7:
277
+ add(join_intent(v(), f"files newer than {ref}", pp), cmd(p, "-type f", "-newer", ref))
278
+ else:
279
+ add(join_intent(v(), f"files older than {ref}", pp), cmd(p, "-type f", "! -newer", ref))
280
+
281
+
282
+ def g_newermt():
283
+ d = rng.choice(["2026-01-01", "2025-06-15", "2026-11-01", "yesterday", "2026-10-31 12:00"])
284
+ p, pp = path()
285
+ if rng.random() < 0.5:
286
+ add(join_intent(v(), f"files modified after {d}", pp), cmd(p, "-type f", "-newermt", q(d)))
287
+ else:
288
+ d2 = "2026-03-31"
289
+ add(join_intent(v(), f"files modified between 2026-01-01 and {d2}", pp),
290
+ cmd(p, "-type f", "-newermt", q("2026-01-01"), "! -newermt", q(d2)))
291
+
292
+
293
+ def g_perm():
294
+ e, phr = rng.choice(PERMS)
295
+ p, pp = path()
296
+ add(join_intent(v(), "files", rng.choice(phr), pp), cmd(p, "-type f", e))
297
+
298
+
299
+ def g_user():
300
+ u = rng.choice(USERS)
301
+ p, pp = path()
302
+ add(join_intent(v(), rng.choice([f"files owned by {u}", f"files belonging to user {u}", f"{u}'s files"]), pp),
303
+ cmd(p, "-user", u))
304
+
305
+
306
+ def g_not_user():
307
+ u = rng.choice(USERS)
308
+ p, pp = path()
309
+ add(join_intent(v(), f"files not owned by {u}", pp), cmd(p, "-type f", "! -user", u))
310
+
311
+
312
+ def g_group():
313
+ g = rng.choice(GROUPS)
314
+ p, pp = path()
315
+ add(join_intent(v(), f"files in group {g}", pp), cmd(p, "-group", g))
316
+
317
+
318
+ def g_orphan():
319
+ p, pp = path()
320
+ if rng.random() < 0.5:
321
+ add(join_intent(v(), rng.choice(["files with no owner", "orphaned files with no valid user",
322
+ "files whose user doesn't exist"]), pp), cmd(p, "-nouser"))
323
+ else:
324
+ add(join_intent(v(), "files with no valid group", pp), cmd(p, "-nogroup"))
325
+
326
+
327
+ def g_access():
328
+ p, pp = path()
329
+ k = rng.choice([("-executable", "executable files"), ("-readable", "files I can read"),
330
+ ("-writable", "files I can write to"), ("! -readable", "files I can't read")])
331
+ add(join_intent(v(), k[1], pp), cmd(p, "-type f", k[0]))
332
+
333
+
334
+ def g_depth():
335
+ p, pp = path()
336
+ n = rng.choice([1, 2, 3])
337
+ ext, phr = rng.choice(list(EXTS.items()))
338
+ phrase = {1: rng.choice(["without recursing", "only at the top level", "not in subdirectories",
339
+ "non-recursively"]),
340
+ 2: "at most 2 levels deep", 3: "no deeper than 3 levels"}[n]
341
+ add(join_intent(v(), rng.choice(phr), pp, phrase), cmd(p, f"-maxdepth {n}", "-type f", "-name", q("*." + ext)))
342
+
343
+
344
+ def g_mindepth():
345
+ p, pp = path()
346
+ add(join_intent(v(), "everything except the starting directory itself", pp), cmd(p, "-mindepth 1"))
347
+
348
+
349
+ def g_subdirs_only():
350
+ p, pp = path()
351
+ add(join_intent(v(), "immediate subdirectories", pp), cmd(p, "-mindepth 1", "-maxdepth 1", "-type d"))
352
+
353
+
354
+ def g_xdev():
355
+ p, pp = path()
356
+ add(join_intent(v(), "large files", pp, "without crossing into other filesystems"),
357
+ cmd(p, "-xdev", "-type f", "-size", "+100M"))
358
+
359
+
360
+ def g_prune():
361
+ ex = rng.choice(EXCLUDES)
362
+ ext, phr = rng.choice(list(EXTS.items()))
363
+ p, pp = path()
364
+ add(join_intent(v(), rng.choice(phr), pp, rng.choice([f"excluding {ex}", f"but skip {ex}",
365
+ f"ignoring the {ex} directory"])),
366
+ cmd(p, "-name", ex, "-prune", "-o", "-type f", "-name", q("*." + ext), "-print"))
367
+
368
+
369
+ def g_prune_two():
370
+ a, b = rng.sample(EXCLUDES, 2)
371
+ p, pp = path()
372
+ add(join_intent(v(), "all files", pp, f"excluding {a} and {b}"),
373
+ cmd(p, "\\(", "-name", a, "-o", "-name", b, "\\)", "-prune", "-o", "-type f", "-print"))
374
+
375
+
376
+ def g_path():
377
+ p, pp = path()
378
+ d = rng.choice(["test", "tests", "config", "migrations", "logs"])
379
+ add(join_intent(v(), f"files under any {d} directory", pp), cmd(p, "-type f", "-path", q(f"*/{d}/*")))
380
+
381
+
382
+ def g_not_path():
383
+ p, pp = path()
384
+ d = rng.choice(EXCLUDES)
385
+ add(join_intent(v(), "files", pp, f"whose path doesn't include {d}"),
386
+ cmd(p, "-type f", "-not -path", q(f"*/{d}/*")))
387
+
388
+
389
+ def g_regex():
390
+ p, pp = path()
391
+ k = rng.choice([
392
+ ("files whose name is all digits", "-regextype posix-extended", "-regex", q(".*/[0-9]+")),
393
+ ("files named like IMG_1234.jpg", "-regextype posix-extended", "-regex", q(".*/IMG_[0-9]{4}\\.jpg")),
394
+ ("files ending in .log or .log.N", "-regextype posix-extended", "-regex", q(".*\\.log(\\.[0-9]+)?")),
395
+ ("jpg, jpeg or png images using a regex", "-regextype posix-extended", "-iregex", q(".*\\.(jpe?g|png)")),
396
+ ])
397
+ add(join_intent(v(), k[0], pp), cmd(p, "-type f", *k[1:]))
398
+
399
+
400
+ def g_links():
401
+ p, pp = path()
402
+ add(join_intent(v(), "files with more than one hard link", pp), cmd(p, "-type f", "-links", "+1"))
403
+
404
+
405
+ def g_samefile():
406
+ f = rng.choice(["data.bin", "/etc/hosts", "report.pdf"])
407
+ p, pp = path()
408
+ add(join_intent(v(), f"all hard links to {f}", pp), cmd(p, "-samefile", f))
409
+
410
+
411
+ def g_broken_links():
412
+ p, pp = path()
413
+ add(join_intent(v(), rng.choice(["broken symlinks", "dangling symbolic links", "dead links"]), pp),
414
+ cmd(p, "-xtype", "l"))
415
+
416
+
417
+ def g_follow():
418
+ p, pp = path()
419
+ ext, phr = rng.choice(list(EXTS.items()))
420
+ add(join_intent(v(), rng.choice(phr), pp, "following symlinks"),
421
+ "find -L " + p + " -type f -name " + q("*." + ext))
422
+
423
+
424
+ def g_multitype():
425
+ p, pp = path()
426
+ add(join_intent(v(), "files, directories and symlinks", pp), cmd(p, "-type", "f,d,l"))
427
+
428
+
429
+ # ---- actions
430
+ def g_delete():
431
+ p, pp = path()
432
+ choice = rng.random()
433
+ if choice < 0.35:
434
+ ext, phr = rng.choice(list(EXTS.items()))
435
+ add(join_intent(rng.choice(["delete", "remove", "clean up", "get rid of"]), "all", rng.choice(phr), pp),
436
+ cmd(p, "-type f", "-name", q("*." + ext), "-delete"))
437
+ elif choice < 0.65:
438
+ e, tp = rng.choice([m for m in MTIMES if "+" in m[0]])
439
+ ext, phr = rng.choice(list(EXTS.items()))
440
+ add(join_intent(rng.choice(["delete", "remove", "purge"]), rng.choice(phr), rng.choice(tp), pp),
441
+ cmd(p, "-type f", "-name", q("*." + ext), e, "-delete"))
442
+ elif choice < 0.85:
443
+ add(join_intent(rng.choice(["delete", "remove"]), "empty directories", pp), cmd(p, "-type d", "-empty", "-delete"))
444
+ else:
445
+ add(join_intent(rng.choice(["delete", "remove"]), "empty files", pp), cmd(p, "-type f", "-empty", "-delete"))
446
+
447
+
448
+ def g_rm_xargs():
449
+ n = rng.choice(["core", "*.tmp", "*.pyc", ".DS_Store", "Thumbs.db"])
450
+ p, pp = path()
451
+ add(join_intent("delete all", n, "files", pp, "safely handling spaces in names"),
452
+ cmd(p, "-name", q(n), "-type f", "-print0", pipe="xargs -0 rm -f"))
453
+
454
+
455
+ def g_exec_grep():
456
+ w = rng.choice(GREP_WORDS)
457
+ ext, phr = rng.choice(list(EXTS.items()))
458
+ p, pp = path()
459
+ add(join_intent(rng.choice(["find", "list", "which"]), rng.choice(phr), pp, f"contain {w}"),
460
+ cmd(p, "-type f", "-name", q("*." + ext), "-exec", "grep", "-l", q(w), "{} +"))
461
+
462
+
463
+ def g_exec_chmod():
464
+ p, pp = path()
465
+ k = rng.choice([("f", "644", "files"), ("d", "755", "directories"), ("f", "600", "files")])
466
+ add(join_intent(rng.choice(["set permissions", "chmod"]), f"{k[1]} on all {k[2]}", pp),
467
+ cmd(p, f"-type {k[0]}", "-exec", "chmod", k[1], "{} +"))
468
+
469
+
470
+ def g_exec_chown():
471
+ u = rng.choice(USERS)
472
+ p, pp = path()
473
+ add(join_intent(f"change owner of all files {pp} to {u}"), cmd(p, "-type f", "-exec", "chown", u, "{} +"))
474
+
475
+
476
+ def g_exec_ls():
477
+ s, sp = rng.choice(SIZES[:5])
478
+ p, pp = path()
479
+ add(join_intent(rng.choice(["show details of", "ls -l", "list with sizes"]), "files", rng.choice(sp), pp),
480
+ cmd(p, "-type f", "-size", s, "-exec", "ls", "-lh", "{} +"))
481
+
482
+
483
+ def g_ls_action():
484
+ ext, phr = rng.choice(list(EXTS.items()))
485
+ p, pp = path()
486
+ add(join_intent("long listing of", rng.choice(phr), pp), cmd(p, "-type f", "-name", q("*." + ext), "-ls"))
487
+
488
+
489
+ def g_exec_gzip():
490
+ e, tp = rng.choice([m for m in MTIMES if "+" in m[0]])
491
+ p = "/var/log"
492
+ add(join_intent("compress log files", rng.choice(tp), "in /var/log"),
493
+ cmd(p, "-type f", "-name", q("*.log"), e, "-exec", "gzip", "{} \\;"))
494
+
495
+
496
+ def g_exec_mv():
497
+ ext, phr = rng.choice(list(EXTS.items()))
498
+ dest = rng.choice(["/backup", "~/archive", "/mnt/old"])
499
+ p, pp = path()
500
+ add(join_intent(rng.choice(["move", "relocate"]), "all", rng.choice(phr), pp, "to", dest),
501
+ cmd(p, "-type f", "-name", q("*." + ext), "-exec", "mv", "-t", dest, "{} +"))
502
+
503
+
504
+ def g_exec_cp():
505
+ ext, phr = rng.choice(list(EXTS.items()))
506
+ dest = rng.choice(["/backup", "~/copies"])
507
+ p, pp = path()
508
+ add(join_intent("copy all", rng.choice(phr), pp, "to", dest),
509
+ cmd(p, "-type f", "-name", q("*." + ext), "-exec", "cp", "{}", dest, "\\;"))
510
+
511
+
512
+ def g_execdir():
513
+ p, pp = path()
514
+ add(join_intent("run file on every file", pp, "securely from each file's directory"),
515
+ cmd(p, "-type f", "-execdir", "file", "{} +"))
516
+
517
+
518
+ def g_exec_file():
519
+ p, pp = path()
520
+ add(join_intent("run the file command on every file", pp), cmd(p, "-type f", "-exec", "file", q("{}"), "\\;"))
521
+
522
+
523
+ def g_ok():
524
+ ext, phr = rng.choice(list(EXTS.items()))
525
+ p, pp = path()
526
+ add(join_intent("delete", rng.choice(phr), pp, rng.choice(["but ask before each one", "interactively",
527
+ "with confirmation"])),
528
+ cmd(p, "-type f", "-name", q("*." + ext), "-ok", "rm", "{} \\;"))
529
+
530
+
531
+ def g_count():
532
+ ext, phr = rng.choice(list(EXTS.items()))
533
+ p, pp = path()
534
+ add(join_intent(rng.choice(["count", "how many", "number of"]), rng.choice(phr), pp),
535
+ cmd(p, "-type f", "-name", q("*." + ext), pipe="wc -l"))
536
+
537
+
538
+ def g_count_dirs():
539
+ p, pp = path()
540
+ add(join_intent("how many directories are", pp), cmd(p, "-type d", pipe="wc -l"))
541
+
542
+
543
+ def g_printf_size():
544
+ p, pp = path()
545
+ add(join_intent(v(), "file sizes in bytes with paths", pp), cmd(p, "-type f", "-printf", q("%s %p\\n")))
546
+
547
+
548
+ def g_largest():
549
+ p, pp = path()
550
+ n = rng.choice([5, 10, 20])
551
+ add(join_intent(v(), f"the {n} largest files", pp),
552
+ cmd(p, "-type f", "-printf", q("%s %p\\n"), pipe=f"sort -rn | head -n {n}"))
553
+
554
+
555
+ def g_newest():
556
+ p, pp = path()
557
+ n = rng.choice([5, 10])
558
+ add(join_intent(v(), f"the {n} most recently modified files", pp),
559
+ cmd(p, "-type f", "-printf", q("%T@ %p\\n"), pipe=f"sort -rn | head -n {n}"))
560
+
561
+
562
+ def g_printf_names():
563
+ p, pp = path()
564
+ add(join_intent(v(), "just the file names without directories", pp), cmd(p, "-type f", "-printf", q("%f\\n")))
565
+
566
+
567
+ def g_printf_perm():
568
+ p, pp = path()
569
+ add(join_intent(v(), "files with their octal permissions and owner", pp),
570
+ cmd(p, "-type f", "-printf", q("%#m %u %p\\n")))
571
+
572
+
573
+ def g_printf_relative():
574
+ p, pp = path()
575
+ add(join_intent(v(), "files with paths relative to the start dir", pp), cmd(p, "-type f", "-printf", q("%P\\n")))
576
+
577
+
578
+ def g_quit():
579
+ n = rng.choice(NAMES)
580
+ p, pp = path()
581
+ add(join_intent(rng.choice(["find the first", "stop at the first match for", "find just one"]), n, pp),
582
+ cmd(p, "-name", q(n), "-print", "-quit"))
583
+
584
+
585
+ def g_fprint():
586
+ p, pp = path()
587
+ out = rng.choice(["/tmp/big.txt", "files.txt", "~/list.txt"])
588
+ add(join_intent("save a list of files over 100MB", pp, "to", out), cmd(p, "-type f", "-size", "+100M", "-fprint", out))
589
+
590
+
591
+ def g_print0():
592
+ ext, phr = rng.choice(list(EXTS.items()))
593
+ p, pp = path()
594
+ add(join_intent(v(), rng.choice(phr), pp, "null-separated for xargs"),
595
+ cmd(p, "-type f", "-name", q("*." + ext), "-print0"))
596
+
597
+
598
+ def g_du():
599
+ ext, phr = rng.choice(list(EXTS.items()))
600
+ p, pp = path()
601
+ add(join_intent("total size of all", rng.choice(phr), pp),
602
+ cmd(p, "-type f", "-name", q("*." + ext), "-print0", pipe="du -ch --files0-from=- | tail -n 1"))
603
+
604
+
605
+ def g_tar():
606
+ ext, phr = rng.choice(list(EXTS.items()))
607
+ p, pp = path()
608
+ add(join_intent("archive all", rng.choice(phr), pp, "into a tarball"),
609
+ cmd(p, "-type f", "-name", q("*." + ext), "-print0", pipe="tar --null -czf archive.tar.gz -T -"))
610
+
611
+
612
+ def g_suid_report():
613
+ add(join_intent(rng.choice(["audit", "list", "find"]), "all setuid binaries on the system"),
614
+ "find / -xdev -type f -perm -4000 -exec ls -l {} +")
615
+
616
+
617
+ def g_world_writable_dirs():
618
+ add(join_intent(v(), "world-writable directories without the sticky bit"),
619
+ "find / -xdev -type d -perm -0002 ! -perm -1000")
620
+
621
+
622
+ def g_vcs_roots():
623
+ p, pp = path()
624
+ add(join_intent(v(), "git repositories", pp), cmd(p, "-type d", "-name", ".git", "-prune", pipe="xargs -n1 dirname"))
625
+
626
+
627
+ def g_and_user_size():
628
+ u = rng.choice(USERS)
629
+ s, sp = rng.choice(SIZES)
630
+ p, pp = path()
631
+ add(join_intent(v(), f"files owned by {u}", rng.choice(sp), pp), cmd(p, "-type f", "-user", u, "-size", s))
632
+
633
+
634
+ def g_daystart():
635
+ p, pp = path()
636
+ add(join_intent(v(), "files modified since midnight", pp), cmd(p, "-daystart", "-type f", "-mtime 0"))
637
+
638
+
639
+ GENS = [
640
+ (g_name_ext, 300), (g_exact_name, 150), (g_iname, 100), (g_name_contains, 100), (g_name_starts, 60),
641
+ (g_type, 120), (g_multi_ext, 100), (g_not_name, 60), (g_size, 180), (g_size_range, 60), (g_empty, 100),
642
+ (g_time, 200), (g_ext_time, 200), (g_ext_size, 150), (g_newer, 80), (g_newermt, 80), (g_perm, 160),
643
+ (g_user, 100), (g_not_user, 50), (g_group, 70), (g_orphan, 40), (g_access, 70), (g_depth, 150),
644
+ (g_mindepth, 15), (g_subdirs_only, 30), (g_xdev, 30), (g_prune, 150), (g_prune_two, 60), (g_path, 60),
645
+ (g_not_path, 60), (g_regex, 50), (g_links, 20), (g_samefile, 25), (g_broken_links, 35), (g_follow, 50),
646
+ (g_multitype, 20), (g_delete, 200), (g_rm_xargs, 50), (g_exec_grep, 150), (g_exec_chmod, 50),
647
+ (g_exec_chown, 40), (g_exec_ls, 60), (g_ls_action, 50), (g_exec_gzip, 30), (g_exec_mv, 60),
648
+ (g_exec_cp, 50), (g_execdir, 15), (g_exec_file, 15), (g_ok, 50), (g_count, 100), (g_count_dirs, 25),
649
+ (g_printf_size, 25), (g_largest, 50), (g_newest, 40), (g_printf_names, 25), (g_printf_perm, 25),
650
+ (g_printf_relative, 20), (g_quit, 50), (g_fprint, 25), (g_print0, 50), (g_du, 50), (g_tar, 50),
651
+ (g_suid_report, 5), (g_world_writable_dirs, 5), (g_vcs_roots, 30), (g_and_user_size, 60),
652
+ (g_daystart, 20),
653
+ ]
654
+
655
+ # Hand-written examples taken from the man page EXAMPLES section
656
+ MANPAGE = [
657
+ ("delete files named core under /tmp", "find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f"),
658
+ ("run file on every file in or below the current directory", "find . -type f -exec file '{}' \\;"),
659
+ ("files modified in the last 24 hours in my home directory", "find $HOME -mtime 0"),
660
+ ("files in /sbin and /usr/sbin that are executable but not readable", "find /sbin /usr/sbin -executable \\! -readable -print"),
661
+ ("files with permissions exactly 664", "find . -perm 664"),
662
+ ("files writable by owner or group", "find . -perm /220"),
663
+ ("files writable by both owner and group", "find . -perm -220"),
664
+ ("files readable by everyone, writable by someone, executable by nobody", "find . -perm -444 -perm /222 \\! -perm /111"),
665
+ ("skip src/emacs and print everything else", "find . -path ./src/emacs -prune -o -print"),
666
+ ("find the first file named needle on the system and stop", "find / -name needle -print -quit"),
667
+ ("files, directories and symlinks in /tmp", "find /tmp -type f,d,l"),
668
+ ("in one pass, save setuid files to /root/suid.txt and files over 100M to /root/big.txt",
669
+ "find / \\( -perm -4000 -fprintf /root/suid.txt '%#m %u %p\\n' \\) , \\( -size +100M -fprintf /root/big.txt '%-10s %p\\n' \\)"),
670
+ ("find project roots in repo/ that contain .git, .svn or CVS",
671
+ "find repo/ \\( -exec test -d '{}/.svn' \\; -or -exec test -d '{}/.git' \\; -or -exec test -d '{}/CVS' \\; \\) -print -prune"),
672
+ ]
673
+
674
+
675
+ def generate(seed=SEED):
676
+ """Return unique (intent, command) pairs, deterministic for a given seed."""
677
+ rng.seed(seed)
678
+ pairs.clear()
679
+ for g, n in GENS:
680
+ for _ in range(n):
681
+ g()
682
+ for i, c in MANPAGE:
683
+ add(i, c)
684
+ seen, out = set(), []
685
+ for i, c in pairs:
686
+ i = i.strip()
687
+ if i and i.lower() not in seen:
688
+ seen.add(i.lower())
689
+ out.append((i, c))
690
+ return out