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.



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

def initialize
  @decisions = []
  @packages = Set.new
  @licenses = Hash.new { |h, k| h[k] = Set.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



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

def ignored
  @ignored
end

#ignored_groupsObject (readonly)

READ



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

def ignored_groups
  @ignored_groups
end

#inherited_decisionsObject (readonly)

READ



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

def inherited_decisions
  @inherited_decisions
end

#packagesObject (readonly)

READ



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

def packages
  @packages
end

#permittedObject (readonly)

READ



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

def permitted
  @permitted
end

#project_nameObject (readonly)

READ



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

def project_name
  @project_name
end

#restrictedObject (readonly)

READ



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

def restricted
  @restricted
end

Class Method Details

.fetch_saved(file) ⇒ Object

PERSIST



272
273
274
# File 'lib/license_finder/decisions.rb', line 272

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

.read!(file) ⇒ Object



301
302
303
# File 'lib/license_finder/decisions.rb', line 301

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

.restore(persisted, result = new) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/license_finder/decisions.rb', line 280

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

  actions = YAML.load(persisted)

  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



232
233
234
# File 'lib/license_finder/decisions.rb', line 232

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

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



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

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



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

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



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

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)


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

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



218
219
220
221
222
223
224
# File 'lib/license_finder/decisions.rb', line 218

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



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

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

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



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

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

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



113
114
115
116
117
# File 'lib/license_finder/decisions.rb', line 113

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

#homepage_of(name) ⇒ Object



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

def homepage_of(name)
  @homepages[name]
end

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



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

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

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



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

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

#ignored?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#ignored_group?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#inherit_from(filepath_info) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/license_finder/decisions.rb', line 195

def inherit_from(filepath_info)
  decisions =
    if filepath_info.is_a?(Hash)
      resolve_inheritance(filepath_info)
    elsif filepath_info =~ %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



101
102
103
104
105
# File 'lib/license_finder/decisions.rb', line 101

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

#licenses_of(name) ⇒ Object



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

def licenses_of(name)
  @licenses[name]
end

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



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

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

#open_uri(uri, auth = nil) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/license_finder/decisions.rb', line 243

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')
    # rubocop:disable Security/Open
    open(uri, header)
    # rubocop:enable Security/Open
  else
    URI.open(uri, header)
  end
end

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



135
136
137
138
139
# File 'lib/license_finder/decisions.rb', line 135

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

#permitted?(lic) ⇒ Boolean

Returns:

  • (Boolean)


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

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



297
298
299
# File 'lib/license_finder/decisions.rb', line 297

def persist
  YAML.dump(@decisions)
end

#remove_inheritance(filepath) ⇒ Object



226
227
228
229
230
# File 'lib/license_finder/decisions.rb', line 226

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

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



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

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

#resolve_authorization(auth) ⇒ Object



258
259
260
261
262
263
264
265
266
# File 'lib/license_finder/decisions.rb', line 258

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



210
211
212
213
214
215
216
# File 'lib/license_finder/decisions.rb', line 210

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



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

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

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



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

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

#restricted?(lic) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#save!(file) ⇒ Object



276
277
278
# File 'lib/license_finder/decisions.rb', line 276

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

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



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

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

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



107
108
109
110
111
# File 'lib/license_finder/decisions.rb', line 107

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

#unname_project(txn = {}) ⇒ Object



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

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

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



141
142
143
144
145
# File 'lib/license_finder/decisions.rb', line 141

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

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



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

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

#write!(value, file) ⇒ Object



305
306
307
308
309
310
# File 'lib/license_finder/decisions.rb', line 305

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