license_auto 0.1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +47 -0
- data/.travis.yml +9 -0
- data/CHANGELOG.md +17 -0
- data/Gemfile +27 -0
- data/Gemfile.lock +103 -0
- data/LICENSE +23 -0
- data/README.md +136 -0
- data/Rakefile +20 -0
- data/examples/get_license_files_of_bundler.rb +12 -0
- data/lib/api.rb +60 -0
- data/lib/api/bitbucket.rb +142 -0
- data/lib/api/code_google_com.rb +66 -0
- data/lib/api/excel_export.rb +189 -0
- data/lib/api/gem_data.rb +30 -0
- data/lib/api/git_kernel_org.rb +59 -0
- data/lib/api/github.rb +376 -0
- data/lib/api/go_pkg_in.rb +41 -0
- data/lib/api/golang_org.rb +63 -0
- data/lib/api/google_source_com.rb +68 -0
- data/lib/api/gradle2.rb +41 -0
- data/lib/api/helper.rb +26 -0
- data/lib/api/j_center.rb +23 -0
- data/lib/api/maven_central_repository.rb +192 -0
- data/lib/api/mq.rb +30 -0
- data/lib/api/npm_registry.rb +169 -0
- data/lib/api/pattern.rb +33 -0
- data/lib/api/remote_source_package.rb +319 -0
- data/lib/api/spider.rb +47 -0
- data/lib/cloner.rb +154 -0
- data/lib/db.rb +267 -0
- data/lib/license_auto.rb +20 -0
- data/lib/license_auto/config/config.rb +52 -0
- data/lib/license_auto/errors.rb +0 -0
- data/lib/license_auto/license/frequency.rb +30 -0
- data/lib/license_auto/license/similarity.rb +247 -0
- data/lib/license_auto/license/templates/AFL2.0.txt +43 -0
- data/lib/license_auto/license/templates/AFL2.1.txt +47 -0
- data/lib/license_auto/license/templates/AFL3.0.txt +45 -0
- data/lib/license_auto/license/templates/AGPL3.0.txt +236 -0
- data/lib/license_auto/license/templates/APSL 2.0.txt +100 -0
- data/lib/license_auto/license/templates/Apache1.0.txt +18 -0
- data/lib/license_auto/license/templates/Apache1.1.txt +18 -0
- data/lib/license_auto/license/templates/Apache2.0.txt +201 -0
- data/lib/license_auto/license/templates/Artistic1.0.txt +45 -0
- data/lib/license_auto/license/templates/Artistic2.0.txt +70 -0
- data/lib/license_auto/license/templates/BSD.txt +29 -0
- data/lib/license_auto/license/templates/CDDL1.0.txt +1 -0
- data/lib/license_auto/license/templates/CDDL1.1.txt +209 -0
- data/lib/license_auto/license/templates/CPL1.0.txt +211 -0
- data/lib/license_auto/license/templates/CPOL 1.02.txt +41 -0
- data/lib/license_auto/license/templates/ClarifiedArtistic.txt +140 -0
- data/lib/license_auto/license/templates/Creative Commons1.0.txt +28 -0
- data/lib/license_auto/license/templates/EPL1.0.txt +70 -0
- data/lib/license_auto/license/templates/ERLANG1.1.txt +286 -0
- data/lib/license_auto/license/templates/GFDL1.1.txt +101 -0
- data/lib/license_auto/license/templates/GFDL1.2.txt +108 -0
- data/lib/license_auto/license/templates/GFDL1.3.txt +116 -0
- data/lib/license_auto/license/templates/GPL1.0.txt +105 -0
- data/lib/license_auto/license/templates/GPL2.0.txt +340 -0
- data/lib/license_auto/license/templates/GPL3.0.txt +220 -0
- data/lib/license_auto/license/templates/H2.txt +118 -0
- data/lib/license_auto/license/templates/IBMPublic1.0.txt +58 -0
- data/lib/license_auto/license/templates/LGPL2.0.txt +481 -0
- data/lib/license_auto/license/templates/LGPL2.1.txt +166 -0
- data/lib/license_auto/license/templates/LGPL3.0.txt +56 -0
- data/lib/license_auto/license/templates/Lucent Public License 1.02.txt +244 -0
- data/lib/license_auto/license/templates/MIT.txt +21 -0
- data/lib/license_auto/license/templates/MIT2.0.txt +11 -0
- data/lib/license_auto/license/templates/MPL 2.0.txt +373 -0
- data/lib/license_auto/license/templates/MPL1.1.txt +470 -0
- data/lib/license_auto/license/templates/MS-LPL.txt +22 -0
- data/lib/license_auto/license/templates/MS-PL.txt +20 -0
- data/lib/license_auto/license/templates/MS-RL.txt +19 -0
- data/lib/license_auto/license/templates/NPL1.0.txt +138 -0
- data/lib/license_auto/license/templates/OpenSSL.txt +127 -0
- data/lib/license_auto/license/templates/Oracle license.txt +69 -0
- data/lib/license_auto/license/templates/PublicDomain.txt +7 -0
- data/lib/license_auto/license/templates/Python.txt +15 -0
- data/lib/license_auto/license/templates/QPL1.0.txt +45 -0
- data/lib/license_auto/license/templates/RubyClause-6.txt +56 -0
- data/lib/license_auto/license/templates/SQLite Copyright.txt +33 -0
- data/lib/license_auto/license/templates/Sleepycat.txt +133 -0
- data/lib/license_auto/license/templates/SunPublic1.0.txt +183 -0
- data/lib/license_auto/license/templates/WTFPL license.txt +13 -0
- data/lib/license_auto/license_info.rb +12 -0
- data/lib/license_auto/matcher.rb +100 -0
- data/lib/license_auto/package.rb +73 -0
- data/lib/license_auto/parser/gemfile.rb +0 -0
- data/lib/license_auto/parser/golang.rb +0 -0
- data/lib/license_auto/parser/gradle.rb +0 -0
- data/lib/license_auto/version.rb +20 -0
- data/lib/license_auto/website.rb +11 -0
- data/lib/license_auto/website/github.rb +49 -0
- data/lib/license_auto/website/ruby_gems_org.rb +79 -0
- data/lib/license_auto/website/ruby_gems_org_db.rb +52 -0
- data/lib/license_auto/website/rubydoc_info.rb +12 -0
- data/lib/message.rb +5 -0
- data/lib/misc.rb +131 -0
- data/lib/parser/enums.rb +161 -0
- data/lib/parser/gemfile_parser.rb +221 -0
- data/lib/parser/golang_parser.rb +104 -0
- data/lib/parser/gradle_parser.rb +141 -0
- data/lib/parser/manifest_parser.rb +66 -0
- data/lib/parser/maven_parser.rb +91 -0
- data/lib/parser/npm_parser.rb +82 -0
- data/lib/parser/pip_parser.rb +1 -0
- data/lib/parser/rebar_parser.rb +61 -0
- data/lib/recorder.rb +184 -0
- data/lib/script/apt-get.deps.sh +37 -0
- data/lib/script/debian_dpkg_list.sh +2 -0
- data/lib/script/rubygems.org.importdb.sh +22 -0
- data/license_auto.gemspec +44 -0
- data/remove/license_auto +4 -0
- data/remove/mq_pack.rb +219 -0
- data/remove/mq_repo.rb +106 -0
- metadata +249 -0
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'anemone'
|
2
|
+
|
3
|
+
require_relative '../api/pattern'
|
4
|
+
require_relative '../../lib/misc'
|
5
|
+
|
6
|
+
module API
|
7
|
+
class GoogleSourceCom
|
8
|
+
attr_reader :repo_url, :protocol, :host, :owner, :repo, :ref
|
9
|
+
|
10
|
+
# repo_url: https://go.googlesource.com/crypto
|
11
|
+
def initialize(repo_url, db_ref=nil)
|
12
|
+
@repo_url = repo_url
|
13
|
+
|
14
|
+
repo_url_pattern = API::SOURCE_URL_PATTERN[:google_source_com]
|
15
|
+
regex_group = repo_url_pattern.match(repo_url)
|
16
|
+
@protocol = regex_group[:protocol]
|
17
|
+
@host = regex_group[:host]
|
18
|
+
@owner = regex_group[:owner]
|
19
|
+
@repo = regex_group[:repo]
|
20
|
+
|
21
|
+
@http_option = {}
|
22
|
+
http_proxy = Misc.get_http_proxy
|
23
|
+
if http_proxy
|
24
|
+
@http_option[:proxy_host] = http_proxy[:addr]
|
25
|
+
@http_option[:proxy_port] = http_proxy[:port]
|
26
|
+
end
|
27
|
+
@ref = db_ref
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
# URL.
|
32
|
+
def last_commits
|
33
|
+
last_commit = nil
|
34
|
+
opts = {:discard_page_bodies => true, :depth_limit => 0}.merge(@http_option)
|
35
|
+
commit_page = "#{@repo_url}"
|
36
|
+
Anemone.crawl(commit_page, opts) do |anemone|
|
37
|
+
anemone.on_every_page do |page|
|
38
|
+
xpath = "//ol[@class='CommitLog']/li[1]/a[1]"
|
39
|
+
target_link = page.doc.xpath(xpath)
|
40
|
+
# p target_link
|
41
|
+
if target_link.size == 0
|
42
|
+
raise "last_commit error: #{self}, #{@repo_url}"
|
43
|
+
else
|
44
|
+
# full_href = text.attr('href')
|
45
|
+
short_sha = target_link.text()
|
46
|
+
full_sha = target_link.attr('href').value
|
47
|
+
last_commit = {
|
48
|
+
'sha' => full_sha.split('/+/').last
|
49
|
+
}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
last_commit
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
if __FILE__ == $0
|
60
|
+
url = 'https://go.googlesource.com/crypto'
|
61
|
+
g = API::GoogleSourceCom.new(url)
|
62
|
+
p g.last_commits
|
63
|
+
# p g.protocol
|
64
|
+
# p g.host
|
65
|
+
# p g.owner
|
66
|
+
# p g.repo
|
67
|
+
# p g.repo_url
|
68
|
+
end
|
data/lib/api/gradle2.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module API
|
2
|
+
class Gradle
|
3
|
+
def initialize(gradle_file)
|
4
|
+
@gradle_file = gradle_file
|
5
|
+
@websites = {
|
6
|
+
mvn: 'http://mvnrepository.com/',
|
7
|
+
maven: 'https://maven-repository.com/search',
|
8
|
+
rpmfind: 'http://www.rpmfind.net/'
|
9
|
+
}
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
# DOC: http://pkaq.github.io/gradledoc/docs/userguide/ch11/tutorial_gradle_command_line.html#sec:listing_dependencies
|
14
|
+
def list_dependencies()
|
15
|
+
deps = {
|
16
|
+
:pack_name_1 => 'pack_version_1',
|
17
|
+
:pack_name_2 => 'pack_version_2'
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
# JSON: http://search.maven.org/solrsearch/select?q=g:%22org.apache.commons%22&rows=20&wt=json
|
22
|
+
# JSON: http://search.maven.org/solrsearch/select?q=g:%22org.apache.commons%22%20AND%20a:%22commons-lang3%22&rows=2000&wt=json
|
23
|
+
# JSON: http://search.maven.org/solrsearch/select?q=g:%22org.apache.commons%22%20AND%20a:%22commons-lang3%22%20AND%20v:%223.0%22&rows=2000&wt=json
|
24
|
+
# http://search.maven.org/solrsearch/select?q=g:%22org.apache.maven.indexer%22+AND+a:%22maven-indexer%22&rows=20&core=gav
|
25
|
+
|
26
|
+
def fetch_license_info_from_website()
|
27
|
+
license = nil
|
28
|
+
license_url = nil
|
29
|
+
license_text = nil
|
30
|
+
source_code_download_url = nil
|
31
|
+
source_package_page_link = nil
|
32
|
+
license_info = {
|
33
|
+
license: license,
|
34
|
+
license_url: license_url,
|
35
|
+
license_text: license_text,
|
36
|
+
source_url: source_code_download_url,
|
37
|
+
homepage: source_package_page_link
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/api/helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative './pattern'
|
2
|
+
module API
|
3
|
+
|
4
|
+
class Helper
|
5
|
+
def self.is_license_file(filename)
|
6
|
+
return filename =~ API::FILE_NAME_PATTERN[:license_file]
|
7
|
+
end
|
8
|
+
|
9
|
+
# file_pathname = 'foo/to/bar'
|
10
|
+
def self.is_root_file(file_pathname)
|
11
|
+
return file_pathname.split('/').size == 2
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.is_readme_file(filename)
|
15
|
+
return filename =~ API::FILE_NAME_PATTERN[:readme_file]
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.is_notice_file(filename)
|
19
|
+
return filename =~ API::FILE_NAME_PATTERN[:notice_file]
|
20
|
+
end
|
21
|
+
def self.is_debian_copyright_file(file_pathname)
|
22
|
+
return file_pathname =~ API::FILE_NAME_PATTERN[:debian_copyright_file]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/lib/api/j_center.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module API
|
2
|
+
class JCenter
|
3
|
+
|
4
|
+
# Site: https://bintray.com/bintray/jcenter
|
5
|
+
# DOC: https://bintray.com/docs/api/
|
6
|
+
def initialize(group, name, version)
|
7
|
+
end
|
8
|
+
|
9
|
+
def get_license_info()
|
10
|
+
{
|
11
|
+
license: nil,
|
12
|
+
license_text: nil,
|
13
|
+
source_url: nil,
|
14
|
+
homepage: nil,
|
15
|
+
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'httparty'
|
4
|
+
require_relative '../../config/config'
|
5
|
+
|
6
|
+
module API
|
7
|
+
# DOC: http://search.maven.org/#api
|
8
|
+
class MavenCentralRepository
|
9
|
+
def initialize(group_id, artifact_id, version)
|
10
|
+
@group_id = group_id
|
11
|
+
@artifact_id = artifact_id
|
12
|
+
@version = version
|
13
|
+
# @scope = scope
|
14
|
+
# TODO: what is this field?
|
15
|
+
@classifier = ''
|
16
|
+
|
17
|
+
@api_url = "http://search.maven.org/solrsearch/select?q=g%3A%22com.google.inject%22&rows=20&wt=json"
|
18
|
+
end
|
19
|
+
|
20
|
+
# DOC: http://search.maven.org/solrsearch/select?q=g:"com.google.inject"+AND+a:"guice"&core=gav&rows=20&wt=json
|
21
|
+
def select()
|
22
|
+
url = "http://search.maven.org/solrsearch/select?q=g:\"#{@group_id}\"+AND+a:\"#{@artifact_id}\"&core=gav&rows=20&wt=json"
|
23
|
+
url = URI.escape(url)
|
24
|
+
$plog.debug(url)
|
25
|
+
response = HTTParty.get(url)
|
26
|
+
if response.code == 200
|
27
|
+
query_set = JSON.parse(response.licenses)
|
28
|
+
else
|
29
|
+
raise "CentralRepository select error: #{response}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Eg: http://search.maven.org/solrsearch/select?q=g:%22com.google.inject%22%20AND%20a:%22guice%22%20AND%20v:%223.0%22%20AND%20l:%22javadoc%22%20AND%20p:%22jar%22&rows=20&wt=json
|
34
|
+
def advance_search
|
35
|
+
url = "http://search.maven.org/solrsearch/select?q=g:\"#{@group_id}\" AND a:\"#{@artifact_id}\" AND v:\"#{@version}\" AND l:\"#{@classifier}\" AND p:\"jar\"&rows=20&wt=json"
|
36
|
+
url = URI.escape(url)
|
37
|
+
$plog.debug("api_url: #{url}")
|
38
|
+
response = HTTParty.get(url)
|
39
|
+
if response.code == 200
|
40
|
+
query_set = JSON.parse(response.licenses)
|
41
|
+
else
|
42
|
+
raise "CentralRepository select error: #{response}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# POM: https://repo1.maven.org/maven2/com/google/inject/guice/3.0/guice-3.0.pom
|
47
|
+
def get_package_pom(group, name, version)
|
48
|
+
pom = nil
|
49
|
+
|
50
|
+
central_prefix = 'https://repo1.maven.org/maven2'
|
51
|
+
central_body = [group.gsub(/\./, '/'), name, version].join('/')
|
52
|
+
central_tail = "#{name}-#{version}.pom"
|
53
|
+
pom_url = [central_prefix, central_body, central_tail].join('/')
|
54
|
+
# http://stackoverflow.com/questions/25814210/opensslsslsslerror-ssl-connect-syscall-returned-5-errno-0-state-sslv3-read
|
55
|
+
opts = {
|
56
|
+
:ssl_version => 'TLSv1'
|
57
|
+
}
|
58
|
+
response = HTTParty.get(pom_url, options=opts)
|
59
|
+
if response.code == 200
|
60
|
+
$plog.debug("pom_url: #{pom_url}")
|
61
|
+
pom = response.licenses
|
62
|
+
else
|
63
|
+
$plog.error("CentralRepository get_package_pom error: pom_url: #{pom_url}, #{response}")
|
64
|
+
end
|
65
|
+
return pom_url, pom
|
66
|
+
end
|
67
|
+
|
68
|
+
def get_license_info()
|
69
|
+
license_info = {
|
70
|
+
homepage: nil,
|
71
|
+
source_url: nil,
|
72
|
+
licenses: [],
|
73
|
+
project_url: nil,
|
74
|
+
pom_url: nil
|
75
|
+
}
|
76
|
+
query_set = select
|
77
|
+
|
78
|
+
if query_set
|
79
|
+
query_set['response']['docs'].each {|d|
|
80
|
+
version = d['v']
|
81
|
+
ec = d['ec']
|
82
|
+
if version == @version && ec.index('.pom')
|
83
|
+
license_info[:project_url] = "https://maven-repository.com/artifact/#{@group_id}/#{@artifact_id}/#{@version}"
|
84
|
+
pom_url, pom = get_package_pom(@group_id, @artifact_id, @version)
|
85
|
+
if pom
|
86
|
+
license_info[:pom_url] = pom_url
|
87
|
+
|
88
|
+
# $plog.debug("pom: #{pom}")
|
89
|
+
doc = Nokogiri::XML(pom).remove_namespaces!
|
90
|
+
|
91
|
+
source_code_node = doc.xpath("/project/scm/url")
|
92
|
+
license_info[:source_url] = source_code_node.text if source_code_node
|
93
|
+
|
94
|
+
homepage_node = doc.xpath("/project/url")
|
95
|
+
license_info[:homepage] = homepage_node.text if homepage_node
|
96
|
+
|
97
|
+
xpath = "//licenses/license"
|
98
|
+
licenses = doc.xpath(xpath)
|
99
|
+
$plog.debug("licenses: #{licenses.to_xml}")
|
100
|
+
|
101
|
+
# Multi licenses: https://maven-repository.com/artifact/org.cryptacular/cryptacular/1.0
|
102
|
+
licenses.each {|node|
|
103
|
+
license = nil
|
104
|
+
license_url = nil
|
105
|
+
license_text = nil
|
106
|
+
if node.xpath(".//name")
|
107
|
+
license = node.xpath(".//name").text
|
108
|
+
# $plog.debug("license: #{license}")
|
109
|
+
end
|
110
|
+
license_url = node.xpath(".//url").text if licenses.xpath(".//url")
|
111
|
+
|
112
|
+
# TODO: find a license_text demo
|
113
|
+
# license_text = licenses.xpath(".//text").text if licenses.xpath(".//text")
|
114
|
+
|
115
|
+
license_info[:licenses] << {
|
116
|
+
license: license,
|
117
|
+
license_url: license_url,
|
118
|
+
license_text: license_text
|
119
|
+
}
|
120
|
+
}
|
121
|
+
if licenses.size == 0
|
122
|
+
$plog.debug("licenses.size: #{licenses.size}")
|
123
|
+
# Comment license text info: eg. https://repo1.maven.org/maven2/commons-io/commons-io/2.4/commons-io-2.4.pom
|
124
|
+
comment_node = doc.xpath("/comment()[contains(., 'license')]")
|
125
|
+
if comment_node.size > 0
|
126
|
+
license_text = comment_node.to_xml
|
127
|
+
license_info[:licenses] << {
|
128
|
+
license: 'UNKNOWN',
|
129
|
+
license_url: nil,
|
130
|
+
license_text: license_text
|
131
|
+
}
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
break
|
137
|
+
end
|
138
|
+
}
|
139
|
+
end
|
140
|
+
license_info
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
if __FILE__ == $0
|
146
|
+
# One license
|
147
|
+
# item = {:group=>"net.sourceforge.nekohtml", :name=>"nekohtml", :version=>"1.9.20"}
|
148
|
+
|
149
|
+
# Two license
|
150
|
+
# item = {:group=>"org.cryptacular", :name=>"cryptacular", :version=>"1.0"}
|
151
|
+
|
152
|
+
# License in comments
|
153
|
+
item = {:group=>"commons-io", :name=>"commons-io", :version=>"2.4"}
|
154
|
+
|
155
|
+
|
156
|
+
'aopalliance:aopalliance:1.0'
|
157
|
+
'cglib:cglib-nodep:3.1'
|
158
|
+
'com.beust:jcommander:1.48'
|
159
|
+
'com.google.guava:guava:18.0'
|
160
|
+
'com.google.inject:guice:no_aop:4.0'
|
161
|
+
'com.jayway.awaitility:awaitility:1.6.3'
|
162
|
+
'javax.inject:javax.inject:1'
|
163
|
+
'junit:junit:4.10'
|
164
|
+
'org.apache.ant:ant:1.7.0'
|
165
|
+
'org.apache.ant:ant-launcher:1.7.0'
|
166
|
+
'org.assertj:assertj-core:3.1.0'
|
167
|
+
'org.beanshell:bsh:2.0b4'
|
168
|
+
'org.hamcrest:hamcrest-core:1.3'
|
169
|
+
'org.hamcrest:hamcrest-library:1.3'
|
170
|
+
'org.objenesis:objenesis:2.1'
|
171
|
+
'org.testng:testng:6.9.6'
|
172
|
+
'org.yaml:snakeyaml:1.15'
|
173
|
+
|
174
|
+
begin
|
175
|
+
i = 'javax.inject:javax.inject:1'
|
176
|
+
g, a, v = i.split(':')
|
177
|
+
c = API::MavenCentralRepository.new(g, a, v)
|
178
|
+
|
179
|
+
license_info = c.get_license_info
|
180
|
+
$plog.debug("license_info #{license_info}")
|
181
|
+
|
182
|
+
if license_info[:licenses].size == 1
|
183
|
+
elsif license_info[:licenses].size > 1
|
184
|
+
$plog.debug("#{g}:#{a}:#{v} -- has multi license")
|
185
|
+
end
|
186
|
+
|
187
|
+
rescue Exception => e
|
188
|
+
$plog.error("#{g}:#{a}:#{v}, #{e}")
|
189
|
+
end
|
190
|
+
|
191
|
+
$plog.info("results: #{license_info}")
|
192
|
+
end
|
data/lib/api/mq.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'bunny'
|
2
|
+
|
3
|
+
module API
|
4
|
+
|
5
|
+
class RabbitMQ
|
6
|
+
# "amqp://guest:guest@localhost:5672"
|
7
|
+
def initialize(rabbit_mq_conn_str)
|
8
|
+
@conn = Bunny.new(rabbit_mq_conn_str)
|
9
|
+
@conn.start
|
10
|
+
@ch = @conn.create_channel
|
11
|
+
end
|
12
|
+
|
13
|
+
def publish(queue_name, message, auto_release=true, durable=true, check_exist=false)
|
14
|
+
# TODO: @Micfan, 检查队列中是否存在该消息,如果已存在或正在处理,则返回false
|
15
|
+
def queue_exist(message)
|
16
|
+
false
|
17
|
+
end
|
18
|
+
|
19
|
+
q = @ch.queue(queue_name, :auto_release => auto_release, :durable => durable)
|
20
|
+
x = @ch.default_exchange
|
21
|
+
|
22
|
+
if check_exist and not queue_exist(message)
|
23
|
+
x.publish(message, :routing_key => q.name)
|
24
|
+
else
|
25
|
+
x.publish(message, :routing_key => q.name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'open3'
|
3
|
+
require 'httparty'
|
4
|
+
|
5
|
+
require_relative '../../config/config'
|
6
|
+
require_relative '../api/pattern'
|
7
|
+
require_relative '../api/github'
|
8
|
+
|
9
|
+
module API
|
10
|
+
# I have no time to research the npm cli source code(https://github.com/npm/npm-registry-client),
|
11
|
+
# quickly write this.
|
12
|
+
class NpmRegistry
|
13
|
+
def initialize(pack_name, pack_version)
|
14
|
+
@default_registry = 'http://registry.npmjs.org/'
|
15
|
+
@pack_name = pack_name
|
16
|
+
@pack_version = pack_version
|
17
|
+
@pack_meta = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
# RESTful API: http://registry.npmjs.org/:pack_name
|
21
|
+
# TEST: http://registry.npmjs.org/grunt
|
22
|
+
def get_package_meta
|
23
|
+
package_meta = nil
|
24
|
+
api_url = "#{@default_registry}#{@pack_name}"
|
25
|
+
$plog.debug(api_url)
|
26
|
+
response = HTTParty.get(api_url)
|
27
|
+
if response.code == 200
|
28
|
+
package_meta = JSON.parse(response.licenses)
|
29
|
+
elsif response.code == 404
|
30
|
+
$plog.error("!!! Npm registry API 404 Not found: #{response}")
|
31
|
+
else
|
32
|
+
$plog.error("!!! Npm registry API error response: #{response}")
|
33
|
+
end
|
34
|
+
package_meta
|
35
|
+
end
|
36
|
+
|
37
|
+
# RESTful API: http://registry.npmjs.org/grunt/?version=0.1.0
|
38
|
+
def get_package_info_by_version
|
39
|
+
package_info = nil
|
40
|
+
api_url = "#{@default_registry}#{@pack_name}/?version=#{@pack_version}"
|
41
|
+
$plog.debug(api_url)
|
42
|
+
response = HTTParty.get(api_url)
|
43
|
+
if response.code == 200
|
44
|
+
package_info = JSON.parse(response.licenses)
|
45
|
+
elsif response.code == 404
|
46
|
+
$plog.error("!!! Npm registry API 404 Not found: #{response}")
|
47
|
+
else
|
48
|
+
$plog.error("!!! Npm registry API error response: #{response}")
|
49
|
+
end
|
50
|
+
package_info
|
51
|
+
end
|
52
|
+
|
53
|
+
# DOC: https://www.npmjs.com/package/semver
|
54
|
+
# DOC: https://github.com/npm/node-semver
|
55
|
+
# sem_version_range: '~1.2.3'
|
56
|
+
def get_available_versions(sem_version_range)
|
57
|
+
available_versions = []
|
58
|
+
package_meta = get_package_meta
|
59
|
+
all_versions = package_meta['versions']
|
60
|
+
$plog.debug("sem_version_range: #{sem_version_range}")
|
61
|
+
all_versions.each_key {|version|
|
62
|
+
# node -e "var semver = require('semver'); var result = semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3'); console.log(result);"
|
63
|
+
cmd = "node -e \"var semver = require('semver'); var available = semver.satisfies('#{version}', '#{sem_version_range}'); console.log(available);\""
|
64
|
+
# $plog.debug(cmd)
|
65
|
+
Open3.popen3(cmd) {|i,o,e,t|
|
66
|
+
out = o.readlines
|
67
|
+
error = e.readlines
|
68
|
+
if error.length > 0
|
69
|
+
$plog.error(error)
|
70
|
+
raise "node -e evaluate script error: #{cmd}, #{error}"
|
71
|
+
elsif out.length > 0
|
72
|
+
available = out[0].gsub(/\n/,'')
|
73
|
+
if available == 'true'
|
74
|
+
$plog.debug("available version: #{version}")
|
75
|
+
available_versions.push(version)
|
76
|
+
end
|
77
|
+
else
|
78
|
+
raise "node -e evaluate script error: #{cmd}, #{error}"
|
79
|
+
end
|
80
|
+
}
|
81
|
+
}
|
82
|
+
available_versions
|
83
|
+
end
|
84
|
+
|
85
|
+
def chose_one_available_version(sem_version_range)
|
86
|
+
available_versions = get_available_versions(sem_version_range)
|
87
|
+
# TODO: is it right?
|
88
|
+
$plog.debug("chosed version: #{available_versions.last}")
|
89
|
+
available_versions.last
|
90
|
+
end
|
91
|
+
|
92
|
+
def get_license_info()
|
93
|
+
license = nil
|
94
|
+
license_url = nil
|
95
|
+
license_text = nil
|
96
|
+
source_url = nil
|
97
|
+
homepage = nil
|
98
|
+
|
99
|
+
pack_info = get_package_info_by_version
|
100
|
+
$plog.debug("Npm registry pack_info: #{pack_info}")
|
101
|
+
if pack_info
|
102
|
+
licenses = pack_info['licenses']
|
103
|
+
if licenses != nil
|
104
|
+
# TODO: multi licenses:
|
105
|
+
# eg. http://registry.npmjs.org/grunt/?version=0.1.0
|
106
|
+
license = licenses[0]['type']
|
107
|
+
|
108
|
+
# TODO: convert http://github.com/cowboy/grunt/blob/master/LICENSE-MIT to raw
|
109
|
+
license_url = licenses[0]['url']
|
110
|
+
if license_url =~ API::SOURCE_URL_PATTERN[:github_html_page]
|
111
|
+
license_url, license_text = API::Github.convert_htmlpage_to_raw_url(license_url)
|
112
|
+
end
|
113
|
+
else
|
114
|
+
license = pack_info['license']
|
115
|
+
end
|
116
|
+
|
117
|
+
homepage = pack_info['homepage']
|
118
|
+
repository = pack_info['repository']
|
119
|
+
if repository
|
120
|
+
url = repository['url']
|
121
|
+
if repository['type'] == 'git'
|
122
|
+
source_url =
|
123
|
+
if url =~ /^git/
|
124
|
+
url.gsub(/(git:\/\/|git\+ssh:\/\/git\@)/, 'http://').gsub(/\.git$/, '')
|
125
|
+
elsif url =~ /^http/
|
126
|
+
url.gsub(/tree.*$/, '')
|
127
|
+
end
|
128
|
+
elsif repository['type'] =~ 'http'
|
129
|
+
source_url = repository['url'].gsub(/tree.*$/, '')
|
130
|
+
else
|
131
|
+
$plog.info("repository special type: #{repository}")
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
if license_text == nil
|
136
|
+
# TODO:
|
137
|
+
end
|
138
|
+
end
|
139
|
+
{
|
140
|
+
license: license,
|
141
|
+
license_url: license_url,
|
142
|
+
license_text: license_text,
|
143
|
+
source_url: source_url,
|
144
|
+
homepage: homepage
|
145
|
+
}
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
if __FILE__ == $0
|
151
|
+
# name = 'browserify'
|
152
|
+
# version = '12.0.1'
|
153
|
+
name = 'grunt'
|
154
|
+
version = '0.1.0'
|
155
|
+
n = API::NpmRegistry.new(name, version)
|
156
|
+
pack_info = n.get_package_info_by_version
|
157
|
+
|
158
|
+
license_info = n.get_license_info
|
159
|
+
$plog.info(license_info)
|
160
|
+
|
161
|
+
# pack_meta = n.get_package_meta
|
162
|
+
# p pack_meta
|
163
|
+
# p pack_meta['versions']
|
164
|
+
|
165
|
+
# sem_ver = '>=2.5.0'
|
166
|
+
# vs = n.get_available_versions(sem_ver)
|
167
|
+
# p vs
|
168
|
+
end
|
169
|
+
|