forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.bat
266 lines (238 loc) · 6.63 KB
/
start.bat
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
@CALL :CLEANUP
@REM preserve script for help
@SET "_SCRIPT=%~0"
@REM get source path
@PUSHD "%~dp0\.."
@SET "_SRC=%CD%"
@POPD
@REM parse args
:ARGS_LOOP
@IF "%~1"=="" @GOTO :ARGS_END
@REM convert to uppercase
@FOR /F "usebackq delims=" %%I IN (`powershell.exe "'%~1'.toUpper()"`) DO @SET "_ARG=%%~I"
@IF "%_ARG%"=="/P" (
@SET "_PYTHON=%~2"
@SHIFT
@SHIFT
@GOTO :ARGS_LOOP
)
@IF "%_ARG%"=="/U" (
@SET "_UPDATE=0"
@SHIFT
@GOTO :ARGS_LOOP
)
@IF "%_ARG%"=="/D" (
@SET "_DEVENV=%~2"
@SHIFT
@SHIFT
@GOTO :ARGS_LOOP
)
@IF "%_ARG%"=="/N" (
@SET "_DRYRUN=0"
@SHIFT
@GOTO :ARGS_LOOP
)
@IF "%_ARG%"=="/?" (
@ECHO Usage: %_SCRIPT% [options]
@ECHO.
@ECHO Options:
@ECHO /P VERSION Python version for the env to activate. ^(default: 3.8^)
@ECHO /U Force update packages. ^(default: update every 24 hours^)
@ECHO /D PATH Path to base env install, can also be defined in ~\.condarc.
@ECHO Path is appended with Windows. ^(default: devenv^)
@ECHO /N Display env to activate. ^(default: false^)
@ECHO. /? Display this.
@EXIT /B 0
)
@ECHO Error: unknown option %~1 1>&2
@EXIT /B 1
:ARGS_END
@REM fallback to default values
@IF "%_PYTHON%"=="" @SET "_PYTHON=3.8"
@IF "%_UPDATE%"=="" @SET "_UPDATE=1"
@IF "%_DRYRUN%"=="" @SET "_DRYRUN=1"
@REM read devenv from ~\.condarc
@IF "%_DEVENV%"=="" @CALL :CONDARC
@REM fallback to devenv in source default
@IF "%_DEVENV%"=="" @SET "_DEVENV=%_SRC%\devenv"
@REM include OS
@SET "_DEVENV=%_DEVENV%\Windows"
@REM ensure exists
@IF %_DRYRUN%==1 @IF NOT EXIST "%_DEVENV%" @MKDIR "%_DEVENV%"
@REM other environment variables
@SET "_NAME=devenv-%_PYTHON%-c"
@SET "_ENV=%_DEVENV%\envs\%_NAME%"
@SET "_UPDATED=%_ENV%\.devenv-updated"
@SET "_BASEEXE=%_DEVENV%\Scripts\conda.exe"
@SET "_ENVEXE=%_ENV%\Scripts\conda.exe"
@SET "_PYTHONEXE=%_ENV%\python.exe"
@SET "_CONDABAT=%_ENV%\condabin\conda.bat"
@REM dry-run printout
@IF %_DRYRUN%==0 @GOTO :DRYRUN
@REM deactivate any prior envs
@IF "%CONDA_SHLVL%"=="" @GOTO DEACTIVATED
@IF %CONDA_SHLVL%==0 @GOTO DEACTIVATED
@ECHO Deactivating %CONDA_SHLVL% environment(s)...
:DEACTIVATING
@IF "%CONDA_SHLVL%"=="0" @GOTO DEACTIVATED
@CALL conda deactivate
@IF NOT %ErrorLevel%==0 (
@ECHO Error: failed to deactivate environment^(s^) 1>&2
@EXIT /B 1
)
@GOTO DEACTIVATING
:DEACTIVATED
@REM does miniconda install exist?
@IF EXIST "%_DEVENV%\conda-meta\history" @GOTO INSTALLED
@REM downloading miniconda
@IF EXIST "%_DEVENV%\miniconda.exe" @GOTO DOWNLOADED
@ECHO Downloading miniconda...
@powershell.exe "Invoke-WebRequest -Uri 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe' -OutFile '%_DEVENV%\miniconda.exe' | Out-Null"
@IF NOT %ErrorLevel%==0 (
@ECHO Error: failed to download miniconda 1>&2
@EXIT /B 1
)
:DOWNLOADED
@REM installing miniconda
@ECHO Installing development environment...
@START /wait "" "%_DEVENV%\miniconda.exe" /InstallationType=JustMe /RegisterPython=0 /AddToPath=0 /S /D=%_DEVENV% > NUL
@IF NOT %ErrorLevel%==0 (
@ECHO Error: failed to install development environment 1>&2
@EXIT /B 1
)
@REM Windows doesn't ship with git so ensure installed into base otherwise auxlib will act up
@CALL :CONDA install -yq --name base defaults::git > NUL
:INSTALLED
@REM create empty env if it doesn't exist
@IF EXIST "%_ENV%" @GOTO ENVEXISTS
@ECHO Creating %_NAME%...
@CALL :CONDA create -yq --prefix "%_ENV%" > NUL
@IF NOT %ErrorLevel%==0 (
@ECHO Error: failed to create %_NAME% 1>&2
@EXIT /B 1
)
:ENVEXISTS
@REM check if explicitly updating or if 24 hrs since last update
@CALL :UPDATING
@IF NOT %ErrorLevel%==0 @GOTO UPTODATE
@ECHO Updating %_NAME%...
@CALL :CONDA update -yq --all > NUL
@IF NOT %ErrorLevel%==0 (
@ECHO Error: failed to update development environment 1>&2
@EXIT /B 1
)
@CALL :CONDA install ^
-yq ^
--prefix "%_ENV%" ^
--override-channels ^
-c defaults ^
--file "%_SRC%\tests\requirements.txt" ^
pywin32 ^
"python=%_PYTHON%" > NUL
@IF NOT %ErrorLevel%==0 (
@ECHO Error: failed to update %_NAME% 1>&2
@EXIT /B 1
)
@REM update timestamp
@IF EXIST "%_UPDATED%" @DEL "%_UPDATED%"
@ECHO > "%_UPDATED%"
:UPTODATE
@REM initialize conda command
@ECHO Initializing shell integration...
@IF "%CONDA_AUTO_ACTIVATE_BASE%"=="" (
@SET "_AUTOBASE=undefined"
) ELSE (
@SET "_AUTOBASE=%CONDA_AUTO_ACTIVATE_BASE%"
)
@SET "CONDA_AUTO_ACTIVATE_BASE=false"
@CALL :CONDA init --dev cmd.exe > NUL
@CALL dev-init.bat > NUL
@IF "%_AUTOBASE%"=="undefined" (
@SET CONDA_AUTO_ACTIVATE_BASE=
) ELSE (
@SET "CONDA_AUTO_ACTIVATE_BASE=%_AUTOBASE%"
)
@IF NOT %ErrorLevel%==0 (
@ECHO Error: failed to initialize shell integration 1>&2
@EXIT /B 1
)
@DEL /Q dev-init.bat
@REM activate env
@ECHO Activating %_NAME%...
@CALL conda activate "%_ENV%" > NUL
@IF NOT %ErrorLevel%==0 (
@ECHO Error: failed to activate %_NAME% 1>&2
@EXIT /B 1
)
@SET "CONDA_EXE=%_ENVEXE%"
@SET "CONDA_PYTHON_EXE=%_PYTHONEXE%"
@SET "CONDA_BAT=%_CONDABAT%"
@DOSKEY conda="%CONDA_BAT%" $*
:CLEANUP
@SET _ARG=
@SET _AUTOBASE=
@SET _BASEEXE=
@SET _CONDABAT=
@SET _DEVENV=
@SET _DRYRUN=
@SET _ENV=
@SET _ENVEXE=
@SET _NAME=
@SET _PATH=
@SET _PYTHON=
@SET _PYTHONEXE=
@SET _SCRIPT=
@SET _SRC=
@SET _UPDATE=
@SET _UPDATED=
@GOTO :EOF
:CONDA *args
@REM include OpenSSL & git on %PATH%
@SET "_PATH=%PATH%"
@SET "PATH=%_DEVENV%\Library\bin;%PATH%"
@CALL "%_BASEEXE%" %*
@IF NOT %ErrorLevel%==0 @EXIT /B %ErrorLevel%
@REM restore %PATH%
@SET "PATH=%_PATH%"
@SET _PATH=
@GOTO :EOF
:CONDARC
@REM read devenv from ~\.condarc
@REM check if ~\.condarc exists
@IF NOT EXIST "%USERPROFILE%\.condarc" @EXIT /B 2
@REM check if devenv key is defined
@FINDSTR /R /C:"^devenv:" "%USERPROFILE%\.condarc" > NUL
@IF NOT %ErrorLevel%==0 @EXIT /B 1
@REM read devenv key
@FOR /F "usebackq delims=" %%I IN (`powershell.exe "(Select-String -Path '~\.condarc' -Pattern '^devenv:\s*(.+)' | Select-Object -Last 1).Matches.Groups[1].Value -replace '^~',""$Env:UserProfile"""`) DO @SET "_DEVENV=%%~fI"
@GOTO :EOF
:UPDATING
@REM check if explicitly updating or if 24 hrs since last update
@IF %_UPDATE%==0 @EXIT /B 0
@IF NOT EXIST "%_UPDATED%" @EXIT /B 0
@powershell.exe "Exit (Get-Item '"%_UPDATED%"').LastWriteTime -ge (Get-Date).AddHours(-24)"
@EXIT /B %ErrorLevel%
:DRYRUN
@REM dry-run printout
@ECHO Python: %_PYTHON%
@CALL :UPDATING
@IF %ErrorLevel%==0 (
@ECHO Updating: [yes]
) ELSE (
@ECHO Updating: [no]
)
@IF EXIST "%_DEVENV%" (
@ECHO Devenv: %_DEVENV% [exists]
) ELSE (
@ECHO Devenv: %_DEVENV% [pending]
)
@ECHO.
@ECHO Name: %_NAME%
@IF EXIST "%_ENV%" (
@ECHO Path: %_ENV% [exists]
) ELSE (
@ECHO Path: %_ENV% [pending]
)
@ECHO.
@ECHO Source: %_SRC%
@EXIT /B 0