-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles
More file actions
executable file
·315 lines (254 loc) · 9.88 KB
/
Copy pathfiles
File metadata and controls
executable file
·315 lines (254 loc) · 9.88 KB
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
#!/usr/bin/env python3
"""
files - file operation helpers.
Subcommands:
rename INPUT OUTPUT [-d] [-v] [-o FILE]
batch-rename files in the current directory
using a regex INPUT pattern and ${N} output
template; writes a rename.sh script by default
format-cpp PATH reformat C/C++ source files using the bundled
Uncrustify config
Run `files <subcommand> -h` for per-command help.
"""
import argparse
import codecs
import glob
import json
import logging
import os
import platform
import re
import shutil
import subprocess
import sys
from lib.bc_config import load as load_bcconfig
LOG = logging.getLogger(__name__)
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING)
# ---------------------------------------------------------------------------
# Config schema
# ---------------------------------------------------------------------------
BCCONFIG_SCHEMA = {
"files": [
("uncrustify_config", "",
"Path to Uncrustify config file; empty = use bundled source_code/allman.uncrust.cfg"),
],
}
def _schema_for_metadata():
sections = {}
for section, entries in BCCONFIG_SCHEMA.items():
sections[section] = [
{"key": key, "default": default, "description": desc}
for key, default, desc in entries
]
return sections
def bc_metadata():
return {
"schema_version": 1,
"name": "files",
"summary": "File operation helpers",
"command_style": "subcommands",
"config_sections": _schema_for_metadata(),
"subcommands": [
{
"name": "rename",
"summary": "Batch-rename files using regex capture groups",
"safety": "write",
"args": [
{"name": "input", "label": "Input regex (capture groups)",
"kind": "string", "required": True},
{"name": "output", "label": "Output template (${N} substitution)",
"kind": "string", "required": True},
],
"options": [
{"name": "dry", "flag": "--dry", "short_flag": "-d", "kind": "boolean",
"label": "Dry run: show matches without writing script"},
{"name": "verbose", "flag": "--verbose", "short_flag": "-v",
"kind": "boolean", "label": "Verbose output"},
{"name": "outfile", "flag": "--outfile", "short_flag": "-o",
"kind": "path", "label": "Output script filename (default: rename.sh)"},
],
"artifacts": [
{"kind": "shell", "path_option": "outfile"},
],
},
{
"name": "format-cpp",
"summary": "Reformat C/C++ files with the bundled Uncrustify config",
"safety": "write",
"args": [
{"name": "path", "label": "Source file or directory", "kind": "path",
"required": True, "exists": True},
],
"options": [
{"name": "config", "flag": "--config", "kind": "path",
"label": "Uncrustify config file",
"config": "files.uncrustify_config"},
],
"artifacts": [],
},
],
}
# ---------------------------------------------------------------------------
# rename (mrename logic)
# ---------------------------------------------------------------------------
_IS_POSIX = platform.system() in ("Darwin", "Linux")
class _SubstString:
_re_subst = re.compile(r"\$\{(\d+)\}")
def prep_input(self, re_str_in):
try:
self._re_in = re.compile(re_str_in, re.DOTALL)
except re.error as exc:
LOG.error(str(exc))
sys.exit(1)
def match_input(self, s):
self._match = self._re_in.match(s)
return self._match is not None
def calc_output(self, str_out):
groups = self._match.groups()
def repl(m):
return groups[int(m.group(1)) - 1]
return self._re_subst.sub(repl, str_out)
class _OutFile:
def __init__(self):
self.cmds = []
if _IS_POSIX:
self.cmds += ["#!/usr/bin/env bash", ""]
def rename(self, fn_from, fn_to):
if _IS_POSIX:
self.cmds.append('mv "{}" "{}"'.format(fn_from, fn_to))
def write(self, outfile):
try:
with open(outfile, "w", encoding="utf-8") as fh:
fh.write("\n".join(self.cmds))
except IOError as exc:
LOG.error(str(exc))
def _unescaped(s):
return codecs.decode(str(s), "unicode_escape")
def cmd_rename(args):
if args.verbose:
LOG.setLevel(logging.DEBUG)
fn_in = _unescaped(args.input) if not args.dry else args.input
fn_out = args.output
outfile = args.outfile or "rename.sh"
files = sorted(os.listdir("."))
LOG.info("%d files found in .", len(files))
ss = _SubstString()
ss.prep_input(fn_in)
cmds = _OutFile()
matched = 0
for fn in files:
LOG.debug("Filename: %s", fn)
if ss.match_input(fn):
fn_subst = ss.calc_output(fn_out)
LOG.info("%s --> %s", fn, fn_subst)
if not args.dry:
cmds.rename(fn, fn_subst)
matched += 1
if not args.dry:
cmds.write(outfile)
LOG.info("Wrote commands to %s", outfile)
else:
print(f"{matched} files matched.")
return 0
# ---------------------------------------------------------------------------
# format-cpp (uncrust logic)
# ---------------------------------------------------------------------------
_CPP_EXTENSIONS = ("h", "hpp", "inl", "cpp")
def _default_uncrust_cfg():
bc_dir = os.environ.get("BC_INSTALL_DIR") or os.path.dirname(os.path.abspath(__file__))
return os.path.join(bc_dir, "source_code", "allman.uncrust.cfg")
def _resolve_uncrust_cfg(cli_cfg):
if cli_cfg:
return cli_cfg
bcfg = load_bcconfig()
if bcfg.has_section("files"):
val = bcfg.get("files", "uncrustify_config", fallback="").strip()
if val:
return os.path.expanduser(val)
return _default_uncrust_cfg()
def _uncrust_file(uncrustify, cfg, path):
print(f"Formatting {path}")
tmp = path + ".uncrust.tmp"
result = subprocess.run(
[uncrustify, "-f", path, "-l", "cpp", "-c", cfg, "-o", tmp],
capture_output=True, text=True,
)
if result.returncode != 0:
print(result.stderr.strip(), file=sys.stderr)
if os.path.exists(tmp):
os.remove(tmp)
return False
os.replace(tmp, path)
return True
def _collect_sources(directory):
paths = []
for ext in _CPP_EXTENSIONS:
for p in glob.glob(os.path.join(directory, "**", f"*.{ext}"), recursive=True):
paths.append(p)
return sorted(paths)
def cmd_format_cpp(args):
uncrustify = shutil.which("uncrustify")
if not uncrustify:
print("error: uncrustify not found on PATH. Install via: brew install uncrustify",
file=sys.stderr)
return 2
cfg = _resolve_uncrust_cfg(args.config)
if not os.path.isfile(cfg):
print(f"error: Uncrustify config not found: {cfg}", file=sys.stderr)
return 2
target = args.path
if os.path.isdir(target):
sources = _collect_sources(target)
if not sources:
print(f"No C/C++ source files found under {target}")
return 0
errors = 0
for path in sources:
if not _uncrust_file(uncrustify, cfg, path):
errors += 1
print(f"Formatted {len(sources) - errors}/{len(sources)} files.")
return 1 if errors else 0
elif os.path.isfile(target):
return 0 if _uncrust_file(uncrustify, cfg, target) else 1
else:
print(f"error: path does not exist: {target}", file=sys.stderr)
return 2
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
def _build_parser():
parser = argparse.ArgumentParser(
prog="files",
description="File operation helpers.",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
subs = parser.add_subparsers(dest="subcommand", metavar="SUBCOMMAND")
p = subs.add_parser("rename", help="batch-rename files using a regex pattern")
p.add_argument("input", help="input regex with capture groups")
p.add_argument("output", help="output template using ${N} capture group substitution")
p.add_argument("-d", "--dry", action="store_true",
help="dry run: show matches without writing rename script")
p.add_argument("-v", "--verbose", action="store_true", help="verbose output")
p.add_argument("-o", "--outfile", default="rename.sh",
help="name of the shell script to write (default: rename.sh)")
p.set_defaults(handler=cmd_rename)
p = subs.add_parser("format-cpp",
help="reformat C/C++ files with the bundled Uncrustify config")
p.add_argument("path", help="source file or directory to reformat")
p.add_argument("--config", help="Uncrustify config file (overrides .bcconfig and bundled default)")
p.set_defaults(handler=cmd_format_cpp)
return parser
def main(argv=None):
argv = list(sys.argv[1:] if argv is None else argv)
if argv == ["--bc-metadata"]:
print(json.dumps(bc_metadata(), indent=2, sort_keys=True))
return 0
parser = _build_parser()
args = parser.parse_args(argv)
handler = getattr(args, "handler", None)
if handler is None:
parser.print_help()
return 1
return handler(args) or 0
if __name__ == "__main__":
sys.exit(main())