Today, we’ll use
Tangerine (download at
the end of this post) to convert the ports information file of
RPA into a
Debian-dctrl-like format. This has some
advantages, for example, we will be able to use
grep-dctrl and other
tools that expect the Debian-format.
The ports.info is a simply
YAML file with entries like these:
A text progress bar library.
Ruby/ProgressBar is a text progress bar library for Ruby. It can indicate
progress with percentage, a progress bar, and estimated remaining time.
So, let’s write a small script to load a YAML file and apply a
Tangerine template on it:
# Convenient hashes
# Defined methods can be accessed by prefixing with _, e.g. _to_s.
def method_missing(name, *args)
name = name.to_s
name = name[1..-1] if name =~ /^_/
object = YAML.load File.read(ARGV)
We also added an extension to Hash to write
hash.key instead of
hash[key]. This makes using Tangerine easier (The better solution
would have been to make YAML generate a OpenStruct, but I just
couldn’t get it doing that…).
For testing, the first template: Simply show all packages:
Save that into a file, and run it like this:
$ ruby yaml2whatever.rb ports.info < packages.tng
You should now see a list of packages.
This template is interpreted as follows: For each
self (i.e. the
list of entries), output
metadata.name and a newline.
Now, to our debian-like output:
Description: ,,(metadata.description.gsub(/^$/, '.'))
This template has a small twist. Usually
block for each element. But since we only want to save
metadata. all the time, we need to make an array out of it,
,,# in the Depends-line stops
Tangerine from outputting a superfluous newline (Think
% in TeX).
$ ruby yaml2whatever.rb ports.info < debian.tng
Voila, a nice package list. Now, let’s query it, for example all
packages related to XML, but show only the package name:
$ ruby yaml2whatever.rb ports.info < debian.tng | grep-dctrl -s Package XML -
Of course, this was an relatively easy job (RPA already has an query
mechanism too :-)), but what if we’d like to have HTML or XML output?
We’d simply need to adjust the templates without changing any code.
The whole job could have been done using plain Ruby of course, but
such programs get confusing quickly due to mixing of code, strings and
This is a bugfix release without any new features.
Ich trage einen Sturzhelm, weil ich sturzbesoffen bin.
NP: Bob Dylan — License To Kill (Live in Austria)