DSCR_PythonPackage.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 |
Enum Ensure{ Present Absent } [DscResource()] Class pip { [DSCProperty(Key)] [String] $Package [DSCProperty()] [Ensure] $Ensure = [Ensure]::Present # [DSCProperty()] # [String] # $PackageFile [DSCProperty()] [String] $Version [DSCProperty()] [String] $PipPath [pip] Get() { $pip = $this.TestPipPath($this.PipPath) if (-not $pip) { throw 'pip.exe is not found!' } $result = [pip]::new() $result.Package = $this.Package $result.PipPath = $pip $listOfPackage = & $pip list --disable-pip-version-check | Select-Object -Skip 2 | ForEach-Object { $t = ($_ -split '\s+'); [PsCustomObject]@{Name = $t[0]; Version = $t[1]} } $targetPackage = $listOfPackage | Where-Object {$_.Name -eq $this.Package} if ($targetPackage.Version) { Write-Verbose ('The package "{0}" found. Version is "{1}"' -f $targetPackage.Name, $targetPackage.Version) $result.Version = $targetPackage.Version } if ($this.Version) { $targetPackage = $targetPackage | Where-Object {$_.Version -eq $this.Version} } if ($targetPackage) { $result.Ensure = [Ensure]::Present } else { $result.Ensure = [Ensure]::Absent } return $result } [bool] Test() { $result = ($this.Ensure -eq $this.Get().Ensure) Write-Verbose ("Test result is $result") return $result } [void] Set() { $pip = $this.TestPipPath($this.PipPath) if (-not $pip) { throw 'pip.exe is not found!' } if ($this.Ensure -eq [Ensure]::Present) { # if ($this.PackageFile) { # $query = $this.PackageFile # } if ($this.Version) { $query = $this.Package + '==' + $this.Version } else { $query = $this.Package } Write-Verbose ('Start package installation : {0}' -f $this.Package) & $pip install --disable-pip-version-check --no-warn-script-location --no-cache-dir --force-reinstall --progress-bar off $query } elseif ($this.Ensure -eq [Ensure]::Absent) { $query = $this.Package Write-Verbose ('Start package uninstallation : {0}' -f $this.Package) & $pip uninstall --disable-pip-version-check -y $query } } Hidden [string] TestPipPath([string]$Path) { if ($Path) { if (-not (Test-Path $Path -PathType Leaf)) { return '' } if ((Split-Path $Path -Leaf) -ne 'pip.exe') { return '' } return (Resolve-Path $Path).Path } else { $Command = Get-Command 'pip' -CommandType Application -ErrorAction SilentlyContinue if (-not $Command) { return '' } return $Command.Source } } } |