install.ps1 6.4 KB
Newer Older
1
param ([Switch] $Development)
2
3
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

4

5
6
7
$install_node = $true
$install_yarn = $true

8
if ([Environment]::Is64BitOperatingSystem) {
9
10
    $OS_VERSION = 'win64'
}
11
else {
12
13
    $OS_VERSION = 'win32'
}
14
# nodejs
15
$nodeUrl = "https://aka.ms/nni/nodejs-download/" + $OS_VERSION
16
17
18
19
$yarnUrl = "https://yarnpkg.com/latest.tar.gz"
$unzipNodeDir = "node-v*"
$unzipYarnDir = "yarn-v*"

20
$NNI_DEPENDENCY_FOLDER = [System.IO.Path]::GetTempPath() + $env:USERNAME
21
22

$WHICH_PYTHON = where.exe python
23
if ($WHICH_PYTHON -eq $null) {
24
25
    throw "Can not find python"
}
26
else {
27
    $pyVersion = & python -V 2>&1
28
29
    $pyVersion = ([string]$pyVersion).substring(7, 3)
    if ([double]$pyVersion -lt 3.5) {
30
31
32
33
34
        throw "python version should >= 3.5"
    }
}

$WHICH_PIP = where.exe pip
35
if ($WHICH_PIP -eq $null) {
36
37
38
39
    throw "Can not find pip"
}

$env:PYTHONIOENCODING = "UTF-8"
40
if ($env:VIRTUAL_ENV) {
41
42
43
44
    $NNI_PYTHON3 = $env:VIRTUAL_ENV + "\Scripts"
    $NNI_PKG_FOLDER = $env:VIRTUAL_ENV + "\nni"
    $NNI_PYTHON_SCRIPTS = $NNI_PYTHON3
}
45
else {
46
47
    $NNI_PYTHON3 = $(python -c 'import site; from pathlib import Path; print(Path(site.getsitepackages()[0]))')
    $NNI_PKG_FOLDER = $NNI_PYTHON3 + "\nni"
48
    $NNI_PYTHON_SCRIPTS = $NNI_PYTHON3 + "\Scripts"
49
50
}

51
$PIP_INSTALL = """$NNI_PYTHON3\python"" -m pip install "
52

53
if (!(Test-Path $NNI_DEPENDENCY_FOLDER)) {
54
55
    New-Item $NNI_DEPENDENCY_FOLDER -ItemType Directory
}
56
57
58
59
60
$NNI_NODE_ZIP = $NNI_DEPENDENCY_FOLDER + "\nni-node.zip"
$NNI_NODE_FOLDER = $NNI_DEPENDENCY_FOLDER + "\nni-node"
$NNI_YARN_TARBALL = $NNI_DEPENDENCY_FOLDER + "\nni-yarn.tar.gz"
$NNI_YARN_FOLDER = $NNI_DEPENDENCY_FOLDER + "\nni-yarn"
$NNI_YARN = $NNI_YARN_FOLDER + "\bin\yarn"
61
62
63
64
65

## Version number
$NNI_VERSION_VALUE = $(git describe --tags)
$NNI_VERSION_TEMPLATE = "999.0.0-developing"

66
if (!(Test-Path $NNI_NODE_ZIP)) {
67
68
69
70
    Write-Host "Downloading Node..."
    (New-Object Net.WebClient).DownloadFile($nodeUrl, $NNI_NODE_ZIP)
}

71
if (!(Test-Path $NNI_YARN_TARBALL)) {
72
73
74
75
76
77
78
    Write-Host "Downloading Yarn..."
    (New-Object Net.WebClient).DownloadFile($yarnUrl, $NNI_YARN_TARBALL)
}

$NNI_YARN_TARBALL = $NNI_YARN_TARBALL -split '\\' -join '\\'
$NNI_DEPENDENCY_FOLDER = $NNI_DEPENDENCY_FOLDER -split '\\' -join '\\'
$SCRIPT_PATH = $NNI_DEPENDENCY_FOLDER + '\extract.py'
79
80
81
$SCRIPT = "import tarfile",
        ("tar = tarfile.open(""{0}"")" -f $NNI_YARN_TARBALL),
        ("tar.extractall(""{0}"")" -f $NNI_DEPENDENCY_FOLDER),
82
83
84
85
        "tar.close()"
[System.IO.File]::WriteAllLines($SCRIPT_PATH, $SCRIPT)

Add-Type -AssemblyName System.IO.Compression.FileSystem
86
function Unzip {
87
88
89
90
91
    param([string]$zipfile, [string]$outpath)
    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
if ($install_node) {
    ### nodejs install
92
    if (!(Test-Path $NNI_NODE_FOLDER)) {
93
94
95
96
97
        Unzip $NNI_NODE_ZIP $NNI_DEPENDENCY_FOLDER
        $unzipNodeDir = Get-ChildItem "$NNI_DEPENDENCY_FOLDER\$unzipNodeDir"
        Rename-Item $unzipNodeDir "nni-node"
    }
    Copy-Item "$NNI_NODE_FOLDER\node.exe" $NNI_PYTHON_SCRIPTS -Recurse -Force
98
99
100
}

if ($install_yarn) {
101
    ### yarn install
102
    if (!(Test-Path $NNI_YARN_FOLDER)) {
103
104
105
106
107
108
109
110
111
        cmd /C """$NNI_PYTHON3\python""" $SCRIPT_PATH
        $unzipYarnDir = Get-ChildItem "$NNI_DEPENDENCY_FOLDER\$unzipYarnDir"
        Rename-Item $unzipYarnDir "nni-yarn"
    }
}

## install-python-modules:
### Installing Python SDK
(Get-Content setup.py).replace($NNI_VERSION_TEMPLATE, $NNI_VERSION_VALUE) | Set-Content setup.py
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

if ($Development) {
    $PYTHON_BUILD = "build"
    if (Test-Path $PYTHON_BUILD) { 
        # To compat with file and links.
        cmd /c rmdir /s /q $PYTHON_BUILD
    }
    New-Item $PYTHON_BUILD -ItemType Directory
    New-Item -ItemType Junction -Path "$($PYTHON_BUILD)\nni" -Target "src\sdk\pynni\nni"
    New-Item -ItemType Junction -Path "$($PYTHON_BUILD)\nnicli" -Target "src\sdk\pycli\nnicli"
    New-Item -ItemType Junction -Path "$($PYTHON_BUILD)\nni_annotation" -Target "tools\nni_annotation"
    New-Item -ItemType Junction -Path "$($PYTHON_BUILD)\nni_cmd" -Target "tools\nni_cmd"
    New-Item -ItemType Junction -Path "$($PYTHON_BUILD)\nni_trial_tool" -Target "tools\nni_trial_tool"
    New-Item -ItemType Junction -Path "$($PYTHON_BUILD)\nni_gpu_tool" -Target "tools\nni_gpu_tool"

    Copy-Item setup.py $PYTHON_BUILD
    Copy-Item README.md $PYTHON_BUILD

    Push-Location build
    #update folders in setup file
    (Get-Content setup.py).replace("src/sdk/pynni/", "") | Set-Content setup.py
    (Get-Content setup.py).replace("src/sdk/pycli/", "") | Set-Content setup.py
    (Get-Content setup.py).replace("src/sdk/pynni", ".") | Set-Content setup.py
    (Get-Content setup.py).replace("tools/", "") | Set-Content setup.py
    # install current folder.
    cmd /c $PIP_INSTALL -e .
    Pop-Location
}
else {
    cmd /c $PIP_INSTALL .
}
143
144

# Building NNI Manager
145
$env:PATH = $NNI_PYTHON_SCRIPTS + ';' + $env:PATH
146
147
148
149
150
cd src\nni_manager
cmd /c $NNI_YARN
cmd /c $NNI_YARN build
Copy-Item config -Destination .\dist\ -Recurse -Force
# Building WebUI
151
# office-ui-fabric-react need longer time. the 180000 is in ms, mean 180 seconds, longer than default 30 seconds.
152
cd ..\webui
153
cmd /c $NNI_YARN --network-timeout 180000
154
cmd /c $NNI_YARN build
SparkSnail's avatar
SparkSnail committed
155
156
# Building NasUI
cd ..\nasui
157
cmd /c $NNI_YARN --network-timeout 180000
SparkSnail's avatar
SparkSnail committed
158
cmd /c $NNI_YARN build
159
160
161
162

cd ..\..

## install-node-modules
163
164
165
166
167
168
169
170
171
172
173
174
175
if (Test-Path $NNI_PKG_FOLDER) {
    # it needs to remove the whole folder for following copy.
    cmd /c rmdir /s /q $NNI_PKG_FOLDER
}

$NNI_PKG_FOLDER_STATIC = $NNI_PKG_FOLDER + "\static"
$NASUI_PKG_FOLDER = $NNI_PKG_FOLDER + "\nasui"

if ($Development) {
    New-Item -ItemType Junction -Path $($NNI_PKG_FOLDER) -Target "src\nni_manager\dist"
    New-Item -ItemType Junction -Path "$($NNI_PKG_FOLDER)\node_modules" -Target "src\nni_manager\node_modules"
    New-Item -ItemType Junction -Path $($NNI_PKG_FOLDER_STATIC) -Target "src\webui\build"
    New-Item -ItemType Junction -Path $($NASUI_PKG_FOLDER) -Target "src\nasui\build"
176
}
177
178
179
180
181
182
else {
    Copy-Item "src\nni_manager\dist" $NNI_PKG_FOLDER -Recurse
    Copy-Item "src\webui\build" $NNI_PKG_FOLDER_STATIC -Recurse
    Copy-Item "src\nasui\build" $NASUI_PKG_FOLDER -Recurse
}

183
184
185
Copy-Item "src\nni_manager\package.json" $NNI_PKG_FOLDER
$PKG_JSON = $NNI_PKG_FOLDER + "\package.json"
(Get-Content $PKG_JSON).replace($NNI_VERSION_TEMPLATE, $NNI_VERSION_VALUE) | Set-Content $PKG_JSON
SparkSnail's avatar
SparkSnail committed
186
Copy-Item "src\nasui\server.js" $NASUI_PKG_FOLDER -Recurse
187
188
189
190

if (!$Development) {
    cmd /c $NNI_YARN --prod --cwd $NNI_PKG_FOLDER
}