diff --git a/bin/dr b/bin/dr index 05a9334..3a51c2e 100755 --- a/bin/dr +++ b/bin/dr @@ -288,7 +288,8 @@ class RepoCLI < ExtendedThor :components => ["main"], :suites => ["stable-security", "stable", "testing", "unstable"], :build_environment => :kano, - :codenames => [] + :codenames => [], + :default_branches => [] } name = ask " Repository name "<< "[#{repo_conf[:name].fg("yellow")}]:" @@ -358,6 +359,8 @@ class RepoCLI < ExtendedThor repo_conf[:suites].each do |s| codename = ask " Codename for '#{s.fg("yellow")}':" repo_conf[:codenames].push codename + default_branch = ask " Default branch for '#{s.fg("yellow")}':" + repo_conf[:default_branches].push default_branch end r = Dr::Repo.new location @@ -493,7 +496,7 @@ class RepoCLI < ExtendedThor repo.list_packages(suite).each do |pkg| log :info, "Updating #{pkg.name.style "pkg-name"}" begin - version = pkg.build + version = pkg.build :suite => suite rescue Dr::Package::UnableToBuild log :info, "" next diff --git a/lib/dr/gitpackage.rb b/lib/dr/gitpackage.rb index 58fe388..a78131f 100644 --- a/lib/dr/gitpackage.rb +++ b/lib/dr/gitpackage.rb @@ -5,6 +5,7 @@ require "dr/pkgversion" require "dr/shellcmd" require "dr/utils" +require "dr/config" require "yaml" require "octokit" @@ -67,7 +68,10 @@ def self.setup(repo, git_addr, default_branch, force=false) def initialize(name, repo) super name, repo - @git_dir = "#{repo.location}/packages/#{name}/source" + @pkg_dir = "#{repo.packages_dir}/#{name}" + @git_dir = "#{pkg_dir}/source" + @pkg_metadata_path = "#{@pkg_dir}/metadata" + @default_branch = get_current_branch end @@ -122,9 +126,8 @@ def reinitialise_repo(git_addr=nil, branch=nil) end def get_configuration - md_file = "#{@repo.location}/packages/#{@name}/metadata" - if File.exists? md_file - Utils::symbolise_keys YAML.load_file md_file + if File.exists? @pkg_metadata_path + Utils::symbolise_keys YAML.load_file @pkg_metadata_path else {} end @@ -132,14 +135,13 @@ def get_configuration def set_configuration(config) # TODO: Some validation needed - md_file = "#{@repo.location}/packages/#{@name}/metadata" - File.open(md_file, "w") do |f| + File.open(@pkg_metadata_path, "w") do |f| YAML.dump Utils::stringify_symbols(config), f end end - def build(branch=nil, force=false) - branch = @default_branch unless branch + def build(branch=nil, force=false, suite=nil) + branch = get_default_branch(:suite => suite) unless branch version = nil @@ -412,6 +414,49 @@ def get_architectures(control_file) arches.uniq end + def get_default_branch(suite = nil, conf = nil) + if conf.nil? + conf = get_configuration + end + + unless suite.nil? + if conf.has_key? :suites + suites = src_meta[:suites] + + if suites.has_key? suite + suite_conf = suites[suite] + + if suite_conf.has_key? :default_branch + return suite_conf[:default_branch].to_sym + end + end + end + end + + if conf.has_key? :default_branch + return conf[:default_branch].to_sym + end + + suite_conf = @repo.get_suite_config suite + if suite_conf.has_key? :default_branch + return suite_conf[:default_branch].to_sym + end + + repo_conf = @repo.get_configuration + if repo_conf.has_key? :default_branch + return repo_conf[:default_branch].to_sym + end + + if Dr::config.has_key? :default_branch + return Dr::config[:default_branch].to_sym + end + + # TODO: Figure out what the default should be + @default_branch + # get_current_branch + # 'master' + end + def get_current_branch git_cmd = ShellCmd.new "git --git-dir #{@git_dir} --bare branch", { :tag => "git" diff --git a/lib/dr/repo.rb b/lib/dr/repo.rb index 9b8bca1..765f3f8 100644 --- a/lib/dr/repo.rb +++ b/lib/dr/repo.rb @@ -21,6 +21,9 @@ class Repo include Logger attr_reader :location + attr_reader :packages_dir + attr_reader :repo_metadata_path + attr_reader :pkg_build_meta_template def get_archive_path "#{@location}/archive" @@ -30,6 +33,12 @@ def initialize(loc) @location = File.expand_path loc @packages_dir = "#{@location}/packages" + + @repo_archive_dir = "#{@location}/archive" + @repo_conf_dir = "#{@repo_archive_dir}/conf" + @repo_conf_path = "#{@repo_conf_dir}/distributions" + @repo_metadata_path = "#{@location}/metadata" + @pkg_build_meta_template = "#{@packages_dir}/%{pkg_name}/builds/%{version}/.metadata" end def setup(conf) @@ -41,15 +50,15 @@ def setup(conf) raise e end - FileUtils.mkdir_p "#{@location}/archive" + FileUtils.mkdir_p @repo_archive_dir gpg = GnuPG.new "#{@location}/gnupg-keyring" key = gpg.generate_key conf[:gpg_name], conf[:gpg_mail], conf[:gpg_pass] - gpg.export_pub key, "#{@location}/archive/repo.gpg.key" + gpg.export_pub key, "#{@repo_archive_dir}/repo.gpg.key" log :info, "Writing the configuration file" - FileUtils.mkdir_p "#{@location}/archive/conf" - File.open "#{@location}/archive/conf/distributions", "w" do |f| + FileUtils.mkdir_p @repo_conf_dir + File.open @repo_conf_path, "w" do |f| conf[:suites].each_with_index do |s, i| f.puts "Suite: #{s}" @@ -57,6 +66,10 @@ def setup(conf) f.puts "Codename: #{conf[:codenames][i]}" end + if conf[:default_branches][i].length > 0 + f.puts "DefaultBranch: #{conf[:default_branches][i]}" + end + if conf[:name].length > 0 f.puts "Origin: #{conf[:name]} - #{s}" f.puts "Label: #{conf[:name]} - #{s}" @@ -80,15 +93,14 @@ def setup(conf) metadata = { "default_build_environment" => conf[:build_environment].to_s } - File.open("#{@location}/metadata", "w" ) do |out| + File.open(@repo_metadata_path, "w" ) do |out| out.write metadata.to_yaml end end def get_configuration - meta_file = "#{@location}/metadata" - if File.exists? meta_file - Utils::symbolise_keys YAML.load_file(meta_file) + if File.exists? @repo_metadata_path + Utils::symbolise_keys YAML.load_file(@repo_metadata_path) else {} end @@ -96,7 +108,7 @@ def get_configuration def set_configuration(new_metadata) # TODO: Some validation needed - File.open("#{@location}/metadata", "w" ) do |out| + File.open(@repo_metadata_path, "w" ) do |out| out.write Utils::stringify_symbols(new_metadata).to_yaml end end @@ -143,33 +155,43 @@ def get_package(name) end end - def get_suites + def get_suites_config suites = nil - File.open "#{@location}/archive/conf/distributions", "r" do |f| + File.open @repo_conf_path, "r" do |f| suites = f.read.split "\n\n" end suites.map do |s| - suite = nil - codename = nil - s.each_line do |l| - m = l.match /^Suite: (.+)/ - suite = m.captures[0].chomp if m - - m = l.match /^Codename: (.+)/ - codename = m.captures[0].chomp if m + YAML.load s + end + end + + def get_suite_config(suite) + suites = get_suites_config + + suites.each do |s| + if s["Suite"] == suite || s["Codename"] == suite + return s end - [suite, codename] + end + + nil + end + + def get_suites + suites = get_suites_config + + suites.map do |s| + [s["Suite"], s["Codename"]] end end def get_architectures arches = [] - File.open "#{@location}/archive/conf/distributions", "r" do |f| - f.each_line do |l| - m = l.match /^Architectures: (.+)/ - arches += m.captures[0].chomp.split(" ") if m - end + suites = get_suites_config + + suites.each do |s| + arches += s["Architectures"].chomp.split(" ") if s.has_key? "Architectures" end arches.uniq @@ -364,7 +386,7 @@ def get_build_metadata(pkg_name, version) pkg = get_package pkg_name raise "Build #{version} doesn't exist" unless pkg.build_exists? version - md_file = "#{@location}/packages/#{pkg.name}/builds/#{version}/.metadata" + md_file = @pkg_build_meta_template % {pkg_name: pkg.name, version: version} if File.exists? md_file YAML.load_file md_file else @@ -403,7 +425,7 @@ def is_used?(pkg_name, version=nil) private def get_key - File.open "#{@location}/archive/conf/distributions", "r" do |f| + File.open @repo_conf_path, "r" do |f| f.each_line do |line| m = line.match /^SignWith: (.+)/ return m.captures[0] if m