<s> import sublime , sublime_plugin <EOL> import subprocess , os <EOL> import threading <EOL> from queue import Queue <EOL> class CodeSearchIndexCommand ( sublime_plugin . WindowCommand ) : <EOL> def __init__ ( self , window ) : <EOL> sublime_plugin . WindowCommand . __init__ ( self , window ) <EOL> self . path_cindex = None <EOL> self . indexing = False <EOL> def run ( self ) : <EOL> data = self . window . project_data ( ) <EOL> self . folders = [ ] <EOL> for folder in data [ "<STR_LIT>" ] : <EOL> self . folders . append ( os . path . expanduser ( folder [ "<STR_LIT:path>" ] ) ) <EOL> if "<STR_LIT>" in data : <EOL> if "<STR_LIT>" in data [ "<STR_LIT>" ] : <EOL> self . csearchindex = os . path . expanduser ( data [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> settings = sublime . load_settings ( '<STR_LIT>' ) <EOL> self . path_cindex = settings . get ( "<STR_LIT>" ) <EOL> indexThread = threading . Thread ( target = self . runIndexing ) <EOL> indexThread . start ( ) <EOL> return <EOL> sublime . error_message ( "<STR_LIT>" ) <EOL> def runIndexing ( self ) : <EOL> csearchdir = os . path . abspath ( os . path . dirname ( self . csearchindex ) ) <EOL> if not os . path . isdir ( csearchdir ) : <EOL> os . makedirs ( csearchdir ) <EOL> self . indexing = True <EOL> project_file_name = self . window . project_file_name ( ) <EOL> working_dir = os . path . dirname ( project_file_name ) <EOL> self . total_files = <NUM_LIT:0> <EOL> self . num_files = <NUM_LIT:0> <EOL> for folder in self . folders : <EOL> dir = os . path . join ( working_dir , folder ) <EOL> self . total_files += self . getNumberOfFiles ( dir ) <EOL> self . updateStatus ( ) <EOL> my_env = os . environ . copy ( ) <EOL> my_env [ "<STR_LIT>" ] = self . csearchindex <EOL> commandLine = [ self . path_cindex , "<STR_LIT>" ] <EOL> try : <EOL> startupinfo = subprocess . STARTUPINFO ( ) <EOL> startupinfo . dwFlags |= subprocess . STARTF_USESHOWWINDOW <EOL> except : <EOL> startupinfo = None <EOL> self . popen = subprocess . Popen ( commandLine + self . folders , cwd = working_dir , env = my_env , startupinfo = startupinfo , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) <EOL> stdoutQueue = Queue ( ) <EOL> readStdOutThread = threading . Thread ( target = self . readStdout , args = ( self . popen . stdout , stdoutQueue ) ) <EOL> readStdOutThread . daemon = True <EOL> readStdOutThread . start ( ) <EOL> stderrQueue = Queue ( ) <EOL> readStdErrThread = threading . Thread ( target = self . readStderr , args = ( self . popen . stderr , stderrQueue ) ) <EOL> readStdErrThread . daemon = True <EOL> readStdErrThread . start ( ) <EOL> self . popen . wait ( ) <EOL> self . popen = None <EOL> self . indexing = False <EOL> self . window . active_view ( ) . erase_status ( "<STR_LIT>" ) <EOL> def getNumberOfFiles ( self , folder ) : <EOL> numberOfFiles = <NUM_LIT:0> <EOL> for root , dirs , files in os . walk ( folder ) : <EOL> numberOfFiles += len ( files ) <EOL> return numberOfFiles <EOL> def readStdout ( self , out , queue ) : <EOL> for line in iter ( out . readline , b'<STR_LIT>' ) : <EOL> pass <EOL> out . close ( ) <EOL> def readStderr ( self , out , queue ) : <EOL> self . num_files = <NUM_LIT:0> <EOL> for line in iter ( out . readline , b'<STR_LIT>' ) : <EOL> self . num_files += <NUM_LIT:1> <EOL> out . close ( ) <EOL> def updateStatus ( self ) : <EOL> if self . indexing : <EOL> percent = <NUM_LIT> <EOL> if self . total_files > <NUM_LIT:0> : <EOL> percent = self . num_files / self . total_files * <NUM_LIT:100> <EOL> self . window . active_view ( ) . set_status ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" . format ( self . num_files , self . total_files , int ( percent ) ) ) <EOL> sublime . set_timeout ( self . updateStatus , <NUM_LIT> ) </s>
