scour.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
## On module removal, release file lock on file indexes
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = {
    foreach($searcher in $SCRIPT:searchers.Values)
    {
        $searcher.IndexReader.Dispose()
        $searcher = $null
    }

    foreach($indexDirectory in $SCRIPT:indexDirectories.Values)
    {
        $indexDirectory.Dispose()
        $indexDirectory = $null
    }

    [GC]::Collect()
}

## Creates an index of the files in the current location, storing the index
## in the __scour subdirectory.
function Initialize-ScourIndex
{
    [CmdletBinding()]
    param(
        ## The pattern to use for file indexing. Defaults to *.txt + common source extensions
        [string[]] $Path = ("*.ps1","*.psm1","*.cs","*.c","*.cpp","*.h","*.py","*.java","*.txt")
    )

    try
    {
        ## Open the index from the "__scour" subdirectory of the current location
        $indexDirectory = [Lucene.Net.Store.FSDirectory]::Open("$pwd\__scour")
        $analyzer = New-Object Lucene.Net.Analysis.Standard.StandardAnalyzer "LUCENE_CURRENT"

        $unlimited = [Lucene.Net.Index.IndexWriter+MaxFieldLength]::UNLIMITED
        $indexWriter = New-Object Lucene.Net.Index.IndexWriter $indexDirectory,$analyzer,$true,$unlimited
    
        $parallelScript = {
            param($IndexWriter, $InputQueue, $OutputProgress, $ThreadId)

            $processed = 0
            $file = ""

            while($true)
            {
                if($InputQueue.TryDequeue([ref] $file))
                {
                    $content = Get-Content -LiteralPath $file -Raw
                    $hash = Get-FileHash -LiteralPath $file | % Hash
                    $indexPath = (Resolve-Path $file -Relative).Substring(2)
                
                    ## Create the Lucene document and add it to the index. Retain the path so that we can
                    ## use it for quick searches later.
                    $document = New-Object Lucene.Net.Documents.Document
                    $document.Add( (New-Object Lucene.Net.Documents.Field "path", $indexPath, "YES","ANALYZED") )
                    $document.Add( (New-Object Lucene.Net.Documents.Field "content", $content, "YES","ANALYZED") )
                    $document.Add( (New-Object Lucene.Net.Documents.Field "hash", $hash, "YES","NO") )
                    $indexWriter.AddDocument($document)

                    $processed++
                    $OutputProgress[$ThreadId] = $processed
                }
                else
                {
                    Start-Sleep -m 100
                }
            }
        }
        
        $threads = Get-WmiObject Win32_Processor | % NumberOfLogicalProcessors
        $runspaces = 1..$threads | % {
            $rs = [PowerShell]::Create()
            $null = $rs.Runspace.SessionStateProxy.Path.SetLocation($pwd.Path)
            $rs
        }
        $inputQueue = New-Object 'System.Collections.Concurrent.ConcurrentQueue[String]'
        $outputProgress = New-Object 'Int[]' $threads

        for($counter = 0; $counter -lt $threads; $counter++)
        {   
            $null = $runspaces[$counter].
                AddScript($parallelScript).
                    AddParameter("IndexWriter", $indexWriter).
                    AddParameter("InputQueue", $inputQueue).
                    AddParameter("OutputProgress", $outputProgress).
                    AddParameter("ThreadId", $counter).BeginInvoke()
        }    
        
        ## Count the number of files so that we can get an accurate progress measurement
        Write-Progress -Activity "Collecting files for processing"
        $fileCount = 0
        
        ## Go through each of the files and index them
        $path | Foreach-Object {
            $extension = $_
            Write-Progress -Activity "Searching for $extension files"

            Get-ChildItem -AF -Filter $extension -Recurse | Foreach-Object {
                $file = $_
                $fileCount++

                if(($fileCount % 1000) -eq 0)
                {
                    Write-Progress -Activity "Preparing $extension - $($file.Name) - collected $fileCount files"
                }

                $inputQueue.Enqueue($file.FullName)
            }
        }

        do
        {
            $totalProcessed = $outputProgress | Measure-Object -Sum | % Sum
            Write-Progress -Activity "Processing $totalProcessed of $fileCount" -PercentComplete ($totalProcessed * 100 / $fileCount)

            Start-Sleep -Seconds 2
        } while($inputQueue.Count -gt 0)

        ## Optimize and commit the index
        Write-Progress -Activity "Optimizing index"
        $indexWriter.Commit()
    }
    finally
    {
        ## Clean up
        $indexWriter.Dispose()
        $indexDirectory.Dispose()
        
        $runspaces | % { $_.Stop(); $_.Dispose() }

        [GC]::Collect()
    }
}

## Search the indexed database for a given regular expression pattern
function Search-ScourContent
{
    [CmdletBinding()]
    param(
        ## The query to use when searching
        [Parameter(Mandatory, Position = 0)]
        [String[]] $Query,

        ## The regular expression to apply to results, if any
        [Parameter()]
        [String] $RegularExpression,

        ## The file pattern to limit the search to, if any
        [Parameter()]
        $Path = "*"
    )

    ## Ensure they've created an index for the current location. Don't do this for them automatically,
    ## as it's likely to take a long time. Search parent directories if required. If the index is found
    ## in a parent directory, we will use the current subdirectory as a filter for results.
    $scourRoot = $pwd.Path
    $driveRoot = $pwd.Drive.Root
    while($scourRoot -ne $driveRoot)
    {
        if(Test-Path "$scourRoot\__scour")
        {
            break
        }

        $scourRoot = (Resolve-Path "$scourRoot\..").Path
    }

    ## If we couldn't find the index, throw an error.
    if(-not (Test-Path "$scourRoot\__scour"))
    {
        $PSCmdlet.ThrowTerminatingError(
            (New-Object System.Management.Automation.ErrorRecord `
                "Scour has not yet analyzed the current directory or any of its parents. To create a Scour index, run Initialize-ScourIndex.",
                "NoIndexForCurrentDirectory",
                "OpenError",
                $pwd))
    }

    ## Retain the searchers and index directories in the module scope so that we don't
    ## have to re-open the indexes for every search.
    if(-not $SCRIPT:searchers)
    {
        $SCRIPT:searchers = @{}
        $SCRIPT:indexDirectories = @{}
    }

    ## If we haven't created the searcher for this location yet, create it now.
    if(-not $searchers.ContainsKey($scourRoot))
    {
        Write-Verbose "Getting new searcher"
        $indexDirectory = [Lucene.Net.Store.FSDirectory]::Open("$scourRoot\__scour")
        $searchers[$scourRoot] = New-Object Lucene.Net.Search.IndexSearcher ([Lucene.Net.Index.IndexReader]::Open($indexDirectory, $true))
        $indexDirectories[$scourRoot] = $indexDirectory
    }

    ## Parse the user's query
    $searcher = $searchers[$scourRoot]
    $analyzer = New-Object Lucene.Net.Analysis.Standard.StandardAnalyzer "LUCENE_CURRENT"
    $parser = New-Object Lucene.Net.QueryParsers.QueryParser "LUCENE_CURRENT","content",$analyzer
    $queryObject = $parser.Parse($Query)

    ## Collect the search results
    $collector = [Lucene.Net.Search.TopScoreDocCollector]::Create($searcher.MaxDoc, $true)
    $searcher.Search($queryObject, $collector)

    ## Go through the search results
    $collector.TopDocs().ScoreDocs | Foreach-Object Doc | Get-Unique | Foreach-Object {
        $indexPath = $searcher.Doc($_).Get("path")
        $indexPath = Join-Path $ScourRoot $indexPath 

        if($indexPath.StartsWith($pwd.Path))
        {
            if($indexPath -like $Path)
            {
                if(-not $RegularExpression)
                {
                    Get-Item -LiteralPath $indexPath
                }
                else {
                    Select-String -LiteralPath $indexPath -Pattern $RegularExpression    
                }
            }
        }
    }
}