Bash script to copy files in order to my Coby mp305

By kenglish

I’m one of those people that refuses to get an IPOD. I think they are too expensive and they don’t play nice with Linux.

Last Christmas, I bought myself the 4GB Coby mp305 because it has more capacity than the Sandisk Sansa m200. The interface is crap compared to the Sansa m200. It doesn’t read the mp3 ID3tags at all. The navigation tree is simply the directory structure.

The major flaw is that it does not always sort files in the directory in the correct order. I finally figured out that it sorts files by the order that they were put on the device. However, for some reason in linux if you do “cp -R”, it doesn’t put them on in the proper order.

Here’s my script to put files on the device, it’s call coby_copy.sh:

#!/bin/bash
 
if [ !-d $1 ]; then
   echo "Source Directory does not exists"
   exit
fi
 
if [ !-d $2 ]; then
   echo "Target Directory does not exists"
   exit
fi
 
echo "arg1 = $1 arg2 = $2"
 
IFS=`echo -en "\n\b"`
 
for FILENAME in `find $1 -type f -iname "*mp3" -print | sort | sed 's/^\.\///'` 
do
  DIR=`dirname $FILENAME`
  mkdir -p $2/$DIR
  echo $FILENAME
  cp $FILENAME "$2/$DIR"
done

To run it:

coby_copy.sh "Harry Potter and Leopard-Walk-Up-to-Dragon"  "/mnt/disk/Audio Books"

categoriaLinux, Programming commentoNo Comments dataAugust 18th, 2009
Read All

MsSql: Select table column names

By kenglish

Sometimes I need to match table column names in Microsoft SQL Server. This seems to be the best way to do it:

SELECT COLUMN_NAME, TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%COST%'

categoriaProgramming commentoNo Comments dataAugust 18th, 2009
Read All

Install ruby gem libxml-ruby on Ubuntu 9.04 (Jaunty)

By kenglish

Quick note on how to install the libxml-ruby gem on Ubuntu:

sudo apt-get install libxml2 libxml2-dev
sudo gem install libxml-ruby

categoriaLinux, Programming commentoNo Comments dataAugust 18th, 2009
Read All

What makes a really good Ruby IDE?

By kenglish

Chad Woolley over at pivotal has a blog entry about The Great Ruby IDE Smackdown of ‘09. He compares the IDE’s by doing a task that no Rails developer will ever need to do. What do we really do all day: Model, View, Controller, Test, routes, etc. The IDE should provide an easy way to switch between these. Netbeans does this. Aptana does this. RudyMine does this. They are all functional and when used properly, very effecient. What really matter to me? VI intergration. Netbeans has this with jVi. I love it.

I got a kick out of this:

“To me, the benefits of a memory- and processor-sucking IDE with tons of unnecessary, unconfigurable, resource-eating tiny-ass-fonts and chrome did not justify giving up the speed and responsiveness of a great text editor.”

Memory- and processor-sucking IDE? Is he running a 486dx? Are Macs really that slow? Dude, switch to linux! Or, here’s 10 Reasons You Should Not Switch To Linux.

Here’s another nice feature of NetBeans that your Text Editor won’t do. Notice on line 127, I have a mispelling of the word worksheet. Netbeans bolds the misspelled varialbe to tell me that I have a variable here that has never been used before. This is very helpful.

NetBeans-coolness

categoriaProgramming commentoNo Comments dataJuly 16th, 2009
Read All

Example SQL Report with the Ruby Spreadsheet Gem

By kenglish

If you want to create a Excel reports for your users, this can be done rather easily in Ruby using the Spreadsheet Gem.

def spreadsheet_report(excel_filename, worksheet_name, 
                       column_order, result)
    book = Spreadsheet::Workbook.new
    sheet1 = book.create_worksheet :name => worksheet_name
 
    rownum = 0
    for column in column_order
      sheet1.row(rownum).push column
    end
    for row in result
      rownum += 1
      for column in column_order
        sheet1.row(rownum).push row[column].nil? ? 'N/A' : row[column]
      end
    end
    book.write "#{excel_filename}.xls"
 
end

Here’s what the code would like in your rake task

column_order = ["Name", "DOB", "Rank","Hire Date", 
                "Height", "Weight" ]
 
sql =<<-END
  SELECT name AS Name,
   date_of_birth AS DOB, 
   rank AS Rank,
   hire_date Hire Date,
   height AS Height,
   weight AS  Weight
  FROM fire_fighters
  ORDER BY name
END
 
conn  = ActiveRecord::Base.connection
result    = conn.select_all(sql)
 
excel_filename = "FireFighterReport#{Time.year}" 
worksheet_name = "FireFighter Report #{Time.year}"
spreadsheet_report(excel_filename, worksheet_name, 
                   column_order, result)

Now, that’s easy!

Ruby Spreadsheet Gem Documentation

categoriaMysql, Programming commento1 Comment dataJune 17th, 2009
Read All

svn2git for real men

By kenglish

On the server, set up the remote repositories:

1
2
3
mkdir project1.git
cd project1.git
git --bare init

Here’s a script to do them all in one shot, just modifiy the REPOS variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh
 
REPOS="project1 project2 project3 project4"
 
for repo in $REPOS
do
    repo_dir="$repo.git"
    mkdir -p $repo_dir
    echo "Creating git directory  $repo_dir"
    cd $repo_dir
    git --bare init
    cd ..
done
exit

Now, on the workstation:

1
2
sudo apt-get install git-core git-svn
sudo gem install nirvdrum-svn2git --source http://gems.github.com

Create the authors.txt in the following format:

1
2
dburger = David Burger <email@email.com>
jdoe = John Doe <jdoe@doe.com>

For one project, do the following:

1
2
3
4
5
mkdir project1
cd project1
svn2git  https://svn.myserver.org/repos/ses --authors ../authors.txt
git remote add origin hailstorm.myserver.org:/home/kenglish/repotest/ses.git
git push --all

The script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/sh
 
REPOS="project1 project2 project3 project4"
 
for repo in $REPOS
do
    mkdir $repo
    cd $repo
 
    cmd="svn2git https://svn.myserver.org/repos/$repo --authors ../authors.txt"
    echo $cmd
    `$cmd`
    cmd="git remote add origin hailstorm.myserver.org:/home/kenglish/repotest/$repo.git"
    echo $cmd
    `$cmd`
    cmd="git push --all"
    echo $cmd
    `$cmd`
    cd ..
    echo "DONE EXPORTING $repo" 
done
exit

Don’t diss the shell script, I leave in the echoes in case i need to test stuff out…

Note: Another option for the authors file is place it in your home directory .svn2git/authors (e.g. /home/kenglish/.svn2git/authors). Svn2git will automatically detect it and use it.
Any questions? Comments?

categoriaMysql, Programming, Tech commento2 Comments dataMay 21st, 2009
Read All

config.gem: Unpacked gem ezcrypto-0.7 in vendor/gems has no specification file. Run ‘rake gems:refresh_specs’ to fix this.

By kenglish

Thanks to Giles, I finally found the answer to this problem.

Open config/environment.rb and add “Rails::VendorGemSourceIndex.silence_spec_warnings = true”

1
2
3
4
5
6
RAILS_GEM_VERSION = '2.2.2' unless defined? RAILS_GEM_VERSION
 
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
 
Rails::VendorGemSourceIndex.silence_spec_warnings = true

You can put an awesome comment like this guy did:

http://gilesbowkett.blogspot.com/2009/03/configgem-unpacked-gem-whatever-in.html

categoriaProgramming, Tech commentoNo Comments dataMarch 18th, 2009
Read All

Getting started with BioRuby and Ruby on Rails.

By kenglish

Started messing with Bioruby this week. I went through George Githinji’s excellent tutorial on his biorlated blog, Bio-graphics, BioSQL and Ruby on Rails part 1 and Bio-graphics, BioSQL and Ruby on Rails part 2. This is very helpful. Before you do the stuff below, read through his tutorial so you understand what’s going on. Here’s what I had to do to get it working:

Install the gems
I got started by installing the bio, bioruby gem.

sudo gem install bioruby
sudo gem install bio

I discovered that the bio-graphics gem needs cairo. Here’s how to install it on Ubuntu. This had 50-something dependencies. I just sat back and let them install:

sudo apt-get install cairo-clock cairo-dock-dev libcairo-ruby libcairo2

I also had to install ruby support for pango:

sudo apt-get install -y libpango1-ruby

I installed bio-graphics and the cairo gem.

gem install bio-graphics cairo

George also uses rails_sql_views so I installed this:

sudo gem install rails_sql_views

As George warned in his blog, I had to comment out these lines in

unless Kernel.respond_to?(:gem)
  Kernel.send :alias_method, :gem, :require_gem
end

in rails_sql_views(0.6.1). Mine was located in /usr/lib/ruby/gems/1.8/gems/rails_sql_views-0.6.1/lib/rails_sql_views.rb. I commented out lines 28-30.

Install the biosql_rails_example application

To get george’s app running, I cloned the biorelate repository into my /home/kenglish/workspace directory:

cd /home/kenglish/workspace
git clone  git://github.com/georgeG/biosql_rails_example.git

I edited the database.yml file and added my mysql infomration. You can create the database by hand as George explains but I put in the mysql root username and password in the database.yml file and ran:

rake db:create.

Install the biosql database schema

As George explains, I had to download the biosql schema and untared it. I created the tables like this:

cd /home/kenglish/downloads/
tar xzvf biosql-1.0.1.tar.gz
cd biosql-1.0.1/sql
mysql -uroot -p var_biosql_development < biosqldb-mysql.sql

I put these in the directory biosql-1.0.1/scripts and loaded them with the script load_ncbi_taxonomy.pl.

perl load_ncbi_taxonomy.pl --dbname var_biosql_development  --dbuser root --host localhost --download

I don’t have any bio-related data so I downloaded the following files from the Ncbi ftp site (ftp://ftp.ncbi.nih.gov/pub/taxonomy/): gi_taxid_nucl.dmp.gz taxcat.tar.gz and taxdump.tar.gz.

Now I was able to run George’s Rails app! To be continued…

categoriaProgramming commentoNo Comments dataMarch 5th, 2009
Read All

Running an individual shoulda context test

By kenglish

I’m suffering from serious CRS. Yesterday, I was wondering how to run my shoulda tests individually. My co-worker said “Just google for it.” I entered a google search term and the 4th or 5th result was a post by ME to the shoulda google group. This was the post: Can you controller context test just one test via the -n flag?

To reinforce the lesson, you can run a single context using a regular expression.

context "on GET to :show for first record" do
  setup do
    get :show, :id => 1
  end
 
  should_assign_to :sweater
  should_respond_with :success
  should_render_template :show
end

Although, you need to put quotes around the regular expression or the command line interpreter mistaked the spaces for separate arguments:

ruby test/functional/sweater_controller_test.rb -n "/show for first record/"

categoriaProgramming, Tech commentoNo Comments dataFebruary 25th, 2009
Read All

Using gitk

By kenglish

Just installed gitk. Very nice…. Here’s how to install it on Ubuntu:

apt-get install gitk

Awesome git resources:

gitready.com
gitcasts.com

I hope this post has been helpful.

categoriaLinux, Programming commentoNo Comments dataFebruary 25th, 2009
Read All