Skip to content

Commit b410594

Browse files
committed
Improve detection of junk URLs
This help make the function reusable for use elsewhere. Signed-off-by: Philippe Ombredanne <pombredanne@aboutcode.org>
1 parent 9c12d93 commit b410594

6 files changed

Lines changed: 209 additions & 70 deletions

File tree

src/cluecode/finder_data.py

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ def set_from_text(text):
1616
return set(u.lower().strip('/') for u in text.split())
1717

1818

19+
def urls_set_from_text(text):
20+
"""
21+
Return a set from text, ensuring that both http and https scheme are
22+
injected for every URL.
23+
"""
24+
https_urls = text.replace("http://", "https://").split()
25+
http_urls = text.replace("https://", "http://").split()
26+
return set(u.lower().strip('/') for u in http_urls + https_urls)
27+
28+
1929
JUNK_EMAILS = set_from_text(u'''
2030
test@test.com
2131
exmaple.com
@@ -26,7 +36,6 @@ def set_from_text(text):
2636
localhost
2737
''')
2838

29-
3039
JUNK_HOSTS_AND_DOMAINS = set_from_text(u'''
3140
exmaple.com
3241
example.com
@@ -60,37 +69,15 @@ def set_from_text(text):
6069
some.com
6170
''')
6271

63-
JUNK_URLS = set_from_text(u'''
72+
JUNK_URLS = urls_set_from_text(u'''
6473
http://www.adobe.com/2006/mxml
65-
http://www.w3.org/1999/XSL/Transform
6674
http://docs.oasis-open.org/ns/xri/xrd-1.0
67-
http://www.w3.org/2001/XMLSchema-instance
68-
http://www.w3.org/2001/XMLSchema}string
69-
http://www.w3.org/2001/XMLSchema
70-
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
7175
http://bing.com
7276
http://google.com
7377
http://msn.com
7478
http://maven.apache.org/maven-v4_0_0.xsd
7579
http://maven.apache.org/POM/4.0.0
7680
http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd
77-
http://www.w3.org/1999/02/22-rdf-syntax-ns
78-
http://www.w3.org/1999/xhtml
79-
http://www.w3.org/1999/XMLSchema
80-
http://www.w3.org/1999/XMLSchema-instance
81-
http://www.w3.org/2000/svg
82-
http://www.w3.org/2001/XMLSchema
83-
http://www.w3.org/2000/10/XMLSchema
84-
http://www.w3.org/2000/10/XMLSchema-instance
85-
http://www.w3.org/2001/XMLSchema
86-
http://www.w3.org/2001/XMLSchema-instance
87-
http://www.w3.org/2002/12/soap-encoding
88-
http://www.w3.org/2002/12/soap-envelope
89-
http://www.w3.org/2005/Atom
90-
http://www.w3.org/2006/01/wsdl
91-
http://www.w3.org/2006/01/wsdl/http
92-
http://www.w3.org/2006/01/wsdl/soap
93-
http://www.w3.org/2006/vcard/ns
9481
http://www.w3.org/International/O-URL-and-ident.html
9582
http://www.w3.org/MarkUp
9683
http://www.w3.org/WAI/GL
@@ -106,37 +93,24 @@ def set_from_text(text):
10693
http://]hostname
10794
http://+
10895
http://www
109-
http://www.w3.org/1999/xhtml
110-
http://www.w3.org/1999/XSL/Transform
111-
http://www.w3.org/2001/XMLSchema
112-
http://www.w3.org/2001/XMLSchema-instance
113-
http://www.w3.org/hypertext/WWW/Protocols/HTTP/HTRESP.html
114-
http://www.w3.org/hypertext/WWW/Protocols/HTTP/Object_Headers.html
11596
http://www.w3.org/P3P
11697
http://www.w3.org/pub/WWW
117-
http://www.w3.org/TR/html4/strict.dtd
118-
http://www.w3.org/TR/REC-html40/loose.dtd
119-
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
120-
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
121-
http://www.w3.org/TR/xslt
12298
https:
12399
https://+
124-
http://www.example.com
125-
http://www.example.com/dir/file
126-
http://www.example.com:dir/file
127-
http://www.your.org.here
128-
http://hostname
129100
https://www.trustedcomputinggroup.org/XML/SCHEMA/TNCCS_1.0.xsd
130101
http://glade.gnome.org/glade-2.0.dtd
131102
http://pagesperso-orange.fr/sebastien.godard/sysstat.dtd
132103
http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd
133104
http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd
134105
http://gcc.gnu.org/bugs.html
135106
http://nsis.sf.net/NSIS_Error
107+
http://www.your.org.here
136108
''')
137109

138-
139-
JUNK_URL_PREFIXES = tuple(sorted(set_from_text('''
110+
JUNK_URL_PREFIXES = sorted(urls_set_from_text('''
111+
http://hostname
112+
http://www.example.com
113+
http://example.com
140114
http://www.springframework.org/dtd/
141115
http://www.slickedit.com/dtd/
142116
http://www.oexchange.org/spec/0.8/
@@ -146,15 +120,21 @@ def set_from_text(text):
146120
http://foo.bar.baz
147121
http://foo.bar.com
148122
http://foobar.com
149-
http://java.sun.com/xml/ns/
123+
http://java.sun.com/xml/ns
150124
http://java.sun.com/j2se/1.4/docs/
151125
http://java.sun.com/j2se/1.5.0/docs/
126+
http://java.sun.com/dtd/
127+
http://java.sun.com/j2ee/dtds/
128+
http://java.sun.com/xml/ns/
152129
http://developer.apple.com/certificationauthority/
153130
http://www.apple.com/appleca/
154131
https://www.apple.com/certificateauthority/
155132
http://schemas.microsoft.com/
156133
http://dublincore.org/schemas/
157134
http://www.w3.org/TR/
135+
http://www.w3.org/1
136+
http://www.w3.org/2
137+
http://www.w3.org/hypertext/WWW/Protocols/
158138
http://www.apple.com/DTDs
159139
http://apache.org/xml/features/
160140
http://apache.org/xml/properties/
@@ -166,8 +146,6 @@ def set_from_text(text):
166146
http://csc3-2009-2-crl.verisign.com
167147
http://dellincca.dell.com/crl
168148
http://ts-crl.ws.symantec.com
169-
http://java.sun.com/dtd/
170-
http://java.sun.com/j2ee/dtds/
171149
http://jakarta.apache.org/commons/dtds/
172150
http://jakarta.apache.org/struts/dtds/
173151
http://www.jboss.org/j2ee/dtd/
@@ -193,14 +171,9 @@ def set_from_text(text):
193171
http://www.microsoft.com/pki/certs/
194172
http://www.microsoft.com/pkiops/crl
195173
http://www.microsoft.com/PKI/
196-
''')))
174+
'''))
197175

198-
JUNK_DOMAIN_SUFFIXES = tuple(sorted(set_from_text('''
199-
.png
200-
.jpg
201-
.gif
202-
.jpeg
203-
''')))
176+
JUNK_DOMAIN_SUFFIXES = ('.gif', '.jpeg', '.jpg', '.png')
204177

205178

206179
def classify(s, data_set, suffixes=None, ignored_hosts=None):
@@ -241,10 +214,10 @@ def classify(s, data_set, suffixes=None, ignored_hosts=None):
241214
ignored_hosts=JUNK_EXACT_DOMAIN_NAMES,
242215
)
243216

244-
# a regex for copyright lexer
245-
JUNK_URLS_RE = [f"^\/*{re.escape(u)}\/*$" for u in JUNK_URLS]
246-
JUNK_URL_PREFIXES_RE = [f"^\/*{re.escape(u)}\/*" for u in JUNK_URL_PREFIXES]
247-
JUNK_DOMAIN_SUFFIXES_RE = [f"\/*{re.escape(u)}\/*$" for u in JUNK_DOMAIN_SUFFIXES]
217+
# a Junk URL big regex for use in copyright lexers
218+
JUNK_URLS_RE = [fr"^/?{re.escape(u)}/?$" for u in JUNK_URLS]
219+
JUNK_URL_PREFIXES_RE = [fr"^/?{re.escape(u)}/?" for u in JUNK_URL_PREFIXES]
220+
JUNK_DOMAIN_SUFFIXES_RE = [fr"{re.escape(u)}/?$" for u in JUNK_DOMAIN_SUFFIXES]
248221
JUNK_ALL_URLS = "|".join(JUNK_URLS_RE + JUNK_URL_PREFIXES_RE + JUNK_DOMAIN_SUFFIXES_RE)
249222
JUNK_ALL_URLS = f"({JUNK_ALL_URLS})"
250223
IS_JUNK_URL = re.compile(JUNK_ALL_URLS, flags=re.IGNORECASE).search
@@ -254,11 +227,4 @@ def classify_url(url):
254227
"""
255228
Return True if a URL is a proper URL, or False if this is a JUNK URL
256229
"""
257-
if not url:
258-
return False
259-
u = url.lower().strip('/')
260-
if (u in JUNK_URLS or
261-
u.startswith(JUNK_URL_PREFIXES)
262-
or u.endswith(JUNK_DOMAIN_SUFFIXES)):
263-
return False
264-
return True
230+
return url and not IS_JUNK_URL(url)

src/licensedcode/data/licenses/componentace-jcraft.LICENSE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ ignorable_copyrights:
1313
ignorable_holders:
1414
- ComponentAce
1515
- ymnk, JCraft,Inc.
16+
ignorable_authors:
17+
- Jean-loup Gailly (jloup@gzip.org) and Mark Adler (madler@alumni.caltech.edu) and contributors
1618
ignorable_urls:
1719
- http://www.componentace.com/
1820
ignorable_emails:

src/licensedcode/data/licenses/saf.LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ category: Permissive
66
owner: Service Availability Forum
77
spdx_license_key: LicenseRef-scancode-saf
88
ignorable_copyrights:
9-
- Copyright (c) Service Availability(TM) Forum
9+
- Copyright (c) Service Availability (TM) Forum
1010
ignorable_holders:
11-
- Service Availability(TM) Forum
11+
- Service Availability (TM) Forum
1212
---
1313

1414
Service Availability Forum License

src/licensedcode/data/licenses/sata.LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ other_urls:
1010
- https://github.com/zTrix/sata-license
1111
- https://github.com/kongtianyi/sata-license
1212
ignorable_copyrights:
13-
- Copyright (c) 2014 zTrix(i@ztrix.me) kongtianyi(kongtianyi@foxmail.com)
13+
- Copyright (c) 2014 zTrix (i@ztrix.me) kongtianyi (kongtianyi@foxmail.com)
1414
ignorable_holders:
15-
- zTrix(i@ztrix.me) kongtianyi(kongtianyi@foxmail.com)
15+
- zTrix
1616
ignorable_urls:
1717
- https://github.com/kongtianyi/sata-license
1818
- https://github.com/zTrix/sata-license

tests/cluecode/test_finder_data.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) nexB Inc. and others. All rights reserved.
4+
# ScanCode is a trademark of nexB Inc.
5+
# SPDX-License-Identifier: Apache-2.0
6+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
7+
# See https://github.com/nexB/scancode-toolkit for support or download.
8+
# See https://aboutcode.org for more information about nexB OSS projects.
9+
#
10+
11+
import pytest
12+
13+
from cluecode.finder_data import JUNK_URL_PREFIXES
14+
from cluecode.finder_data import JUNK_URLS
15+
from cluecode.finder_data import JUNK_DOMAIN_SUFFIXES
16+
from cluecode.finder_data import classify_url
17+
18+
19+
@pytest.mark.parametrize('url_prefix', sorted(JUNK_URL_PREFIXES))
20+
def test_classify_url__junk_urls_prefixes(url_prefix):
21+
assert not classify_url(url_prefix)
22+
assert classify_url(f"foobar/{url_prefix}")
23+
24+
25+
@pytest.mark.parametrize('url', sorted(JUNK_URLS))
26+
def test_classify_url__junk_urls(url):
27+
assert not classify_url(url)
28+
assert classify_url(f"{url}/foobar")
29+
30+
31+
@pytest.mark.parametrize('suffix', sorted(JUNK_DOMAIN_SUFFIXES))
32+
def test_classify_url__junk_domain_suffix(suffix):
33+
assert not classify_url(f"http://foo/bar{suffix}")
34+
assert classify_url(f"{suffix}/some/bar")
35+
36+
37+
MORE_JUNK_URLS = '''
38+
http://www.adobe.com/2006/mxml
39+
http://www.w3.org/1999/XSL/Transform
40+
http://docs.oasis-open.org/ns/xri/xrd-1.0
41+
http://www.w3.org/2001/XMLSchema-instance
42+
http://www.w3.org/2001/XMLSchema}string
43+
http://www.w3.org/2001/XMLSchema
44+
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
45+
http://bing.com
46+
http://google.com
47+
http://msn.com
48+
http://maven.apache.org/maven-v4_0_0.xsd
49+
http://maven.apache.org/POM/4.0.0
50+
http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd
51+
http://www.w3.org/1999/02/22-rdf-syntax-ns
52+
http://www.w3.org/1999/xhtml
53+
http://www.w3.org/1999/XMLSchema
54+
http://www.w3.org/1999/XMLSchema-instance
55+
http://www.w3.org/2000/svg
56+
http://www.w3.org/2001/XMLSchema
57+
http://www.w3.org/2000/10/XMLSchema
58+
http://www.w3.org/2000/10/XMLSchema-instance
59+
http://www.w3.org/2001/XMLSchema
60+
http://www.w3.org/2001/XMLSchema-instance
61+
http://www.w3.org/2002/12/soap-encoding
62+
http://www.w3.org/2002/12/soap-envelope
63+
http://www.w3.org/2005/Atom
64+
http://www.w3.org/2006/01/wsdl
65+
http://www.w3.org/2006/01/wsdl/http
66+
http://www.w3.org/2006/01/wsdl/soap
67+
http://www.w3.org/2006/vcard/ns
68+
http://www.w3.org/International/O-URL-and-ident.html
69+
http://www.w3.org/MarkUp
70+
http://www.w3.org/WAI/GL
71+
http://xml.apache.org/axis/session
72+
http://xml.apache.org/xml-soap
73+
http://docs.oasis-open.org/ns/xri/xrd-1.0
74+
http://cobertura.sourceforge.net/xml/coverage-01.dtd
75+
http://findbugs.googlecode.com/svn/trunk/findbugs/etc/docbook/docbookx.dtd
76+
http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd
77+
http://hibernate.sourceforge.net/hibernate-generic.dtd
78+
http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd
79+
http://www.opensymphony.com/xwork/xwork-1.0.dtd
80+
http://]hostname
81+
http://+
82+
http://www
83+
http://www.w3.org/1999/xhtml
84+
http://www.w3.org/1999/XSL/Transform
85+
http://www.w3.org/2001/XMLSchema
86+
http://www.w3.org/2001/XMLSchema-instance
87+
http://www.w3.org/hypertext/WWW/Protocols/HTTP/HTRESP.html
88+
http://www.w3.org/hypertext/WWW/Protocols/HTTP/Object_Headers.html
89+
http://www.w3.org/P3P
90+
http://www.w3.org/pub/WWW
91+
http://www.w3.org/TR/html4/strict.dtd
92+
http://www.w3.org/TR/REC-html40/loose.dtd
93+
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
94+
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
95+
http://www.w3.org/TR/xslt
96+
https:
97+
https://+
98+
http://www.example.com
99+
http://www.example.com/dir/file
100+
http://www.example.com:dir/file
101+
http://www.your.org.here
102+
http://hostname
103+
https://www.trustedcomputinggroup.org/XML/SCHEMA/TNCCS_1.0.xsd
104+
http://glade.gnome.org/glade-2.0.dtd
105+
http://pagesperso-orange.fr/sebastien.godard/sysstat.dtd
106+
http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd
107+
http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd
108+
http://gcc.gnu.org/bugs.html
109+
http://nsis.sf.net/NSIS_Error
110+
http://www.springframework.org/dtd/
111+
http://www.slickedit.com/dtd/
112+
http://www.oexchange.org/spec/0.8/
113+
http://www.puppycrawl.com/dtds/
114+
http://adobe.com/AS3/2006/builtin
115+
http://careers.msn.com
116+
http://foo.bar.baz
117+
http://foo.bar.com
118+
http://foobar.com
119+
http://java.sun.com/xml/ns/
120+
http://java.sun.com/j2se/1.4/docs/
121+
http://java.sun.com/j2se/1.5.0/docs/
122+
http://developer.apple.com/certificationauthority/
123+
http://www.apple.com/appleca/
124+
https://www.apple.com/certificateauthority/
125+
http://schemas.microsoft.com/
126+
http://dublincore.org/schemas/
127+
http://www.w3.org/TR/
128+
http://www.apple.com/DTDs
129+
http://apache.org/xml/features/
130+
http://apache.org/xml/properties/
131+
http://crl.verisign.com/
132+
http://crl.globalsign.net/
133+
http://crl.microsoft.com/
134+
http://crl.thawte.com/
135+
http://CSC3-2004-crl.verisign.com
136+
http://csc3-2009-2-crl.verisign.com
137+
http://dellincca.dell.com/crl
138+
http://ts-crl.ws.symantec.com
139+
http://java.sun.com/dtd/
140+
http://java.sun.com/j2ee/dtds/
141+
http://jakarta.apache.org/commons/dtds/
142+
http://jakarta.apache.org/struts/dtds/
143+
http://www.jboss.org/j2ee/dtd/
144+
http://glassfish.org/dtds/
145+
http://docbook.org/xml/simple/
146+
http://www.oasis-open.org/docbook/xml/
147+
http://www.w3.org/XML/1998/namespace
148+
https://www.w3.org/XML/1998/namespace
149+
http://www.w3.org/2000/xmlns/
150+
https://www.w3.org/2000/xmlns/
151+
http://ts-aia.ws.symantec.com/
152+
https://ts-aia.ws.symantec.com/
153+
https://www.verisign.com/rpa
154+
http://csc3-2010-crl.verisign.com/
155+
https://www.verisign.com/rpa
156+
http://csc3-2010-aia.verisign.com/
157+
https://www.verisign.com/cps
158+
http://logo.verisign.com/
159+
http://ocsp2.globalsign.com/
160+
http://crl.globalsign.com/
161+
http://secure.globalsign.com/cacert/
162+
https://www.globalsign.com/repository/
163+
http://www.microsoft.com/pki/certs/
164+
http://www.microsoft.com/pkiops/crl
165+
http://www.microsoft.com/PKI/
166+
'''.split()
167+
168+
169+
@pytest.mark.parametrize('url', sorted(MORE_JUNK_URLS))
170+
def test_classify_url__more_junk_urls(url):
171+
assert not classify_url(url)

tests/licensedcode/test_detect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,8 +1075,8 @@ def test_match_has_correct_line_positions_in_automake_perl_file(self):
10751075
expected = [
10761076
# detected, match.lines(), match.qspan,
10771077
('gpl-2.0-plus', (12, 25), Span(51, 160)),
1078-
('fsf-unlimited-no-warranty', (231, 238), Span(986, 1049)),
1079-
('warranty-disclaimer', (306, 307), Span(1359, 1381)),
1078+
('fsf-unlimited-no-warranty', (231, 238), Span(998, 1061)),
1079+
('warranty-disclaimer', (306, 307), Span(1371, 1393)),
10801080
]
10811081
self.check_position('positions/automake.pl', expected)
10821082

0 commit comments

Comments
 (0)