evolver-tools 1.4.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.
- evolver_tools/__init__.py +2 -0
- evolver_tools/__main__.py +3 -0
- evolver_tools/cli.py +89 -0
- evolver_tools/vendor/b64/__init__.py +2 -0
- evolver_tools/vendor/b64/b64.py +176 -0
- evolver_tools/vendor/cal_tool/__init__.py +1 -0
- evolver_tools/vendor/cal_tool/cli.py +234 -0
- evolver_tools/vendor/chart_cli/__init__.py +444 -0
- evolver_tools/vendor/chart_cli/__main__.py +3 -0
- evolver_tools/vendor/colors/__init__.py +5 -0
- evolver_tools/vendor/colors/__main__.py +97 -0
- evolver_tools/vendor/csv_stats/__init__.py +5 -0
- evolver_tools/vendor/csv_stats/__main__.py +4 -0
- evolver_tools/vendor/csv_stats/analyzer.py +258 -0
- evolver_tools/vendor/csv_stats/cli.py +45 -0
- evolver_tools/vendor/dirsize/__init__.py +183 -0
- evolver_tools/vendor/envcheck/__init__.py +426 -0
- evolver_tools/vendor/ff/__init__.py +427 -0
- evolver_tools/vendor/ff/__main__.py +3 -0
- evolver_tools/vendor/find_dups/__init__.py +7 -0
- evolver_tools/vendor/find_dups/cli.py +392 -0
- evolver_tools/vendor/hashsum/__init__.py +211 -0
- evolver_tools/vendor/hashsum/__main__.py +5 -0
- evolver_tools/vendor/http_live/__init__.py +265 -0
- evolver_tools/vendor/http_live/__main__.py +2 -0
- evolver_tools/vendor/ipinfo/__init__.py +3 -0
- evolver_tools/vendor/ipinfo/__main__.py +30 -0
- evolver_tools/vendor/jq_lite/__init__.py +257 -0
- evolver_tools/vendor/jq_lite/__main__.py +5 -0
- evolver_tools/vendor/json2csv/__init__.py +3 -0
- evolver_tools/vendor/json2csv/__main__.py +82 -0
- evolver_tools/vendor/jsonql/__init__.py +326 -0
- evolver_tools/vendor/jsonql/__main__.py +5 -0
- evolver_tools/vendor/license_cli/__init__.py +1 -0
- evolver_tools/vendor/license_cli/__main__.py +4 -0
- evolver_tools/vendor/license_cli/cli.py +289 -0
- evolver_tools/vendor/markdown_check/__init__.py +211 -0
- evolver_tools/vendor/nb/__init__.py +319 -0
- evolver_tools/vendor/nb/__main__.py +3 -0
- evolver_tools/vendor/passgen/__init__.py +224 -0
- evolver_tools/vendor/portcheck/__init__.py +2 -0
- evolver_tools/vendor/portcheck/__main__.py +66 -0
- evolver_tools/vendor/project_doctor/__init__.py +412 -0
- evolver_tools/vendor/project_doctor/__main__.py +3 -0
- evolver_tools/vendor/ren/__init__.py +283 -0
- evolver_tools/vendor/ren/__main__.py +3 -0
- evolver_tools/vendor/siege_lite/__init__.py +250 -0
- evolver_tools/vendor/siege_lite/__main__.py +3 -0
- evolver_tools/vendor/smellfinder/__init__.py +376 -0
- evolver_tools/vendor/smellfinder/__main__.py +3 -0
- evolver_tools/vendor/sqlite_cli/__init__.py +326 -0
- evolver_tools/vendor/sqlite_cli/__main__.py +5 -0
- evolver_tools/vendor/sysmon/__init__.py +299 -0
- evolver_tools/vendor/sysmon/__main__.py +3 -0
- evolver_tools/vendor/timer/__init__.py +127 -0
- evolver_tools/vendor/treedir/__init__.py +2 -0
- evolver_tools/vendor/treedir/__main__.py +128 -0
- evolver_tools/vendor/urlparse_tool/__init__.py +3 -0
- evolver_tools/vendor/urlparse_tool/cli.py +212 -0
- evolver_tools/vendor/web_summary/__init__.py +341 -0
- evolver_tools/vendor/web_summary/__main__.py +3 -0
- evolver_tools/vendor/wordcount/__init__.py +2 -0
- evolver_tools/vendor/wordcount/__main__.py +101 -0
- evolver_tools-1.4.0.dist-info/METADATA +107 -0
- evolver_tools-1.4.0.dist-info/RECORD +69 -0
- evolver_tools-1.4.0.dist-info/WHEEL +5 -0
- evolver_tools-1.4.0.dist-info/entry_points.txt +34 -0
- evolver_tools-1.4.0.dist-info/licenses/LICENSE +21 -0
- evolver_tools-1.4.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
"""
|
|
2
|
+
jsonql — Zero-dependency JSON query tool for CLI.
|
|
3
|
+
|
|
4
|
+
Supports:
|
|
5
|
+
- Read from file or stdin
|
|
6
|
+
- Path queries: $.key.subkey, $.arr[0], $.arr[*].field
|
|
7
|
+
- Filter: $[?(@.key > 5)]
|
|
8
|
+
- Output: pretty, compact, raw (plain text extraction)
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
import re
|
|
14
|
+
import sys
|
|
15
|
+
import argparse
|
|
16
|
+
from typing import Any, Callable, Optional
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# ---------- query parsing ----------
|
|
20
|
+
|
|
21
|
+
def _tokenize(path: str) -> list:
|
|
22
|
+
"""Tokenize a JSON path expression into tokens."""
|
|
23
|
+
tokens = []
|
|
24
|
+
i = 0
|
|
25
|
+
# strip leading $. or $
|
|
26
|
+
if path.startswith('$.'):
|
|
27
|
+
path = path[2:]
|
|
28
|
+
elif path.startswith('$'):
|
|
29
|
+
path = path[1:]
|
|
30
|
+
|
|
31
|
+
while i < len(path):
|
|
32
|
+
c = path[i]
|
|
33
|
+
if c == '.':
|
|
34
|
+
i += 1 # skip dot separator
|
|
35
|
+
elif c == '[':
|
|
36
|
+
# bracket expression: [0], [*], [?(@.key op value)]
|
|
37
|
+
end = path.index(']', i)
|
|
38
|
+
expr = path[i+1:end]
|
|
39
|
+
i = end + 1
|
|
40
|
+
if expr == '*':
|
|
41
|
+
tokens.append(('wildcard',))
|
|
42
|
+
elif expr.startswith('?(') and expr.endswith(')'):
|
|
43
|
+
tokens.append(('filter', expr[2:-1]))
|
|
44
|
+
else:
|
|
45
|
+
try:
|
|
46
|
+
tokens.append(('index', int(expr)))
|
|
47
|
+
except ValueError:
|
|
48
|
+
tokens.append(('key', expr.strip("'\"")))
|
|
49
|
+
elif c == '*' and (i + 1 >= len(path) or path[i+1] in ('.', '[') or i+1 == len(path)):
|
|
50
|
+
tokens.append(('wildcard',))
|
|
51
|
+
i += 1
|
|
52
|
+
else:
|
|
53
|
+
# dot-key
|
|
54
|
+
j = i
|
|
55
|
+
while j < len(path) and path[j] not in ('.', '['):
|
|
56
|
+
j += 1
|
|
57
|
+
key = path[i:j]
|
|
58
|
+
if key == '*':
|
|
59
|
+
tokens.append(('wildcard',))
|
|
60
|
+
else:
|
|
61
|
+
tokens.append(('key', key))
|
|
62
|
+
i = j
|
|
63
|
+
return tokens
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _compile_filter(expr: str) -> Callable[[Any], bool]:
|
|
67
|
+
"""Compile a filter expression like '@.age > 25' into a callable."""
|
|
68
|
+
expr = expr.strip()
|
|
69
|
+
# Match: @.path OPERATOR value
|
|
70
|
+
# Operators: ==, !=, >, <, >=, <=, =~ (regex)
|
|
71
|
+
m = re.match(
|
|
72
|
+
r'@(?:\.([a-zA-Z_][a-zA-Z0-9_]*))?\s*'
|
|
73
|
+
r'(==|!=|>=|<=|>|<|=~)\s*'
|
|
74
|
+
r'(.*)',
|
|
75
|
+
expr
|
|
76
|
+
)
|
|
77
|
+
if not m:
|
|
78
|
+
# Try partial: just @.field (truthy)
|
|
79
|
+
m2 = re.match(r'@(?:\.([a-zA-Z_][a-zA-Z0-9_]*))?', expr)
|
|
80
|
+
if m2:
|
|
81
|
+
field = m2.group(1)
|
|
82
|
+
def truthy(x):
|
|
83
|
+
v = x.get(field) if field else x
|
|
84
|
+
return bool(v) if v is not None else False
|
|
85
|
+
return truthy
|
|
86
|
+
return lambda x: True
|
|
87
|
+
|
|
88
|
+
field_path = m.group(1) # may be None
|
|
89
|
+
op = m.group(2)
|
|
90
|
+
raw_val = m.group(3).strip().strip("'\"")
|
|
91
|
+
|
|
92
|
+
# Try to convert value to int/float
|
|
93
|
+
try:
|
|
94
|
+
val = int(raw_val)
|
|
95
|
+
except ValueError:
|
|
96
|
+
try:
|
|
97
|
+
val = float(raw_val)
|
|
98
|
+
except ValueError:
|
|
99
|
+
val = raw_val # keep as string
|
|
100
|
+
|
|
101
|
+
def _get_val(item):
|
|
102
|
+
if field_path:
|
|
103
|
+
return item.get(field_path) if isinstance(item, dict) else None
|
|
104
|
+
return item
|
|
105
|
+
|
|
106
|
+
def _cmp(item):
|
|
107
|
+
v = _get_val(item)
|
|
108
|
+
if v is None:
|
|
109
|
+
return False
|
|
110
|
+
if op == '==': return v == val
|
|
111
|
+
if op == '!=': return v != val
|
|
112
|
+
if op == '>': return (v if isinstance(v, (int, float)) else 0) > (val if isinstance(val, (int, float)) else 0)
|
|
113
|
+
if op == '<': return (v if isinstance(v, (int, float)) else 0) < (val if isinstance(val, (int, float)) else 0)
|
|
114
|
+
if op == '>=': return (v if isinstance(v, (int, float)) else 0) >= (val if isinstance(val, (int, float)) else 0)
|
|
115
|
+
if op == '<=': return (v if isinstance(v, (int, float)) else 0) <= (val if isinstance(val, (int, float)) else 0)
|
|
116
|
+
if op == '=~': return bool(re.search(str(val), str(v)))
|
|
117
|
+
return False
|
|
118
|
+
|
|
119
|
+
return _cmp
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _resolve(data: Any, tokens: list) -> Any:
|
|
123
|
+
"""Resolve a list of tokens against data, returning matched result(s)."""
|
|
124
|
+
if not tokens:
|
|
125
|
+
return data
|
|
126
|
+
|
|
127
|
+
tok = tokens[0]
|
|
128
|
+
rest = tokens[1:]
|
|
129
|
+
|
|
130
|
+
if tok[0] == 'key':
|
|
131
|
+
key = tok[1]
|
|
132
|
+
if isinstance(data, dict):
|
|
133
|
+
val = data.get(key)
|
|
134
|
+
return _resolve(val, rest) if rest else val
|
|
135
|
+
return None
|
|
136
|
+
|
|
137
|
+
if tok[0] == 'index':
|
|
138
|
+
idx = tok[1]
|
|
139
|
+
if isinstance(data, (list, tuple)):
|
|
140
|
+
try:
|
|
141
|
+
val = data[idx]
|
|
142
|
+
return _resolve(val, rest) if rest else val
|
|
143
|
+
except IndexError:
|
|
144
|
+
return None
|
|
145
|
+
return None
|
|
146
|
+
|
|
147
|
+
if tok[0] == 'wildcard':
|
|
148
|
+
if isinstance(data, (list, tuple)):
|
|
149
|
+
results = []
|
|
150
|
+
for item in data:
|
|
151
|
+
if rest:
|
|
152
|
+
r = _resolve(item, rest)
|
|
153
|
+
if r is not None:
|
|
154
|
+
if isinstance(r, list):
|
|
155
|
+
results.extend(r)
|
|
156
|
+
else:
|
|
157
|
+
results.append(r)
|
|
158
|
+
else:
|
|
159
|
+
results.append(item)
|
|
160
|
+
return results
|
|
161
|
+
elif isinstance(data, dict):
|
|
162
|
+
results = []
|
|
163
|
+
for v in data.values():
|
|
164
|
+
if rest:
|
|
165
|
+
r = _resolve(v, rest)
|
|
166
|
+
if r is not None:
|
|
167
|
+
if isinstance(r, list):
|
|
168
|
+
results.extend(r)
|
|
169
|
+
else:
|
|
170
|
+
results.append(r)
|
|
171
|
+
else:
|
|
172
|
+
results.append(v)
|
|
173
|
+
return results
|
|
174
|
+
return data
|
|
175
|
+
|
|
176
|
+
if tok[0] == 'filter':
|
|
177
|
+
predicate = _compile_filter(tok[1])
|
|
178
|
+
if isinstance(data, list):
|
|
179
|
+
results = []
|
|
180
|
+
for item in data:
|
|
181
|
+
if predicate(item):
|
|
182
|
+
if rest:
|
|
183
|
+
r = _resolve(item, rest)
|
|
184
|
+
if r is not None:
|
|
185
|
+
if isinstance(r, list):
|
|
186
|
+
results.extend(r)
|
|
187
|
+
else:
|
|
188
|
+
results.append(r)
|
|
189
|
+
else:
|
|
190
|
+
results.append(item)
|
|
191
|
+
return results
|
|
192
|
+
return data
|
|
193
|
+
|
|
194
|
+
return None
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def query(data: Any, path: str) -> Any:
|
|
198
|
+
"""Run a JSON path query against data.
|
|
199
|
+
|
|
200
|
+
Args:
|
|
201
|
+
data: Parsed JSON (dict/list/str/int/float/bool/None)
|
|
202
|
+
path: JSON path expression like '$.users[?(@.age > 25)].name'
|
|
203
|
+
|
|
204
|
+
Returns:
|
|
205
|
+
Query result (list, dict, scalar, or None)
|
|
206
|
+
"""
|
|
207
|
+
if not path or path == '$' or path == '.':
|
|
208
|
+
return data
|
|
209
|
+
tokens = _tokenize(path)
|
|
210
|
+
return _resolve(data, tokens)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
# ---------- CLI arg parsing ----------
|
|
214
|
+
|
|
215
|
+
def parse_args(argv: list = None) -> argparse.Namespace:
|
|
216
|
+
p = argparse.ArgumentParser(
|
|
217
|
+
prog='jsonql',
|
|
218
|
+
description='Zero-dependency JSON query tool for CLI. '
|
|
219
|
+
'Query, filter, and extract from JSON data.',
|
|
220
|
+
epilog='Examples:\n'
|
|
221
|
+
' cat data.json | jsonql users\n'
|
|
222
|
+
' jsonql data.json "$.users[?(@.age > 25)].name"\n'
|
|
223
|
+
' curl api.example.com/users | jsonql -r "$[*].email"',
|
|
224
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
225
|
+
)
|
|
226
|
+
p.add_argument('expr', nargs='*', default=[],
|
|
227
|
+
help='File path and/or JSON path expression. '
|
|
228
|
+
'If the first argument looks like a file path '
|
|
229
|
+
'(has an extension or is a file on disk), it is '
|
|
230
|
+
'treated as the input file. All remaining args '
|
|
231
|
+
'are joined as the JSON path.')
|
|
232
|
+
p.add_argument('-r', '--raw', action='store_true',
|
|
233
|
+
help='Output raw strings only (no JSON formatting)')
|
|
234
|
+
p.add_argument('-c', '--compact', action='store_true',
|
|
235
|
+
help='Compact JSON output (no indentation)')
|
|
236
|
+
p.add_argument('-k', '--keys', action='store_true',
|
|
237
|
+
help='Output only keys for objects')
|
|
238
|
+
return p.parse_args(argv)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _resolve_args(expr_args: list) -> tuple:
|
|
242
|
+
"""Smart resolve: figure out which arg is file and which is path."""
|
|
243
|
+
file_path = None
|
|
244
|
+
path = '$' # default
|
|
245
|
+
|
|
246
|
+
if not expr_args:
|
|
247
|
+
return None, '$'
|
|
248
|
+
|
|
249
|
+
# Check if first arg is a file path
|
|
250
|
+
first = expr_args[0]
|
|
251
|
+
|
|
252
|
+
# Definitively a path expression, not a file
|
|
253
|
+
if first.startswith('$') or '[' in first or '?' in first:
|
|
254
|
+
return None, ' '.join(expr_args)
|
|
255
|
+
|
|
256
|
+
# Check if it's an actual file on disk
|
|
257
|
+
if os.path.isfile(first):
|
|
258
|
+
file_path = first
|
|
259
|
+
rest = expr_args[1:]
|
|
260
|
+
if rest:
|
|
261
|
+
path = ' '.join(rest)
|
|
262
|
+
return file_path, path
|
|
263
|
+
|
|
264
|
+
# Check extension heuristic: only treat as file if it looks like a filename
|
|
265
|
+
# (has extension AND no special query chars in basename)
|
|
266
|
+
base = os.path.basename(first)
|
|
267
|
+
if '.' in base and not any(c in first for c in '?[@'):
|
|
268
|
+
file_path = first
|
|
269
|
+
rest = expr_args[1:]
|
|
270
|
+
if rest:
|
|
271
|
+
path = ' '.join(rest)
|
|
272
|
+
else:
|
|
273
|
+
path = ' '.join(expr_args)
|
|
274
|
+
|
|
275
|
+
return file_path, path
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def main(argv: list = None) -> int:
|
|
279
|
+
args = parse_args(argv)
|
|
280
|
+
file_path, path = _resolve_args(args.expr)
|
|
281
|
+
|
|
282
|
+
# Read JSON
|
|
283
|
+
try:
|
|
284
|
+
if file_path:
|
|
285
|
+
with open(file_path, 'r') as f:
|
|
286
|
+
data = json.load(f)
|
|
287
|
+
else:
|
|
288
|
+
raw = sys.stdin.read()
|
|
289
|
+
if not raw.strip():
|
|
290
|
+
print("Error: no input (pipe JSON or provide file)", file=sys.stderr)
|
|
291
|
+
return 1
|
|
292
|
+
data = json.loads(raw)
|
|
293
|
+
except json.JSONDecodeError as e:
|
|
294
|
+
print(f"Error: invalid JSON — {e}", file=sys.stderr)
|
|
295
|
+
return 1
|
|
296
|
+
except FileNotFoundError:
|
|
297
|
+
print(f"Error: file not found — {file_path}", file=sys.stderr)
|
|
298
|
+
return 1
|
|
299
|
+
|
|
300
|
+
# Run query
|
|
301
|
+
result = query(data, path)
|
|
302
|
+
|
|
303
|
+
# Handle None
|
|
304
|
+
if result is None:
|
|
305
|
+
return 0
|
|
306
|
+
|
|
307
|
+
# Output
|
|
308
|
+
if args.keys and isinstance(result, dict):
|
|
309
|
+
for k in result:
|
|
310
|
+
print(k)
|
|
311
|
+
elif args.raw:
|
|
312
|
+
if isinstance(result, list):
|
|
313
|
+
for item in result:
|
|
314
|
+
print(item)
|
|
315
|
+
else:
|
|
316
|
+
print(result)
|
|
317
|
+
else:
|
|
318
|
+
indent = None if args.compact else 2
|
|
319
|
+
# Ensure ASCII safe output
|
|
320
|
+
print(json.dumps(result, indent=indent, ensure_ascii=False, default=str))
|
|
321
|
+
|
|
322
|
+
return 0
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
if __name__ == '__main__':
|
|
326
|
+
sys.exit(main())
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""license-cli: 开源许可证文本生成器"""
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""license-cli — 开源许可证文本生成器
|
|
3
|
+
|
|
4
|
+
Usage:
|
|
5
|
+
license-cli mit -a "John Doe"
|
|
6
|
+
license-cli gpl-3.0 -a "John Doe" -o LICENSE
|
|
7
|
+
license-cli list
|
|
8
|
+
license-cli apache-2.0 --year 2024
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import sys
|
|
13
|
+
import os
|
|
14
|
+
from datetime import datetime
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
# ── Embedded short licenses ──
|
|
18
|
+
|
|
19
|
+
LICENSES_DATA = {}
|
|
20
|
+
|
|
21
|
+
LICENSES_DATA["mit"] = """MIT License
|
|
22
|
+
|
|
23
|
+
Copyright (c) {year} {author}
|
|
24
|
+
|
|
25
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
26
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
27
|
+
in the Software without restriction, including without limitation the rights
|
|
28
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
29
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
30
|
+
furnished to do so, subject to the following conditions:
|
|
31
|
+
|
|
32
|
+
The above copyright notice and this permission notice shall be included in all
|
|
33
|
+
copies or substantial portions of the Software.
|
|
34
|
+
|
|
35
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
36
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
37
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
38
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
39
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
40
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
41
|
+
SOFTWARE.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
LICENSES_DATA["bsd-2-clause"] = """BSD 2-Clause License
|
|
45
|
+
|
|
46
|
+
Copyright (c) {year}, {author}
|
|
47
|
+
|
|
48
|
+
Redistribution and use in source and binary forms, with or without
|
|
49
|
+
modification, are permitted provided that the following conditions are met:
|
|
50
|
+
|
|
51
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
52
|
+
list of conditions and the following disclaimer.
|
|
53
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
54
|
+
this list of conditions and the following disclaimer in the documentation
|
|
55
|
+
and/or other materials provided with the distribution.
|
|
56
|
+
|
|
57
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
58
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
59
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
60
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
61
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
62
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
63
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
64
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
65
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
66
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
LICENSES_DATA["bsd-3-clause"] = """BSD 3-Clause License
|
|
70
|
+
|
|
71
|
+
Copyright (c) {year}, {author}
|
|
72
|
+
|
|
73
|
+
Redistribution and use in source and binary forms, with or without
|
|
74
|
+
modification, are permitted provided that the following conditions are met:
|
|
75
|
+
|
|
76
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
77
|
+
list of conditions and the following disclaimer.
|
|
78
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
79
|
+
this list of conditions and the following disclaimer in the documentation
|
|
80
|
+
and/or other materials provided with the distribution.
|
|
81
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
82
|
+
contributors may be used to endorse or promote products derived from
|
|
83
|
+
this software without specific prior written permission.
|
|
84
|
+
|
|
85
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
86
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
87
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
88
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
89
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
90
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
91
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
92
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
93
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
94
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
95
|
+
"""
|
|
96
|
+
|
|
97
|
+
LICENSES_DATA["isc"] = """ISC License
|
|
98
|
+
|
|
99
|
+
Copyright (c) {year}, {author}
|
|
100
|
+
|
|
101
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
102
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
103
|
+
copyright notice and this permission notice appear in all copies.
|
|
104
|
+
|
|
105
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
106
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
107
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
108
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
109
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
110
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
111
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
LICENSES_DATA["unlicense"] = """This is free and unencumbered software released into the public domain.
|
|
115
|
+
|
|
116
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
117
|
+
distribute this software, either in source code form or as a compiled
|
|
118
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
119
|
+
means.
|
|
120
|
+
|
|
121
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
122
|
+
of this software dedicate any and all copyright interest in the
|
|
123
|
+
software to the public domain. We make this dedication for the benefit
|
|
124
|
+
of the public at large and to the detriment of our heirs and
|
|
125
|
+
successors. We intend this dedication to be an overt act of
|
|
126
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
127
|
+
software under copyright law.
|
|
128
|
+
|
|
129
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
130
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
131
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
132
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
133
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
134
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
135
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
136
|
+
|
|
137
|
+
For more information, please refer to <https://unlicense.org/>
|
|
138
|
+
"""
|
|
139
|
+
|
|
140
|
+
LICENSES_DATA["mpl-2.0"] = """Mozilla Public License Version 2.0
|
|
141
|
+
==================================
|
|
142
|
+
|
|
143
|
+
1. Definitions
|
|
144
|
+
--------------
|
|
145
|
+
|
|
146
|
+
1.1. "Contributor"
|
|
147
|
+
means each individual or legal entity that creates, contributes to
|
|
148
|
+
the creation of, or owns Covered Software.
|
|
149
|
+
|
|
150
|
+
1.2. "Contributor Version"
|
|
151
|
+
means the combination of the Contributions of others (if any) used
|
|
152
|
+
by a Contributor and that particular Contributor's Contribution.
|
|
153
|
+
|
|
154
|
+
1.3. "Contribution"
|
|
155
|
+
means Covered Software of a particular Contributor.
|
|
156
|
+
|
|
157
|
+
1.4. "Covered Software"
|
|
158
|
+
means Source Code Form to which the initial Contributor has attached
|
|
159
|
+
the notice in Exhibit A, the Executable Form of such Source Code
|
|
160
|
+
Form, and Modifications of such Source Code Form, in each case
|
|
161
|
+
including portions thereof.
|
|
162
|
+
|
|
163
|
+
[Full text available at https://www.mozilla.org/MPL/2.0/]
|
|
164
|
+
|
|
165
|
+
Exhibit A - Source Code Form License Notice
|
|
166
|
+
-------------------------------------------
|
|
167
|
+
|
|
168
|
+
This Source Code Form is subject to the terms of the Mozilla Public
|
|
169
|
+
License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
170
|
+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
171
|
+
|
|
172
|
+
Exhibit B - "Incompatible With Secondary Licenses" Notice
|
|
173
|
+
-----------------------------------------------------------
|
|
174
|
+
|
|
175
|
+
This Source Code Form is "Incompatible With Secondary Licenses", as
|
|
176
|
+
defined by the Mozilla Public License, v. 2.0.
|
|
177
|
+
"""
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
LICENSES_META = {
|
|
181
|
+
"mit": {"name": "MIT License", "spdx": "MIT"},
|
|
182
|
+
"apache-2.0": {"name": "Apache License 2.0", "spdx": "Apache-2.0"},
|
|
183
|
+
"gpl-3.0": {"name": "GNU General Public License v3.0", "spdx": "GPL-3.0-only"},
|
|
184
|
+
"bsd-2-clause": {"name": "BSD 2-Clause \"Simplified\"", "spdx": "BSD-2-Clause"},
|
|
185
|
+
"bsd-3-clause": {"name": "BSD 3-Clause \"New\"", "spdx": "BSD-3-Clause"},
|
|
186
|
+
"mpl-2.0": {"name": "Mozilla Public License 2.0", "spdx": "MPL-2.0"},
|
|
187
|
+
"unlicense": {"name": "The Unlicense", "spdx": "Unlicense"},
|
|
188
|
+
"isc": {"name": "ISC License", "spdx": "ISC"},
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
ALIASES = {
|
|
192
|
+
"apache": "apache-2.0", "gpl": "gpl-3.0",
|
|
193
|
+
"gplv3": "gpl-3.0", "gpl3": "gpl-3.0",
|
|
194
|
+
"bsd2": "bsd-2-clause", "bsd3": "bsd-3-clause",
|
|
195
|
+
"bsd": "bsd-3-clause", "mpl": "mpl-2.0",
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _load_license_text(key):
|
|
200
|
+
"""Try data/ files first (full texts), then embedded."""
|
|
201
|
+
data_dir = Path(__file__).parent / "data"
|
|
202
|
+
txt_file = data_dir / f"{key}.txt"
|
|
203
|
+
if txt_file.exists():
|
|
204
|
+
return txt_file.read_text(encoding="utf-8")
|
|
205
|
+
if key in LICENSES_DATA:
|
|
206
|
+
return LICENSES_DATA[key]
|
|
207
|
+
return None
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def get_supported():
|
|
211
|
+
items = sorted(LICENSES_META.items(), key=lambda x: x[1]["name"])
|
|
212
|
+
lines = []
|
|
213
|
+
for key, info in items:
|
|
214
|
+
aliases = [a for a, v in ALIASES.items() if v == key]
|
|
215
|
+
has_text = "✓" if _load_license_text(key) else "✗"
|
|
216
|
+
alias_str = f" (also: {', '.join(aliases)})" if aliases else ""
|
|
217
|
+
lines.append(f" {key:20s} {info['name']:40s} {has_text}{alias_str}")
|
|
218
|
+
return "\n".join(lines)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def generate(license_id, author, year):
|
|
222
|
+
key = ALIASES.get(license_id, license_id)
|
|
223
|
+
if key not in LICENSES_META:
|
|
224
|
+
sys.stderr.write(f"error: unknown license '{license_id}'\n")
|
|
225
|
+
sys.stderr.write(f"Run 'license-cli list' to see available licenses.\n")
|
|
226
|
+
sys.exit(1)
|
|
227
|
+
|
|
228
|
+
text = _load_license_text(key)
|
|
229
|
+
if text is None:
|
|
230
|
+
sys.stderr.write(f"error: full text for '{key}' is not embedded.\n")
|
|
231
|
+
sys.stderr.write(f"Try: license-cli {key} --download\n")
|
|
232
|
+
sys.exit(1)
|
|
233
|
+
|
|
234
|
+
return text.format(year=year, author=author)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def main():
|
|
238
|
+
parser = argparse.ArgumentParser(
|
|
239
|
+
prog="license-cli",
|
|
240
|
+
description="开源许可证文本生成器 — 一键生成 MIT/GPL/Apache/BSD/MPL/Unlicense/ISC",
|
|
241
|
+
epilog="Example: license-cli mit -a 'John Doe' -o LICENSE",
|
|
242
|
+
)
|
|
243
|
+
parser.add_argument("license", nargs="?", default=None,
|
|
244
|
+
help="许可证类型 (e.g. mit, apache, gpl, bsd, mpl, unlicense, isc)")
|
|
245
|
+
parser.add_argument("-a", "--author", default=None,
|
|
246
|
+
help="版权人/作者名 (默认: git config user.name)")
|
|
247
|
+
parser.add_argument("-y", "--year", default=None,
|
|
248
|
+
help="年份 (默认: 当前年份)")
|
|
249
|
+
parser.add_argument("-o", "--output", default=None,
|
|
250
|
+
help="输出到文件 (默认输出到 stdout)")
|
|
251
|
+
|
|
252
|
+
args = parser.parse_args()
|
|
253
|
+
|
|
254
|
+
if not args.license or args.license == "list":
|
|
255
|
+
print("Available licenses:")
|
|
256
|
+
print(get_supported())
|
|
257
|
+
print()
|
|
258
|
+
print("Usage: license-cli <license> [-a AUTHOR] [-y YEAR] [-o FILE]")
|
|
259
|
+
return
|
|
260
|
+
|
|
261
|
+
year_val = args.year or str(datetime.now().year)
|
|
262
|
+
|
|
263
|
+
author_val = args.author
|
|
264
|
+
if not author_val:
|
|
265
|
+
try:
|
|
266
|
+
import subprocess
|
|
267
|
+
result = subprocess.run(
|
|
268
|
+
["git", "config", "user.name"],
|
|
269
|
+
capture_output=True, text=True, timeout=2
|
|
270
|
+
)
|
|
271
|
+
if result.returncode == 0 and result.stdout.strip():
|
|
272
|
+
author_val = result.stdout.strip()
|
|
273
|
+
except Exception:
|
|
274
|
+
pass
|
|
275
|
+
if not author_val:
|
|
276
|
+
author_val = "Author"
|
|
277
|
+
|
|
278
|
+
text = generate(args.license, author_val, year_val)
|
|
279
|
+
|
|
280
|
+
if args.output:
|
|
281
|
+
with open(args.output, "w", encoding="utf-8") as f:
|
|
282
|
+
f.write(text)
|
|
283
|
+
print(f"✅ License written to {args.output}")
|
|
284
|
+
else:
|
|
285
|
+
print(text)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
if __name__ == "__main__":
|
|
289
|
+
main()
|