Skip to content

Commit

Permalink
CPDBear: Fix for crash when many files are given
Browse files Browse the repository at this point in the history
Until now, the files to run on were given to CPD with
the --files option, which would raise
'OSError: [Errno 7] Argument list too long'. Now, the
files are saved in a temporary file, and that file is
given to CPD with the --filelist option, which takes a
csv file containing a list of files to run on.

Fixes coala/coala#3701
  • Loading branch information
impmihai committed Feb 14, 2017
1 parent 9fb36a9 commit f553f5b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion bears/general/CPDBear.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from shutil import which
from xml.etree import ElementTree

Expand Down Expand Up @@ -36,6 +37,7 @@ class CPDBear(GlobalBear):
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Duplication'}
TEMP_FILELIST = '.tmp_filelist'

@classmethod
def check_prerequisites(cls): # pragma: no cover
Expand Down Expand Up @@ -89,11 +91,15 @@ def run(self, language: str, minimum_tokens: int=20,
'--skip-duplicate-files': skip_duplicate_files}

files = ','.join(self.file_dict.keys())
f = open(self.TEMP_FILELIST, 'w')
f.write(str(files))
f.close()

executable = which('pmd') or which('run.sh')
arguments = ('bash', executable, 'cpd', '--skip-lexical-errors',
'--minimum-tokens', str(minimum_tokens),
'--language', self.lowered_lang_dict[language],
'--files', files,
'--filelist', self.TEMP_FILELIST,
'--format', 'xml')

arguments += tuple(option
Expand All @@ -102,6 +108,11 @@ def run(self, language: str, minimum_tokens: int=20,

stdout_output, _ = run_shell_command(arguments)

try:
os.remove(self.TEMP_FILELIST)
except OSError:
pass

if stdout_output:
root = ElementTree.fromstring(stdout_output)

Expand Down

0 comments on commit f553f5b

Please sign in to comment.