Skip to content

Commit 332acf2

Browse files
committed
Ignore version control artifacts by default when scanning. #35
1 parent 4c01a26 commit 332acf2

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

src/scancode/cli.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424

2525
from __future__ import print_function, absolute_import
2626

27+
import os
28+
from functools import partial
2729
import json
2830

2931
import click
3032

3133
from commoncode.fileutils import file_iter
34+
from commoncode import ignore
3235

3336
from scancode import __version__ as version
3437
from scancode.api import as_html
@@ -39,7 +42,6 @@
3942
from scancode.api import get_licenses
4043
from scancode.api import HtmlAppAssetCopyWarning
4144
from scancode.api import HtmlAppAssetCopyError
42-
import os
4345

4446

4547
info_text = '''
@@ -268,13 +270,16 @@ def scancode(ctx, input, output_file, extract, copyright, license, format, verbo
268270
click.secho('Scanning files...', fg='green')
269271
results = []
270272

273+
ignored = partial(ignore.is_ignored, ignores=ignore.ignores_VCS, unignores={})
274+
files = file_iter(abs_input, ignored=ignored)
275+
271276
if not verbose:
272277
# only display a progress bar
273-
with click.progressbar(file_iter(abs_input), show_pos=True) as files:
278+
with click.progressbar(files, show_pos=True) as files:
274279
for input_file in files:
275280
results.append(scan_one(input_file, copyright, license, verbose))
276281
else:
277-
for input_file in file_iter(abs_input):
282+
for input_file in file_iter(files):
278283
results.append(scan_one(input_file, copyright, license, verbose))
279284

280285
if format == 'html':

tests/commoncode/data/ignore/vcs.tgz

55 Bytes
Binary file not shown.

tests/scancode/data/ignore/vcs.tgz

5.55 KB
Binary file not shown.

tests/scancode/test_cli.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
from __future__ import absolute_import, print_function
2626

2727
import os
28+
import json
2829

2930
from click.testing import CliRunner
3031

3132
from commoncode.testcase import FileBasedTesting
32-
from scancode import cli
3333
from commoncode.fileutils import as_posixpath
3434

35+
from scancode import cli
36+
3537

3638
class TestCommandLine(FileBasedTesting):
3739
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
@@ -85,7 +87,7 @@ def test_extract_option_works_with_relative_paths(self):
8587
scancode_tmp = join(scancode_root, 'tmp')
8688
fileutils.create_dir(scancode_tmp)
8789
scancode_root_abs = abspath(scancode_root)
88-
test_src_dir = tempfile.mkdtemp(dir=scancode_tmp).replace(scancode_root_abs, '').strip('\\/')
90+
test_src_dir = tempfile.mkdtemp(dir=scancode_tmp).replace(scancode_root_abs, '').strip('\\/')
8991
test_file = self.get_test_loc('extract_relative_path/basic.zip')
9092
shutil.copy(test_file, test_src_dir)
9193
test_src_file = join(test_src_dir, 'basic.zip')
@@ -98,8 +100,8 @@ def test_extract_option_works_with_relative_paths(self):
98100
assert not 'WARNING' in result.output
99101
assert not 'ERROR' in result.output
100102
expected = ['/c/a/a.txt', '/c/b/a.txt', '/c/c/a.txt']
101-
file_result = [as_posixpath(f.replace(test_tgt_dir, '')) for f in fileutils.file_iter(test_tgt_dir)]
102-
assert sorted(expected)==sorted(file_result)
103+
file_result = [as_posixpath(f.replace(test_tgt_dir, '')) for f in fileutils.file_iter(test_tgt_dir)]
104+
assert sorted(expected) == sorted(file_result)
103105

104106
def test_copyright_option_detects_copyrights(self):
105107
test_dir = self.get_test_loc('copyright', copy=True)
@@ -120,3 +122,15 @@ def test_license_option_detects_licenses(self):
120122
assert 'Scanning done' in result.output
121123
assert os.path.exists(output_json)
122124
assert len(open(output_json).read()) > 10
125+
126+
def test_scancode_skip_vcs_files_and_dirs_by_default(self):
127+
test_dir = self.extract_test_tar('ignore/vcs.tgz')
128+
runner = CliRunner()
129+
output_json = self.get_temp_file('json')
130+
result = runner.invoke(cli.scancode, ['--copyright', test_dir, output_json])
131+
assert result.exit_code == 0
132+
with open(output_json) as res:
133+
scan_result = json.load(res)
134+
# a single test.tst file that is not a VCS file should be listed
135+
assert 1 == scan_result['count']
136+
assert scan_result['results'][0]['location'].endswith('vcs.tgz/vcs/test.txt')

0 commit comments

Comments
 (0)