You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

require "fileutils"
require_relative "db"
PROJECTS_STRUCTURE = {
:REPO => "repo",
:LOGS => "logs",
:CONFIGS => "configs",
:SRCPRP => "srcprp",
:SIGNED => "signed",
:SRC => "src",
}
class ProjectsActions
attr :path, :error, :db
def initialize(path, db)
@path = nil
@error = nil
@db = db
if File.absolute_path?(path)
if File.exist?(path)
@path = path
end
else
apath = File.realpath(path)
if File.exist?(apath)
@path = apath
end
end
end
def get_projects
prj = []
File.open("locks/prjcreate", "r") do |f|
f.flock(File::LOCK_SH)
prj = @db.proj_list
end
prj
end
def get_project(id)
prj = nil
File.open("locks/prjcreate", "r") do |f|
f.flock(File::LOCK_SH)
prj = @db.proj(id.to_i)
end
prj
end
def get_project_path(id)
@error = nil
fname = nil
prj = @db.proj(id)
if prj.nil?
@error = "Проекта с id = #{id} не существует"
else
fname = File.expand_path("#{prj[:projname]}.prj", @path)
end
fname
end
def get_project_config(id)
@error = nil
fname = nil
prj = @db.proj(id)
if prj.nil?
@error = "Проекта с id = #{id} не существует"
else
fname = File.expand_path("#{prj[:projname]}.prj", @path)
fname = File.join(fname, PROJECTS_STRUCTURE[:CONFIGS], "#{prj[:projname]}.cfg")
end
fname
end
def get_project_path_git(id, gitname)
proj_path = get_project_path(id)
File.join(proj_path, PROJECTS_STRUCTURE[:SRC], gitname)
end
def create_project(name, description, configuration)
@error = nil
ret_val = 0
project_name = sanitize_rcptname(name)
File.open("locks/prjcreate", "r") do |f|
f.flock(File::LOCK_EX)
fname = File.expand_path("#{project_name}.prj", @path)
if File.exist?(fname)
@error = "Проект с таким именем уже существует: #{project_name}"
ret_val = 1
else
created = false
begin
Dir.mkdir(fname)
PROJECTS_STRUCTURE.each_pair do |key, value|
new_path = File.join(fname, value)
Dir.mkdir(new_path)
end
if File.exist?(configuration)
conf_path = File.join(fname, PROJECTS_STRUCTURE[:CONFIGS], "#{project_name}.cfg")
proj_config = <<~PRJ_CFG
include("#{configuration}")
config_opts['plugin_conf']['ccache_enable'] = True
config_opts['plugin_conf']['ccache_opts']['max_cache_size'] = '4G'
config_opts['plugin_conf']['ccache_opts']['hashdir'] = True
config_opts['plugin_conf']['ccache_opts']['debug'] = False
config_opts['plugin_conf']['ccache_opts']['show_stats'] = True
config_opts['plugin_conf']['package_state_enable'] = True
config_opts['plugin_conf']['procenv_enable'] = True
config_opts['plugin_conf']['root_cache_enable'] = True
config_opts['plugin_conf']['showrc_enable'] = True
config_opts['plugin_conf']['yum_cache_enable'] = True
PRJ_CFG
File.open(conf_path, "w") { |f| f << proj_config }
@error = @db.proj_create(project_name, description)
if @error.nil?
created = true
end
else
ret_val = 1
@error = "Конфигурация #{configuration} не существует"
end
rescue => e
ret_val = 1
@error = e.message
end
unless created
FileUtils.rm_rf(fname, secure: true)
end
end
end
ret_val
end
def get_project_gits(id, repo)
res = []
File.open("locks/prjcreate", "r") do |f|
f.flock(File::LOCK_EX)
res = @db.get_gits_for_projects(id)
res_sync = res.map do |item|
prj_p = get_project_path(id)
path = File.join(prj_p, PROJECTS_STRUCTURE[:SRC], item[:reponame])
item[:is_repo_synced] = repo.is_repos_sync(item[:reponame], path)
item
end
end
res
end
def add_git_to_project(prj_id, git_id, repo, git_name)
path = get_project_path(prj_id)
if @error.nil?
path = File.join(path, PROJECTS_STRUCTURE[:SRC], git_name)
err = repo.clone_repo_master(git_id, path)
if err.nil?
@db.save_git_project(prj_id, git_id)
end
else
err = @error
end
err
end
def renew_git_to_project(prj_id, git_id, repo, git_name)
path = get_project_path(prj_id)
if @error.nil?
path = File.join(path, PROJECTS_STRUCTURE[:SRC], git_name)
err = repo.clone_repo_master(git_id, path)
else
err = @error
end
err
end
end