Archive for the ‘automation’ Category
Subversion Parallel Multi-Branch Development And Merging
Reading time: 2 – 2 minutes
As discussed in my previous post, I dislike merging-based-development, preferring Trunk Based Development instead. But, sometimes you’re stuck with a long-lived development branch, and you need to merge changes (subversion tree-conflicts and all). At the end of the post, I have several scripts I used to make this easier. Not the prettiest, but saved a lot of pain when we had major refactorings in trunk, and needed to locate and merge the changes to those files in a long lived (read: horrible) dev branch.
Imagine this scenario: Multiple streams of development, with a long-lived “3.0 dev” branch that has never reintegrated with the trunk. (Because 3.0 has new features that won’t go into production for many months).
There are substantial dangers in this approach. This diagram only touches on the surface of the areas of risk in which a merge could fail. Solution? Trunk based development / branch by abstraction.
Given this required scenario, I developed a few best practices and scripts for merging. The best practices involved having multiple branches checked out into different directories. And then we would find equivalent files that have moved and merge the tree-conflicts.
Scripts to assist in Subversion 3 way merging.
Custom diff3-cmd configuration setting in svn:
Less Hate with Maven Part 2: The Wrapper Script
Reading time: < 1 minute
I previously wrote about useful debugging techniques with maven. Our maven builds have become complex, with Branch By Abstraction, and about 40 devs working simultaneously on the codebase in 2 continents. We have at least 3 profiles for each of the branch abstractions that are currently running in the codebase. I’m one of the tech leads, and in order to keep the team’s build consistent and easy to remember, we have a wrapper script (thanks to Cosmin).
Here it is:
maven + growlnotify for notification when your build finishes
Reading time: 1 – 2 minutes
Working on os x with Spaces means I want to read something on another space instead of waiting idly for a 50 second build. But, I don’t want to get distracted. So, I use Growl and growlnotify for notifications of the build’s completion.
#!/bin/sh # this file is called: mvn (and is executable, and added to path before actual mvn command) # capture all args passed in to forward to real mvn ARGS=$* # We need the client's specific settings.xml, so always specify it now /usr/bin/mvn -s /Volumes/TrueCryptClient/opt/maven/conf/settings.xml $ARGS # when you have growlnotify installed and on your path, this will pop it up # when the build is done growlnotify -m "DONE: maven $ARGS"
Note: if you get this error from growlnotify: could not find local GrowlApplicationBridgePathway, falling back to NSDNC, it probably means growl is not started. Start up growl in your System Preferences.
Update: Thanks Cosmin, for the enhancement. Use this snipped in the script. Have an environmental variable for what the notify command is. And say what the build status is in the growl notify.:
if [[ -n $NOTIFY ]]; then
($command && $NOTIFY "Build Complete" && exit 0) || ($NOTIFY "Build Failed" && exit 127)
else
$command
fi
How to do 3-way merges with Subversion and Kdiff3
Reading time: 4 – 7 minutes
I do not endorse branch based development. I prefer trunk based development. Specifically I like what my colleague Paul calls Branch By Abstraction, coined by Stacy Curl, and recently mentioned by Martin Fowler (All one time ThoughtWorkers, and 2 currently).
If you’re stuck with merging though, 3-way merges make it much easier. Doing it with subversion is easy. Instructions are for Linux.
- apt-get or yum install kdiff3.
- Edit your /etc/subversion/config and fin the line with diff3-cmd, set it to: diff3-cmd=/usr/local/bin/svndiff.sh
- Next, create the file /usr/local/bin/svndiff.sh. See below for the script you’ll want to enter in it.
Now when you get a merge conflict you will choose M and merge will open in kdiff3. On the left is the base revision, in the middle is your working copy, and on the right the incoming change. This is a little more to look at, but it is invaluable when dealing with merges. I wouldn’t go back to 2 way diff ever again.
#!/bin/bash
# tim/paul: this is a copy of the file located at http://www.yolinux.com/TUTORIALS/src/svndiffwrapper.txt
# modified to do a non-conflicting merge automatically. see #HERE#
# Return an errorcode of 0 on successful merge, 1 if unresolved conflicts
# remain in the result. Any other errorcode will be treated as fatal.
# Author: Michael Bradley
#NOTE: all output must be redirected to stderr with "1>&2" as all stdout output is written to the output file
VDIFF3="kdiff3"
DIFF3="diff3"
DIFF="kdiff3"
promptUser ()
{
read answer
case "${answer}" in
"M" )
echo "" 1>&2
echo "Attempting to merge ${baseFileName} with ${DIFF}" 1>&2
$VDIFF3 $older $mine $theirs --L1 $labelOlder --L2 $labelMine --L3 $labelTheirs -o $output 1>&2
bLoop=1
if [ -f $output ]; then
if [ -s $output ]; then
#output succesfully written
bLoop=0
fi
fi
if [ $bLoop = 0 ]; then
cat $output
rm -f $output
exit 0
else
echo "Merge failed, try again" 1>&2
fi
;;
"m" )
echo "" 1>&2
echo "Attempting to auto-merge ${baseFileName}" 1>&2
diff3 -L $labelMine -L $labelOlder -L $labelTheirs -Em $mine $older $theirs > $output
if [ $? = 1 ]; then
#Can't auto merge
rm -f $output
$VDIFF3 $older $mine $theirs --L1 $labelOlder --L2 $labelMine --L3 $labelTheirs -o $output --auto 1>&2
bLoop=1
if [ -f $output ]; then
if [ -s $output ]; then
#output succesfully written
bLoop=0
fi
fi
if [ $bLoop = 0 ]; then
cat $output
rm -f $output
exit 0
else
echo "Merge failed, try again" 1>&2
fi
else
#We can automerge, and we already did it
cat $output
rm -f $output
exit 0
fi
;;
"diff3" | "Diff3" | "DIFF3" )
echo "" 1>&2
echo "Diffing..." 1>&2
$VDIFF3 $older $mine $theirs --L1 $labelOlder --L2 $labelMine --L3 $labelTheirs 1>&2
;;
"diff" | "Diff" | "DIFF" )
echo "" 1>&2
echo "Diffing..." 1>&2
$DIFF $mine $theirs -L $labelMine -L $labelTheirs 1>&2
;;
"A" | "a" )
echo "" 1>&2
echo "Accepting remote version of file..." 1>&2
cat ${theirs}
exit 0
;;
"I" | "i" )
echo "" 1>&2
echo "Keeping local modifications..." 1>&2
cat ${mine}
exit 0
;;
"R" | "r" )
echo "" 1>&2
echo "Reverting to base..." 1>&2
cat ${older}
exit 0
;;
"D" | "d" )
echo "" 1>&2
echo "Runnig diff3..." 1>&2
diff3 -L $labelMine -L $labelOlder -L $labelTheirs -Em $mine $older $theirs
#Exit with return vaule of the diff3 (to write out files if necessary)
exit $?
;;
"S" | "s" )
echo "" 1>&2
echo "Saving for later..." 1>&2
cat ${mine}
#Exit with return vaule of 1 to force writting of files
exit 1
;;
"Fail" | "fail" | "FAIL" )
echo "" 1>&2
echo "Failing..." 1>&2
exit 2
;;
"H" | "h" )
echo "" 1>&2
echo "USAGE OPTIONS:" 1>&2
echo " [A]ccept Accept $labelTheirs and throw out local modifications" 1>&2
echo " [D]efault Use diff3 to merge files (same behavior as vanilla SVN)" 1>&2
echo " [Fail] Kills the command (not suggested)" 1>&2
echo " [H]elp Print this message" 1>&2
echo " [I]gnore Keep your locally modified version as is" 1>&2
echo " [M]erge Manually merge using ${VDIFF3}" 1>&2
echo " [m]erge Same as "M" but attempts to automerge if possible" 1>&2
echo " [R]evert Revert to base version (${labelOlder})" 1>&2
echo " [S]ave Same as 'I' but writes out rold, rnew, and rmine files to deal with later" 1>&2
echo " [diff] Type 'diff' to diff versions $labelMine and $labelTheirsthe before making a descision" 1>&2
echo " [diff3] Type 'diff3' to diff all three versions before making a descision" 1>&2
echo "" 1>&2
;;
* )
echo "'${answer}' is not an option, try again." 1>&2
;;
esac
}
if [ -z $2 ]
then
echo ERROR: This script expects to be called by subversion
exit 1
fi
if [ $2 = "-m" ]
then
#Setup vars
labelMine=${4}
labelOlder=${6}
labelTheirs=${8}
mine=${9}
older=${10}
theirs=${11}
output=${9}.svnDiff3TempOutput
baseFileName=`echo $mine | sed -e "s/.tmp$//"`
#HERE#
diff3 -L $labelMine -L $labelOlder -L $labelTheirs -Em $mine $older $theirs > $output
if [ $? = 1 ]; then
#Can't auto merge
#Prompt user for direction
while [ 1 ]
do
echo "" 1>&2
echo "${baseFileName} requires merging." 1>&2
echo "" 1>&2
echo "What would you like to do?" 1>&2
echo "[M]erge [A]ccept [I]gnore [R]evert [D]efault [H]elp" 1>&2
promptUser
done
else
#We can automerge, and we already did it
cat $output
rm -f $output
exit 0
fi
else
L="-L" #Argument option for left label
R="-L" #Argument option for right label
label1=$3 #Left label
label2=$5 #Right label
file1=$6 #Left file
file2=$7 #Right file
$DIFF $file1 $file2 $L "$label1" $L "$label2" &
#$DIFF $file1 $file2 &
#wait for the command to finish
wait
fi
exit 0Note: I also posted this to a gist on github: svndiff.sh.
Ruby Script to Organize Mp3’s based on ID3 Genre Tag
Reading time: 2 – 4 minutes
I had one gigantic directory of all my tagged and organized mp3 files. Problem is it was too big to use. This bloated my library and I have since not been able to fit my music on my laptop. I needed to manipulate mp3 files by genre and extract them out of this single directory to create smaller libraries. I spent all of about two minutes looking for a program to do this before deciding to write a script. Truthfully, it was worse: once upon a time I over-enthusiastically downloaded StepMania and 493 DDR games/songs. And then, I added all the songs into my music library. It’s a great party game, but not the kind of music I want to listen to.
Many implementations exist for reading ID3 tags. I first tried ruby-mp3info, however it didn’t read my custom genre (‘DDR’) so then I moved to id3lib-ruby which uses the c++ id3lib library.
This worked like a charm. I ran the script over all my directories and built up a list of the directories.
#!/usr/bin/env ruby # find_music.sh require "rubygems" require 'id3lib' require 'find' require 'set' ddr_files = [] ddr_dirs = Set.new search_dir = '~/media/music/music_categorized' Find.find(search_dir) do |file| next if file !~ /.*mp3$/ mp3 = ID3Lib::Tag.new(file) next if mp3.genre != 'DDR' ddr_dirs << File.dirname(file) ddr_files << file puts "%s, %s --> AT: %s" % [mp3.genre, mp3.album, file] end File.open('result-ddr-files.txt', 'w') do |f| f.write(ddr_files.join("\n")) end File.open('result-ddr-dirs.txt', 'w') do |f| ddr_dirs.each { |d| f.write("%s\n" % d)} end
Next I reviewed the two output files, then ran the file result-ddr-dirs.txt in as an argument into this next script. That removed almost a gig of music from my library.
#!/usr/bin/env ruby if (ARGV.length != 1) puts "Usage: #{__FILE__} input_file" exit(1) end destination="/home/jwolter/media/music/music_ddr_questionable_value/" File.foreach(ARGV[0]) do |line| next if line.strip == "" cmd = "mv \"#{line.strip}\" \"#{destination}\"" #puts cmd `#{cmd}` end
Bonus: In the process searching for this, I ran into the ID3 Tags RubyQuiz.
One of the nicest benefits of being a software engineer is I avoid doing boring manual tasks on my computer. Writing a script is more fun, and faster. I’ve got many scripts to automate file manipulation, online banking, and more. What bit of your automation scripts do you think is the most helpful?
Merging pdf’s on Mac OS X from a non-duplex scanner
Reading time: 2 – 3 minutes
Goal: scan in hundreds of duplex documents in a non-duplex scanner and combine into 1 pdf in automated way. Status: it was harder than it should have been, and not that automated, but this works.
Scan in the papers as pdf’s from your paper-feed equipped scanner. Scan them right side up, then flip over and scan the other sides. The two pdf’s will contain pages: 1, 3, 5… and 2, 4, 6…
Reverse the even pages.
#!/usr/bin/ruby if __FILE__ == $0 puts "Run this on ubuntu or somewhere that pdftk is easy to be had. (which isn't os x)" if ARGV.length != 1 puts "Syntax: #{__FILE__} pdf_to_reverse.pdf" exit end pdf = ARGV[0] reversed_pdf = pdf.gsub(/\.pdf/i, "_reversed.pdf") page_count = `pdfinfo #{pdf} | grep Pages`.scan(/\d+/) `pdftk #{pdf} cat #{page_count}-#{1} output #{reversed_pdf}` end
Lastly, combine the two pdf’s, shuffling every other page, starting with the odds. Note it has some dependencies on pdftk and pdfinfo for the reversing (which are excruciatingly difficult to install on os x), and os x (for the merging).
#!/usr/bin/ruby if __FILE__ == $0 puts "Run this on os x to shuffle two pdf's, where the even pages are already reversed (reverse them with other script)" if ARGV.length != 3 puts "Syntax: #{__FILE__} odds.pdf reversed_evens.pdf output.pdf" exit end odds_pdf = ARGV[0] reversed_evens_pdf = ARGV[1] output_pdf = ARGV[2] # obviously, only works on os x. I didn't see an easy way to combine pdf's # in pdftk or other tools I searched for `python '/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py' --output '#{output_pdf}' --shuffle '#{odds_pdf}' '#{reversed_evens_pdf}'` end
References:


