Skip to content

Instantly share code, notes, and snippets.

@ChadRehmKineticData
Last active April 2, 2019 15:03
Show Gist options
  • Save ChadRehmKineticData/f8634714fc0a4adadd6253bab11faceb to your computer and use it in GitHub Desktop.
Save ChadRehmKineticData/f8634714fc0a4adadd6253bab11faceb to your computer and use it in GitHub Desktop.
Peripheral repo refactor
require 'csv'
require 'fileutils'
require 'open3'
INPUT_FILE = 'peripheral-input.csv'
GIT_CLONE_LOC = 'https://github.com/kineticdata'
CLONE_STORAGE_DIR_NAME = 'clone-storage'
PERIPH_STORAGE_DIR_NAME = 'peripherial-storage'
REPO_PRIFIX = 'peripherals-'
LOG_FILE = 'log.txt'
ERROR_FILE = 'error.txt'
def write_log(log_entery, write_file)
File.open(write_file, "a") { |file|
file.write log_entery + "\n"
}
end
# csv header row:
# destination repo, source repo, git branch, repo type
def read_csv(input_file)
write_log("Reading csv #{input_file}", LOG_FILE)
return CSV.read(input_file)
end
def execute_command(cmd)
stdout, stderr, status = Open3.capture3(cmd)
if !status.success?
write_log("ERROR: #{stderr}", ERROR_FILE)
end
return status.success?
end
# write commit has to a .sync file
# The sync file is used to see if the commit history on the repo has changed since last run
def write_sync(row, commit_hash)
write_log("Adding commit hash #{commit_hash} for #{row[1]} to #{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{row[0]}/#{row[3]}/.sync.csv", LOG_FILE)
File.open("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{row[0]}/#{row[3]}/.sync.csv", "a") {
|file| file.write "#{row[0]},#{commit_hash}\n"
}
end
def check_sync(commit_hash, periph_repo, type)
File.open("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/#{type}/.sync.csv", "w") unless File.exists?("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/#{type}/.sync.csv")
found = false
csv = read_csv("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/#{type}/.sync.csv")
csv.each { |row|
if (row[1] == commit_hash)
found = true
end
}
return found
end
# if repo exists return current commit
def get_commit_hash(repo_name)
if File.exist?("#{CLONE_STORAGE_DIR_NAME}/#{repo_name}/.git")
commit_hash = `git --git-dir #{CLONE_STORAGE_DIR_NAME}/#{repo_name}/.git rev-parse HEAD`
write_log("Commit hash #{commit_hash} found for repo #{repo_name}.", LOG_FILE)
return commit_hash
end
write_log("File #{CLONE_STORAGE_DIR_NAME}/#{repo_name}/.git did not exist. Commit hash not returned.", LOG_FILE)
return nil
end
def check_repo_exists(repo_name)
exists = false
if Dir.exist?("#{CLONE_STORAGE_DIR_NAME}/#{repo_name}/.git")
exists = true
write_log("#{repo_name} exists", LOG_FILE)
end
return exists
end
def clone_repo(repo_name, branch)
success = false
Dir.mkdir("#{CLONE_STORAGE_DIR_NAME}/#{repo_name}") unless File.exists?("#{CLONE_STORAGE_DIR_NAME}/#{repo_name}")
write_log("Starting clone of #{repo_name}", LOG_FILE)
success = execute_command("git clone -b #{branch} #{GIT_CLONE_LOC}/#{repo_name}.git #{CLONE_STORAGE_DIR_NAME}/#{repo_name}")
return success
end
# Build the dir structure for the peripheral repo.
def make_periph_dir(periph_repo)
periph_repo_path = "#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}"
if !Dir.exist?("#{periph_repo_path}")
File.open(LOG_FILE, "a") { |file| file.write "Making #{periph_repo} dir\n"}
Dir.mkdir("#{periph_repo_path}")
Dir.mkdir("#{periph_repo_path}/handlers")
File.new("#{periph_repo_path}/handlers/.sync.csv", "w")
Dir.mkdir("#{periph_repo_path}/routines")
#File.new("#{periph_repo_path}/routines/.sync.csv")
Dir.mkdir("#{periph_repo_path}/bridge-adapters")
File.new("#{periph_repo_path}/bridge-adapters/.sync.csv", "w")
Dir.mkdir("#{periph_repo_path}/bridge-models")
#File.new("#{periph_repo_path}/bridge-models/.sync.csv")
Dir.mkdir("#{periph_repo_path}/file-adapters")
File.new("#{periph_repo_path}/file-adapters/.sync.csv", "w")
Dir.mkdir("#{periph_repo_path}/authentication-adapters")
File.new("#{periph_repo_path}/authentication-adapters/.sync.csv", "w")
Dir.mkdir("#{periph_repo_path}/source-adapters")
File.new("#{periph_repo_path}/source-adapters/.sync.csv", "w")
end
end
def move_handlers(periph_repo, repo_name)
begin
FileUtils.copy_entry("#{CLONE_STORAGE_DIR_NAME}/#{repo_name}/published",
"#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/handlers")
FileUtils.remove("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/handlers/.published") unless File.exists?("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/handlers/.published")
rescue StandardError => e
write_log("Error moving handlers for #{repo_name} to #{periph_repo}: " + e.message, ERROR_FILE)
end
end
def move_bridge(periph_repo, repo_name)
begin
Dir.mkdir("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/bridge-adapters/#{repo_name}") unless File.exists?("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/bridge-adapters/#{repo_name}")
FileUtils.copy_entry("#{CLONE_STORAGE_DIR_NAME}/#{repo_name}",
"#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/bridge-adapters/#{repo_name}")
FileUtils.remove("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/bridge-adapters/#{repo_name}/.gitignore") unless !File.exists?("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/bridge-adapters/#{repo_name}/.gitignore")
FileUtils.remove_dir("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/bridge-adapters/#{repo_name}/.git")
rescue StandardError => e
write_log("Error moving handlers for #{repo_name} to #{periph_repo}: " + e.message, ERROR_FILE)
end
end
def move_file(periph_repo, repo_name)
begin
Dir.mkdir("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/file-adapters/#{repo_name}") unless File.exists?("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/file-adapters/#{repo_name}")
FileUtils.copy_entry("#{CLONE_STORAGE_DIR_NAME}/#{repo_name}",
"#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/file-adapters/#{repo_name}")
FileUtils.remove("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/file-adapters/#{repo_name}/.gitignore") unless !File.exists?("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/file-adapters/.gitignore")
FileUtils.remove_dir("#{PERIPH_STORAGE_DIR_NAME}/#{REPO_PRIFIX}#{periph_repo}/file-adapters/#{repo_name}/.git")
rescue StandardError => e
write_log("Error moving handlers for #{repo_name} to #{periph_repo}: " + e.message, ERROR_FILE)
end
end
def setup_file_structure()
Dir.mkdir(CLONE_STORAGE_DIR_NAME) unless File.exists?(CLONE_STORAGE_DIR_NAME)
Dir.mkdir(PERIPH_STORAGE_DIR_NAME) unless File.exists?(PERIPH_STORAGE_DIR_NAME)
File.open(LOG_FILE, "w")
File.open(ERROR_FILE, "w")
end
def main()
setup_file_structure()
input_arr = read_csv(INPUT_FILE)
# skip the head row
input_arr[1..-1].each_with_index { |row, idx|
make_periph_dir(row[0])
puts "On cound #{idx} for priph #{row[0]} and clone #{row[1]}"
# Check if repo already has been cloned
commit_hash = get_commit_hash(row[1])
success = clone_repo(row[1], row[2]) unless check_repo_exists(row[1])
if (success == true)
write_sync(row, commit_hash) unless commit_hash.nil?
if (row[3] == 'handlers')
move_handlers(row[0], row[1])
elsif (row[3] == 'bridge-adapters')
move_bridge(row[0], row[1])
elsif (row[3] == 'file-adapters')
move_file(row[0], row[1])
else
write_log("Row number #{idx + 1}: #{row[3]} is not a valid input.", LOG_FILE)
end
end
}
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment