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.

81 lines
1.9 KiB

require "fileutils"
require_relative "db"
PROJECTS_STRUCTURE = {
:REPO => "repo",
:LOGS => "logs",
:CONFIGS => "configs",
:SRCPRP => "srcprp",
:SIGNED => "signed",
}
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 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")
FileUtils.cp(configuration, conf_path)
@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
end