Commit 5d5e8779 authored by Balasankar C's avatar Balasankar C Committed by Balasankar C

Remove repeated tasks and generate a single file

We were generating a flat JSON file with license information of all
dependencies. However, with
https://gitlab.com/gitlab-org/omnibus-gitlab/issues/2517 , we want to
categorize them based on dependencies. We can do this either using a
separate file (which has additional dependency relationship) or we can
modify the existing code to generate a single file that has all the
necessary information. This commit does the latter and can be reverted
in case it is not needed.
parent 3f95e427
......@@ -3,8 +3,8 @@ require 'json'
module Omnibus
class DependencyInformation
include Logging
JSON_FILE = "dependency_licenses.json".freeze
DEP_FILE = "dependency_relations.json".freeze
class << self
def process!(project)
......@@ -22,78 +22,46 @@ module Omnibus
File.expand_path(JSON_FILE, project.install_dir)
end
def dep_location
File.expand_path(DEP_FILE, project.install_dir)
end
def process!
csv_output = ""
output = {}
final_output = []
# Handling all libraries
project.library.each do |component|
csv_output += "#{component.name},#{component.version},#{component.license}\n"
end
# Concatenating contents of CSV files from individual components with the
# one generate above.
csv_dir = File.expand_path("licenses", project.install_dir)
Dir.glob(File.expand_path("*.csv", csv_dir)).each do |csv_file|
csv_output += File.open(csv_file).read + "\n"
end
# Creating JSON output
csv_output.each_line do |line|
line = line.strip
next if line.empty?
components = line.split(",")
output[components[0]] = {"version": components[1], "license": components[2]}
end
File.open(json_location, "w") do |f|
f.write(JSON.pretty_generate(output))
end
result = {name: component.name, version: component.version, license: component.license, dependencies: []}
component.dependencies.each do |name|
dependency = project.library.find {|software| software.name == name }
version = dependency.version
license = dependency.license
generate_relation(project, output)
end
def generate_relation(project, output)
relations = {}
keys = []
project.library.each do |component|
unless relations.has_key?(component)
keys << component.name
relations[component.name] = []
end
component.dependencies.each do |dependency|
relations[component.name] << {'name': dependency, 'version': output[dependency][:version], 'license': output[dependency][:license] }
result[:dependencies] << {name: name, 'version': version, license: license }
end
final_output << result
end
# Handling external dependencies of those libraries, if provided explicitly
csv_dir = File.expand_path("licenses", project.install_dir)
Dir.glob(File.expand_path("*.csv", csv_dir)).each do |csv_file|
# Getting the software name from file name
component = File.basename(csv_file, File.extname(csv_file))
unless relations.has_key?(component)
keys << component
relations[component] = []
end
# Find index of the software in final output
index = final_output.each_index.find{ |software_index| final_output[software_index][:name] == component }
csv_output = File.open(csv_file).read
csv_output.each_line do |line|
line = line.strip
next if line.empty?
dependencies = line.split(",")
dependency = dependencies[0]
relations[component] << {'name': dependency, 'version': output[dependency][:version], 'license': output[dependency][:license] }
information = line.split(",")
dependency = information[0]
version = information[1]
license = information[2]
final_output[index][:dependencies] << {name: dependency, 'version': version, license: license}
end
end
f = File.open(dep_location, "w")
final_output = []
keys.each do |key|
final_output << {name: key, version: output[key][:version], license: output[key][:license], dependencies: relations[key]}
File.open(json_location, "w") do |f|
f.write(JSON.pretty_generate(final_output))
end
f.write(JSON.pretty_generate(final_output))
f.close()
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment