From dbca442bc5b7dce7ac5db2376ea412e2622b7253 Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Sat, 8 Apr 2006 22:09:40 +0200 Subject: [PATCH] initial import --- Rakefile | 39 ++++++++ lib/cruxutils.rb | 7 ++ lib/cruxutils/ohash.rb | 66 +++++++++++++ lib/cruxutils/package.rb | 18 ++++ lib/cruxutils/packagedb.rb | 46 ++++++++++ lib/cruxutils/port.rb | 183 +++++++++++++++++++++++++++++++++++++ test/test_comments.rb | 23 +++++ test/valgrind/Pkgfile | 17 ++++ 8 files changed, 399 insertions(+) create mode 100644 Rakefile create mode 100644 lib/cruxutils.rb create mode 100644 lib/cruxutils/ohash.rb create mode 100644 lib/cruxutils/package.rb create mode 100644 lib/cruxutils/packagedb.rb create mode 100644 lib/cruxutils/port.rb create mode 100644 test/test_comments.rb create mode 100644 test/valgrind/Pkgfile diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..c848b98 --- /dev/null +++ b/Rakefile @@ -0,0 +1,39 @@ +require "rake/clean" +require "rake/testtask" +require "rake/packagetask" +require "rake/contrib/sshpublisher" + +PKG_NAME = "ruby-cruxutils" +PKG_VERSION = "0.1.1" + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.test_files = FileList["test/test_*.rb"] + t.verbose = true +end + +task :install do + destdir = ENV["DESTDIR"] || "" + + ddir = destdir + Config::CONFIG["sitelibdir"] + FileUtils::Verbose.mkdir_p(ddir) unless File.directory?(ddir) + FileUtils::Verbose.install("lib/cruxutils.rb", ddir, :mode => 0644) + + ddir = destdir + Config::CONFIG["sitelibdir"] + "/cruxutils" + FileUtils::Verbose.mkdir_p(ddir) unless File.directory?(ddir) + FileUtils::Verbose.install(Dir["lib/cruxutils/*.rb"], ddir, + :mode => 0644) +end + +Rake::PackageTask.new(PKG_NAME, PKG_VERSION) do |t| + t.need_tar_gz = true + t.package_files.include("[A-Z]*", "lib/cruxutils.rb", + "lib/cruxutils/*.rb", "test/**/*") +end + +task :publish => [:package] do + Rake::SshFilePublisher.new("code-monkey.de", + ".", "pkg", + "#{PKG_NAME}-#{PKG_VERSION}.tar.gz"). + upload +end diff --git a/lib/cruxutils.rb b/lib/cruxutils.rb new file mode 100644 index 0000000..5e54bb2 --- /dev/null +++ b/lib/cruxutils.rb @@ -0,0 +1,7 @@ +module CruxUtils + class CruxUtilsError < StandardError; end +end + +require "cruxutils/port.rb" +require "cruxutils/package.rb" +require "cruxutils/packagedb.rb" diff --git a/lib/cruxutils/ohash.rb b/lib/cruxutils/ohash.rb new file mode 100644 index 0000000..e7c26e5 --- /dev/null +++ b/lib/cruxutils/ohash.rb @@ -0,0 +1,66 @@ +#-- +# PDF::Writer for Ruby. +# http://rubyforge.org/projects/ruby-pdf/ +# Copyright 2003 - 2005 Austin Ziegler. +# +# Licensed under a MIT-style licence. See LICENCE in the main distribution +# for full licensing information. +# +# $Id: ohash.rb,v 1.2 2005/05/16 03:59:21 austin Exp $ +#++ + # Based on [ruby-talk:20551]. Updated to (hopefully) be 1.8 friendly. +class OHash < Hash + alias_method :store, :[]= + alias_method :each_pair, :each + + def initialize(*args) + @keys = [] + super + end + + def []=(key, val) + @keys << key unless has_key?(key) + super + end + + def delete(key) + @keys.delete(key) if has_key?(key) + super + end + + def each + @keys.each { |k| yield k, self[k] } + end + + def each_key + @keys.each { |k| yield k } + end + + def each_value + @keys.each { |k| yield self[k] } + end + + def first + self[@keys[0]] + end + + def last + self[@keys[-1]] + end + + def first?(item) + self[@keys[0]] == item + end + + def last?(item) + self[@keys[-1]] == item + end + + def replace(other) + other.each do |key, value| + self[key] = value + end + + self + end +end diff --git a/lib/cruxutils/package.rb b/lib/cruxutils/package.rb new file mode 100644 index 0000000..5fc1368 --- /dev/null +++ b/lib/cruxutils/package.rb @@ -0,0 +1,18 @@ +module CruxUtils + +class Package + attr_reader :name, :version, :release, :files + + def initialize(name, version, release, files) + @name = name.to_str.dup.freeze + @version = version.freeze + @release = release.freeze + @files = files.dup.freeze + end + + def filename + "%s#%s-%s.pkg.tar.gz" % [@name, @version, @release] + end +end + +end diff --git a/lib/cruxutils/packagedb.rb b/lib/cruxutils/packagedb.rb new file mode 100644 index 0000000..11f769c --- /dev/null +++ b/lib/cruxutils/packagedb.rb @@ -0,0 +1,46 @@ +module CruxUtils + +class PackageDb + class PackageDbError < CruxUtilsError; end + class PackageNotFoundError < PackageDbError; end + + FILE = "/var/lib/pkg/db" + + def PackageDb.open + db = PackageDb.new + + yield db + ensure + db.close + end + + def initialize + @lines = File.read(FILE).split("\n").map { |l| l.strip } + end + + def close + end + + def [](name) + name = name.to_str.strip + found = -1 + + found = @lines.find { |l| l == name } + raise(PackageNotFoundError) if found.nil? + + i = @lines.index(found) + + version, release = @lines[i + 1].match(/^(.*)-(\d+)$/).captures + files = [] + + (i + 2).upto(@lines.length) do |i| + break if @lines[i].strip.empty? + + files << @lines[i] + end + + Package.new(name, version, release, files.sort) + end +end + +end diff --git a/lib/cruxutils/port.rb b/lib/cruxutils/port.rb new file mode 100644 index 0000000..0e84fa5 --- /dev/null +++ b/lib/cruxutils/port.rb @@ -0,0 +1,183 @@ +require "cruxutils/ohash.rb" +require "tempfile" +require "fileutils" + +module CruxUtils + +class Port + COMMENT_MAP = OHash.new + COMMENT_MAP["Description"] = :description + COMMENT_MAP["URL"] = :url + COMMENT_MAP["Packager"] = :packager + COMMENT_MAP["Maintainer"] = :maintainer + COMMENT_MAP["Depends on"] = :dependencies + COMMENT_MAP["Group"] = :group + COMMENT_MAP["Nice to have"] = :nice_to_have + + def Port.open(dir) + yield Port.new(dir) + end + + def initialize(dir) + @dir = dir + + @attribs = Hash.new { |h, k| h[k] = "" } + + @attribs[:dependencies] = Dependencies.new + @attribs[:source] = Sources.new + @attribs[:build] = BuildFunc.new + + read + end + + def method_missing(meth, *args) + if meth.to_s.match(/=$/) then + m2 = meth.to_s[0...-1].to_sym + @attribs[m2] = (args.length < 2 ? args.first : args) + else + @attribs[meth] + end + end + + def dependencies + @attribs[:dependencies].uniq! + @attribs[:dependencies] + end + + def has_footprint? + File.exist?(footprint) + end + + def has_md5sum? + File.exist?(md5sum) + end + + def write + Tempfile.open("pkgfile") do |file| + COMMENT_MAP.each do |key, symbol| + value = @attribs[symbol] + next if value.empty? + + file << "# #{key}: " << value << "\n" + end + + file << "\n" + + vars = [:name, :version, :release, :source] + vars.each do |var| + file << "#{var}=" << + @attribs[var] << "\n" + end + + file << "\n" << build << "\n" + file.flush + + FileUtils.cp(file.path, pkgfile) + end + end + + private + def read + vars = [:name, :version, :release] + section = :comments + + cmt_map = OHash.new.replace(COMMENT_MAP) + + File.read(pkgfile).split("\n").each do |line| + line.chomp! + + cmt_map.each do |key, symbol| + md = line.match(/^#\s*#{key}: (.+)$/) + unless md.nil? + @attribs[symbol].replace(md.captures.first.strip) + cmt_map.delete(key) + end + end + + vars.each do |var| + md = line.match(/^#{var}=(.+)$/) + unless md.nil? + @attribs[var] = $1.strip + vars.delete(key) + end + end + + # handle source array + section = :void if line.match(/^\s*$/) && section != :build + + if section == :source && line.match(/(.+)\)$/) + @attribs[:source] << $1.strip + elsif section == :source && line.match(/(.+)\\$/) + @attribs[:source] << $1.strip + elsif line.match(/^source=\((.+)\)$/) + @attribs[:source] << $1.strip + elsif line.match(/^source=\((.+)\\$/) + @attribs[:source] << $1.strip + section = :source + end + + section = :end unless line.match(/^\}/).nil? + if section == :build + # remove initial indentation + tmp = line.sub(/^( |\t)/, "") + + @attribs[:build] << (tmp || "") + end + section = :build unless line.match(/^build/).nil? + end + end + + def pkgfile + File.join(@dir, "Pkgfile") + end + + def footprint + File.join(@dir, ".footprint") + end + + def md5sum + File.join(@dir, ".md5sum") + end +end + +class Dependencies < Array + def to_s + uniq.join(", ") + end + + def replace(arg) + if arg.is_a?(Array) + super + else + clear + + arg.split(", ").each { |d| self << d.strip } + end + + uniq! + + self + end +end + +class Sources < Array + def to_s + indent = " " * "source=(".length + "(" + join(" \\\n" + indent) + ")" + end +end + +class BuildFunc < Array + def to_s + inject("build() {\n") do |a, line| + if line != "" + line = "\t" + line + end + + a << line << "\n" + a + end + "}" + end +end + +end diff --git a/test/test_comments.rb b/test/test_comments.rb new file mode 100644 index 0000000..2fcc748 --- /dev/null +++ b/test/test_comments.rb @@ -0,0 +1,23 @@ +require "test/unit" +require "cruxutils" + +class TestComments < Test::Unit::TestCase + def setup + @port = CruxUtils::Port.new(File.dirname(__FILE__) + "/valgrind") + end + + def test_comments + assert_equal("An open-source memory debugger for x86-GNU/Linux", + @port.description) + assert_equal("http://valgrind.org", @port.url) + assert_equal("Tilman Sauerbeck, tilman at code-monkey dot de", + @port.maintainer) + assert_equal("", @port.packager) + assert_equal(["foo", "bar"], @port.dependencies) + assert_equal("valgrind", @port.name) + assert_equal("3.1.0", @port.version) + assert_equal("1", @port.release) + assert_equal(["http://$name.org/downloads/$name-$version.tar.bz2"], + @port.source) + end +end diff --git a/test/valgrind/Pkgfile b/test/valgrind/Pkgfile new file mode 100644 index 0000000..18c6020 --- /dev/null +++ b/test/valgrind/Pkgfile @@ -0,0 +1,17 @@ +# Description: An open-source memory debugger for x86-GNU/Linux +# URL: http://valgrind.org +# Maintainer: Tilman Sauerbeck, tilman at code-monkey dot de +# Depends on: foo, bar + +name=valgrind +version=3.1.0 +release=1 +source=(http://$name.org/downloads/$name-$version.tar.bz2) + +build() { + cd $name-$version + ./configure --prefix=/usr + make + make DESTDIR=$PKG install + rm -rf $PKG/usr/share +} -- 2.30.2