Skip to content

Instantly share code, notes, and snippets.

@tats-u
Last active January 12, 2020 14:43
Show Gist options
  • Save tats-u/4e320ae71920a3ceb70ed3283250559e to your computer and use it in GitHub Desktop.
Save tats-u/4e320ae71920a3ceb70ed3283250559e to your computer and use it in GitHub Desktop.
Download TeXLive ISO with Aria2 + PowerShell
<#
This script is for Japanese TeX users.
TeXLive ISO高速ダウンローダー
後日GitHub本体の方でリポジトリ化する予定
Copyright © 2020 Tatsunori Uchino
ライセンス: MIT
#>
function Get-SHA512ISOHash([string]$Repo, [string]$ISOPath) {
return [Regex]::Match([Text.Encoding]::UTF8.GetString((Invoke-WebRequest "$Repo$ISOPath.sha512").Content), "^[0-9a-fA-F]+").Value
}
if (-not (Get-Command aria2c -ErrorAction Ignore)) {
Write-Error "本スクリプトの実行にはAria2が必要です。インストールし、パスを通してください。"
if ($IsWindows -in ($true, $null)) {
if(Get-Command cinst -ErrorAction Ignore) {
Write-Error "cinst -y aria2"
} else if(Get-Command scoop -ErrorAction Ignore) {
Write-Error "scoop install aria2"
}
} else if ($IsMacOS) {
Write-Error "brew install aria2"
} else if ($IsLinux) {
$Cmd =
if (Get-Command apt -ErrorAction Ignore) { "apt install -y" } else
if (Get-Command dnf -ErrorAction Ignore) { "dnf install -y" } else
if (Get-Command yum -ErrorAction Ignore) { "yum instal -y" } else
if (Get-Command zypper -ErrorAction Ignore) { "zypper in -y" } else
if (Get-Command pacman -ErrorAction Ignore) { "pacman -S" }
if ($Cmd -ne $null) {
Write-Error "$Cmd aria2"
}
}
exit 1
}
$ParentRepo = "http://www.tug.org/texlive/Images/"
$ISOPath = (Invoke-WebRequest $ParentRepo).Links.Href | Where-Object { $_ -match "texlive[0-9]+\.iso$" } | Sort-Object -Descending | Select-Object -f 1
Write-Output "ターゲット: $ISOPath"
$Mirrors = (Invoke-WebRequest "https://texwiki.texjp.org/?TeX Live").Links.Href | ? { $_ -match "https?://.+jp.+/Images/?" }
Write-Output "ミラー合計: $($Mirrors.Length) 個"
# ボトルネック?
# ToDo: PowerShell 7では ForEach-Object -Parallelが使える
$SHA512Lengths = $Mirrors | ForEach-Object {
(Invoke-WebRequest "$_$ISOPath.sha512" -Method Head).headers["Content-Length"]
}
$CheckSHA512Indices = 0..($Mirrors.Length - 1) | Where-Object {
$SHA512Lengths[$_] -le 2048
}
$BaseSHA512 = Get-SHA512ISOHash -Repo $Mirrors[$CheckSHA512Indices[0]] -ISOPath $ISOPath
Write-Output "SHA512: $BaseSHA512"
$SameSHA512List = $CheckSHA512Indices | Where-Object { ($_ -eq 0) -or ((Get-SHA512ISOHash -Repo $Mirrors[$_] -ISOPath $ISOPath) -eq $BaseSHA512) }
$VerifiedMirrors = $Mirrors | Select-Object -Index $SameSHA512List
Write-Output "使用ミラー: $($VerifiedMirrors.Length) 個"
# ToDo: -x・-sオプションを指定
$Aria2Args = , "--checksum=sha-512=$BaseSHA512" + ($VerifiedMirrors | ForEach-Object { "$_$ISOPath" })
aria2c $Aria2Args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment