-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add powershell scripts * Add script to execute matlab command * Make setting PYTHON_EX_PATH optional in execute_matlab_command
- Loading branch information
1 parent
4f43133
commit 2dfbd75
Showing
3 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
. $PSScriptRoot/powershell_helpers.ps1 <# Imports: | ||
Write-And-Invoke, Get-Conda-Env-Dir | ||
#> | ||
|
||
Write-And-Invoke "conda remove --name $env:CONDA_ENV_NAME --all -y" | ||
|
||
# Force remove any remaining files | ||
$CONDA_ENV_DIR = Get-Conda-Env-Dir | ||
try { | ||
Write-And-Invoke "Remove-Item -Force -Recurse -Path $CONDA_ENV_DIR -ErrorAction Stop" | ||
} catch { | ||
Write-Output "Could not remove directory '$CONDA_ENV_DIR'`n$($_.Exception)" | ||
} | ||
|
||
Write-And-Invoke "conda create --name $env:CONDA_ENV_NAME python=3.6 -y" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
. $PSScriptRoot/powershell_helpers.ps1 <# Imports: | ||
Write-And-Invoke, Get-From-Registry, Get-Conda-Env-Dir | ||
#> | ||
|
||
<# | ||
.SYNOPSIS | ||
Optionally sets the PYTHON_EX_PATH environment variable for calling Python from Matlab | ||
(if the CONDA_ENV_DIR env var has been set), then executes a Matlab command | ||
.DESCRIPTION | ||
Optionally, for a specific Conda environment (specified in the CONDA_ENV_DIR environment variable), | ||
determines the Python executable location and uses it to set the PYTHON_EX_PATH environment variable, | ||
this allows Python to be activated from Matlab if required. It then uses the MATLAB_VERSION environment | ||
variable to find the Matlab executable path and runs the command specified in the first argument to | ||
this script. | ||
.PARAMETER command | ||
The MATLAB command to execute | ||
.EXAMPLE | ||
execute_matlab_command.ps1 "setup_and_run_tests" | ||
.NOTES | ||
Required environment variables: | ||
MATLAB_VERSION - Matlab version to use e.g. 2019b | ||
Optional environment variables: | ||
CONDA_ENV_DIR - Name of the conda environment containing the Python executable e.g. py36_pace_integration_2019b | ||
#> | ||
|
||
$ErrorActionPreference = 'Stop' | ||
|
||
$MATLAB_VERSION_MAP = @{ | ||
'2018a' = '9.4'; | ||
'2018b' = '9.5'; | ||
'2019a' = '9.6'; | ||
'2019b' = '9.7'; | ||
'2020a' = '9.8'; | ||
'2020b' = '9.9'; | ||
} | ||
|
||
$matlab_command = $args[0] | ||
|
||
# Get path to Conda environment Python, and set as environment variable to | ||
# be accessed by the Matlab command - if CONDA_ENV_DIR has been set | ||
Try { | ||
$CONDA_ENV_DIR = Get-Conda-Env-Dir | ||
Write-Output "$CONDA_ENV_DIR" | ||
$PYTHON_EX_PATH = "$CONDA_ENV_DIR\python" | ||
Write-And-Invoke "Set-Item -Path Env:PYTHON_EX_PATH -Value $PYTHON_EX_PATH" | ||
} Catch [System.ArgumentNullException] { | ||
Write-Warning ("Couldn't get conda environment dir, maybe CONDA_ENV_DIR wasn't set?") | ||
} | ||
|
||
# Get Matlab root directory from registry, and path to MATLAB exe | ||
$MATLAB_REG = Get-From-Registry "HKEY_LOCAL_MACHINE\SOFTWARE\Mathworks\MATLAB\$($MATLAB_VERSION_MAP[$Env:MATLAB_VERSION])" | ||
$MATLAB_ROOT = ($MATLAB_REG).MATLABROOT | ||
|
||
. $MATLAB_ROOT\bin\matlab.exe -nosplash -nodesktop -wait -batch $matlab_command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
function Write-And-Invoke([string]$command) { | ||
<# | ||
.SYNOPSIS | ||
Write a command to the terminal before executing it. | ||
.DESCRIPTION | ||
Uses `Write-Ouptut` to print the given command then uses `Invoke-Expression` | ||
to execute it. | ||
The command is written to the terminal with a preceeding '+ ' to indicate | ||
that this function printed it, and it's not an output of the given command. | ||
.PARAMETER command | ||
Command to execute. | ||
.EXAMPLE | ||
Write-And-Invoke "Write-Output 'Hello, World!'" | ||
Outputs: | ||
+ Write-Output 'Hello, World!' | ||
Hello, World! | ||
#> | ||
Write-Output "+ $command" | ||
Invoke-Expression "$command" | ||
} | ||
|
||
function Invoke-In-Dir { | ||
<# | ||
.SYNOPSIS | ||
Execute a command in the given directory then return to the original | ||
directory - printing the given command. | ||
.DESCRIPTION | ||
Changes directory before executing the command. It uses a try-finally block | ||
so that the original directory is returned to even if the given command | ||
exits the script. The command is executed using Write-And-Invoke, so it is | ||
written to the terminal with a preceeding '+ ' before being executed. | ||
.PARAMETER directory | ||
The directory to execute the command in. | ||
.PARAMETER command | ||
Command to execute. | ||
.EXAMPLE | ||
cd C:\Users\Public\ | ||
Get-Location | ||
Invoke-In-Dir C:\Users\Public\Documents Get-Location | ||
Get-Location | ||
Outputs: | ||
C:\Users\Public\ | ||
+ Get-Location | ||
C:\Users\Public\Documents | ||
C:\Users\Public\ | ||
#> | ||
param([string]$directory, [string]$command) | ||
Push-Location -Path $directory | ||
try { | ||
Write-And-Invoke "$command" | ||
} | ||
finally { | ||
Pop-Location | ||
} | ||
} | ||
|
||
function Get-From-Registry ([string]$key) { | ||
<# | ||
.SYNOPSIS | ||
Search the Windows registry for a specific key and return its value | ||
.DESCRIPTION | ||
Uses `Get-ItemProperty` to get the key. Uses `Write-Error` if the key can't | ||
be found | ||
.PARAMETER key | ||
The key to use e.g. HKEY_LOCAL_MACHINE\SOFTWARE\Mathworks\MATLAB\9.8 | ||
.EXAMPLE | ||
Get-From-Registry "HKEY_LOCAL_MACHINE\SOFTWARE\Mathworks\MATLAB\9.8" | ||
#> | ||
Try { | ||
$reg = Get-ItemProperty "Registry::$key" -ErrorAction Stop | ||
} Catch [System.Management.Automation.ItemNotFoundException] { | ||
Write-Error ("Couldn't find $key in the Windows registry, ensure the correct software version is " + | ||
"definitely installed and the correct Powershell architecture is being used. A 32-bit " + | ||
"Powershell may not be able to search a 64-bit registry and vice versa`n$($_.Exception)") | ||
} | ||
return $reg | ||
} | ||
|
||
function Get-Conda-Env-Dir () { | ||
<# | ||
.SYNOPSIS | ||
Gets the path of the conda env used for Windows pace-integration tests | ||
.DESCRIPTION | ||
Uses `Get-From-Registry` to get the base dir, then appends the environment | ||
name | ||
.NOTES | ||
Required environment variables: | ||
CONDA_ENV_NAME - Name of the Conda environment e.g. py36_pace_integration_2019b | ||
.EXAMPLE | ||
$CONDA_ENV_DIR = Get-Conda-Env-Dir | ||
#> | ||
if ($null -eq $env:CONDA_ENV_NAME) { | ||
throw (New-Object System.ArgumentNullException("$CONDA_ENV_NAME has not been set")) | ||
} | ||
$conda_reg = Get-From-Registry "HKEY_LOCAL_MACHINE\SOFTWARE\Python\ContinuumAnalytics\Anaconda39-64\InstallPath" | ||
$conda_dir = "$(($conda_reg).'(default)')\envs\$env:CONDA_ENV_NAME" | ||
return $conda_dir | ||
} |