Files
mock-gui/classes/projects.rb

203 lines
5.9 KiB
Ruby
Raw Normal View History

2025-02-24 23:54:28 +03:00
require "fileutils"
2025-02-23 23:59:45 +03:00
require_relative "db"
2025-03-08 23:57:56 +03:00
require_relative "repomanage"
2025-02-23 23:59:45 +03:00
2025-02-24 23:54:28 +03:00
PROJECTS_STRUCTURE = {
:REPO => "repo",
:LOGS => "logs",
:CONFIGS => "configs",
:SRCPRP => "srcprp",
:SIGNED => "signed",
2025-03-01 00:07:20 +03:00
:SRC => "src",
2025-02-24 23:54:28 +03:00
}
2025-02-23 23:59:45 +03:00
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
2025-03-08 23:57:56 +03:00
@db.proj_list
2025-02-23 23:59:45 +03:00
end
2025-02-24 23:54:28 +03:00
2025-02-26 23:55:39 +03:00
def get_project(id)
2025-03-08 23:57:56 +03:00
@db.proj(id.to_i)
2025-02-26 23:55:39 +03:00
end
2025-03-01 00:07:20 +03:00
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
2025-03-08 00:13:31 +03:00
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
2025-03-01 23:38:05 +03:00
def get_project_path_git(id, gitname)
proj_path = get_project_path(id)
File.join(proj_path, PROJECTS_STRUCTURE[:SRC], gitname)
end
2025-03-08 23:57:56 +03:00
def generate_linked_repos(id, proj_path, proj_name)
linked_prj = []
proj_repo_path = File.join(proj_path, PROJECTS_STRUCTURE[:REPO])
proj_repo = <<~PRJ_CFG
[#{proj_name}-repository]
name=Project repository #{proj_name}
baseurl=file://#{proj_repo_path}
enabled=1
priority=80
skip_if_unavailable=True
PRJ_CFG
linked_prj << proj_repo
unless id.nil?
link_prj_list = @db.get_projects_links(id)
unless link_prj_list.nil?
link_prj_list.each do |item|
internal_repo = ProjectsActions.new(@path, @db)
internal_path = internal_repo.get_project_path(item[:proj_id_repository])
internal_repo_path = File.join(internal_path, PROJECTS_STRUCTURE[:REPO])
internal_proj_info = internal_repo.get_project(item[:proj_id_repository])
proj_repo = <<~PRJ_CFG
[#{internal_proj_info[:projname]}-repository]
name=Project repository #{internal_proj_info[:projname]}
baseurl=file://#{internal_repo_path}
enabled=1
skip_if_unavailable=True
PRJ_CFG
linked_prj << proj_repo
end
end
end
File.open(prj_incl_path, "w") { |f| f << linked_prj.join("\n\n\n") }
end
def generate_config(id, configuration_path, proj_path, proj_name)
proj_conf_path = File.join(proj_path, PROJECTS_STRUCTURE[:CONFIGS], "#{proj_name}.cfg")
conf_path = File.join(proj_path, PROJECTS_STRUCTURE[:CONFIGS])
prj_incl_path = File.join(conf_path, "repos_include.tpl")
proj_config = <<~PRJ_CFG
include("#{configuration}")
include("#{prj_incl_path}")
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(proj_conf_path, "w") { |f| f << proj_config }
generate_linked_repos(id, proj_path, proj_name)
end
2025-02-24 23:54:28 +03:00
def create_project(name, description, configuration)
@error = nil
ret_val = 0
project_name = sanitize_rcptname(name)
2025-03-08 23:57:56 +03:00
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)
generate_config(nil, configuration, fname, project_name)
@error = @db.proj_create(project_name, description)
if @error.nil?
created = true
2025-02-24 23:54:28 +03:00
end
2025-03-08 23:57:56 +03:00
repo_path = File.join(fname, PROJECTS_STRUCTURE[:REPO])
repoman = RepoManager.new(repo_path)
repoman.create_repo
else
2025-02-24 23:54:28 +03:00
ret_val = 1
2025-03-08 23:57:56 +03:00
@error = "Конфигурация #{configuration} не существует"
2025-02-24 23:54:28 +03:00
end
2025-03-08 23:57:56 +03:00
rescue => e
ret_val = 1
@error = e.message
end
unless created
FileUtils.rm_rf(fname, secure: true)
2025-02-24 23:54:28 +03:00
end
end
ret_val
end
2025-02-26 23:55:39 +03:00
2025-03-01 00:07:20 +03:00
def get_project_gits(id, repo)
2025-03-08 23:57:56 +03:00
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
2025-03-01 00:07:20 +03:00
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
2025-02-26 23:55:39 +03:00
end
2025-02-28 00:13:04 +03:00
2025-03-01 00:07:20 +03:00
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
2025-02-28 00:13:04 +03:00
end
2025-02-23 23:59:45 +03:00
end