-
Notifications
You must be signed in to change notification settings - Fork 7
/
P4ImportBulk.ps1
executable file
·384 lines (303 loc) · 10.2 KB
/
P4ImportBulk.ps1
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env pwsh
#
# P4ImportBulk.ps1
#
# Import Bulk (a LOT of files) into P4.
#
# When working with a truly massive number of files, P4 will crash
# and cause you a lot of time trying to figure out where it crashed
# and how to successfully complete the import. I'm guessing it runs
# out of memory?
#
# The purpose of this script is to break the submit up into chunks,
# and submit each chunk individually to avoid crashing the p4 client.
#
# For Usage, see:
#
# P4ImportBulk.ps1 -Help
#
[CmdletBinding()]
param(
[switch]$CreateList,
[switch]$ImportList,
[switch]$NoParallel,
[switch]$DryRun,
[switch]$Help,
[Parameter()]$BatchSize=50000,
[Parameter()]$BucketSize=50,
[Parameter()]$SyncFile=".p4sync.txt",
[Parameter()]$StartLine=1,
[Parameter()]$StopLine=-1,
[Parameter()]$MaxLines=-1
)
# Make sure the powershell version is good, or throw an exception
& $PSScriptRoot/PSVersionCheck.ps1
# Import the P4 helper module
Import-Module -Name $PSScriptRoot/Modules/P4.psm1
$ScriptName = $MyInvocation.MyCommand.Name
$SyncFileItem = Get-Item -Path $SyncFile 2> $null
$ValidUsage = $false
function GetHelpOutput()
{
$FullPathDisplay = $SyncFileItem ? "`"$($SyncFileItem.FullName)`"" : "NO_SUCH_FILE"
return @"
############################################################
##
## Usage for ${ScriptName}:
##
Current SyncFile = "$SyncFile"
Full Path = $FullPathDisplay
# FIRST: CREATE THE IMPORT LIST:
& $ScriptName -CreateList
Create $SyncFile like: "p4 add -f -n ... > $SyncFile"
# THEN: if this is a brand new depot, 'p4 add .p4ignore.txt'
# before you bulk import anything.
# THEN: IMPORT THE IMPORT LIST YOU CREATED:
& $ScriptName -ImportList
Read previously-created $SyncFile and execute multiple
"p4 submit" with reasonably-sized buckets that won't crash p4.
Optional additional flags:
-BatchSize = Max paths to submit to "p4 submit" at once.
50k works for me. Too high, p4 will crash during
the update. Too low, you get tons of import CLs.
-BucketSize = Max number of files to include in one command line
(at some point your OS complains max command line
length exceeded; not much value in bumping this up)
-DryRun = Don't actually do anything, just a test run
-NoParallel = Disable parallel processing in "p4 submit" (SLOW)
(Do not use this option unless your P4 server requires it)
"@
}
if ($Help)
{
return &GetHelpOutput
}
################################################################################
## Create p4sync.txt
################################################################################
if ($CreateList)
{
$args = @("add", "-f", "-n", "...")
Write-Host "EXEC: p4 $($args -join ' ') > $SyncFile"
$process = Start-Process -NoNewWindow -PassThru -FilePath "p4" -ArgumentList $args -RedirectStandardOutput $SyncFile
if (!$process)
{
throw "Failed to start p4 add"
}
# Wait for the process we started to exit
# (NOTE: If it spawns child processes, we do NOT wait for those)
Wait-Process -InputObject $process
$e = $process.ExitCode
if ($e -ne 0)
{
throw "p4 add failed"
}
$ValidUsage = $true
}
################################################################################
## Import p4sync.txt
################################################################################
function AddPaths()
{
[CmdletBinding()]
param(
[System.Collections.ArrayList] $Paths
)
$args = New-Object System.Collections.ArrayList
$args.Add("add")
$args.Add("-f")
# Add each file path as a quoted argument
foreach ($Path in $Paths)
{
$Escaped = $Path -replace '"','`"'
$args.Add("`"$Escaped`"")
}
if ($DryRun)
{
Write-Debug "Skipping add exec due to -DryRun switch"
Write-Host "NOEXEC: p4 $($args -join ' ')"
return $null
}
Write-Host "EXEC: p4 $($args -join ' ')"
$process = Start-Process -NoNewWindow -PassThru -FilePath "p4" -ArgumentList $args
if (!$process)
{
throw "Failed to start p4 add"
}
# Wait for the process we started to exit
# (NOTE: If it spawns child processes, we do NOT wait for those)
Wait-Process -InputObject $process
$e = $process.ExitCode
if ($e -ne 0)
{
throw "p4 add failed"
}
return $process
}
function SubmitPaths()
{
[CmdletBinding()]
param(
[string] $Description
)
# Escape quotes in $Description for argument safety
$args = @("submit", "-d", "`"Bulk Import $($Description -replace '"','`"')`"")
# If Parallel processing is not explicitly disabled, then enable it,
# which makes "p4 submit" execute much faster
if (!$NoParallel)
{
$args += "--parallel=threads=8,batch=32"
}
if ($DryRun)
{
Write-Debug "Skipping submit exec due to -DryRun switch"
Write-Host "NOEXEC: p4 $($args -join ' ')"
return $null
}
Write-Host "EXEC: p4 $($args -join ' ')"
$process = Start-Process -NoNewWindow -PassThru -FilePath "p4" -ArgumentList $args
if (!$process)
{
throw "Failed to start p4 submit"
}
# Wait for the process we started to exit
# (NOTE: If it spawns child processes, we do NOT wait for those)
Wait-Process -InputObject $process
$e = $process.ExitCode
if ($e -ne 0)
{
throw "p4 submit failed"
}
return $process
}
function ImportInBatches
{
begin
{
# It's meaningless to start at line less than 1
if ($StartLine -lt 1)
{
$StartLine = 1
}
$LineNum = 0
$TotalOutputLineNum = 0
$CurrentBatchSize = 0
$CurrentBucketSize = 0
$CurrentBucket = New-Object System.Collections.ArrayList
$SubmitStartLine = $StartLine
# System.IO.StreamReader REQUIRES AN ABSOLUTE PATH TO THE FILE
$file = New-Object System.IO.StreamReader($SyncFileItem.FullName)
}
process
{
if (!$file)
{
throw "Cannot open file: $($SyncFileItem.FullName)"
}
# If we're seeking deep into the file, it might take a while,
# which looks like the program is fucked; show that it's not.
if ($StartLine -gt 1000)
{
Write-Host "Seeking to Line ${StartLine}..."
}
# DISABLE CTRL+C DURING `p4 submit`
[Console]::TreatControlCAsInput = $true
while (($line = $file.readline()) -ne $null)
{
# If user pressed CTRL+C during `p4 submit`, bail out now that it has completed
if ([Console]::KeyAvailable)
{
$key = [Console]::ReadKey($true)
if ($key.key -eq "C" -and $key.modifiers -eq "Control")
{
# Clean up and exit
Write-Error "Exiting due to CTRL+C. To resume where you left off: $ScriptName -ImportList -StartLine $SubmitStartLine"
# User pressed CTRL+C; don't add anymore files
throw "Terminated by CTRL+C"
}
}
$LineNum++
# Don't process this line if we're not yet supposed to start
if ($LineNum -lt $StartLine)
{
continue
}
Write-Debug "${LineNum}:$line"
# If the output says the file should be added, then lets add it
if ($line -imatch '#\d+ - opened for add$')
{
$Path = $line -ireplace '#\d+ - opened for add$',''
# `p4 add -m` writes filenames in an Encoded format, so we need to decode it
# see: https://www.perforce.com/manuals/cmdref/Content/CmdRef/filespecs.html
$DecodedPath =& P4_DecodePath $Path
[void] $CurrentBucket.Add($DecodedPath)
$TotalOutputLineNum++
$CurrentBucketSize++
}
# Stop if we've reached the max line to process
if ($StopLine -ge 1 -and $LineNum -ge $StopLine)
{
break
}
# Stop when we've output enough lines
if ($MaxLines -gt 0 -and $TotalOutputLineNum -ge $MaxLines)
{
break
}
# If the bucket is full, add files
if ($BucketSize -gt 0 -and $CurrentBucketSize -ge $BucketSize)
{
# Add these paths and submit to the depot
$process =& AddPaths -Paths $CurrentBucket
# Update Batch
$CurrentBatchSize += $CurrentBucketSize
# Reset Bucket
$CurrentBucketSize = 0
$CurrentBucket = New-Object System.Collections.ArrayList
}
# Each time the buckets add up to a batch, submit to the server
if ($CurrentBatchSize -ge $BatchSize)
{
$process =& SubmitPaths -Description "${SubmitStartLine}..${LineNum}"
# Reset Batch
$SubmitStartLine = $LineNum + 1
$CurrentBatchSize = 0
}
}
# Consume any remaining bucket contents
if ($CurrentBucketSize -gt 0)
{
$process =& AddPaths -Paths $CurrentBucket
# Update Batch
$CurrentBatchSize += $CurrentBucketSize
}
# Submit any remaining batch contents
if ($CurrentBatchSize -gt 0)
{
$process =& SubmitPaths -Description "${SubmitStartLine}..${LineNum}"
}
}
end
{
# RE-ENABLE CTRL+C AFTER `p4 submit`
[Console]::TreatControlCAsInput = $false
[void] $file.Dispose()
}
}
if ($ImportList)
{
if (!$SyncFileItem -or !$SyncFileItem.Exists)
{
throw "No [$SyncFile] exists; create it with -CreateList"
}
Write-Debug "Reading SyncFile=`"$($SyncFileItem.FullName)`""
ImportInBatches
$ValidUsage = $true
}
################################################################################
## If not $ValidUsage, show Help
################################################################################
if (!$ValidUsage)
{
GetHelpOutput | Write-Warning
}