Skip to content

Commit f9601df

Browse files
authored
Merge pull request #2538 from nexB/new-generated-code-clues
Add new generated code clues
2 parents 249eb2c + adc8a66 commit f9601df

2 files changed

Lines changed: 40 additions & 28 deletions

File tree

src/summarycode/generated.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from itertools import islice
1111

12-
1312
from commoncode.datautils import Boolean
1413
from commoncode.text import toascii
1514
from plugincode.scan import ScanPlugin
@@ -19,7 +18,7 @@
1918
import typecode.contenttype
2019

2120
"""
22-
Tag files as generated.
21+
Tag files as generated code based on conspicuous strings.
2322
"""
2423

2524
# Tracing flag
@@ -77,7 +76,7 @@ def generated_scanner(location, **kwargs):
7776
return dict(is_generated=is_generated)
7877

7978

80-
GENERATED_KEYWORDS = tuple(g.lower() for g in (
79+
GENERATED_KEYWORDS_LOWERED = tuple(g.lower() for g in (
8180
'generated by',
8281
'auto-generated',
8382
'automatically generated',
@@ -157,25 +156,39 @@ def generated_scanner(location, **kwargs):
157156
'function: static codebooks autogenerated by',
158157

159158
# yarn lock
160-
'THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.',
161-
'This file has been automatically created by',
162-
'Please do not edit this file, all changes will be lost',
159+
'this is an autogenerated file. do not edit this file directly.',
160+
'this file has been automatically created by',
161+
'please do not edit this file, all changes will be lost',
163162

164-
# protoc
165-
'Generated by the protocol buffer compiler. DO NOT EDIT!',
163+
# thrift
164+
'this is an automatically-generated file.',
165+
'it could get re-generated if the allow_idl_generation flag is on',
166166

167-
))
167+
# aws sdk ruby
168+
'warning about generated code',
169+
'this file is generated. see the contributing guide for more information',
170+
'this file is generated',
168171

172+
# seen in Go files:
173+
'code generated file. do not edit',
174+
'code generated by running "go generate". do not edit.'
169175

170-
max_lines = 150
176+
# protoc
177+
'generated by the protocol buffer compiler. do not edit!',
178+
'generated by the protocol buffer compiler.',
179+
))
171180

172181

173-
def get_generated_code_hint(location, generated_keywords=GENERATED_KEYWORDS):
182+
def get_generated_code_hint(
183+
location,
184+
max_lines=150,
185+
generated_keywords=GENERATED_KEYWORDS_LOWERED
186+
):
174187
"""
175188
Return a line of extracted text from a file if that file is likely
176189
generated source code.
177190
178-
for each of the the first few lines of a source code file
191+
for each of the first few lines of a source code file
179192
if generated keywords are found in the line as lowercase
180193
yield the line text as a 'potentially_ generated' annotation
181194
"""
@@ -184,9 +197,8 @@ def get_generated_code_hint(location, generated_keywords=GENERATED_KEYWORDS):
184197
return
185198
with open(location, 'rb') as filein:
186199
for line in islice(filein, max_lines):
187-
text = toascii(line.strip())
188-
textl = text.lower()
189-
if any(kw in textl for kw in generated_keywords):
200+
text = toascii(line.strip()).lower()
201+
if any(kw in text.lower() for kw in generated_keywords):
190202
# yield only the first 100 chars..
191203
yield text[:100]
192204

tests/summarycode/test_generated.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,62 @@ class TestGeneratedCode(FileBasedTesting):
2020

2121
def test_basic(self):
2222
expected = [
23-
'// This file was generated by the JavaTM Architecture '
24-
'for XML Binding(JAXB) Reference Implementation',
25-
'// Generated on: 2011.08.01 at 11:35:59 AM CEST'
23+
'// This file was generated by the JavaTM Architecture '.lower() +
24+
'for XML Binding(JAXB) Reference Implementation'.lower(),
25+
'// Generated on: 2011.08.01 at 11:35:59 AM CEST'.lower()
2626
]
2727
test_file = self.get_test_loc('generated/simple/generated_1.java')
2828
result = list(generated.get_generated_code_hint(location=test_file))
2929
assert result == expected
3030

3131
def test_basic_alt(self):
3232
expected = [
33-
'// This file was generated by the JavaTM Architecture '
34-
'for XML Binding(JAXB) Reference Implementation',
35-
'// Generated on: 2013.11.15 at 04:17:00 PM CET'
33+
'// This file was generated by the JavaTM Architecture '.lower() +
34+
'for XML Binding(JAXB) Reference Implementation'.lower(),
35+
'// Generated on: 2013.11.15 at 04:17:00 PM CET'.lower()
3636
]
3737
test_file = self.get_test_loc('generated/simple/generated_3.java')
3838
result = list(generated.get_generated_code_hint(location=test_file))
3939
assert result == expected
4040

4141
def test_basic2(self):
42-
expected = ['* This class was generated by the JAX-WS RI.']
42+
expected = ['* This class was generated by the JAX-WS RI.'.lower()]
4343
test_file = self.get_test_loc('generated/simple/generated_2.java')
4444
result = list(generated.get_generated_code_hint(location=test_file))
4545
assert result == expected
4646

4747
def test_basic3(self):
48-
expected = ['/* This class was automatically generated']
48+
expected = ['/* This class was automatically generated'.lower()]
4949
test_file = self.get_test_loc('generated/simple/generated_4.java')
5050
result = list(generated.get_generated_code_hint(location=test_file))
5151
assert result == expected
5252

5353
def test_basic4(self):
5454
expected = [
55-
'* <p>The following schema fragment specifies the '
56-
'expected content contained within this class.'
55+
'* <p>The following schema fragment specifies the '.lower() +
56+
'expected content contained within this class.'.lower()
5757
]
5858
test_file = self.get_test_loc('generated/simple/generated_5.java')
5959
result = list(generated.get_generated_code_hint(location=test_file))
6060
assert result == expected
6161

6262
def test_basic5(self):
63-
expected = ['/* DO NOT EDIT THIS FILE - it is machine generated */']
63+
expected = ['/* DO NOT EDIT THIS FILE - it is machine generated */'.lower()]
6464
test_file = self.get_test_loc('generated/simple/generated_6.c')
6565
result = list(generated.get_generated_code_hint(location=test_file))
6666
assert result == expected
6767

6868
def test_configure(self):
6969
expected = [
70-
'# Generated by GNU Autoconf 2.64 for Apache CouchDB 1.0.1.'
70+
'# Generated by GNU Autoconf 2.64 for Apache CouchDB 1.0.1.'.lower()
7171
]
7272
test_file = self.get_test_loc('generated/simple/configure')
7373
result = list(generated.get_generated_code_hint(location=test_file))
7474
assert result == expected
7575

7676
def test_tomcat_jspc(self):
7777
expected = [
78-
'<!--Automatically created by Apache Jakarta Tomcat JspC.'
78+
'<!--Automatically created by Apache Jakarta Tomcat JspC.'.lower()
7979
]
8080
test_file = self.get_test_loc('generated/jspc/web.xml')
8181
result = list(generated.get_generated_code_hint(location=test_file))

0 commit comments

Comments
 (0)