From b9b4b494edeacaff40a02117c3eeb304095a4e2a Mon Sep 17 00:00:00 2001 From: Tom Bettany Date: Thu, 25 Aug 2016 11:46:07 +0100 Subject: [PATCH 1/2] Config: Make configuration paths reusable The configuration paths were hard-coded every time they were used which is prone to misconfiguration when changing the paths and reduces readability for locating these files. Move them into class variables for reuse elsewhere. --- lib/dr/gitpackage.rb | 13 +++++++------ lib/dr/repo.rb | 28 ++++++++++++++++++---------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/dr/gitpackage.rb b/lib/dr/gitpackage.rb index 58fe388..80d9b62 100644 --- a/lib/dr/gitpackage.rb +++ b/lib/dr/gitpackage.rb @@ -67,7 +67,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 +125,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,8 +134,7 @@ 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 diff --git a/lib/dr/repo.rb b/lib/dr/repo.rb index 9b8bca1..4354d34 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}" @@ -80,15 +89,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 +104,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 @@ -364,7 +372,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 From bde7791c64d59dc7b3ffedc0fbfed86b85bfa6cb Mon Sep 17 00:00:00 2001 From: Tom Bettany Date: Fri, 26 Aug 2016 11:37:32 +0100 Subject: [PATCH 2/2] [WIP] Default Branches: Permit repos to have a default branch Currently, the way of determining which branch should be used, should the branch CLI option not be provided, is to use the currently checked out branch. This makes it difficult to specify a policy for a suite, or even have a different branch for each suite. Add the beginnings of an approach to permit default branches to be specified by providing an option in the suite definitions which can be overridden by the package specific options. --- bin/dr | 7 +++++-- lib/dr/gitpackage.rb | 48 ++++++++++++++++++++++++++++++++++++++++++-- lib/dr/repo.rb | 48 ++++++++++++++++++++++++++++---------------- 3 files changed, 82 insertions(+), 21 deletions(-) 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 80d9b62..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" @@ -139,8 +140,8 @@ def set_configuration(config) 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 @@ -413,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 4354d34..765f3f8 100644 --- a/lib/dr/repo.rb +++ b/lib/dr/repo.rb @@ -66,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}" @@ -151,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 @@ -411,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