Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Creates batch file to set up the environment 

4""" 

5from __future__ import print_function 

6 

7import os 

8import sys 

9 

10if sys.version_info[0] == 2: 

11 from codecs import open 

12 

13 

14def create_win_batches(folders, verbose=False, selection=None, fLOG=print, module_list=None): 

15 """ 

16 create batchs for the setup, they will be placed in 

17 *folders["config"]* 

18 

19 @param folders dictionary with the keys *tools*, *config*, *python*, *workspace* 

20 @param verbose verbose 

21 @param fLOG logging function 

22 @param selection list of batchs to create 

23 @param module_list list of python modules to install, to know which script to install or not 

24 @return operations (list of what was done) 

25 

26 .. versionchanged:: 1.1 

27 Add batch file for glue and to run some checkings. 

28 """ 

29 if selection is None: 

30 raise ValueError("selection cannot be None") 

31 if module_list is None: 

32 raise ValueError("module_list cannot be None") 

33 

34 has_jupyter = False 

35 has_spyder = False 

36 has_rss = False 

37 has_glue = False 

38 has_orange = False 

39 has_sqlite_bro = False 

40 for mod in module_list: 

41 if mod.name == "jupyter": 

42 has_jupyter = True 

43 if mod.name == "orange3": 

44 has_orange = True 

45 if mod.name == "spyder": 

46 has_spyder = True 

47 if mod.name == "pyrsslocal": 

48 has_rss = True 

49 if mod.name == "glueviz": 

50 has_glue = True 

51 if mod.name == "sqlite_bro": 

52 has_sqlite_bro = True 

53 

54 list_functions = [create_win_env, 

55 create_win_scite, 

56 create_win_putty, 

57 create_win_sqllitespy, 

58 create_win_python_console, 

59 update_all_packages, 

60 run_checkings, 

61 win_replace_shebang, 

62 win_check_installation, 

63 ] 

64 

65 if has_jupyter: 

66 list_functions.extend([create_win_jupyter_console, 

67 create_win_jupyter_qtconsole, 

68 create_win_jupyter_notebook, 

69 win_install_kernels, 

70 ]) 

71 

72 if has_orange: 

73 list_functions.append(create_win_orange) 

74 

75 if has_spyder: 

76 list_functions.append(create_win_spyder) 

77 

78 if has_rss: 

79 list_functions.append(create_win_rss) 

80 

81 if has_sqlite_bro: 

82 list_functions.append(create_win_sqlite_bro) 

83 

84 if has_glue: 

85 list_functions.append(create_win_glue) 

86 

87 if "r" in selection: 

88 list_functions.append((create_win_r_console, "r")) 

89 list_functions.append((create_win_r_gui, "r")) 

90 

91 if "julia" in selection: 

92 list_functions.append((create_win_julia_console, "julia")) 

93 

94 operations = [] 

95 for func in list_functions: 

96 if isinstance(func, tuple): 

97 func, name = func 

98 else: 

99 name = None 

100 

101 if name is None or name in selection: 

102 op = func(folders) 

103 if verbose: 

104 for o in op: 

105 fLOG("[pymy] " + " ".join(o)) 

106 operations.extend(op) 

107 return operations 

108 

109 

110def create_win_env(folders): 

111 """ 

112 create a batch file to set up the environment 

113 

114 @param folders see @see fn create_win_batches 

115 @return operations (list of what was done) 

116 """ 

117 tools = folders["tools"] 

118 text = ['@echo off', 

119 'set CURRENT=%~dp0', 

120 'set PYTHON_TOOLS=%CURRENT%..\\tools', 

121 'set PYTHON_WINHOME=%CURRENT%..\\python', 

122 'set PYTHON_WINSCRIPTS=%CURRENT%..\\python\\Scripts', 

123 'set WORKSPACE=%CURRENT%..\\workspace', 

124 'set PATH=%PYTHON_WINHOME%;%PYTHON_WINSCRIPTS%;%PATH%'] 

125 if os.path.exists(os.path.join(tools, "R")): 

126 text.append('set R_HOME=%PYTHON_TOOLS%\\R') 

127 text.append('set R_LIBS=%PYTHON_TOOLS%\\R\\library') 

128 if os.path.exists(os.path.join(tools, "Julia")): 

129 text.append('set JULIA_HOME=%PYTHON_TOOLS%\\Julia') 

130 text.append('set JULIA_PKGDIR=%PYTHON_TOOLS%\\Julia\\pkg') 

131 if os.path.exists(os.path.join(tools, "TDM")): 

132 text.append('set PATH=%PATH%;%PYTHON_TOOLS%\\TDM\\bin') 

133 if os.path.exists(os.path.join(tools, "MinGW")): 

134 text.append('set PATH=%PATH%;%PYTHON_TOOLS%\\MinGW\\bin') 

135 if os.path.exists(os.path.join(tools, "Graphviz")): 

136 text.append('set PATH=%PATH%;%PYTHON_TOOLS%\\Graphviz\\bin') 

137 if os.path.exists(os.path.join(tools, "JDK")): 

138 text.append('set PATH=%PATH%;%PYTHON_TOOLS%\\JDK\\bin') 

139 

140 text = "\n".join(text) 

141 name = os.path.join(folders["config"], "env.bat") 

142 with open(name, "w") as f: 

143 f.write(text) 

144 return [('batch', name)] 

145 

146 

147def create_win_jupyter_console(folders): 

148 """ 

149 create a batch file to start jupyter 

150 

151 @param folders see @see fn create_win_batches 

152 @return operations (list of what was done) 

153 """ 

154 text = ['@echo off', 

155 'set CURRENT2=%~dp0', 

156 'call "%CURRENT2%env.bat"', 

157 'set JUPYTERC=%PYTHON_WINSCRIPTS%\\jupyter-console.exe', 

158 '"%JUPYTERC%" console'] 

159 # command jupyter console does not work yet even if the documentation says 

160 # so 

161 

162 text = "\n".join(text) 

163 name = os.path.join(folders["config"], "jupyter_console.bat") 

164 with open(name, "w") as f: 

165 f.write(text) 

166 return [('batch', name)] 

167 

168 

169def create_win_orange(folders): 

170 """ 

171 create a batch file to start orange 

172 

173 @param folders see @see fn create_win_batches 

174 @return operations (list of what was done) 

175 """ 

176 text = ['@echo off', 

177 'set CURRENT2=%~dp0', 

178 'call "%CURRENT2%env.bat"', 

179 'set ORANGE=%PYTHON_WINSCRIPTS%\\orange-canvas.exe', 

180 '"%ORANGE%"'] 

181 # command jupyter console does not work yet even if the documentation says 

182 # so 

183 

184 text = "\n".join(text) 

185 name = os.path.join(folders["config"], "orange_canvas.bat") 

186 with open(name, "w") as f: 

187 f.write(text) 

188 return [('batch', name)] 

189 

190 

191def create_win_jupyter_qtconsole(folders): 

192 """ 

193 create a batch file to start Jupyter QtConsole 

194 

195 @param folders see @see fn create_win_batches 

196 @return operations (list of what was done) 

197 """ 

198 text = ['@echo off', 

199 'set CURRENT2=%~dp0', 

200 'call "%CURRENT2%env.bat"', 

201 'set JUPYTERQTC=%PYTHON_WINSCRIPTS%\\jupyter-qtconsole.exe', 

202 'start "jupyter-qtconsole" /B "%JUPYTERQTC%"'] 

203 

204 text = "\n".join(text) 

205 name = os.path.join(folders["config"], "jupyter_qtconsole.bat") 

206 with open(name, "w") as f: 

207 f.write(text) 

208 return [('batch', name)] 

209 

210 

211def create_win_jupyter_notebook(folders): 

212 """ 

213 create a batch file to start Jupyter Notebook 

214 

215 @param folders see @see fn create_win_batches 

216 @return operations (list of what was done) 

217 """ 

218 text = ['@echo off', 

219 'set CURRENT2=%~dp0', 

220 'call "%CURRENT2%env.bat"', 

221 'set JUPYTERNB=%PYTHON_WINSCRIPTS%\\jupyter-notebook.exe', 

222 '"%JUPYTERNB%" "--notebook-dir=%WORKSPACE%" "--config=%CURRENT2%profile_win_profile\\ipython_kernel_config.py"'] 

223 

224 text = "\n".join(text) 

225 name = os.path.join(folders["config"], "jupyter_notebook.bat") 

226 with open(name, "w") as f: 

227 f.write(text) 

228 return [('batch', name)] 

229 

230 

231def create_win_scite(folders): 

232 """ 

233 create a batch file to start scite 

234 

235 @param folders see @see fn create_win_batches 

236 @return operations (list of what was done) 

237 """ 

238 text = ['@echo off', 

239 'set CURRENT2=%~dp0', 

240 'call "%CURRENT2%env.bat"', 

241 'set SCITE=%PYTHON_TOOLS%\\Scite\\wscite\\scite.exe', 

242 'start "scite" /B "%SCITE%" "%1"'] 

243 

244 text = "\n".join(text) 

245 name = os.path.join(folders["config"], "scite.bat") 

246 with open(name, "w") as f: 

247 f.write(text) 

248 return [('batch', name)] 

249 

250 

251def create_win_putty(folders): 

252 """ 

253 create a batch file to start scite 

254 

255 @param folders see @see fn create_win_batches 

256 @return operations (list of what was done) 

257 """ 

258 text = ['@echo off', 

259 'set CURRENT2=%~dp0', 

260 'call %CURRENT2%env.bat', 

261 'set PUTTY=%PYTHON_TOOLS%\\Putty\\putty.exe', 

262 '"start "putty" /B "%PUTTY%"'] 

263 

264 text = "\n".join(text) 

265 name = os.path.join(folders["config"], "putty.bat") 

266 with open(name, "w") as f: 

267 f.write(text) 

268 return [('batch', name)] 

269 

270 

271def create_win_sqllitespy(folders): 

272 """ 

273 create a batch file to start sqlitespy 

274 

275 @param folders see @see fn create_win_batches 

276 @return operations (list of what was done) 

277 """ 

278 text = ['@echo off', 

279 'set CURRENT2=%~dp0', 

280 'call "%CURRENT2%env.bat"', 

281 'set SQLITESPY=%PYTHON_TOOLS%\\SQLiteSpy\\SQLiteSpy.exe', 

282 'cd "%WORKSPACE%"', 

283 'start "SqliteSpy" /B "%SQLITESPY%"'] 

284 

285 text = "\n".join(text) 

286 name = os.path.join(folders["config"], "sqlitespy.bat") 

287 with open(name, "w") as f: 

288 f.write(text) 

289 return [('batch', name)] 

290 

291 

292def create_win_python_console(folders): 

293 """ 

294 create a batch file to start python 

295 

296 @param folders see @see fn create_win_batches 

297 @return operations (list of what was done) 

298 """ 

299 text = ['@echo off', 

300 'set CURRENT2=%~dp0', 

301 'call "%CURRENT2%env.bat"', 

302 'set PYTHON=%PYTHON_WINHOME%\\python.exe', 

303 '"%PYTHON%"'] 

304 

305 text = "\n".join(text) 

306 name = os.path.join(folders["config"], "python_console.bat") 

307 with open(name, "w") as f: 

308 f.write(text) 

309 return [('batch', name)] 

310 

311 

312def create_win_julia_console(folders): 

313 """ 

314 create a batch file to start julia 

315 

316 @param folders see @see fn create_win_batches 

317 @return operations (list of what was done) 

318 """ 

319 text = ['@echo off', 

320 'set CURRENT2=%~dp0', 

321 'call "%CURRENT2%env.bat"', 

322 'set JULIA=%PYTHON_TOOLS%\\Julia\\bin\\julia.exe', 

323 '"%JULIA%"'] 

324 

325 text = "\n".join(text) 

326 name = os.path.join(folders["config"], "julia_console.bat") 

327 with open(name, "w") as f: 

328 f.write(text) 

329 return [('batch', name)] 

330 

331 

332def create_win_spyder(folders): 

333 """ 

334 create a batch file to start spyder 

335 

336 @param folders see @see fn create_win_batches 

337 @return operations (list of what was done) 

338 

339 .. index:: Spyder, PySide, PyQt, PySide2 

340 

341 This installation uses `PySide2 <https://pypi.python.org/pypi/PySide2/>`_ 

342 instead of `PyQt <https://www.riverbankcomputing.com/software/pyqt/intro>`_. 

343 

344 set QT_API=pyside 

345 """ 

346 text = ['@echo off', 

347 'set CURRENT2=%~dp0', 

348 'call "%CURRENT2%env.bat"', 

349 'set QT_API=pyside', 

350 '"%PYTHON_WINSCRIPTS%\\spyder.bat" "--workdir=%WORKSPACE%"'] 

351 

352 text = "\n".join(text) 

353 name = os.path.join(folders["config"], "spyder.bat") 

354 with open(name, "w") as f: 

355 f.write(text) 

356 return [('batch', name)] 

357 

358 

359def create_win_r_console(folders): 

360 """ 

361 create a batch file to start R 

362 

363 @param folders see @see fn create_win_batches 

364 @return operations (list of what was done) 

365 """ 

366 text = ['@echo off', 

367 'set CURRENT2=%~dp0', 

368 'call "%CURRENT2%env.bat"', 

369 'set REXE=%PYTHON_TOOLS%\\R\\bin\\x64\\R.exe', 

370 '"%REXE%"'] 

371 

372 text = "\n".join(text) 

373 name = os.path.join(folders["config"], "r_console.bat") 

374 with open(name, "w") as f: 

375 f.write(text) 

376 return [('batch', name)] 

377 

378 

379def create_win_r_gui(folders): 

380 """ 

381 create a batch file to start R Gui 

382 

383 @param folders see @see fn create_win_batches 

384 @return operations (list of what was done) 

385 """ 

386 text = ['@echo off', 

387 'set CURRENT2=%~dp0', 

388 'call "%CURRENT2%env.bat"', 

389 'set RGUI=%PYTHON_TOOLS%\\R\\bin\\x64\\Rgui.exe', 

390 'start "RGui" /B "%RGUI%"'] 

391 

392 text = "\n".join(text) 

393 name = os.path.join(folders["config"], "r_gui.bat") 

394 with open(name, "w") as f: 

395 f.write(text) 

396 return [('batch', name)] 

397 

398 

399def win_install_kernels(folders, suffix=""): 

400 """ 

401 create a batch file to install kernels 

402 

403 @param folders see @see fn create_win_batches 

404 @param suffix add a suffix 

405 @return operations (list of what was done) 

406 """ 

407 text = ['@echo off', 

408 'set CURRENT2=%~dp0', 

409 'call "%CURRENT2%env.bat"', 

410 ('"%PYTHON_WINHOME%\\pythonw.exe" -u -c "from pymyinstall.win_installer ' + 

411 'import inno_install_kernels;inno_install_kernels(\'CURRENT2\', \'%1\')"')] 

412 

413 text = "\n".join(text) 

414 name = os.path.join(folders["config"], "add_kernels.bat") 

415 with open(name, "w") as f: 

416 f.write(text) 

417 return [('batch', name)] 

418 

419 

420def win_replace_shebang(folders, suffix=""): 

421 """ 

422 create a batch file to replace the shebang 

423 

424 @param folders see @see fn create_win_batches 

425 @param suffix add a suffix 

426 @return operations (list of what was done) 

427 """ 

428 text = ['@echo off', 

429 'set CURRENT2=%~dp0', 

430 'call "%CURRENT2%env.bat"', 

431 '"%PYTHON_WINHOME%\\pythonw.exe" -u -c "import os;' + 

432 'from pymyinstall.win_installer import win_patch_paths;win_patch_paths(\'PYTHON_WINSCRIPTS\', None)"'] 

433 

434 text = "\n".join(text) 

435 name = os.path.join(folders["config"], "replace_shebang.bat") 

436 with open(name, "w") as f: 

437 f.write(text) 

438 return [('batch', name)] 

439 

440 

441def win_check_installation(folders, suffix=""): 

442 """ 

443 create a batch file to check the installation when well 

444 

445 @param folders see @see fn create_win_batches 

446 @param suffix add a suffix (unused) 

447 @return operations (list of what was done) 

448 """ 

449 text = ['@echo off', 

450 'set CURRENT2=%~dp0', 

451 'call "%CURRENT2%env.bat"', 

452 '"%PYTHON_WINHOME%\\python.exe" -u -c "import sys;from pymyinstall.win_installer import import_every_module;' + 

453 'import_every_module(sys.executable, None, fLOG=print)"'] 

454 

455 text = "\n".join(text) 

456 name = os.path.join(folders["config"], "win_check_installation.bat") 

457 with open(name, "w") as f: 

458 f.write(text) 

459 return [('batch', name)] 

460 

461 

462def create_win_rss(folders, suffix=""): 

463 """ 

464 create a batch file to start RSS reader 

465 

466 @param folders see @see fn create_win_batches 

467 @param suffix add a suffix 

468 @return operations (list of what was done) 

469 """ 

470 text = ['@echo off', 

471 'set CURRENT2=%~dp0', 

472 'call "%CURRENT2%env.bat"', 

473 '"%PYTHON_WINHOME%\\python.exe" -u -c "from pyquickhelper.loghelper import fLOG;' + 

474 'from pyquickhelper.pycode.blog_helper import rss_update_run_server;fLOG(OutputPrint=True);' + 

475 'rss_update_run_server(r\'%CURRENT2%rss_database.db3\', r\'%CURRENT2%rss_list.xml\')"'] 

476 

477 text = "\n".join(text) 

478 name = os.path.join(folders["config"], "run_fetch_rss.bat") 

479 with open(name, "w") as f: 

480 f.write(text) 

481 

482 text = """ 

483 <?xml version="1.0" encoding="UTF-8"?> 

484 <opml version="1.0"> 

485 <head> 

486 <title>teachings subscriptions</title> 

487 </head> 

488 <body> 

489 <outline text="XD blog" title="XD blog" type="rss" 

490 xmlUrl="http://www.xavierdupre.fr/blog/xdbrss.xml" 

491 htmlUrl="http://www.xavierdupre.fr/blog/xd_blog.html" /> 

492 </body> 

493 </opml> 

494 """.replace(' ', "") 

495 

496 rss_name = os.path.join(folders["config"], "rss_list.xml") 

497 with open(rss_name, 'w', encoding="utf8") as f: 

498 f.write(text) 

499 

500 return [('batch', name), ('rss', rss_name)] 

501 

502 

503def create_win_glue(folders, suffix=""): 

504 """ 

505 create a batch file to start Glue 

506 

507 @param folders see @see fn create_win_batches 

508 @param suffix add a suffix 

509 @return operations (list of what was done) 

510 

511 .. versionadded:: 1.1 

512 """ 

513 text = ['@echo off', 

514 'set CURRENT2=%~dp0', 

515 'call "%CURRENT2%env.bat"', 

516 '"%PYTHON_WINSCRIPTS%\\glue.exe" %1'] 

517 

518 text = "\n".join(text) 

519 name = os.path.join(folders["config"], "run_glue.bat") 

520 with open(name, "w") as f: 

521 f.write(text) 

522 return [('batch', name)] 

523 

524 

525def create_win_sqlite_bro(folders, suffix=""): 

526 """ 

527 create a batch file to start SqliteBro 

528 

529 @param folders see @see fn create_win_batches 

530 @param suffix add a suffix 

531 @return operations (list of what was done) 

532 

533 .. versionadded:: 1.1 

534 """ 

535 text = ['@echo off', 

536 'set CURRENT2=%~dp0', 

537 'call "%CURRENT2%env.bat"', 

538 '"%PYTHON_WINSCRIPTS%\\sqlite_bro.exe" %1'] 

539 

540 text = "\n".join(text) 

541 name = os.path.join(folders["config"], "sqlite_bro.bat") 

542 with open(name, "w") as f: 

543 f.write(text) 

544 return [('batch', name)] 

545 

546 

547def update_all_packages(folders, suffix=""): 

548 """ 

549 create a batch file to update all packages 

550 

551 @param folders see @see fn create_win_batches 

552 @param suffix add a suffix 

553 @return operations (list of what was done) 

554 

555 .. versionchanged:: 1.1 

556 Bug fix, update script to import function update_all (fails in 1.2). 

557 """ 

558 text = ['@echo off', 

559 'set CURRENT2=%~dp0', 

560 'call "%CURRENT2%env.bat"', 

561 '"%PYTHON_WINHOME%\\python.exe" -u -c "from pymyinstall.packaged import update_all;' + 

562 'update_all(temp_folder=\'%WORKSPACE%/update_modules\', verbose=True)"'] 

563 

564 text = "\n".join(text) 

565 name = os.path.join(folders["config"], "run_update_all_packages.bat") 

566 with open(name, "w") as f: 

567 f.write(text) 

568 return [('batch', name)] 

569 

570 

571def run_checkings(folders, suffix=""): 

572 """ 

573 create a batch file to update all packages 

574 

575 @param folders see @see fn create_win_batches 

576 @param suffix add a suffix 

577 @return operations (list of what was done) 

578 

579 .. versionadded:: 1.1 

580 """ 

581 text = ['@echo off', 

582 'set CURRENT2=%~dp0', 

583 'call "%CURRENT2%env.bat"', 

584 ('"%PYTHON_WINHOME%\\python.exe" -u -c "from pymyinstall.win_installer import ' + 

585 'distribution_checkings;distribution_checkings(None, None)"')] 

586 

587 text = "\n".join(text) 

588 name = os.path.join(folders["config"], "run_checkings.bat") 

589 with open(name, "w") as f: 

590 f.write(text) 

591 return [('batch', name)]