Windows ã§ããã«ã¯ãªãã¯ã§ããã°ã©ã ãå®è¡ãããã¨æã£ããã çµäºæã®å¾ æ©å¦çã欲ããã£ãããã¾ãããã
ã¿ã¤ãã«éã PowerShell ã®å©ç¨ãåæã¨ãã¦ãã¾ãã å ·ä½çã«ã¯ PowerShell 7 ã§ãã
åæ©
ãã¦ã
ä½ãã®ããã°ã©ãã³ã°è¨èªã§ã³ã¼ããæ¸ããã¨ãã
ããã§å¾
æ©å¦çãæ¸ãã¦ããããã
ä¾ãã° python ã¨ãã 㨠py main.py
ã¿ããã«ããªãã¨ãããªã訳ã§ã
ããã©ã«ãã bash ç°å¢ãªãããã§ãåé¡ãªããã Windows ã¦ã¼ã¶ã¼ã ã¨ã ããããã·ã§ã«ãç«ã¡ä¸ãã¦ããå®è¡ããæéã¯é¢åã
ã ããã .bat ãªã .ps1 ãªããå©ããã (=ããã«ã¯ãªãã¯ããã) å®è¡ã§ããããã«ãããã
bat ã¯ã³ã¼ããã´ã *1 ãªã®ã§ã PowerShell ã使ãã
å ãã¦ã python ã® rich ã¨ãã使ã£ã¦
from rich.traceback import install
ãªããã使ãã¨æ¥½ã«ã¨ã©ã¼çµäºå¦çãæ¸ãããã
ä¾å¤ãæããã¾ã¾çµäºãããªãã¨ãããªãã®ã§ã
python ä¸ã§ã¨ã©ã¼ãã³ããªã³ã°ãã§ããªãã
(ããã¯å¤åç¥ããªãã ãã§ã§ããã¨ã¯æã)
æ´ã«ãããã«ã¯ãªãã¯ã§èµ·åããå¦çã¯ã ä½ãããªããã°å®è¡å¾ä¸ç¬ã§è½ã¡ã¦ãã¾ãã ãã°ã¯å¿è«ç°å¸¸çµäºã®ã¨ã©ã¼å 容ãè¦ããªãã®ã§ã ãããé²ãããã (ãã°ãã¡ã¤ã«ã«åºãã¦ããããã©ãããããéãã®ãæé)
è¦ä»¶
ã¨ãããã¨ã§ã
- ããã«ã¯ãªãã¯ã§ããã°ã©ã ãå®è¡ãããã
- ããã°ã©ã ãä¾å¤ãåºããã¨ããã¨ã©ã¼ãã³ããªã³ã°ããç°å¸¸çµäºããããã
- ãããå¼ã³åºãå´ã§ãããåãä»ãã¦å¦çããããã
- å¦ççµäºæ (ç¹ã«ã¨ã©ã¼çµäºæ) ã«ããã«çµäºãããªãããã«ãããã
- ä¾ãã°ãã¼æ¼ä¸çãæ©ã«çµäºããããã
ã¹ã¯ãªãã
ãããå®è¡ããããã® ã¤ã³ãã¼ãç¨ PowerShell ã¹ã¯ãªãããæ¸ããã ãã¨ããæ¯åæãåºãã¦æ¸ãç´ãã®ãé¢åãªã®ã§ã ããã«ã¾ã¨ãã¦ããã
util.ps1
ã¤ã³ãã¼ãç¨ PowerShell ã¹ã¯ãªããã¨ãã¦ã util.ps1 ã以ä¸ã®ããã«æ¸ãã
#!/usr/bin/env pwsh # ã¦ã¼ã¶ã¼è¨å®å¤æ° $__COUNT_STOP_OK__ = 5 # æåæã®å¾ æ©æé (ç§) $__COUNT_STOP_NG__ = 180 # 失ææã®å¾ æ©æé (ç§) $__PROMPT_INTERVAL__ = 0.1 # å¾ æ©ä¸ã®ãã¼æ¼ä¸å¾ åéé (ç§) $__LAST_WAIT_TIME__ = 0.2 # å ¨çµäºå®äºæã«ä¸ç¬å¾ æ©ãããç§æ° # å®è¡å¾å¾ ã¡åãå¦ç function waitForKeyPress($ret) { # è¨ç®ç¨å¤æ° $enableProceed = $true # ä¸å®æéãã¨ã« "." ã¨å¬ä¿ãã表示ã®æç¡ã $countPerSec = [math]::Truncate(1.0 / $__PROMPT_INTERVAL__) + 1 $countStop = $__COUNT_STOP_OK__ * $countPerSec # çµäºæã®ã¡ãã»ã¼ã¸åºåãåã³ãå¤æ°ã®è¨å® if ($ret) { Write-Host "-----" Write-Host "Completed." -ForegroundColor "Green" } else { Write-Host "-----" Write-Host "Error." -ForegroundColor "Red" $countStop = $__COUNT_STOP_NG__ * $countPerSec $enableProceed = $false } # ãã¼æ¼ä¸ or ã¿ã¤ã ã¢ã¦ãã¾ã§å¾ æ©ããå¦ç $count = 0 Write-Host 'Press any key.' -ForegroundColor "Red" while ($true) { # ãã¼ãæ¼ä¸ãããçµäº if ([Console]::KeyAvailable) { $keyInfo = [Console]::ReadKey() if ($keyinfo.Key) { Write-Host 'Terminated.' -ForegroundColor "Red" break } } # æå®ããç§æ°ã«éãããçµäºã Start-Sleep($__PROMPT_INTERVAL__ = 0.1 # å¾ æ©ä¸ã®ãã¼æ¼ä¸å¾ åéé (ç§) ) if ($count -ge $countStop) { Write-Host Write-Host "Timeout." -ForegroundColor "Red" break } if ($enableProceed) { # 1ç§ãã¨ã«ããã³ããã表示ã if ($count % $countPerSec -eq 0) { Write-Host "." -ForegroundColor "Red" } } $count += 1 } Start-Sleep($__LAST_WAIT_TIME__) }
ãããã¡ãªå¤æ°ã使ã£ã¦ããã®ã§ã ãã¶ãå ´åã¯å¤æ°åãå¤æ´ãããã¨ã
å¼ã³åºãå´
util.ps1 ãå¼ã³åºãã¨ãã¯ä»¥ä¸ã®ããã«ããã
#!/usr/bin/env pwsh . ./util.ps1 # å®è¡ã³ã¼ã py ./main.py # é©å®å¤æ´ waitForKeyPress $?
å®è¡ã³ã¼ã㯠function ã«å ¥ãã¦ããã (ã¨æã) ãã python ã ã¨ã·ã§ã«ãå¼ãç¶ããªãã ãªã®ã§ã rich çã®è²ä»ãæåãç¡å¹åãããã
å®è¡ã³ã¼ããå¥ã«æ¸ãã¦ããã¦
Invoke-Expression ãªããããæ¹æ³ããããã
å®è¡ã³ã¼ãå´ã®çµäºã³ã¼ããåãåããªãã
ããããã¨ã $?
ã¯å¸¸ã« True ã«ãªã£ã¦ãã¾ãã
ã¨ããããã§ããããé²ãã«ã¯ãå°ã®æã§å®è¡ãããããªãã (ãã¨ããããããæ¹æ³ããããªã)
åä½æ¦è¦
æå
æ£å¸¸çµäºæã¯ã
$countStopOK
ã§è¨å®ãããç§æ°ã ãå¾
æ©ãããã
ãã¼æ¼ä¸æã«çµäºããã
ãã®ã¨ãã1ç§ãã¨ã«å¬ä¿ããããã³ãã (.
) ã表示ãããã
ç°å¸¸çµäºæããã¼æ¼ä¸æã«çµäºãããã
$countStopNG
ã大ããåããã¨ã§ã
ã¨ã©ã¼åºåã確èªããæéãè¨ããã
ãã¡ãããã¼æ¼ä¸ã§çµäºã§ããã
ã¨ã©ã¼ç¢ºèªä¸ã«ã¹ã¯ãã¼ã«ãããã®ãé²ãããã«ã
ããã³ããã¯è¡¨ç¤ºããªãã
åä½æ¦è¦
ç¹çãã¹ãã¨æãããç¹ã«ã¤ãã¦è£è¶³ãã¦ããã
- ãã¼æ¼ä¸ã¯
$promptInterval
ã§æå®ããç§æ°ãã¨ã«ç¢ºèªãããã- ãªã¢ã«ã¿ã¤ã ã«ãã¼å ¥åãåãä»ããã®ã§ã¯ãªãã
- ãªã®ã§ãé©å½ãªå¤ã«è¨å®ããã
- é·ãããã¨ãã¼æ¼ä¸ã«åå¿ããªãã
- çãããã¨å¦çè² è·ããããããã
$countStopNG
ã¯é©å®å¤æ´ãããã¨ã- æ£å¸¸çµäºæã¯çãã¦ãã (ä¸è¨ã§ã¯ 5ç§)ã
- ç°å¸¸çµäºæã¯é·ãã«çãæ®ã£ã¦ãã¦ã»ãã (ä¸è¨ã§ã¯ 3å)ã
- ç¹ã«ããããå¦çã ã¨æ¾ãã£ã±ãªãã«ãã¦ãããã¨ãå¤ãã¨æãã®ã§ã
åå大ããªå¤ã«ããã
- 1æéãªã 3600 ç§ã
- 1æ¥ãªã 86400 ç§ã
ãããã«
ã¡ãªã¿ã« bash ã使ãããªããå¾ åå¦çé¨åã¯ã
$timeoutSec = 10 bash -c "read -n 1 -t $timeoutSec -p 'Press any key:'"
ã£ã¦æ¸ãã¾ãã
ä»ã¾ã§ã®è¦å´ã¯(ry *2
bash å©ç¨ç
#!/usr/bin/env pwsh # ã¦ã¼ã¶ã¼è¨å®å¤æ° $__COUNT_STOP_OK__ = 5 # æåæã®å¾ æ©æé (ç§) $__COUNT_STOP_NG__ = 180 # 失ææã®å¾ æ©æé (ç§) $__LAST_WAIT_TIME__ = 0.2 # å ¨çµäºå®äºæã«ä¸ç¬å¾ æ©ãããç§æ° # å®è¡å¾å¾ ã¡åãå¦ç function waitForKeyPress($ret) { # çµäºæã®ã¡ãã»ã¼ã¸åºåãåã³ãå¤æ°ã®è¨å® if ($ret) { Write-Host "-----" Write-Host "Completed." -ForegroundColor "Green" $timeoutSec = $__COUNT_STOP_OK__ } else { Write-Host "-----" Write-Host "Error." -ForegroundColor "Red" $timeoutSec = $__COUNT_STOP_NG__ } # ãã¼æ¼ä¸ or ã¿ã¤ã ã¢ã¦ãã¾ã§å¾ æ©ããå¦ç bash -c "read -n 1 -t $timeoutSec -p 'Press any key:'" Start-Sleep($__LAST_WAIT_TIME__) }
ã©ãèãã¦ããã£ã¡ã使ãã¹ãã§ã¯?
PowerShell ã ãã§ãããªãã¨ãããªãçç±ããã£ãã¯ããªãã ãã©ãªãã
*1:æ§ããã«è¨ã£ã¦ãã´ãã
*2:å®éã PowerShell ã使ã㦠bash 使ããªãç°å¢ã¯ãã¾ãæ³åã§ããªããã æ¬å½ã«ä½ã®ãããåãããªãã