Python の書き出し処理を保護する
サンプルを report.py として保存し、管理画面の製品 ID と信頼する公開鍵を設定します。同じ製品でテスト用ライセンスを 2 つ発行し、最初のライセンスだけに export 権限を付けます。
import os
import sys
from pathlib import Path
from licentivo import Client
PRODUCT_ID = "YOUR_PRODUCT_ID"
TRUSTED_KEYS = {"YOUR_KEY_ID": "YOUR_PUBLIC_KEY"}
def main(args):
if len(args) != 1:
print("Usage: LicenseReport <output.csv>", file=sys.stderr)
return 64
output = Path(args[0])
try:
with Client.open_product(
PRODUCT_ID, TRUSTED_KEYS, os.getenv("LICENTIVO_LICENSE_KEY", "")
) as client:
client.run_feature(
"export", lambda: output.write_text("item,total\nreports,1\n", encoding="utf-8")
)
except Exception as error:
print(error, file=sys.stderr)
return 2
print(output)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))Windows 実行ファイルを作成する
Python 3.12 をインストールした Windows x64 のビルド用 PC で次の PowerShell コマンドを実行します。dist フォルダーに生成された実行ファイルを 1 つだけテスト担当者に渡します。
py -3.12 -m venv .venv
.\.venv\Scripts\python.exe -m pip install "licentivo-sdk==0.11.8" "PyInstaller==6.22.3"
.\.venv\Scripts\python.exe -m PyInstaller --clean --noconfirm --onefile --name LicenseReport report.py配布用アプリを初回認証する
Python をインストールしていない Windows x64 テスト機の空フォルダーに LicenseReport.exe だけをコピーします。そこでコマンドを実行し、文字が表示されない入力欄に最初のテストキーを入力します。成功すると first.csv が作成されます。
$key = Read-Host 'Test license key' -AsSecureString
try {
$env:LICENTIVO_LICENSE_KEY = [System.Net.NetworkCredential]::new('', $key).Password
.\LicenseReport.exe first.csv
$LASTEXITCODE
} finally {
Remove-Item Env:LICENTIVO_LICENSE_KEY -ErrorAction SilentlyContinue
$key.Dispose()
}
Test-Path -LiteralPath .\first.csv同じ Windows アカウントで再起動する
キーの環境変数を削除してから、再度実行します。新しいレポートが作成されれば、SDK はこの Windows アカウントに保存した認証情報を復元できています。
.\LicenseReport.exe restart.csv
$LASTEXITCODE
Get-Content -LiteralPath .\restart.csv書き出し権限のないライセンスを確認する
新しい Windows ユーザープロファイルに切り替えます。2 つ目のキーで認証手順を繰り返し、出力名を denied.csv にします。終了コードは 2 で、ファイルは作成されません。各テストで新しいファイル名を指定します。
Windows x64 で CPython 3.12.10 を使ってビルドし、空のフォルダーには EXE だけをコピーしました。EXE は自身の展開先から Python を読み込み、初回認証時と再起動時にレポートを作成しました。権限がない場合は終了コード 2 でファイルを作成しませんでした。
# In the fresh profile, supply the second key.
$key = Read-Host 'Test license key' -AsSecureString
try {
$env:LICENTIVO_LICENSE_KEY = [System.Net.NetworkCredential]::new('', $key).Password
.\LicenseReport.exe denied.csv
$LASTEXITCODE
} finally {
Remove-Item Env:LICENTIVO_LICENSE_KEY -ErrorAction SilentlyContinue
$key.Dispose()
}
Test-Path -LiteralPath .\denied.csv