0
0
sponsored by Qmonus Value Stream アプリケーション開発に注力するための工夫をシェアしよう!
Qiita Engineer Festa20242024年7月17日まで開催中!

Ryeユーザー必見!全てのライブラリを更新するシェルスクリプト

Last updated at Posted at 2024-07-02

「本記事はQmonus Value Streamの投稿キャンペーン記事です。」

@aimaigh さんよりコメントで指摘いただきました。
rye sync --update-all で全部アップデートできます。
ただし、rye sync --update-all の場合 pyproject.tomldependencies に書かれたバージョンは書き換えません。
以下で紹介するスクリプトは dependencies を書き換えつつ全てアップデートするスクリプトとして使用できます。


Rye で管理しているライブラリを全部アップデートしたい

Pythonでパッケージ管理にRyeを使っています。
Ryeは自身のアップデートをするコマンドが提供されているのですが、pyproject.tomldependencies に書かれたライブラリを一括アップデートするコマンドがありません。
それぞれ rye add xxx yyy zzz とアップデートしていく必要があります。
ライブラリが増えてくるとかなり面倒。
pnpm update --latest みたいに手軽にやりたい!

全部アップデートするシェルスクリプト

#!/bin/bash

# スクリプトが呼ばれたディレクトリに移動
cd "$(pwd)"

# pyproject.toml ファイルのパスを指定
PYPROJECT_FILE="pyproject.toml"

# pyproject.toml ファイルが存在するか確認
if [ ! -f "$PYPROJECT_FILE" ]; then
  echo "$PYPROJECT_FILE が見つかりません。スクリプトを終了します。"
  exit 1
fi

# dependencies セクションを抽出し、コメント行を除外してライブラリ名を取得
DEPENDENCIES=$(awk '/^dependencies = \[/{flag=1; next} /\]/{flag=0} flag' $PYPROJECT_FILE | grep '>=.*' | sed 's/>=.*//' | tr -d '",')

# dev-dependencies セクションを抽出し、コメント行を除外してライブラリ名を取得
DEV_DEPENDENCIES=$(awk '/^dev-dependencies = \[/{flag=1; next} /\]/{flag=0} flag' $PYPROJECT_FILE | grep '>=.*' | sed 's/>=.*//' | tr -d '",')

# ライブラリ名をスペースで区切って変数に格納
LIBS=$(echo $DEPENDENCIES | tr '\n' ' ')
DEV_LIBS=$(echo $DEV_DEPENDENCIES | tr '\n' ' ')

# rye add コマンドを実行
if [ -n "$LIBS" ]; then
  rye add $LIBS
fi

# dev-dependencies 用の rye add --dev コマンドを実行
if [ -n "$DEV_LIBS" ]; then
  rye add --dev $DEV_LIBS
fi

こんな感じのスクリプトを作り、実行権をつけてパスが通ったところに配置し、 pyproject.toml があるディレクトリで呼んであげると・・・

$ rye_library_update.sh
Added bs4>=0.0.2 as regular dependency
Added chromadb>=0.5.3 as regular dependency
Added eventlet>=0.36.1 as regular dependency
Added Flask-Cors>=4.0.1 as regular dependency
Added Flask-SocketIO>=5.3.6 as regular dependency
Added Flask>=3.0.3 as regular dependency
Added google-api-python-client>=2.136.0 as regular dependency
Added google-auth-httplib2>=0.2.0 as regular dependency
Added google-auth-oauthlib>=1.2.0 as regular dependency
Added google-auth>=2.31.0 as regular dependency
Added gunicorn>=22.0.0 as regular dependency
Added langchain-community>=0.2.6 as regular dependency
Added langchain-huggingface>=0.0.3 as regular dependency
Added langchain-openai>=0.1.14 as regular dependency
Added langchain>=0.2.6 as regular dependency
Added langsmith>=0.1.83 as regular dependency
Added openai>=1.35.10 as regular dependency
Added pandas>=2.2.2 as regular dependency
Added pycryptodome>=3.20.0 as regular dependency
Added pymupdf4llm>=0.0.8 as regular dependency
Added python-dotenv>=1.0.1 as regular dependency
Added sentence_transformers>=3.0.1 as regular dependency
Added sentencepiece>=0.2.0 as regular dependency
Added boto3>=1.34.140 as regular dependency
Added langchain-aws>=0.1.9 as regular dependency
Added anthropic>=0.30.1 as regular dependency
Added langfuse>=2.38.0 as regular dependency
Added mypy>=1.10.1 as dev dependency
Added ruff>=0.5.1 as dev dependency

やったね!!
アップデート対象は、>= でバージョン指定しているライブラリに限定しています。
好みに合わせて、最後に rye sync 入れても良いかも
Ryeに一括アップデートのコマンド追加されたりしないかなぁ・・・

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0