Class: LicenseFinder::Decisions

Inherits:
Object
  • Object
show all
Defined in:
lib/license_finder/decisions.rb

Defined Under Namespace

Classes: TXN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDecisions

Returns a new instance of Decisions.



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/license_finder/decisions.rb', line 77

def initialize
  @decisions = []
  @packages = Set.new
  @manual_licenses = ManualLicenses.new
  @homepages = {}
  @approvals = {}
  @permitted = Set.new
  @restricted = Set.new
  @ignored = Set.new
  @ignored_groups = Set.new
  @inherited_decisions = Set.new
end

Instance Attribute Details

#ignoredObject (readonly)

READ



13
14
15
# File 'lib/license_finder/decisions.rb', line 13

def ignored
  @ignored
end

#ignored_groupsObject (readonly)

READ



13
14
15
# File 'lib/license_finder/decisions.rb', line 13

def ignored_groups
  @ignored_groups
end

#inherited_decisionsObject (readonly)

READ



13
14
15
# File 'lib/license_finder/decisions.rb', line 13

def inherited_decisions
  @inherited_decisions
end

#packagesObject (readonly)

READ



13
14
15
# File 'lib/license_finder/decisions.rb', line 13

def packages
  @packages
end

#permittedObject (readonly)

READ



13
14
15
# File 'lib/license_finder/decisions.rb', line 13

def permitted
  @permitted
end

#project_nameObject (readonly)

READ



13
14
15
# File 'lib/license_finder/decisions.rb', line 13

def project_name
  @project_name
end

#restrictedObject (readonly)

READ



13
14
15
# File 'lib/license_finder/decisions.rb', line 13

def restricted
  @restricted
end

Class Method Details

.fetch_saved(file) ⇒ Object

PERSIST



289
290
291
# File 'lib/license_finder/decisions.rb', line 289

def self.fetch_saved(file)
  restore(read!(file))
end

.read!(file) ⇒ Object



323
324
325
# File 'lib/license_finder/decisions.rb', line 323

def self.read!(file)
  file.read if file.exist?
end

.restore(persisted, result = new) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/license_finder/decisions.rb', line 297

def self.restore(persisted, result = new)
  return result unless persisted

  # From https://makandracards.com/makandra/465149-ruby-the-yaml-safe_load-method-hides-some-pitfalls
  actions = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1')
              YAML.safe_load(persisted, permitted_classes: [Symbol, Time], aliases: true)
            else
              YAML.safe_load(persisted, [Symbol, Time], [], true)
            end

  list_of_actions = (actions || []).map(&:first)

  if (list_of_actions & %i[whitelist blacklist]).any?
    raise 'The decisions file seems to have whitelist/blacklist keys which are deprecated. Please replace them with permit/restrict respectively and try again! More info - https://github.com/pivotal/LicenseFinder/commit/a40b22fda11b3a0efbb3c0a021381534bc998dd9'
  end

  (actions || []).each do |action, *args|
    result.send(action, *args)
  end
  result
end

Instance Method Details

#add_decision(decision) ⇒ Object



250
251
252
# File 'lib/license_finder/decisions.rb', line 250

def add_decision(decision)
  @decisions << decision unless @inherited
end

#add_package(name, version, txn = {}) ⇒ Object



90
91
92
93
94
# File 'lib/license_finder/decisions.rb', line 90

def add_package(name, version, txn = {})
  add_decision [:add_package, name, version, txn]
  @packages << ManualPackage.new(name, version)
  self
end

#approval_of(name, version = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/license_finder/decisions.rb', line 23

def approval_of(name, version = nil)
  if !@approvals.key?(name)
    nil
  elsif !version.nil?
    @approvals[name] if @approvals[name][:safe_versions].empty? || @approvals[name][:safe_versions].include?(version)
  elsif @approvals[name][:safe_versions].empty?
    @approvals[name]
  end
end

#approve(name, txn = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/license_finder/decisions.rb', line 136

def approve(name, txn = {})
  add_decision [:approve, name, txn]

  versions = []
  versions = @approvals[name][:safe_versions] if @approvals.key?(name)
  @approvals[name] = TXN.from_hash(txn, versions)
  @approvals[name][:safe_versions].concat(txn[:versions]) unless txn[:versions].nil?
  self
end

#approved?(name, version = nil) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
# File 'lib/license_finder/decisions.rb', line 33

def approved?(name, version = nil)
  if !@approvals.key?(name)
    nil
  elsif !version.nil?
    @approvals.key?(name) && @approvals[name][:safe_versions].empty? || @approvals[name][:safe_versions].include?(version)
  else
    @approvals.key?(name)
  end
end

#gem_config_path(gem_name, relative_config_path) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/license_finder/decisions.rb', line 236

def gem_config_path(gem_name, relative_config_path)
  spec = Gem::Specification.find_by_name(gem_name)
  File.join(spec.gem_dir, relative_config_path)
rescue Gem::LoadError => e
  raise Gem::LoadError,
        "Unable to find gem #{gem_name}; is the gem installed? #{e}"
end

#heed(name, txn = {}) ⇒ Object



182
183
184
185
186
# File 'lib/license_finder/decisions.rb', line 182

def heed(name, txn = {})
  add_decision [:heed, name, txn]
  @ignored.delete(name)
  self
end

#heed_group(name, txn = {}) ⇒ Object



194
195
196
197
198
# File 'lib/license_finder/decisions.rb', line 194

def heed_group(name, txn = {})
  add_decision [:heed_group, name, txn]
  @ignored_groups.delete(name)
  self
end

#homepage(name, homepage, txn = {}) ⇒ Object



130
131
132
133
134
# File 'lib/license_finder/decisions.rb', line 130

def homepage(name, homepage, txn = {})
  add_decision [:homepage, name, homepage, txn]
  @homepages[name] = homepage
  self
end

#homepage_of(name) ⇒ Object



19
20
21
# File 'lib/license_finder/decisions.rb', line 19

def homepage_of(name)
  @homepages[name]
end

#ignore(name, txn = {}) ⇒ Object



176
177
178
179
180
# File 'lib/license_finder/decisions.rb', line 176

def ignore(name, txn = {})
  add_decision [:ignore, name, txn]
  @ignored << name
  self
end

#ignore_group(name, txn = {}) ⇒ Object



188
189
190
191
192
# File 'lib/license_finder/decisions.rb', line 188

def ignore_group(name, txn = {})
  add_decision [:ignore_group, name, txn]
  @ignored_groups << name
  self
end

#ignored?(name) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/license_finder/decisions.rb', line 59

def ignored?(name)
  @ignored.include?(name)
end

#ignored_group?(name) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/license_finder/decisions.rb', line 63

def ignored_group?(name)
  @ignored_groups.include?(name)
end

#inherit_from(filepath_info) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/license_finder/decisions.rb', line 212

def inherit_from(filepath_info)
  decisions =
    case filepath_info
    when Hash
      resolve_inheritance(filepath_info)
    when %r{^https?://}
      open_uri(filepath_info).read
    else
      Pathname(filepath_info).read
    end

  add_decision [:inherit_from, filepath_info]
  @inherited_decisions << filepath_info
  restore_inheritance(decisions)
end

#license(name, lic, txn = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/license_finder/decisions.rb', line 102

def license(name, lic, txn = {})
  add_decision [:license, name, lic, txn]

  versions = txn[:versions]

  if versions.nil? || versions.empty?
    @manual_licenses.assign_to_all_versions(name, lic)
  else
    @manual_licenses.assign_to_specific_versions(name, lic, versions)
  end

  self
end

#licenses_of(name, version = nil) ⇒ Object



15
16
17
# File 'lib/license_finder/decisions.rb', line 15

def licenses_of(name, version = nil)
  @manual_licenses.licenses_of(name, version)
end

#name_project(name, txn = {}) ⇒ Object



200
201
202
203
204
# File 'lib/license_finder/decisions.rb', line 200

def name_project(name, txn = {})
  add_decision [:name_project, name, txn]
  @project_name = name
  self
end

#open_uri(uri, auth = nil) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/license_finder/decisions.rb', line 262

def open_uri(uri, auth = nil)
  header = {}
  auth_header = resolve_authorization(auth)
  header['Authorization'] = auth_header if auth_header

  # ruby < 2.5.0 URI.open is private
  if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5.0')
    open(uri, header)
  else
    URI.open(uri, header)
  end
end

#permit(lic, txn = {}) ⇒ Object



152
153
154
155
156
# File 'lib/license_finder/decisions.rb', line 152

def permit(lic, txn = {})
  add_decision [:permit, lic, txn]
  @permitted << License.find_by_name(lic)
  self
end

#permitted?(lic) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/license_finder/decisions.rb', line 43

def permitted?(lic)
  if @permitted.include?(lic)
    true
  elsif lic.is_a?(OrLicense)
    lic.sub_licenses.any? { |sub_lic| @permitted.include?(sub_lic) }
  elsif lic.is_a?(AndLicense)
    lic.sub_licenses.all? { |sub_lic| @permitted.include?(sub_lic) }
  else
    false
  end
end

#persistObject



319
320
321
# File 'lib/license_finder/decisions.rb', line 319

def persist
  YAML.dump(@decisions)
end

#remove_inheritance(filepath) ⇒ Object



244
245
246
247
248
# File 'lib/license_finder/decisions.rb', line 244

def remove_inheritance(filepath)
  @decisions -= [[:inherit_from, filepath]]
  @inherited_decisions.delete(filepath)
  self
end

#remove_package(name, txn = {}) ⇒ Object



96
97
98
99
100
# File 'lib/license_finder/decisions.rb', line 96

def remove_package(name, txn = {})
  add_decision [:remove_package, name, txn]
  @packages.delete(ManualPackage.new(name))
  self
end

#resolve_authorization(auth) ⇒ Object



275
276
277
278
279
280
281
282
283
# File 'lib/license_finder/decisions.rb', line 275

def resolve_authorization(auth)
  return unless auth

  token_env = auth.match(/\$(\S.*)/)
  return auth unless token_env

  token = ENV[token_env[1]]
  auth.sub(token_env[0], token)
end

#resolve_inheritance(filepath_info) ⇒ Object



228
229
230
231
232
233
234
# File 'lib/license_finder/decisions.rb', line 228

def resolve_inheritance(filepath_info)
  if (gem_name = filepath_info['gem'])
    Pathname(gem_config_path(gem_name, filepath_info['path'])).read
  else
    open_uri(filepath_info['url'], filepath_info['authorization']).read
  end
end

#restore_inheritance(decisions) ⇒ Object



254
255
256
257
258
259
260
# File 'lib/license_finder/decisions.rb', line 254

def restore_inheritance(decisions)
  previous_value = @inherited
  @inherited = true
  self.class.restore(decisions, self)
  @inherited = previous_value
  self
end

#restrict(lic, txn = {}) ⇒ Object



164
165
166
167
168
# File 'lib/license_finder/decisions.rb', line 164

def restrict(lic, txn = {})
  add_decision [:restrict, lic, txn]
  @restricted << License.find_by_name(lic)
  self
end

#restricted?(lic) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/license_finder/decisions.rb', line 55

def restricted?(lic)
  @restricted.include?(lic)
end

#save!(file) ⇒ Object



293
294
295
# File 'lib/license_finder/decisions.rb', line 293

def save!(file)
  write!(persist, file)
end

#unapprove(name, txn = {}) ⇒ Object



146
147
148
149
150
# File 'lib/license_finder/decisions.rb', line 146

def unapprove(name, txn = {})
  add_decision [:unapprove, name, txn]
  @approvals.delete(name)
  self
end

#unlicense(name, lic, txn = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/license_finder/decisions.rb', line 116

def unlicense(name, lic, txn = {})
  add_decision [:unlicense, name, lic, txn]

  versions = txn[:versions]

  if versions.nil? || versions.empty?
    @manual_licenses.unassign_from_all_versions(name, lic)
  else
    @manual_licenses.unassign_from_specific_versions(name, lic, versions)
  end

  self
end

#unname_project(txn = {}) ⇒ Object



206
207
208
209
210
# File 'lib/license_finder/decisions.rb', line 206

def unname_project(txn = {})
  add_decision [:unname_project, txn]
  @project_name = nil
  self
end

#unpermit(lic, txn = {}) ⇒ Object



158
159
160
161
162
# File 'lib/license_finder/decisions.rb', line 158

def unpermit(lic, txn = {})
  add_decision [:unpermit, lic, txn]
  @permitted.delete(License.find_by_name(lic))
  self
end

#unrestrict(lic, txn = {}) ⇒ Object



170
171
172
173
174
# File 'lib/license_finder/decisions.rb', line 170

def unrestrict(lic, txn = {})
  add_decision [:unrestrict, lic, txn]
  @restricted.delete(License.find_by_name(lic))
  self
end

#write!(value, file) ⇒ Object



327
328
329
330
331
332
# File 'lib/license_finder/decisions.rb', line 327

def write!(value, file)
  file.dirname.mkpath
  file.open('w+') do |f|
    f.print value
  end
end