公開後の更新履歴
(2021-02-04, 02:25) rpms.txtをrpms-non-unique.txtに修正しました。
これは何
Fedoraに日本語入力環境を構築する際に、software.opensuse.orgから必要なmozc, fcitx-mozc, mozc-gui-toolsの3ソフトウェアについて、openSUSEの最新の2バージョン(例:Leap15.2と15.3)から、各々rpmの完全なファイル名をテキストファイルに出力するものです。
何が便利
- 上「これは何」を手作業でやらなくて済む。
- 新しいほうから2バージョンを走査するので、例えば15.3ではまだ3つ揃っていなくても15.2を見ればいい。
- 処理時間が短い。
支援スクリプトの構成
- step-00.py: ディレクトリを走査し、結果をversion-table.csvに保存する。
- step-01.py: 最新バージョン(例、15.3)の一式を走査し、rpms-non-unique.txtに保存する。
- step-02.py: 次新バージョン(例、15.2)の一式を走査し、rpms-non-unique.txtに保存する。
Note:step-00は実行必須。step-01とstep-02はどちらかだけでも処理可能。
注意事項
- x86_64用です。
- rpmのバージョンの一致判定は行っていません。
- wgetする場合は、以下のようになります。今回のスクリプトでは、このrpmファイル名の部分のみをテキストファイルに出力します。また、wgetする際には、ファイル名に加え、中間の階層(例:15.2)に、生成されたディレクトリ名(=openSUSEのバージョン番号)をこの例のように入れる必要があります。
$ https://download.opensuse.org/distribution/leap/15.2/repo/oss/x86_64/fcitx-mozc-2.18.2612.102-lp152.6.9.x86_64.rpm
- step-01とstep-02のコードはほとんど同じで共通化の工夫の余地の多くある状態です。
- Fedora Desktop快適です。lxdeでの軽快さはArchやUbuntuを上回るように感じます。
したがってTODO
- 次回の記事でダウンロードとインストールの処理までの「お手軽化」を図ります。
使い方
$ python3 ./step-00.py
$ python3 ./step-01.py
$ python3 ./step-02.py
$ ls
$ (出力先に移動して。例:cd ./15.2)
$ cat ./rpms-unique.txt
$ (あとはお好みでwgetしてdnfに食べさせるなど。なお下方の「補足的な注意事項」も参照のこと)
$ wget https://download.opensuse.org/distribution/leap/ほにゃ/repo/oss/x86_64/うにゃうにゃ.rpm
コード
step-00.py
## thanks to https://qiita.com/azukumas/items/44bb6b07294e8b6e95aa
## imports
import csv, subprocess
from urllib.request import urlopen
from bs4 import BeautifulSoup
## declare variables
seed_dir = "https://download.opensuse.org/distribution/"
release_name = "leap"
table_sort_postfix = "?C=M;O=D"
target_dir = seed_dir + release_name + "/" + table_sort_postfix
table_file_name = "version-table.csv"
## define target html and parse
html = urlopen(target_dir)
soup = BeautifulSoup(html, "html.parser")
## define table
table = soup.findAll("table")[0]
rows = table.findAll("tr")
## export table to csv
with open(table_file_name, "w", encoding='utf-8') as file:
writer = csv.writer(file)
for row in rows:
csvRow = []
for cell in row.findAll(['td']):
csvRow.append(cell.get_text())
writer.writerow(csvRow)
step-01.py
step-02.pyとの違いは## ser target dirs以下に3箇所あるversion_first / version_secondの部分だけです。
## thanks to https://qiita.com/azukumas/items/44bb6b07294e8b6e95aa
## imports
import csv, subprocess, re
from urllib.request import urlopen
from bs4 import BeautifulSoup
# declare variables
table_file_name = "version-table.csv"
rpm_file_name = "rpms-non-unique.txt"
## import csv to array
with open(table_file_name, "r", encoding="utf-8") as file:
reader = csv.reader(file)
csvRow = []
skip = next(reader) # skip 1st line: null #1
skip = next(reader) # skip 2nd line: null #2
skip = next(reader) # skip 3rd line: header
for row in reader:
csvRow.append(row)
## set version numbers as first and second candidates
version_first_latest = csvRow[0][1]
version_second_latest = csvRow[1][1]
## make dirs
cmd = "mkdir -p " + version_first_latest
proc = subprocess.call(cmd.split())
cmd = "mkdir -p " + version_second_latest
proc = subprocess.call(cmd.split())
## ser target dirs
# view-source:https://download.opensuse.org/distribution/leap/15.2/repo/oss/x86_64/
target_dir = "https://download.opensuse.org/distribution/leap/" \
+ version_first_latest \
+ "repo/oss/x86_64/"
## ## specify target_url and parse
# https://qiita.com/hujuu/items/b0339404b8b0460087f9
html = urlopen(target_dir)
soup = BeautifulSoup(html, "html.parser")
links = soup.find_all("a")
## get links to rpm and write out to a text file
with open(version_first_latest + rpm_file_name, 'w') as f:
for link in links:
item = link.get("href")
if "mozc" in item and not("mirrorlist" in item):
print(item, file = f)
else:
pass
## rpm.txt to rpm-unique.txt
cmd = "sort ./" \
+ version_first_latest \
+ "rpms-non-unique.txt | uniq | grep -e ^mozc- -e ^fcitx-mozc- > ./" \
+ version_first_latest \
+ "rpms-uniquue.txt"
proc = subprocess.call(cmd, shell=True)
## end of this script
step-02.py
step-01.pyとの違いは## ser target dirs以下に3箇所あるversion_first / version_secondの部分だけです。
## thanks to https://qiita.com/azukumas/items/44bb6b07294e8b6e95aa
## imports
import csv, subprocess, re
from urllib.request import urlopen
from bs4 import BeautifulSoup
# declare variables
table_file_name = "version-table.csv"
rpm_file_name = "rpms-non-unique.txt"
## import csv to array
with open(table_file_name, "r", encoding="utf-8") as file:
reader = csv.reader(file)
csvRow = []
skip = next(reader) # skip 1st line: null #1
skip = next(reader) # skip 2nd line: null #2
skip = next(reader) # skip 3rd line: header
for row in reader:
csvRow.append(row)
## set version numbers as first and second candidates
version_first_latest = csvRow[0][1]
version_second_latest = csvRow[1][1]
## make dirs
cmd = "mkdir -p " + version_first_latest
proc = subprocess.call(cmd.split())
cmd = "mkdir -p " + version_second_latest
proc = subprocess.call(cmd.split())
## ser target dirs
# view-source:https://download.opensuse.org/distribution/leap/15.2/repo/oss/x86_64/
target_dir = "https://download.opensuse.org/distribution/leap/" \
+ version_second_latest \
+ "repo/oss/x86_64/"
## ## specify target_url and parse
# https://qiita.com/hujuu/items/b0339404b8b0460087f9
html = urlopen(target_dir)
soup = BeautifulSoup(html, "html.parser")
links = soup.find_all("a")
## get links to rpm and write out to a text file
with open(version_second_latest + rpm_file_name, 'w') as f:
for link in links:
item = link.get("href")
if "mozc" in item and not("mirrorlist" in item):
print(item, file = f)
else:
pass
## rpm.txt to rpm-unique.txt
cmd = "sort ./" \
+ version_second_latest \
+ "rpms-non-unique.txt | uniq | grep -e ^mozc- -e ^fcitx-mozc- > ./" \
+ version_second_latest \
+ "rpms-uniquue.txt"
proc = subprocess.call(cmd, shell=True)
## end of this script