Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Default Branches: Permit repos to have a default branch #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions bin/dr
Original file line number Diff line number Diff line change
Expand Up @@ -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")}]:"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
61 changes: 53 additions & 8 deletions lib/dr/gitpackage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "dr/pkgversion"
require "dr/shellcmd"
require "dr/utils"
require "dr/config"

require "yaml"
require "octokit"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -122,24 +126,22 @@ 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
end

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

Expand Down Expand Up @@ -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"
Expand Down
76 changes: 49 additions & 27 deletions lib/dr/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -41,22 +50,26 @@ 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}"

if conf[:codenames][i].length > 0
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}"
Expand All @@ -80,23 +93,22 @@ 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
end

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down