大根's ITブログ

ITとか開発ツールとかビジネスとか色々

scancode-toolkitを使う

びっくりするほど日本語の記事がない。これ結構有名だよね?そうだよね?

https://github.com/nexB/scancode-toolkit

それではやっていきます。

概要

READMEを見ると、意外と色々できそうだと思わされる。

なぜScanCodeを使うのか?
ScanCodeはスタンドアロンコマンドラインツールとして、インストール、実行、CI/CD処理パイプラインへの組み込みが簡単に行えます。WindowsmacOSLinuxで動作します。 ScanCodeは、Eclipse Foundation、OpenEmbedded.org、FSFE、FSFOSS Review Toolkit、ClearlyDefined.io、RedHat Fabric8 analyticsなどのプロジェクトや組織で使用されています。 ScanCodeは、ソースコードとバイナリファイルの両方でライセンス、著作権、パッケージマニフェスト、直接依存関係などを検出します。 ScanCodeは最も正確なライセンス検出エンジンを提供し、近似正規表現パターンや確率的検索、編集距離、機械学習に頼るのではなく、ライセンステキストのデータベースとコードの完全な比較(差分や赤線比較とも呼ばれます)を行います。 Pythonで書かれたScanCodeは、プラグインを使って簡単に拡張することができ、新しいスキャナ、データサマリ、パッケージマニフェストパーサ、新しい出力を提供します。 スキャン結果はJSON、HTML、CSV、SPDXとして保存することができます。ScanCode Workbench GUIアプリを使用して、スキャン結果や統計情報、グラフィックを確認したり、表示したりすることができます。 ScanCodeは積極的にメンテナンスされており、ユーザーと貢献者のコミュニティが成長しています。 ScanCodeは20,000以上の自動化されたテストスイートでテストされています。

ほう。 ドキュメントはこちらにある。なかなかごついね。日本語はないようだ。

https://scancode-toolkit.readthedocs.io/en/latest/

インストール

丁寧にPythonの入れ方が書いてある。 https://scancode-toolkit.readthedocs.io/en/latest/getting-started/install.html#prerequisites

$ sudo yum install python3.6-devel zlib bzip2-libs xz-libs libxml2-devel libxslt-devel

やってみたらなんかpython3.6-develが入らなかった?っぽいので、一応自らpythoin3を入れておく。

$ sudo yum install -y python3 python3-devel

ではscancode-toolkitを入れていこう。 https://scancode-toolkit.readthedocs.io/en/latest/getting-started/install.html#installation-as-an-application-downloading-releases

GItHub Releaseからzipを取ってきて展開するだけっぽい。 Releases · nexB/scancode-toolkit · GitHub

けどなんかバージョン21系と3系があるっぽい?なにこれ。 とりあえずドキュメントにある3.1.1を使おう。そうしよう。

$ curl -O -L https://github.com/nexB/scancode-toolkit/releases/download/v3.1.1/scancode-toolkit-3.1.1.zip
$ unzip scancode-toolkit-3.1.1.zip
$ ls
scancode-toolkit-3.1.1
$ cd scancode-toolkit-3.1.1
$ ls
AUTHORS.rst        apache-2.0.LICENSE   docs             scancode-toolkit.ABOUT
CHANGELOG.rst      appveyor.yml         etc              scancode.bat
CONTRIBUTING.rst   azure-pipelines.yml  extractcode      setup.cfg
ISSUE_TEMPLATE.md  cc0-1.0.LICENSE      extractcode.bat  setup.py
MANIFEST.in        codecov.yml          plugins          src
NOTICE             configure            pyvenv.cfg       thirdparty
PKG-INFO           configure.bat        samples
README.rst         conftest.py          scancode

とりあえずよく分からんがREADMEに書いてあったQuick Startのコマンドを叩く。

./scancode -clip --json-pp - samples
* Configuring ScanCode for first use...

(省略)

OSError: libbz2.so.1.0: 共有オブジェクトファイルを開けません: そのようなファイルやディレクトリはありません

はぁ?って感じなんですけど。 ドキュメントを見たところ、venvやれって書いてある。これかな。

https://scancode-toolkit.readthedocs.io/en/latest/tutorials/how_to_run_a_scan.html

$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ pip install --upgrade pip
(venv) $ pip install scancode-toolkit[full]

スキャン対象はsamplesというフォルダ。まずこのフォルダ内にあるアーカイブファイルを展開する必要があるらしい。

$ extractcode samples

同じエラーで止まった。とりあえずextractはスルーしよう。

スキャン

ということでスキャンだ。以下のコマンドで、sample.jsonというファイルに結果が吐かれるっぽい。

$ scancode -clpeui -n 2 --ignore "*.java" --json-pp sample.json samples
Setup plugins...
Collect file inventory...
Scan files for: info, licenses, copyrights, packages, emails, urls with 2 process(es)...
[####################] 0                          
Scanning done.
Summary:        info, licenses, copyrights, packages, emails, urls with 2 process(es)
Errors count:   0
Scan Speed:     2.12 files/sec. 80.28 KB/sec.
Initial counts: 44 resource(s): 33 file(s) and 11 directorie(s) 
Final counts:   37 resource(s): 26 file(s) and 11 directorie(s) for 985.34 KB
Timings:
  scan_start: 2021-02-21T111455.169845
  scan_end:   2021-02-21T111508.529217
  setup_scan:licenses: 1.06s
  setup: 1.06s
  scan: 12.27s
  total: 13.38s
Removing temporary files...done.

とりあえずスキャンは通った。 まぁとりあえずスキャンしたjsonを見る。以下は一部抜粋。

    {
      "path": "samples/JGroups/LICENSE",
      "type": "file",
      "name": "LICENSE",
      "base_name": "LICENSE",
      "extension": "",
      "size": 26430,
      "date": "2019-02-11",
      "sha1": "e60c2e780886f95df9c9ee36992b8edabec00bcc",
      "md5": "7fbc338309ac38fefcd64b04bb903e34",
      "sha256": "a190dc9c8043755d90f8b0a75fa66b9e42d4af4c980bf5ddc633f0124db3cee7",
      "mime_type": "text/plain",
      "file_type": "ASCII text",
      "programming_language": null,
      "is_binary": false,
      "is_text": true,
      "is_archive": false,
      "is_media": false,
      "is_source": false,
      "is_script": false,
      "licenses": [
        {
          "key": "lgpl-2.1",
          "score": 100.0,
          "name": "GNU Lesser General Public License 2.1",
          "short_name": "LGPL 2.1",
          "category": "Copyleft Limited",
          "is_exception": false,
          "owner": "Free Software Foundation (FSF)",
          "homepage_url": "http://www.gnu.org/licenses/lgpl-2.1.html",
          "text_url": "http://www.gnu.org/licenses/lgpl-2.1.txt",
          "reference_url": "https://scancode-licensedb.aboutcode.org/lgpl-2.1",
          "scancode_text_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/lgpl-2.1.LICENSE",
          "scancode_data_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/lgpl-2.1.yml",
          "spdx_license_key": "LGPL-2.1-only",

(省略)

Visualize

Visualize resultをした方が良さそうだ。

https://scancode-toolkit.readthedocs.io/en/latest/tutorials/how_to_visualize_scan_results.html

ScanCode Workbenchなるものが必要らしい。

https://github.com/nexB/scancode-workbench/releases/tag/v3.1.1

$ curl -O -L https://github.com/nexB/scancode-workbench/releases/download/v3.1.1/ScanCode-Workbench-linux-x64-3.1.1.tar.gz
$ tar -zxvf  ScanCode-Workbench-linux-x64-3.1.1.tar.gz
$ cd ScanCode-Workbench-linux-x64-3.1.1
$ ./ScanCode-Workbench
./ScanCode-Workbench: error while loading shared libraries: libXss.so.1: cannot open shared object file: No such file or directory

よく分からないけどここの情報を参考に必要そうなパッケージをインストールする。 https://sh-yoshida.hatenablog.com/entry/2016/11/30/002247

$ yum install libXScrnSaver
$ ./ScanCode-Workbench

起動したー! けどjsonのインポートとかができない。意味が分からない。

気が向いたらまた遊んでみよう。