<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> import subprocess <EOL> from pants . base . build_environment import get_buildroot <EOL> from pants . base . exceptions import TaskError <EOL> from pants . base . workunit import WorkUnitLabel <EOL> from pants . binaries . binary_util import BinaryUtil <EOL> from pants . task . console_task import ConsoleTask <EOL> from pants . util . contextutil import temporary_dir <EOL> class CountLinesOfCode ( ConsoleTask ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def global_subsystems ( cls ) : <EOL> return super ( CountLinesOfCode , cls ) . global_subsystems ( ) + ( BinaryUtil . Factory , ) <EOL> @ classmethod <EOL> def register_options ( cls , register ) : <EOL> super ( CountLinesOfCode , cls ) . register_options ( register ) <EOL> register ( '<STR_LIT>' , advanced = True , fingerprint = True , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> register ( '<STR_LIT>' , type = bool , fingerprint = True , default = True , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> register ( '<STR_LIT>' , type = bool , fingerprint = True , <EOL> help = '<STR_LIT>' ) <EOL> def _get_cloc_script ( self ) : <EOL> binary_util = BinaryUtil . Factory . create ( ) <EOL> return binary_util . select_script ( '<STR_LIT>' , self . get_options ( ) . version , '<STR_LIT>' ) <EOL> def console_output ( self , targets ) : <EOL> if not self . get_options ( ) . transitive : <EOL> targets = self . context . target_roots <EOL> buildroot = get_buildroot ( ) <EOL> with temporary_dir ( ) as tmpdir : <EOL> list_file = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> with open ( list_file , '<STR_LIT:w>' ) as list_file_out : <EOL> for target in targets : <EOL> for source in target . sources_relative_to_buildroot ( ) : <EOL> list_file_out . write ( os . path . join ( buildroot , source ) ) <EOL> list_file_out . write ( b'<STR_LIT:\n>' ) <EOL> report_file = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> ignored_file = os . path . join ( tmpdir , '<STR_LIT>' ) <EOL> cloc_script = self . _get_cloc_script ( ) <EOL> cmd = [ cloc_script , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( ignored_file ) , <EOL> '<STR_LIT>' . format ( list_file ) , <EOL> '<STR_LIT>' . format ( report_file ) ] <EOL> with self . context . new_workunit ( name = '<STR_LIT>' , <EOL> labels = [ WorkUnitLabel . TOOL ] , <EOL> cmd = '<STR_LIT:U+0020>' . join ( cmd ) ) as workunit : <EOL> result = subprocess . call ( cmd , <EOL> stdout = workunit . output ( '<STR_LIT>' ) , <EOL> stderr = workunit . output ( '<STR_LIT>' ) ) <EOL> if result != <NUM_LIT:0> : <EOL> raise TaskError ( '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( cmd ) , result ) ) <EOL> with open ( report_file , '<STR_LIT:r>' ) as report_file_in : <EOL> for line in report_file_in . read ( ) . split ( '<STR_LIT:\n>' ) : <EOL> yield line <EOL> if self . get_options ( ) . ignored : <EOL> yield '<STR_LIT>' <EOL> with open ( ignored_file , '<STR_LIT:r>' ) as ignored_file_in : <EOL> for line in ignored_file_in . read ( ) . split ( '<STR_LIT:\n>' ) : <EOL> yield line </s>
