0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python】Macで作業環境(Safari、Chromeなどのアプリ+α)のバージョンを一括出力する

Posted at

記事を書く時、不具合を報告する時など環境としてコピペできるように出力してみました。
改善の余地は多々あるけど、とりあえず満足したのでメモがてら残します。

subprocessを使用します。

環境

MacOSバージョン: 12.6.1
python3バージョン: Python 3.9.13

アプリのバージョンをPlistBuddyで取得する

Macにデフォルトで入っているplist編集コマンドPlistBuddyを使用します。

sample.py
import subprocess

result = subprocess.run("/usr/libexec/PlistBuddy -c \"print :CFBundleShortVersionString\" /Applications/Safari.app/Contents/Info.plist", capture_output=True, shell=True, text=True)
print('safariバージョン: %s' % result.stdout) // safariバージョン: 16.1

/Applications/xxx.app/Contents/Info.plistからバージョンCFBundleShortVersionStringを取得・出力してます。
上記がないアプリは対象外。キー名を変更するなど手を加える必要あり。

xx --versionコマンド実行で取得する

sample.py
import subprocess

result = subprocess.run("python3 --version", capture_output=True, shell=True, text=True)
print('python3 バージョン: %s' % result.stdout) // python3 バージョン: Python 3.9.13

配列にアプリ名などを格納・ぶん回してまとめて出力する

コード全体

sample.py
import os
import platform
import subprocess
# import plistlib # ファイル操作ができるらしいが、今回は未使用

print("""## 環境
MacOSバージョン: %s
platform: %s""" % (
    platform.mac_ver()[0],
    platform.platform(),
))

# バージョンを取得するアプリなど格納した配列たち。
show_version_list = ["docker", "python3", "npm", "node", "swift"]
applications = ["XCode", "Safari", "Google\ Chrome", "Firefox"]

results = []

for item in show_version_list:
    result = subprocess.run("%s --version" % (item), capture_output=True, shell=True, text=True)
    results.append({ 'name': item, 'result': result.stdout.replace('\n', '') })

for app in applications:
    # PlistBuddyはMacにデフォルトで入っているplist編集コマンド
    path = "/usr/libexec/PlistBuddy -c \"print :CFBundleShortVersionString\" /Applications/%s.app/Contents/Info.plist" % app
    # 文字列で指定する場合、`shell=True`を付与する
    result = subprocess.run(path, capture_output=True, shell=True, text=True)
    results.append({ 'name': app.replace("\\", ""), 'result': result.stdout.replace('\n', '') })

for i, j in enumerate(results):
    print('{0}バージョン: {1}'.format(j['name'], j['result'])) # 改行なしで出力が分からなかったので、一旦空文字に置換してます

出力結果

## 環境
MacOSバージョン: 12.6.1
platform: macOS-12.6.1-arm64-arm-64bit
dockerバージョン: Docker version 20.10.17, build 100c701
python3バージョン: Python 3.9.13
npmバージョン: 6.14.17
nodeバージョン: v14.19.3
swiftバージョン: Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30)Target: arm64-apple-macosx12.0
XCodeバージョン: 13.2.1
Safariバージョン: 16.1
Google Chromeバージョン: 107.0.5304.110
Firefoxバージョン: 106.0.5
0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?