Skip to content

Commit ea97933

Browse files
committed
Reduce dependencies for license scanning API
Signed-off-by: Shivam Pandey <sd.scores4455@gmail.com>
1 parent 058f439 commit ea97933

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/licensedcode/query.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
from intbitset import intbitset
1919

20-
import typecode
2120

2221
from commoncode.text import toascii
2322
from licensedcode.spans import Span
@@ -120,6 +119,7 @@ def build_query(
120119
Return a Query built from location or query string given an index.
121120
"""
122121
if location:
122+
import typecode
123123
T = typecode.get_type(location)
124124
# TODO: implement additional type-driven heuristics for query chunking.
125125
if not T.contains_text:
@@ -584,6 +584,7 @@ def _tokenize_and_build_runs(self, tokens_by_line, line_threshold=4):
584584
query_runs_append = self.query_runs.append
585585

586586
if self.location:
587+
import typecode
587588
ft = typecode.get_type(self.location)
588589
if ft.is_text_with_long_lines:
589590
self.has_long_lines = True

src/scancode/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
import os
1313
import sys
1414

15+
from license_expression import combine_expressions
1516
from commoncode.filetype import get_last_modified_date
1617
from commoncode.hash import multi_checksums
1718
from scancode import ScancodeError
18-
from typecode.contenttype import get_type
1919

2020
TRACE = os.environ.get('SCANCODE_DEBUG_API', False)
2121

@@ -179,7 +179,6 @@ def get_licenses(
179179
from licensedcode.cache import build_spdx_license_expression
180180
from licensedcode.cache import get_cache
181181
from licensedcode.detection import detect_licenses
182-
from packagedcode.utils import combine_expressions
183182

184183
license_clues = []
185184
license_detections = []
@@ -340,6 +339,7 @@ def get_file_info(location, **kwargs):
340339
"""
341340
Return a mapping of file information collected for the file at `location`.
342341
"""
342+
from typecode.contenttype import get_type
343343
result = {}
344344

345345
# TODO: move date and size these to the inventory collection step???

src/textcode/analysis.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import unicodedata
1515

1616
import chardet
17-
import typecode
1817

1918
from textcode import pdf
2019
from textcode import markup
@@ -86,6 +85,7 @@ def numbered_text_lines(
8685
logger_debug('numbered_text_lines:', 'plain_text')
8786
return enumerate(unicode_text_lines(location), start_line)
8887

88+
import typecode
8989
T = typecode.get_type(location)
9090

9191
if TRACE:
@@ -184,6 +184,7 @@ def unicode_text_lines_from_binary(location):
184184
Return an iterable over unicode text lines extracted from a binary file at
185185
location.
186186
"""
187+
import typecode
187188
T = typecode.get_type(location)
188189
if T.contains_text:
189190
for line in strings.strings_from_file(location):

tests/scancode/test_api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#
99

1010
import os
11+
import subprocess
12+
import sys
1113

1214
from commoncode.testcase import FileBasedTesting
1315
from scancode import api
@@ -144,3 +146,13 @@ def test_get_license_returns_correct_lines(self):
144146
assert results['detected_license_expression'] == 'mit'
145147
assert results['license_detections'][0]['matches'][0]['start_line'] == 2
146148
assert results['license_detections'][0]['matches'][0]['end_line'] == 4
149+
150+
def test_get_licenses_imports_without_heavy_dependencies(self):
151+
script = (
152+
'import sys; '
153+
'from scancode.api import get_licenses; '
154+
'assert "typecode" not in sys.modules; '
155+
'assert "extractcode" not in sys.modules; '
156+
'assert "packagedcode" not in sys.modules'
157+
)
158+
subprocess.check_call([sys.executable, '-c', script])

0 commit comments

Comments
 (0)