license_scout 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,392 @@
1
+ # JSON implementation for Ruby ![Travis Widget]
2
+ [Travis Widget]: http://travis-ci.org/flori/json.svg?branch=master
3
+
4
+ ## Description
5
+
6
+ This is a implementation of the JSON specification according to RFC 7159
7
+ http://www.ietf.org/rfc/rfc7159.txt . Starting from version 1.0.0 on there
8
+ will be two variants available:
9
+
10
+ * A pure ruby variant, that relies on the iconv and the stringscan
11
+ extensions, which are both part of the ruby standard library.
12
+ * The quite a bit faster native extension variant, which is in parts
13
+ implemented in C or Java and comes with its own unicode conversion
14
+ functions and a parser generated by the ragel state machine compiler
15
+ http://www.complang.org/ragel/ .
16
+
17
+ Both variants of the JSON generator generate UTF-8 character sequences by
18
+ default. If an :ascii\_only option with a true value is given, they escape all
19
+ non-ASCII and control characters with \uXXXX escape sequences, and support
20
+ UTF-16 surrogate pairs in order to be able to generate the whole range of
21
+ unicode code points.
22
+
23
+ All strings, that are to be encoded as JSON strings, should be UTF-8 byte
24
+ sequences on the Ruby side. To encode raw binary strings, that aren't UTF-8
25
+ encoded, please use the to\_json\_raw\_object method of String (which produces
26
+ an object, that contains a byte array) and decode the result on the receiving
27
+ endpoint.
28
+
29
+ ## Installation
30
+
31
+ It's recommended to use the extension variant of JSON, because it's faster than
32
+ the pure ruby variant. If you cannot build it on your system, you can settle
33
+ for the latter.
34
+
35
+ Just type into the command line as root:
36
+
37
+ ```
38
+ # rake install
39
+ ```
40
+
41
+ The above command will build the extensions and install them on your system.
42
+
43
+ ```
44
+ # rake install_pure
45
+ ```
46
+
47
+ or
48
+
49
+ ```
50
+ # ruby install.rb
51
+ ```
52
+
53
+ will just install the pure ruby implementation of JSON.
54
+
55
+ If you use Rubygems you can type
56
+
57
+ ```
58
+ # gem install json
59
+ ```
60
+
61
+ instead, to install the newest JSON version.
62
+
63
+ There is also a pure ruby json only variant of the gem, that can be installed
64
+ with:
65
+
66
+ ```
67
+ # gem install json_pure
68
+ ```
69
+
70
+ ## Compiling the extensions yourself
71
+
72
+ If you want to create the `parser.c` file from its `parser.rl` file or draw nice
73
+ graphviz images of the state machines, you need ragel from:
74
+ http://www.complang.org/ragel/
75
+
76
+ ## Usage
77
+
78
+ To use JSON you can
79
+
80
+ ```ruby
81
+ require 'json'
82
+ ```
83
+
84
+ to load the installed variant (either the extension `'json'` or the pure
85
+ variant `'json_pure'`). If you have installed the extension variant, you can
86
+ pick either the extension variant or the pure variant by typing
87
+
88
+ ```ruby
89
+ require 'json/ext'
90
+ ```
91
+
92
+ or
93
+
94
+ ```ruby
95
+ require 'json/pure'
96
+ ```
97
+
98
+ Now you can parse a JSON document into a ruby data structure by calling
99
+
100
+ ```ruby
101
+ JSON.parse(document)
102
+ ```
103
+
104
+ If you want to generate a JSON document from a ruby data structure call
105
+ ```ruby
106
+ JSON.generate(data)
107
+ ```
108
+
109
+ You can also use the `pretty_generate` method (which formats the output more
110
+ verbosely and nicely) or `fast_generate` (which doesn't do any of the security
111
+ checks generate performs, e. g. nesting deepness checks).
112
+
113
+ There are also the JSON and JSON[] methods which use parse on a String or
114
+ generate a JSON document from an array or hash:
115
+
116
+ ```ruby
117
+ document = JSON 'test' => 23 # => "{\"test\":23}"
118
+ document = JSON['test' => 23] # => "{\"test\":23}"
119
+ ```
120
+
121
+ and
122
+
123
+ ```ruby
124
+ data = JSON '{"test":23}' # => {"test"=>23}
125
+ data = JSON['{"test":23}'] # => {"test"=>23}
126
+ ```
127
+
128
+ You can choose to load a set of common additions to ruby core's objects if
129
+ you
130
+
131
+ ```ruby
132
+ require 'json/add/core'
133
+ ```
134
+
135
+ After requiring this you can, e. g., serialise/deserialise Ruby ranges:
136
+
137
+ ```ruby
138
+ JSON JSON(1..10) # => 1..10
139
+ ```
140
+
141
+ To find out how to add JSON support to other or your own classes, read the
142
+ section "More Examples" below.
143
+
144
+ To get the best compatibility to rails' JSON implementation, you can
145
+
146
+ ```ruby
147
+ require 'json/add/rails'
148
+ ```
149
+
150
+ Both of the additions attempt to require `'json'` (like above) first, if it has
151
+ not been required yet.
152
+
153
+ ## More Examples
154
+
155
+ To create a JSON document from a ruby data structure, you can call
156
+ `JSON.generate` like that:
157
+
158
+ ```ruby
159
+ json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
160
+ # => "[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
161
+ ```
162
+
163
+ To get back a ruby data structure from a JSON document, you have to call
164
+ JSON.parse on it:
165
+
166
+ ```ruby
167
+ JSON.parse json
168
+ # => [1, 2, {"a"=>3.141}, false, true, nil, "4..10"]
169
+ ```
170
+
171
+ Note, that the range from the original data structure is a simple
172
+ string now. The reason for this is, that JSON doesn't support ranges
173
+ or arbitrary classes. In this case the json library falls back to call
174
+ `Object#to_json`, which is the same as `#to_s.to_json`.
175
+
176
+ It's possible to add JSON support serialization to arbitrary classes by
177
+ simply implementing a more specialized version of the `#to_json method`, that
178
+ should return a JSON object (a hash converted to JSON with `#to_json`) like
179
+ this (don't forget the `*a` for all the arguments):
180
+
181
+ ```ruby
182
+ class Range
183
+ def to_json(*a)
184
+ {
185
+ 'json_class' => self.class.name, # = 'Range'
186
+ 'data' => [ first, last, exclude_end? ]
187
+ }.to_json(*a)
188
+ end
189
+ end
190
+ ```
191
+
192
+ The hash key `json_class` is the class, that will be asked to deserialise the
193
+ JSON representation later. In this case it's `Range`, but any namespace of
194
+ the form `A::B` or `::A::B` will do. All other keys are arbitrary and can be
195
+ used to store the necessary data to configure the object to be deserialised.
196
+
197
+ If a the key `json_class` is found in a JSON object, the JSON parser checks
198
+ if the given class responds to the `json_create` class method. If so, it is
199
+ called with the JSON object converted to a Ruby hash. So a range can
200
+ be deserialised by implementing `Range.json_create` like this:
201
+
202
+ ```ruby
203
+ class Range
204
+ def self.json_create(o)
205
+ new(*o['data'])
206
+ end
207
+ end
208
+ ```
209
+
210
+ Now it possible to serialise/deserialise ranges as well:
211
+
212
+ ```ruby
213
+ json = JSON.generate [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
214
+ # => "[1,2,{\"a\":3.141},false,true,null,{\"json_class\":\"Range\",\"data\":[4,10,false]}]"
215
+ JSON.parse json
216
+ # => [1, 2, {"a"=>3.141}, false, true, nil, 4..10]
217
+ ```
218
+
219
+ `JSON.generate` always creates the shortest possible string representation of a
220
+ ruby data structure in one line. This is good for data storage or network
221
+ protocols, but not so good for humans to read. Fortunately there's also
222
+ `JSON.pretty_generate` (or `JSON.pretty_generate`) that creates a more readable
223
+ output:
224
+
225
+ ```ruby
226
+ puts JSON.pretty_generate([1, 2, {"a"=>3.141}, false, true, nil, 4..10])
227
+ [
228
+ 1,
229
+ 2,
230
+ {
231
+ "a": 3.141
232
+ },
233
+ false,
234
+ true,
235
+ null,
236
+ {
237
+ "json_class": "Range",
238
+ "data": [
239
+ 4,
240
+ 10,
241
+ false
242
+ ]
243
+ }
244
+ ]
245
+ ```
246
+
247
+ There are also the methods `Kernel#j` for generate, and `Kernel#jj` for
248
+ `pretty_generate` output to the console, that work analogous to Core Ruby's `p` and
249
+ the `pp` library's `pp` methods.
250
+
251
+ The script `tools/server.rb` contains a small example if you want to test, how
252
+ receiving a JSON object from a webrick server in your browser with the
253
+ javasript prototype library http://www.prototypejs.org works.
254
+
255
+ ## Speed Comparisons
256
+
257
+ I have created some benchmark results (see the benchmarks/data-p4-3Ghz
258
+ subdir of the package) for the JSON-parser to estimate the speed up in the C
259
+ extension:
260
+
261
+ ```
262
+ Comparing times (call_time_mean):
263
+ 1 ParserBenchmarkExt#parser 900 repeats:
264
+ 553.922304770 ( real) -> 21.500x
265
+ 0.001805307
266
+ 2 ParserBenchmarkYAML#parser 1000 repeats:
267
+ 224.513358139 ( real) -> 8.714x
268
+ 0.004454078
269
+ 3 ParserBenchmarkPure#parser 1000 repeats:
270
+ 26.755020642 ( real) -> 1.038x
271
+ 0.037376163
272
+ 4 ParserBenchmarkRails#parser 1000 repeats:
273
+ 25.763381731 ( real) -> 1.000x
274
+ 0.038814780
275
+ calls/sec ( time) -> speed covers
276
+ secs/call
277
+ ```
278
+
279
+ In the table above 1 is `JSON::Ext::Parser`, 2 is `YAML.load` with YAML
280
+ compatbile JSON document, 3 is is `JSON::Pure::Parser`, and 4 is
281
+ `ActiveSupport::JSON.decode`. The ActiveSupport JSON-decoder converts the
282
+ input first to YAML and then uses the YAML-parser, the conversion seems to
283
+ slow it down so much that it is only as fast as the `JSON::Pure::Parser`!
284
+
285
+ If you look at the benchmark data you can see that this is mostly caused by
286
+ the frequent high outliers - the median of the Rails-parser runs is still
287
+ overall smaller than the median of the `JSON::Pure::Parser` runs:
288
+
289
+ ```
290
+ Comparing times (call_time_median):
291
+ 1 ParserBenchmarkExt#parser 900 repeats:
292
+ 800.592479481 ( real) -> 26.936x
293
+ 0.001249075
294
+ 2 ParserBenchmarkYAML#parser 1000 repeats:
295
+ 271.002390644 ( real) -> 9.118x
296
+ 0.003690004
297
+ 3 ParserBenchmarkRails#parser 1000 repeats:
298
+ 30.227910865 ( real) -> 1.017x
299
+ 0.033082008
300
+ 4 ParserBenchmarkPure#parser 1000 repeats:
301
+ 29.722384421 ( real) -> 1.000x
302
+ 0.033644676
303
+ calls/sec ( time) -> speed covers
304
+ secs/call
305
+ ```
306
+
307
+ I have benchmarked the `JSON-Generator` as well. This generated a few more
308
+ values, because there are different modes that also influence the achieved
309
+ speed:
310
+
311
+ ```
312
+ Comparing times (call_time_mean):
313
+ 1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
314
+ 547.354332608 ( real) -> 15.090x
315
+ 0.001826970
316
+ 2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
317
+ 443.968212317 ( real) -> 12.240x
318
+ 0.002252414
319
+ 3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
320
+ 375.104545883 ( real) -> 10.341x
321
+ 0.002665923
322
+ 4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
323
+ 49.978706968 ( real) -> 1.378x
324
+ 0.020008521
325
+ 5 GeneratorBenchmarkRails#generator 1000 repeats:
326
+ 38.531868759 ( real) -> 1.062x
327
+ 0.025952543
328
+ 6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
329
+ 36.927649925 ( real) -> 1.018x 7 (>=3859)
330
+ 0.027079979
331
+ 7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
332
+ 36.272134441 ( real) -> 1.000x 6 (>=3859)
333
+ 0.027569373
334
+ calls/sec ( time) -> speed covers
335
+ secs/call
336
+ ```
337
+
338
+ In the table above 1-3 are `JSON::Ext::Generator` methods. 4, 6, and 7 are
339
+ `JSON::Pure::Generator` methods and 5 is the Rails JSON generator. It is now a
340
+ bit faster than the `generator_safe` and `generator_pretty` methods of the pure
341
+ variant but slower than the others.
342
+
343
+ To achieve the fastest JSON document output, you can use the `fast_generate`
344
+ method. Beware, that this will disable the checking for circular Ruby data
345
+ structures, which may cause JSON to go into an infinite loop.
346
+
347
+ Here are the median comparisons for completeness' sake:
348
+
349
+ ```
350
+ Comparing times (call_time_median):
351
+ 1 GeneratorBenchmarkExt#generator_fast 1000 repeats:
352
+ 708.258020939 ( real) -> 16.547x
353
+ 0.001411915
354
+ 2 GeneratorBenchmarkExt#generator_safe 1000 repeats:
355
+ 569.105020353 ( real) -> 13.296x
356
+ 0.001757145
357
+ 3 GeneratorBenchmarkExt#generator_pretty 900 repeats:
358
+ 482.825371244 ( real) -> 11.280x
359
+ 0.002071142
360
+ 4 GeneratorBenchmarkPure#generator_fast 1000 repeats:
361
+ 62.717626652 ( real) -> 1.465x
362
+ 0.015944481
363
+ 5 GeneratorBenchmarkRails#generator 1000 repeats:
364
+ 43.965681162 ( real) -> 1.027x
365
+ 0.022745013
366
+ 6 GeneratorBenchmarkPure#generator_safe 1000 repeats:
367
+ 43.929073409 ( real) -> 1.026x 7 (>=3859)
368
+ 0.022763968
369
+ 7 GeneratorBenchmarkPure#generator_pretty 1000 repeats:
370
+ 42.802514491 ( real) -> 1.000x 6 (>=3859)
371
+ 0.023363113
372
+ calls/sec ( time) -> speed covers
373
+ secs/call
374
+ ```
375
+
376
+ ## Author
377
+
378
+ Florian Frank <mailto:flori@ping.de>
379
+
380
+ ## License
381
+
382
+ Ruby License, see https://www.ruby-lang.org/en/about/license.txt.
383
+
384
+ ## Download
385
+
386
+ The latest version of this library can be downloaded at
387
+
388
+ * https://rubygems.org/gems/json
389
+
390
+ Online Documentation should be located at
391
+
392
+ * http://json.rubyforge.org
@@ -21,11 +21,20 @@ require "license_scout/exceptions"
21
21
  require "license_scout/license_file_analyzer"
22
22
 
23
23
  require "mixlib/shellout"
24
+ require "ffi_yajl"
24
25
 
25
26
  module LicenseScout
26
27
  module DependencyManager
27
28
  class Rebar < Base
28
29
 
30
+ attr_reader :packaged_dependencies
31
+
32
+ def initialize(project_dir, options)
33
+ super(project_dir, options)
34
+
35
+ @packaged_dependencies = {}
36
+ end
37
+
29
38
  def name
30
39
  "erlang_rebar"
31
40
  end
@@ -37,11 +46,33 @@ module LicenseScout
37
46
  def dependencies
38
47
  dependencies = []
39
48
 
49
+ # Some dependencies are obtained via 'pkg' identifier of rebar. These
50
+ # dependencies include their version in the rebar.lock file. Here we
51
+ # parse the rebar.lock and remember all the versions we find.
52
+ parse_packaged_dependencies
53
+
40
54
  Dir.glob("#{project_deps_dir}/*").each do |dep_dir|
41
55
  next unless File.directory?(dep_dir)
42
56
 
43
57
  dep_name = File.basename(dep_dir)
44
- dep_version = git_rev_parse(dep_dir)
58
+
59
+ # First check if this dependency is coming from the parent software.
60
+ # If so we do not need to worry about its version or licenses because
61
+ # it will be covered under the parent software's license.
62
+ next if File.directory?(File.join(project_dir, "apps", dep_name))
63
+
64
+ # Or skip if the dep name is the project name
65
+ next if File.exist?(File.join(project_dir, "_build/default/rel", dep_name))
66
+
67
+ # While determining the dependency version we first check the cache we
68
+ # built from rebar.lock for the dependencies that come via 'pkg'
69
+ # keyword. If this information is not available we try to determine
70
+ # the dependency version via git.
71
+ dep_version = if packaged_dependencies.key?(dep_name)
72
+ packaged_dependencies[dep_name]
73
+ else
74
+ git_rev_parse(dep_dir)
75
+ end
45
76
 
46
77
  override_license_files = options.overrides.license_files_for(name, dep_name, dep_version)
47
78
  license_files =
@@ -63,15 +94,83 @@ module LicenseScout
63
94
 
64
95
  private
65
96
 
97
+ # Some of the dependencies or rebar projects are obtained as a package.
98
+ # These have the 'pkg' key in their rebar.lock file. Since we can not
99
+ # determine the version of them via git, we try to parse the rebar.lock
100
+ # file and remember their versions to use it later.
101
+ def parse_packaged_dependencies
102
+ rebar_lock_path = File.join(project_dir, "rebar.lock")
103
+
104
+ return unless File.exist?(rebar_lock_path)
105
+
106
+ # We parse the rebar.lock using 'config_to_json' from
107
+ # https://github.com/basho/erlang_template_helper This binary requires
108
+ # escript to be on the path so we use the environment provided to
109
+ # license_scout if available.
110
+
111
+ config_to_json_path = File.expand_path("../../../bin/config_to_json", File.dirname(__FILE__))
112
+ s = Mixlib::ShellOut.new("#{config_to_json_path} #{rebar_lock_path}", environment: options.environment)
113
+ s.run_command
114
+ s.error!
115
+
116
+ # Parsed rebar.lock will contain "type" information for each field
117
+ # prepended into the output array. What we get from it looks like this:
118
+ # [["__tuple",
119
+ # "__binary_edown",
120
+ # ["__tuple",
121
+ # "git",
122
+ # "__string_git://github.com/seth/edown.git",
123
+ # ["__tuple", "ref", "__string_30a9f7867d615af45783235faa52742d11a9348e"]],
124
+ # 1],
125
+ # ["__tuple",
126
+ # "__binary_mochiweb",
127
+ # ["__tuple", "pkg", "__binary_mochiweb", "__binary_2.12.2"],
128
+ # 2],
129
+ # ...
130
+ #
131
+ rebar_lock_content = FFI_Yajl::Parser.parse(s.stdout)
132
+
133
+ rebar_lock_content.each do |element|
134
+ # We are trying to match the mochiweb example above. Notice the 'pkg'
135
+ # entry in its source information. We are doing some very specific
136
+ # String matching here because we can not bring over
137
+ # erlang_template_helper gem since it is not released to rubygems.
138
+
139
+ next if !element.is_a?(Array) || element.length < 3
140
+ source_info = element[2]
141
+
142
+ next if !source_info.is_a?(Array) || source_info.length < 4
143
+ if source_info[1] == "pkg"
144
+ source_name = source_info[2].gsub("__binary_", "").gsub("__string_", "")
145
+ source_version = source_info[3].gsub("__binary_", "").gsub("__string_", "")
146
+
147
+ packaged_dependencies[source_name] = source_version
148
+ end
149
+ end
150
+ rescue Mixlib::ShellOut::ShellCommandFailed
151
+ # Continue even if we can not parse the rebar.lock since we can still
152
+ # succeed if all the dependencies are coming from git.
153
+ end
154
+
66
155
  def git_rev_parse(dependency_dir)
67
156
  s = Mixlib::ShellOut.new("git rev-parse HEAD", cwd: dependency_dir)
68
157
  s.run_command
69
158
  s.error!
70
159
  s.stdout.strip
160
+ rescue Mixlib::ShellOut::ShellCommandFailed
161
+ # We wrap the error here in order to be able to learn the cwd, i.e.
162
+ # which dependency is having issues.
163
+ raise LicenseScout::Exceptions::Error.new(
164
+ "Can not determine the git version of rebar dependency at '#{dependency_dir}'."
165
+ )
71
166
  end
72
167
 
73
168
  def project_deps_dir
74
- File.join(project_dir, "deps")
169
+ # rebar dependencies can be found in one of these two directories.
170
+ ["deps", "_build/default/lib"].each do |dir|
171
+ dep_dir = File.join(project_dir, dir)
172
+ return dep_dir if File.exist?(dep_dir)
173
+ end
75
174
  end
76
175
 
77
176
  def rebar_config_path
@@ -17,11 +17,13 @@
17
17
 
18
18
  require "license_scout/dependency_manager/bundler"
19
19
  require "license_scout/dependency_manager/rebar"
20
+ require "license_scout/dependency_manager/cpan"
21
+ require "license_scout/dependency_manager/berkshelf"
20
22
 
21
23
  module LicenseScout
22
24
  module DependencyManager
23
25
  def self.implementations
24
- [Bundler, Rebar]
26
+ [Bundler, Rebar, CPAN, Berkshelf]
25
27
  end
26
28
  end
27
29
  end
@@ -39,19 +39,9 @@ module LicenseScout
39
39
  end
40
40
  end
41
41
 
42
- class DependencyManagerNotRun < Error
43
- def initialize(project_dir, dependency_manager_name)
44
- @project_dir = project_dir
45
- @dependency_manager_name = dependency_manager_name
46
- end
47
-
48
- def to_s
49
- "Dependency manager '#{@dependency_manager_name}' is not yet run for project at '#{@project_dir}'."
50
- end
51
- end
52
-
53
42
  class InaccessibleDependency < Error; end
54
43
  class InvalidOverride < Error; end
44
+ class InvalidOutputReport < Error; end
55
45
 
56
46
  class NetworkError < Error
57
47
 
@@ -80,7 +80,7 @@ module LicenseScout
80
80
 
81
81
  begin
82
82
  options = {
83
- :read_timeout => 60,
83
+ :read_timeout => 300,
84
84
  }
85
85
 
86
86
  open(from_url, options) do |f|
@@ -19,7 +19,7 @@ require "license_scout/overrides"
19
19
 
20
20
  module LicenseScout
21
21
  class Options
22
- SUPPORTED_OPTIONS = [:overrides, :environment, :ruby_bin]
22
+ SUPPORTED_OPTIONS = [:overrides, :environment, :ruby_bin, :cpan_cache]
23
23
 
24
24
  SUPPORTED_OPTIONS.each do |o|
25
25
  self.send(:attr_reader, o)
@@ -39,6 +39,7 @@ module LicenseScout
39
39
  overrides: Overrides.new,
40
40
  environment: {},
41
41
  ruby_bin: nil,
42
+ cpan_cache: Dir.tmpdir,
42
43
  }
43
44
  end
44
45
  end