forked from aboutcode-org/scancode-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerated.py
More file actions
234 lines (180 loc) · 7.09 KB
/
generated.py
File metadata and controls
234 lines (180 loc) · 7.09 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
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
from itertools import islice
from commoncode.datautils import Boolean
from commoncode.text import toascii
from plugincode.scan import ScanPlugin
from plugincode.scan import scan_impl
from commoncode.cliutils import PluggableCommandLineOption
from commoncode.cliutils import OTHER_SCAN_GROUP
import typecode.contenttype
"""
Tag files as generated code based on conspicuous strings.
"""
# Tracing flag
TRACE = False
def logger_debug(*args):
pass
if TRACE:
import logging
import sys
logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.DEBUG)
def logger_debug(*args):
return logger.debug(' '.join(isinstance(a, str) and a or repr(a) for a in args))
@scan_impl
class GeneratedCodeDetector(ScanPlugin):
"""
Tag a file as generated.
"""
resource_attributes = dict(is_generated=Boolean(
help='True if this file is likely an automatically generated file.'))
run_order = 50
sort_order = 50
options = [
PluggableCommandLineOption(('--generated',),
is_flag=True, default=False,
help='Classify automatically generated code files with a flag.',
help_group=OTHER_SCAN_GROUP,
sort_order=50,
)
]
def is_enabled(self, generated, **kwargs):
return generated
def get_scanner(self, **kwargs):
return generated_scanner
def generated_scanner(location, **kwargs):
is_generated = False
for _clue in get_generated_code_hint(location):
# TODO: consider returning the "clue"
is_generated = True
break
return dict(is_generated=is_generated)
GENERATED_KEYWORDS_LOWERED = tuple(g.lower() for g in (
'generated by',
'auto-generated',
'automatically generated',
# Apache Axis
'auto-generated from wsdl',
# jni javahl and others
'do not edit this file',
# jni javahl
'it is machine generated',
'by hibernate tools',
'generated from idl',
# castor generated files
'following schema fragment specifies the',
# Tomcat JSPC
'automatically created by',
# in GNU Classpath
'this file was automatically generated by gnu.localegen from cldr',
'this document is automatically generated by gnu.supplementgen',
# linux kernel/u-boot
'this was automagically generated from',
# angular
'this code is generated',
'this code is generated - do not modify',
# cython
'generated by cython',
# sqlite amalgamation
'this file is an amalgamation of many separate c source files from sqlite',
# various generated or last generated:
'generated on',
'last generated on',
# in freepascal unicode
"this is an automatically created file",
# generated by Postgres ECPG sql to c preprocessor
'processed by ecpg (regression mode)',
'these include files are added by the preprocessor',
'this readme is generated, please do not update',
'this file was automatically generated by',
'any changes will be lost if this file is regenerated',
# yarn lock files
'this is an autogenerated file. do not edit this file directly'
'this file is generated by',
'microsoft visual c++ generated include file',
# OpenJDK:
'generated by the idl-to-java compiler',
'this file was mechanically generated: do not edit!',
'generated by mc.java version 1.0, do not edit by hand!',
'generated from input file',
'generated content - do not edit',
'generators: org.graalvm',
# https://github.com/Microsoft/azure-devops-python-api/blob/0d9537016bd45cf7f2140433c0ec54b44768f726/vsts/vsts/licensing/v4_1/licensing_client.py
'generated file, do not edit',
'changes may cause incorrect behavior and will be lost if the code is regenerated.',
'this file is generated. best not to modify it, as it will likely be overwritten.',
# in Ogg/Vorbis
'function: static codebooks autogenerated by',
# yarn lock
'this is an autogenerated file',
'this is an autogenerated file. do not edit this file directly',
'this file has been automatically created by',
'please do not edit this file, all changes will be lost',
# thrift
'this is an automatically-generated file',
'it could get re-generated if the allow_idl_generation flag is on',
# aws sdk ruby
'warning about generated code',
'this file is generated',
'this file is generated. see the contributing guide for more information',
# seen in Go files:
'code generated file. do not edit',
'code generated by running "go generate". do not edit'
# protoc
'generated by the protocol buffer compiler',
'generated by the protocol buffer compiler. do not edit!',
'generated by the protocol buffer compiler. do not edit!',
'code generated by protoc-gen-go. do not edit.',
'code generated by generate-types. do not edit.',
'code generated by generate-protos. do not edit.',
'do not edit -- your changes will be discarded when the file is', # next line: * regenerated.
'do not edit -- generated code',
'generated code -- do not edit!',
'autogenerated by method_dump.sh. do not edit by hand.',
'this file is generated from the .proto files for the well-known', # next line: types. Do not edit!
'this file was generated by upbc (the upb compiler) from the input',
'do not edit -- your changes will be discarded when the file is', # next line: regenerated
# autotools
'makefile.in generated by automake',
'generated automatically by aclocal',
'generated by gnu autoconf',
'this file was automatically generated',
# misc
'this file has been generated by the gui designer. do not modify',
'file is generated by gopy gen. do not edit',
# javacc
'generated by:javacc: do not edit this line',
'generated by:jjtree: do not edit this line',
'generated code (do not edit this line)',
# in some Go projects like buildkit
'code generated by smithy-go-codegen do not edit',
))
def get_generated_code_hint(
location,
max_lines=150,
generated_keywords=GENERATED_KEYWORDS_LOWERED
):
"""
Return a line of extracted text from a file if that file is likely
generated source code.
for each of the first few lines of a source code file
if generated keywords are found in the line as lowercase
yield the line text as a 'potentially_ generated' annotation
"""
T = typecode.contenttype.get_type(location)
if not T.is_text:
return
with open(location, 'rb') as filein:
for line in islice(filein, max_lines):
text = toascii(line.strip()).lower()
if any(kw in text.lower() for kw in generated_keywords):
# yield only the first 100 chars..
yield text[:100]