standardise the powershell FileUtils (#34969)

This commit is contained in:
Jordan Borean
2018-01-17 14:16:34 +10:00
committed by GitHub
parent 944ae47701
commit 6f9f337a67
8 changed files with 86 additions and 73 deletions

View File

@@ -9,33 +9,39 @@ as possible. They work by using Get-ChildItem with a filter and return the
result from that.
#>
Function Test-FilePath($path) {
# Basic replacement for Test-Path that tests if the file/folder exists at the path
$directory = Split-Path -Path $path -Parent
$filename = Split-Path -Path $path -Leaf
$file = Get-ChildItem -Path $directory -Filter $filename -Force -ErrorAction SilentlyContinue
if ($file -ne $null) {
if ($file -is [Array] -and $file.Count -gt 1) {
throw "found multiple files at path '$path', make sure no wildcards are set in the path"
}
return $true
} else {
Function Test-AnsiblePath {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][string]$Path
)
# Replacement for Test-Path
try {
$file_attributes = [System.IO.File]::GetAttributes($Path)
} catch [System.IO.FileNotFoundException] {
return $false
}
}
Function Get-FileItem($path) {
# Replacement for Get-Item
$directory = Split-Path -Path $path -Parent
$filename = Split-Path -Path $path -Leaf
$file = Get-ChildItem -Path $directory -Filter $filename -Force -ErrorAction SilentlyContinue
if ($file -is [Array] -and $file.Count -gt 1) {
throw "found multiple files at path '$path', make sure no wildcards are set in the path"
if ([Int32]$file_attributes -eq -1) {
return $false
} else {
return $true
}
return $file
}
Export-ModuleMember -Function Test-FilePath, Get-FileItem
Function Get-AnsibleItem {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][string]$Path
)
# Replacement for Get-Item
$file_attributes = [System.IO.File]::GetAttributes($Path)
if ([Int32]$file_attributes -eq -1) {
throw New-Object -TypeName System.Management.Automation.ItemNotFoundException -ArgumentList "Cannot find path '$Path' because it does not exist."
} elseif ($file_attributes.HasFlag([System.IO.FileAttributes]::Directory)) {
return New-Object -TypeName System.IO.DirectoryInfo -ArgumentList $Path
} else {
return New-Object -TypeName System.IO.FileInfo -ArgumentList $Path
}
}
Export-ModuleMember -Function Test-AnsiblePath, Get-AnsibleItem

View File

@@ -28,11 +28,11 @@ $result = @{
cmd = $raw_command_line
}
if ($creates -and $(Test-FilePath -path $creates)) {
if ($creates -and $(Test-AnsiblePath -Path $creates)) {
Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
}
if ($removes -and -not $(Test-FilePath -path $removes)) {
if ($removes -and -not $(Test-AnsiblePath -Path $removes)) {
Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
}

View File

@@ -56,11 +56,11 @@ $result = @{
cmd = $raw_command_line
}
if ($creates -and $(Test-FilePath -path $creates)) {
if ($creates -and $(Test-AnsiblePath -Path $creates)) {
Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
}
if ($removes -and -not $(Test-FilePath -path $removes)) {
if ($removes -and -not $(Test-AnsiblePath -Path $removes)) {
Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
}

View File

@@ -34,7 +34,7 @@ if (Get-Member -inputobject $params -name "get_md5") {
Add-DepreactionWarning -obj $result -message "get_md5 has been deprecated along with the md5 return value, use get_checksum=True and checksum_algorithm=md5 instead" -version 2.9
}
$info = Get-FileItem -path $path
$info = Get-AnsibleItem -Path $path -ErrorAction SilentlyContinue
If ($info -ne $null) {
$epoch_date = Get-Date -Date "01/01/1970"
$attributes = @()
@@ -74,7 +74,7 @@ If ($info -ne $null) {
$stat.owner = $info.GetAccessControl().Owner
# values that are set according to the type of file
if ($info.PSIsContainer) {
if ($info.Attributes.HasFlag([System.IO.FileAttributes]::Directory)) {
$stat.isdir = $true
$share_info = Get-WmiObject -Class Win32_Share -Filter "Path='$($stat.path -replace '\\', '\\')'"
if ($share_info -ne $null) {
@@ -82,11 +82,14 @@ If ($info -ne $null) {
$stat.sharename = $share_info.Name
}
$dir_files_sum = Get-ChildItem $stat.path -Recurse | Measure-Object -property length -sum
if ($dir_files_sum -eq $null) {
try {
$size = 0
foreach ($file in $info.EnumerateFiles("*", [System.IO.SearchOption]::AllDirectories)) {
$size += $file.Length
}
$stat.size = $size
} catch {
$stat.size = 0
} else {
$stat.size = $dir_files_sum.Sum
}
} else {
$stat.extension = $info.Extension

View File

@@ -119,7 +119,7 @@ if ($path -eq $null -and $port -eq $null -and $state -eq "drained") {
$complete = $false
while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
$attempts += 1
if (Test-FilePath -path $path) {
if (Test-AnsiblePath -Path $path) {
if ($search_regex -eq $null) {
$complete = $true
break
@@ -150,7 +150,7 @@ if ($path -eq $null -and $port -eq $null -and $state -eq "drained") {
$complete = $false
while (((Get-Date) - $start_time).TotalSeconds -lt $timeout) {
$attempts += 1
if (Test-FilePath -path $path) {
if (Test-AnsiblePath -Path $path) {
if ($search_regex -ne $null) {
$file_contents = Get-Content -Path $path -Raw
if ($file_contents -notmatch $search_regex) {