Class: Dr::GitPackage

Inherits:
Package show all
Defined in:
lib/dr/gitpackage.rb

Instance Attribute Summary

Attributes inherited from Package

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Package

#<=>, #build_exists?, #history, #remove_build

Methods included from Logger

#log, log, set_logfile, set_verbosity, #tag

Constructor Details

#initialize(name, repo) ⇒ GitPackage

Returns a new instance of GitPackage.



67
68
69
70
71
72
# File 'lib/dr/gitpackage.rb', line 67

def initialize(name, repo)
  super name, repo

  @git_dir = "#{repo.location}/packages/#{name}/source"
  @default_branch = get_current_branch
end

Class Method Details

.setup(repo, git_addr, default_branch, force = false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dr/gitpackage.rb', line 14

def self.setup(repo, git_addr, default_branch, force=false)
  Dir.mktmpdir do |tmp|
    git_cmd = "git clone --mirror --branch #{default_branch} " +
              "#{git_addr} #{tmp}/git"
    ShellCmd.new git_cmd, :tag => "git", :show_out => true

    FileUtils.mkdir_p "#{tmp}/src"

    log :info, "Extracting the sources"
    git_cmd ="git --git-dir #{tmp}/git --bare archive " +
             "--format tar #{default_branch} | tar x -C #{tmp}/src"
    ShellCmd.new git_cmd, :tag => "git", :show_out => true

    unless File.exists? "#{tmp}/src/debian/control"
      log :err, "The debian packaging files not found in the repository"
      raise "Adding a package from #{git_addr} failed"
    end

    src_name = nil
    File.open "#{tmp}/src/debian/control", "r" do |f|
      f.each_line do |line|
        match = line.match(/^Source: (.+)$/)
        if match
          src_name = match.captures[0]
          break
        end
      end
    end

    unless src_name
      log :err, "Couldn't identify the source package"
      raise "Adding a package from #{git_addr} failed"
    end

    pkg_dir = "#{repo.location}/packages/#{src_name}"
    if File.exists? pkg_dir
      log :warn, "The package already exists. Add -f to insert it anyway."
      raise "Adding failed"
    end

    log :info, "Adding #{src_name.style "pkg-name"} to the repository"
    FileUtils.mkdir_p "#{pkg_dir}"

    log :info, "Setting up builds directory"
    FileUtils.mkdir_p "#{pkg_dir}/builds"

    log :info, "Setting up the source directory"
    FileUtils.mv "#{tmp}/git", "#{pkg_dir}/source"

    log :info, "The #{src_name.style "pkg-name"} package added successfully"
  end
end

Instance Method Details

#build(branch = nil, force = false) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/dr/gitpackage.rb', line 141

def build(branch=nil, force=false)
  branch = @default_branch unless branch

  version = nil

  orig_rev, curr_rev = update_from_origin branch

  unless curr_rev
    log :err,  "Branch #{branch.fg "blue"} not found in #{@name.style "pkg-name"}"
    raise "The requested branch doesn't exist in the repository!"
  end

  log :info, "Branch #{branch.fg "blue"}, revision #{curr_rev[0..7].fg "blue"}"
  unless force
    history.each do |v|
       = @repo. @name, v
      if .has_key?("revision") && ["revision"] == curr_rev
        msg = "This revision of #{@name.style "pkg-name"} has already " +
              "been built and is available as #{v.to_s.style "version"}"
        log :info, msg
        return v
      end
    end
  end

  Dir.mktmpdir do |src_dir|
    checkout branch, src_dir

    version_string = get_version "#{src_dir}/debian/changelog"
    unless version_string
      log :err, "Couldn't get the version string from the changelog"
      raise "The changelog format doesn't seem be right"
    end

    version = PkgVersion.new version_string
    log :info, "Source version: #{version.source.style "version"}"

    version.add_build_tag
    while build_exists? version
      version.increment!
    end
    log :info, "Build version: #{version.to_s.style "version"}"

    log :info, "Updating changelog"
    now = Time.new.strftime("%a, %-d %b %Y %T %z")
    ch_entry = "#{@name} (#{version}) kano; urgency=low\n"
    ch_entry << "\n"
    ch_entry << "  * Package rebuilt, updated to revision #{curr_rev[0..7]}.\n"
    ch_entry << "\n"
    ch_entry << " -- Team Kano <[email protected]>  #{now}\n\n"

    changelog = ""
    File.open "#{src_dir}/debian/changelog", "r" do |f|
      changelog = f.read
    end

    File.open "#{src_dir}/debian/changelog", "w" do |f|
      f.write ch_entry
      f.write changelog
    end

    repo_arches = @repo.get_architectures
    pkg_arches = get_architectures("#{src_dir}/debian/control")
    arches = case
      when pkg_arches.include?("any")
        repo_arches
      when pkg_arches.include?("all")
        ["all"]
      else
        repo_arches & pkg_arches
      end

    if repo_arches.length == 0
      log :err, "#{@name.style "pkg-name"} cannot be build for any of " +
                  "the architectures supported by this repository"
      raise "Unable to build the package for this repository"
    end

    benv = :default
    src_meta = get_configuration
    if src_meta.has_key? :build_environment
      benv = src_meta[:build_environment].to_sym
    end

    arches.each do |arch|
      @repo.buildroot(arch, benv).open do |br|
        log :info, "Building the #{@name.style "pkg-name"} package " +
                   "version #{version.to_s.style "version"} for #{arch}"

        # Moving to the proper directory
        build_dir_name = "#{@name}-#{version.upstream}"
        build_dir = "#{br}/#{build_dir_name}"
        FileUtils.cp_r src_dir, build_dir

        # Make orig tarball
        all_files = Dir["#{build_dir}/*"] + Dir["#{build_dir}/.*"]
        excluded_files = ['.', '..', '.git']
        selected_files = all_files.select { |path| !excluded_files.include?(File.basename(path)) }
        files = selected_files.map { |f| "\"#{File.basename f}\"" }.join " "
        log :info, "Creating orig source tarball"
        tar = "tar cz -C #{build_dir} --exclude=debian " +
              "-f #{br}/#{@name}_#{version.upstream}.orig.tar.gz " +
              "#{files}"
        ShellCmd.new tar, :tag => "tar"

        apt = "sudo chroot #{br} apt-get update"
        deps = <<-EOS
sudo chroot #{br} <<EOF
dpkg-source -b "/#{build_dir_name}"
mk-build-deps *.dsc -i -t "apt-get --no-install-recommends -y"
rm -rf #{@name}-build-deps_*
EOF
EOS
      build = <<-EOS
sudo chroot #{br} <<EOF
cd /#{build_dir_name}
debuild -i -uc -us -b
EOF
EOS

        log :info, "Updating the sources lists"
        ShellCmd.new apt, :tag => "apt-get", :show_out => true

        log :info, "Installing build dependencies"
        ShellCmd.new deps, :tag => "mk-build-deps", :show_out => true

        log :info, "Building the package"
        ShellCmd.new build, :tag => "debuild", :show_out => true

        debs = Dir["#{br}/*.deb"]
        expected_pkgs = get_subpackage_names "#{src_dir}/debian/control"
        expected_pkgs.each do |subpkg_name|
          includes = debs.inject(false) do |r, n|
            r || ((/^#{br}\/#{subpkg_name}_#{version.to_s omit_epoch=true}/ =~ n) != nil)
          end

          unless includes
            log :err, "Subpackage #{subpkg_name} did not build properly"
            raise "Building #{name} failed"
          end
        end

        build_dir = "#{@repo.location}/packages/#{@name}/builds/#{version}"
        FileUtils.mkdir_p build_dir
        debs.each do |pkg|
          FileUtils.cp pkg, build_dir

          deb_filename = File.basename(pkg)
          log :info, "Signing the #{deb_filename.style "subpkg-name"} package"
          @repo.sign_deb "#{build_dir}/#{deb_filename}"
        end

        log :info, "Writing package metadata"
        File.open "#{build_dir}/.metadata", "w" do |f|
          YAML.dump({"branch" => branch, "revision" => curr_rev}, f)
        end
        log :info, "The #{@name.style "pkg-name"} package was " +
                   "built successfully."
      end
    end
  end
  version
end

#get_configurationObject



124
125
126
127
128
129
130
131
# File 'lib/dr/gitpackage.rb', line 124

def get_configuration
  md_file = "#{@repo.location}/packages/#{@name}/metadata"
  if File.exists? md_file
    Utils::symbolise_keys YAML.load_file md_file
  else
    {}
  end
end

#reinitialise_repo(git_addr = nil, branch = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dr/gitpackage.rb', line 74

def reinitialise_repo(git_addr=nil, branch=nil)
  git_addr ||= get_repo_url
  branch ||= @default_branch

  log :info, "Re-downloading the source repository of " +
             "#{@name.style "pkg-name"}"
  Dir.mktmpdir do |tmp|
    git_cmd = "git clone --mirror --branch #{branch} " +
              "#{git_addr} #{tmp}/git"
    ShellCmd.new git_cmd, :tag => "git", :show_out => true

    src_dir = "#{tmp}/src"
    FileUtils.mkdir_p src_dir

    checkout branch, src_dir, "#{tmp}/git"

    unless File.exists? "#{src_dir}/debian/control"
      log :err, "The debian packaging files not found in the repository"
      raise "Adding a package from #{git_addr} failed"
    end

    src_name = nil
    File.open "#{tmp}/src/debian/control", "r" do |f|
      f.each_line do |line|
        match = line.match(/^Source: (.+)$/)
        if match
          src_name = match.captures[0]
          break
        end
      end
    end

    unless src_name
      log :err, "Couldn't identify the source package"
      raise "Adding a package from #{git_addr} failed"
    end

    unless src_name == @name
      log :err, "The name of the package in the repo has changed"
      raise "Adding a package from #{git_addr} failed"
    end

    src_dir = "#{@repo.location}/packages/#{@name}/source"
    FileUtils.rm_rf src_dir
    FileUtils.mv "#{tmp}/git", "#{src_dir}"
  end

  @default_branch = branch
end

#set_configuration(config) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/dr/gitpackage.rb', line 133

def set_configuration(config)
  # TODO: Some validation needed
  md_file = "#{@repo.location}/packages/#{@name}/metadata"
  File.open(md_file, "w") do |f|
    YAML.dump Utils::stringify_symbols(config), f
  end
end

#tag_release(tag_name, revision, options = {}) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/dr/gitpackage.rb', line 305

def tag_release(tag_name, revision, options={})
  url = get_repo_url

  log :info, "Tagging #{@name.style "pkg-name"} package for " +
             "#{tag_name.fg "yellow"} release"

  gh_repo = case url
    when /git\@github.com\:/i then url.split(":")[1].gsub(/\.git$/, "").strip
    when /github.com\//i then url.split("/")[-2..-1].join("/").gsub(/\.git$/, "").strip
    else nil
  end

  if gh_repo == nil
    git_cmd = "git --git-dir #{@git_dir} tag #{tag}"
    git = ShellCmd.new git_cmd,
      :tag => "git",
      :show_out => false,
      :raise_on_error => false

    if git.status == 128
      log :warn, "Tag #{tag_name.fg "yellow"} already exists."
      return
    end

    git_cmd = "git --git-dir #{@git_dir} push origin --tags"
    git = ShellCmd.new git_cmd, :show_out => false

    return
  end

  title = options["title"] || "Kano OS #{tag_name}"
  summary = options["summary"] || "https://github.com/KanoComputing/peldins/wiki/Changelog-#{tag_name}"

  token = ENV["GITHUB_API_TOKEN"]
  client = Octokit::Client.new :access_token => token

  releases = client.releases gh_repo
  ri = releases.index { |r| r[:tag_name] == tag_name }

  if ri == nil
    client.create_release gh_repo, tag_name,
      :target_commitish => revision,
      :name => title,
      :body => summary
  else
    log :warn, "The #{tag_name.fg "yellow"} release exists already for #{@name.style "pkg-name"}."
  end
end