Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions make_licenses_list/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
licenses_list.json
licenses_list.txt
licenses_summary.txt
44 changes: 44 additions & 0 deletions make_licenses_list/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# make_licenses_list

python で構築したソフトウェアを配布・公開する際に、利用しているライブラリのライセンス表記をまとめて出力するツールです。

pip-licenses を利用しています。

対象となるライブラリは pip でインストールしたものに限ります。

> コード生成には ChatGPT を利用しています。

## 仮想環境構築
```bash
conda create -n make_licenses_list_env python=3.11
conda activate make_licenses_list_env
pip install pip-licenses==5.0.0
```

## リポジトリインストール
```bash
git clone https://github.com/morikatron/snippet.git
cd snippet/make_licenses_list
```

## ライブラリインストール(サンプル)
```bash
conda activate make_licenses_list_env
pip install -r sample_requirements.txt
```

## 操作手順
```bash
python make_licenses_list.py
# created path: licenses_list.json
# License information has been written to licenses_list.txt
# created path: licenses_summary.txt
# License summary has been written to licenses_summary.txt
```

## 出力ファイル(サンプル)
- [sample_licenses_list.json](sample_licenses_list.json)
- [sample_licenses_list.txt](sample_licenses_list.txt)
- [sample_licenses_summary.txt](sample_licenses_summary.txt)

[ページの先頭に戻る](#)
66 changes: 66 additions & 0 deletions make_licenses_list/make_licenses_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import json
import subprocess


def run_pip_licenses_summary(summay_file):
# Run pip-licenses command to generate the summary file
subprocess.run(
[
"pip-licenses",
"--summary",
f"--output-file={summay_file}",
],
check=True,
)


def run_pip_licenses(json_file):
# Run pip-licenses command to generate the JSON file
subprocess.run(
[
"pip-licenses",
"--format=json",
"--no-license-path",
"--with-license-file",
"--with-urls",
"--with-authors",
f"--output-file={json_file}",
],
check=True,
)


def json_to_plain_text(json_file, output_file):
with open(json_file, "r", encoding="utf-8") as f:
licenses = json.load(f)

with open(output_file, "w", encoding="utf-8") as f:
for license_info in licenses:
f.write(f"Name: {license_info['Name']}\n")
f.write(f"Version: {license_info['Version']}\n")
f.write(f"License: {license_info['License']}\n")
f.write(f"Author: {license_info.get('Author', 'N/A')}\n")
f.write(f"URL: {license_info.get('URL', 'N/A')}\n")
f.write("License File:\n")
f.write(f"{license_info['LicenseText']}\n")
f.write("\n" + "-" * 60 + "\n\n")


if __name__ == "__main__":
json_file = "licenses_list.json"
output_file = "licenses_list.txt"
summary_file = "licenses_summary.txt"

# Run pip-licenses and generate the JSON file
run_pip_licenses(json_file)

# Convert JSON to plain text
json_to_plain_text(json_file, output_file)

print(f"License information has been written to {output_file}")

# Run pip-licenses summary and generate the summary file
run_pip_licenses_summary(summary_file)

print(f"License summary has been written to {summary_file}")

122 changes: 122 additions & 0 deletions make_licenses_list/sample_licenses_list.json

Large diffs are not rendered by default.

Loading