概要
以下の2ステップで対応した。
- 手順1:build.sbt内で指定したライブラリ内で利用されている他のライブラリを含めた一覧を出力する
- 手順2:出力された一覧に対してライセンスを取得するツールを作成する
ライブラリのファイルのどこかにライセンスが記述されてたりするのかもしれないけど見つけられなかったので、手順2ではwebサイトからライセンス情報を引っ張ってきた。
手順1:build.sbt内で指定したライブラリ内で利用されている他のライブラリを含めた一覧を出力する
rubyを例にすると、rubyのbundlerでいうとbuild.sbtのlibraryDependenciesに指定された内容がGemfileに相当する。
で、bundle installした後にできるGemfile.lockのような一覧が欲しかった、ということ。
下記プラグインで、依存関係が存在するライブラリ一覧を出力できる。
https://github.com/jrudolph/sbt-dependency-graph
{sbt_root}/project/plugins.sbt
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.8.2")
{sbt_root}/build.sbt
net.virtualvoid.sbt.graph.DependencyGraphSettings.graphSettings
% sbt
sbt> dependencyTree
:
省略
:
[info] +-com.github.nscala-time:nscala-time_2.11:1.4.0 [S]
[info] | +-joda-time:joda-time:2.4 (evicted by: 2.7)
[info] | +-joda-time:joda-time:2.7
[info] |
[info] +-com.github.scala-incubator.io:scala-io-core_2.11:0.4.3 [S]
[info] | +-com.madgag:scala-arm_2.11:1.3.3 [S]
手順2:出力された一覧に対してライセンスを取得するツールを作成する
下記のツールを作成した。
get_sbt_library_license.rb
#! ~/.rvm/rubies/ruby-2.1.5/bin/ruby
require 'pry'
require 'open-uri'
require 'nokogiri'
class HtmlGetter
def initialize(uri)
@uri = uri
end
def execute
user_agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.63 Safari/537.36'
charset = nil
html = open(@uri, "User-Agent" => user_agent) do |f|
charset = f.charset # 文字種別を取得
f.read
end
html
end
end
class MavenRepositoryAnalyer
BASE_URI = 'https://mvnrepository.com/artifact'
def initialize(organization, library_name)
@organization = organization
@library_name = library_name
@html = HtmlGetter.new(uri).execute
end
def licenses
doc = Nokogiri::HTML.parse(@html)
doc.css('span.lic').map(&:text)
end
def uri
# Example
# URL : https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver
# organization : org.mongodb
# library_name : mongo-java-driver
BASE_URI + '/' + @organization + '/' + @library_name
end
end
if(1 != ARGV.length)
$stderr.printf("Usage ; #{$0} inputfile\n")
exit(1)
end
inputilfe = ARGV[0]
open(inputilfe) do |rd|
rd.each_line do |line|
contents = line.chomp.split(':')
organization = contents[0]
library_name = contents[1]
version = contents[2]
$stderr.printf("Now processing -> %s, %s, %s\n", organization, library_name, version)
analyzer = MavenRepositoryAnalyer.new(organization, library_name)
licenses = analyzer.licenses
printf("%s:%s\t%s\t%s\t%s\n", organization, library_name, version, licenses.join(','), analyzer.uri)
sleep 10
end
end
手順1で出力された一覧を以下の感じで整形する(整形するのもツールにすれば良かったですが、今回はエディタでちゃちゃっとやっちゃいました…)。
list.txt
com.github.nscala-time:nscala-time_2.11:1.4.0
joda-time:joda-time:2.7
com.github.scala-incubator.io:scala-io-core_2.11:0.4.3
com.madgag:scala-arm_2.11:1.3.3
実行すると、下記の情報がタブ区切りで出力される。
- ライブラリ名称
- バージョン
- ライセンス
- 参照先のURL
% ruby get_sbt_library_license.rb list.txt
com.github.nscala-time:nscala-time_2.11 1.4.0 Apache 2.0 https://mvnrepository.com/artifact/com.github.nscala-time/nscala-time_2.11
joda-time:joda-time 2.7 Apache 2.0 https://mvnrepository.com/artifact/joda-time/joda-time
com.github.scala-incubator.io:scala-io-core_2.11 0.4.3 SCALA https://mvnrepository.com/artifact/com.github.scala-incubator.io/scala-io-core_2.11
com.madgag:scala-arm_2.11 1.3.3 BSD 2-clause https://mvnrepository.com/artifact/com.madgag/scala-arm_2.11