Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
nni
Commits
c0385c57
Unverified
Commit
c0385c57
authored
Aug 20, 2021
by
liuzhe-lz
Committed by
GitHub
Aug 20, 2021
Browse files
Escape environment variable PATH for Windows scripts (#3898)
parent
f9512aa5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
2 deletions
+67
-2
ts/nni_manager/common/shellUtils.ts
ts/nni_manager/common/shellUtils.ts
+63
-0
ts/nni_manager/training_service/local/localTrainingService.ts
...ni_manager/training_service/local/localTrainingService.ts
+2
-1
ts/nni_manager/training_service/reusable/environments/localEnvironmentService.ts
..._service/reusable/environments/localEnvironmentService.ts
+2
-1
No files found.
ts/nni_manager/common/shellUtils.ts
0 → 100644
View file @
c0385c57
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
'
use strict
'
;
// for readability
const
singleQuote
=
"
'
"
;
const
doubleQuote
=
'
"
'
;
const
backtick
=
'
`
'
;
const
backslash
=
'
\\
'
;
const
doubleBacktick
=
'
``
'
;
const
doubleBackslash
=
'
\\\\
'
;
const
newline
=
'
\n
'
;
/**
* Convert a string into quoted and escaped string for shell script.
* This function supports multi-line strings as well.
*
* Examples:
* hello --> 'hello'
* C:\Program Files\$app --> 'C:\Program Files\$app'
* a'b"c$d<ENTER>e\f`g --> $'a\'b"c$d\ne\\f`g' (Linux & macOS)
* a'b"c$d<ENTER>e\f`g --> "a'b`"c`$d`ne\f``g" (Windows)
**/
export
function
shellString
(
str
:
string
):
string
{
return
process
.
platform
===
'
win32
'
?
powershellString
(
str
)
:
bashString
(
str
);
}
/**
* Convert a string into quoted and escaped string for bash script. It supports multi-line strings.
**/
export
function
bashString
(
str
:
string
):
string
{
// for readability of generated script,
// use ansi-c quoting when `str` contains single quote or newline,
// use single quotes otherwise
if
(
str
.
includes
(
singleQuote
)
||
str
.
includes
(
newline
))
{
str
=
str
.
replaceAll
(
backslash
,
doubleBackslash
);
str
=
str
.
replaceAll
(
singleQuote
,
backslash
+
singleQuote
);
str
=
str
.
replaceAll
(
newline
,
backslash
+
'
n
'
);
return
'
$
'
+
singleQuote
+
str
+
singleQuote
;
}
else
{
return
singleQuote
+
str
+
singleQuote
;
}
}
/**
* Convert a string into quoted and escaped string for PowerShell script. It supports multi-line strings.
**/
export
function
powershellString
(
str
:
string
):
string
{
// for readability and robustness of generated script,
// use double quotes for multi-line string,
// use single quotes otherwise
if
(
str
.
includes
(
newline
))
{
str
=
str
.
replaceAll
(
backtick
,
doubleBacktick
);
str
=
str
.
replaceAll
(
doubleQuote
,
backtick
+
doubleQuote
);
str
=
str
.
replaceAll
(
newline
,
backtick
+
'
n
'
);
str
=
str
.
replaceAll
(
'
$
'
,
backtick
+
'
$
'
);
return
doubleQuote
+
str
+
doubleQuote
;
}
else
{
str
=
str
.
replaceAll
(
singleQuote
,
singleQuote
+
singleQuote
);
return
singleQuote
+
str
+
singleQuote
;
}
}
ts/nni_manager/training_service/local/localTrainingService.ts
View file @
c0385c57
...
...
@@ -11,6 +11,7 @@ import * as tkill from 'tree-kill';
import
{
NNIError
,
NNIErrorNames
}
from
'
../../common/errors
'
;
import
{
getExperimentId
}
from
'
../../common/experimentStartupInfo
'
;
import
{
getLogger
,
Logger
}
from
'
../../common/log
'
;
import
{
powershellString
}
from
'
../../common/shellUtils
'
;
import
{
HyperParameters
,
TrainingService
,
TrialJobApplicationForm
,
TrialJobDetail
,
TrialJobMetric
,
TrialJobStatus
...
...
@@ -446,7 +447,7 @@ class LocalTrainingService implements TrainingService {
if
(
process
.
platform
!==
'
win32
'
)
{
runScriptContent
.
push
(
'
#!/bin/bash
'
);
}
else
{
runScriptContent
.
push
(
`$env:PATH=
"
${
process
.
env
.
path
}
"
`
)
runScriptContent
.
push
(
`$env:PATH=
${
powershellString
(
process
.
env
.
path
!
)
}
`
)
}
for
(
const
variable
of
variables
)
{
runScriptContent
.
push
(
setEnvironmentVariable
(
variable
));
...
...
ts/nni_manager/training_service/reusable/environments/localEnvironmentService.ts
View file @
c0385c57
...
...
@@ -10,6 +10,7 @@ import * as component from '../../../common/component';
import
{
getLogger
,
Logger
}
from
'
../../../common/log
'
;
import
{
ExperimentConfig
}
from
'
../../../common/experimentConfig
'
;
import
{
ExperimentStartupInfo
}
from
'
../../../common/experimentStartupInfo
'
;
import
{
powershellString
}
from
'
../../../common/shellUtils
'
;
import
{
EnvironmentInformation
,
EnvironmentService
}
from
'
../environment
'
;
import
{
isAlive
,
getNewLine
}
from
'
../../../common/utils
'
;
import
{
execMkdir
,
runScript
,
getScriptName
,
execCopydir
}
from
'
../../common/util
'
;
...
...
@@ -80,7 +81,7 @@ export class LocalEnvironmentService extends EnvironmentService {
private
getScript
(
environment
:
EnvironmentInformation
):
string
[]
{
const
script
:
string
[]
=
[];
if
(
process
.
platform
===
'
win32
'
)
{
script
.
push
(
`$env:PATH=
"
${
process
.
env
.
path
}
"
`
)
script
.
push
(
`$env:PATH=
${
powershellString
(
process
.
env
.
path
!
)
}
`
)
script
.
push
(
`cd $env:
${
this
.
experimentRootDir
}
`
);
script
.
push
(
`New-Item -ItemType "directory" -Path
${
path
.
join
(
this
.
experimentRootDir
,
'
envs
'
,
environment
.
id
)}
-Force`
);
script
.
push
(
`cd envs\
\$
{environment.id}`
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment