VIM cheatsheet

May 7th, 2011 rupert Comments off
set nocompatible
set nu
set expandtab
 
"this will change tab to spaces
set ts=2 
set shiftwidth=2
set expandtab
 
set hlsearch
syntax on
 
"Use TAB to complete when typing words, else inserts TABs as usual.
"Uses dictionary and source files to find matching words to complete.
 
"See help completion for source,
"Note: usual completion is on <C-n> but more trouble to press all the time.
"Never type the same word twice and maybe learn a new spellings!
"Use the Linux dictionary when spelling is in doubt.
"Window users can copy the file to their machine.
function! Tab_Or_Complete()
  if col('.')>1 && strpart( getline('.'), col('.')-2, 3 ) =~ '^\w'
    return "\<C-N>"
  else 
    return "\<Tab>"
  endif
endfunction
:inoremap <Tab> <C-R>=Tab_Or_Complete()<CR>
:set dictionary="/usr/dict/words"
 
set numberwidth=5
set cmdheight=2
"To map: imap ;f foobar => Type f; foobar will be inserted
 
"RSPEC Where ! is execute command. % is the current file
":!rspec %
 
"Map RSPEC test to ,t
":map ,t :w\|!rspec %<cr>
Categories: linux Tags:

iPhone Dev Note #24: Using Frank

May 7th, 2011 rupert Comments off

If you are into testing iphone apps. You might want to check out Frank (https://github.com/moredip/Frank/) and watch the videos, especially this one Testing Your Mobile Apps with Selenium 2 and Frank March 30th, 2011 by Pete Hodgsen.

Main Tutorial

1. Follow this tutorial for steps 1-7 https://github.com/moredip/Frank/blob/master/tutorial/Tutorial.md Note: A bit outdated when you’re in steps 8.

2. Assuming you already have a working “frankified” project running, check by doing http://localhost:37265/

2-symbiote.png

3. Now do:

frank skeleton

4. This will give you a directory structure below.

1-frank-skeleton.png

5. If you try to run “cucumber”, it will complain that APP_BUNDLE_PATH is not set.

Note: Normally, this will be in a build directory within your project. However, I have my XCode build settings as shown below for the reason sometimes I forget to put the build folder on gitignore or svnignore (Sometimes my global git is a mess). Also, it becomes convenient for me to wipe out the whole directory after doing a “clean all”, makes me feel certain that I wiped it out myself. :) So, this is my personal preference only.

xcode-build-settings.png

So all the builds goes to this folder:
app-bundle-path.png

So, I append the APP_BUNDLE_PATH to my .bash_profile so I don’t have to do this everytime I run cucumber in terminal

  35 export vr=/Volumes/rupert
  36 export vrp=/Volumes/rupert/projects
  37 export vrpr=/Volumes/rupert/projects/rails3
  38 export vrpi=/Volumes/rupert/projects/iphone
  39 
  40 #For XCode Frank Testing
  41 export APP_BUNDLE_PATH=/Volumes/temp/iphone-builds/Debug-iphonesimulator/

Another alternative is to define the APP_BUNDLE_PATH in env.rb

require 'frank-cucumber'
#APP_BUNDLE_PATH = File.dirname(__FILE__) + "/../../build/Debug-iphonesimulator/EmployeeAdmin.app"
APP_BUNDLE_PATH = "/Volumes/temp/iphone-builds/Debug-iphonesimulator/Country-Frankified.app"

6. While the project-fankified.app is running on the iOS simulator, I then ran “cucumber”. However, the app went to a background state. If you double-click the iOS Simulator Home Button, you will get all the apps running in the background. After selecting the app, cucumber ran the tests, since it is trying to connect/ping to the frank HTTP server, and the device rotated as expected. However, this is now what I expected.

7. I expect to

  • Build and Run the Frankified app.
  • Run cucumber and see my tests fail or pass.

8. So I edited “launch_steps.rb” as shown below by commenting lines 6-9. This will prevent “press_home_on_simulator”

   1 Given /^I launch the app$/ do
   2 
   3   # kill the app if it's already running, just in case this helps 
   4   # reduce simulator flakiness when relaunching the app. Use a timeout of 5 seconds to 
   5   # prevent us hanging around for ages waiting for the ping to fail if the app isn't running
   6   #begin
   7   #  Timeout::timeout(5) { press_home_on_simulator if frankly_ping }
   8   #rescue Timeout::Error 
   9   #end
  10 
  11   require 'sim_launcher'
  12 
  13   app_path = ENV['APP_BUNDLE_PATH'] || APP_BUNDLE_PATH
  14   raise "APP_BUNDLE_PATH was not set. \nPlease set a APP_BUNDLE_PATH ruby constant or environment variable to the path of your compiled Frankified iOS app      bundle" if app_path.nil?
  15 
  16   if( ENV['USE_SIM_LAUNCHER_SERVER'] )
  17     simulator = SimLauncher::Client.for_iphone_app( app_path, "4.2" )
  18   else
  19     simulator = SimLauncher::DirectClient.for_iphone_app( app_path, "4.2" )
  20   end
  21 
  22   num_timeouts = 0
  23   loop do
  24     begin
  25       simulator.relaunch
  26       wait_for_frank_to_come_up
  27       break # if we make it this far without an exception then we're good to move on
  28 
  29     rescue Timeout::Error
  30       num_timeouts += 1
  31       puts "Encountered #{num_timeouts} timeouts while launching the app."
  32       if num_timeouts > 3
  33         raise "Encountered #{num_timeouts} timeouts in a row while trying to launch the app."
  34       end
  35     end
  36   end
  37 
  38   # TODO: do some kind of waiting check to see that your initial app UI is ready
  39   # e.g. Then "I wait to see the login screen"
  40 
  41 end

Notes and Issues

1. ld: duplicate symbol _OBJC_METACLASS_$_SBJsonParser
Frank comes with “json”, “uispec”, “cocoahttpserver”. Since my project also has json dependency, I just removed the json folder from frank/lib under “Group & Files”. Note: Only delete the references and don’t move to trash.
duplicate-json.png

2. I get “Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[UIImageView panoramaID]“
My project has MapKit as a dependency. I added this to main.m. See https://github.com/moredip/Frank/blob/d8a76223cad7df8143d8b6d3524c12494a65d069/main.m.sample

#ifdef FRANK
//UNCOMMENT THIS SECTION IF YOU'RE USING MapKit AND YOU ARE SEEING CRASHES IN iOS4
//Work around the issue in iOS 4 where exceptions thrown within a NSInvocation are not catchable. This was causing crashes in UIQuery::describeView when trying to dump the DOM w. Symbiote see http://groups.google.com/group/uispec/browse_thread/thread/1879741ebae978d/a90001a8956290af
@implementation NSObject (MapKitUISpecHack) 
- (id)_mapkit_hasPanoramaID { 
	return nil; 
} 
@end
#endif

3. Query a custom graphic ‘Home’ ToolbarButton
toolbar.png
In IB, you cannot specify an accessibilityLabel for a UIBarButtonItem inside a UIToolbar. So, to query this using

toolbarButton accessibilityLabel:'Home'

We need to specify this using code explicitly in viewDidLoad.

- (void)viewDidLoad {
	...
 
	#ifdef FRANK
	barButtonHome.accessibilityLabel = @"Home";
	barButtonSearch.accessibilityLabel = @"Search";
	barButtonMoreResults.accessibilityLabel = @"More";
	barButtonPageCurl.accessibilityLabel = @"Map Settings";
	#endif
 
	...
}

For the search button, the second one from the left and has a magnifying glass icon, it can be queried directly without specifying any code.

toolbarButton accessibilityLabel:'Search'
Categories: iphone Tags:

Rails Note #17: cucumber + factory_girl + has_many associations

February 24th, 2011 rupert Comments off

1. It seems everytime :section is created, the association :app_profile is created as well. Now, there is an existing uniqueness constraint to both :app_profile and :sections.

Say we have an app_profile for “Hotel 1″.

Now app_profile, “Hotel 1″, have two sections “Section 1″, “Section 2″.

Now the behaviour that we want is you cannot add “Section 1″ again. “Section 3″ is fine.

What is the factory definition that I need to specify for :section so that it will create an association if needed but will use an existing association if it already exists?

Validation failed: App name has already been taken (ActiveRecord::RecordInvalid)
./features/step_definitions/seed_steps.rb:6:in `/^there is a section called "([^"]*)"$/'
features/editing_existing_sections.feature:10:in `And there is a section called "Test Section 2"'

2. Now I don’t know if this is ideal but the only way I worked around this is by assigning the factory build instance into an instance variable using “@”, thus it will be available through the whole request.

cucumber_factory_girl_association_thumb.png

Referene: Getting Started with factory_girl

Categories: rails Tags: , ,

Installing Postgres8.3,8.4,9.0, Postgis1.3.3,1.4.1,2.0, pgRouting on OSX (updated)

February 15th, 2011 rupert Comments off

(Updated):Postgres9.0.3 Postgis2.0svn

Before you go any further. Dump data before migrating and installing. Warning: Most of the packages listed below is installed by source.

1. Download the current postgres source. http://www.postgresql.org/ftp/source/

8.3:
$./configure --with-prefix=/usr/local/pgsql --with-python
 
8.4:
$./configure --prefix=/usr/local/pgsql --with-python
 
$make
$sudo make install

2. Don’t delete the postgres folder. You might need this later on for future compilations. See pgadmin3 admin pack below.

3. Add a postgres user from System Preferences > Accounts

4. Initialize the data directory

$mkdir /usr/local/pgsql/data
$chown postgres /usr/local/pgsql/data/
$su - postgres
$/usr/local/pgsql/bin/initdb -E utf8 -D /usr/local/pgsql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale C.
The default text search configuration will be set to "english".

fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 20
selecting default shared_buffers/max_fsm_pages ... 2400kB/20000
creating configuration files ... ok
creating template1 database in /usr/local/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
or
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

4. Install Geos , PROJ4, and Postgis

GEOS:

- install geos with the Kyngchaos installer

- installing by source

svn checkout http://svn.osgeo.org/geos/trunk trunk
cd trunk/
sh autogen.sh
./configure --prefix=/usr/local/geos --enable-python
make
make install
sudo make install

PROJ4:

svn co http://svn.osgeo.org/metacrs/proj/trunk/proj/ proj
cd proj
sh autogen.sh
./configure --prefix=/usr/local/proj4
make
sudo make install

POSTGIS:

8.3:
$./configure --with-pgsql=/usr/local/pgsql/bin/pg_config --with-geos=/usr/local/bin/geos-config
 
8.4 using Kyngchaos GEOS Framework:
$./configure --with-pgsql=/usr/local/pgsql/bin/pg_config --with-geosconfig=/Library/Frameworks/GEOS.framework/unix/bin/geos-config
 
9.0:using Kyngchaos GEOS + PROJ framework:(Update Feb 15, 2011)
./configure --with-pgsql=/usr/local/pgsql/bin/pg_config --with-geosconfig=/Library/Frameworks/GEOS.framework/unix/bin/geos-config --with-projdir=/Library/Frameworks/PROJ.framework/unix/ --with-gettext=/usr/local/share/gettext/
 
Note: libintl.h error. You need gettext
gcc -g -O2  -fno-common -DPIC  -Wall -Wmissing-prototypes  -DUSE_NLS -DLOCALEDIR=\"/usr/local/pgsql/share/locale\" -Iyes/include  -c shp2pgsql-core.c
In file included from shp2pgsql-core.c:15:
shp2pgsql-core.h:17:21: error: libintl.h: No such file or di
 
9.0.4: using src from GEOS + PROJ
./configure --with-pgconfig=/usr/local/pgsql/bin/pg_config --with-geosconfig=/usr/local/geos/bin/geos-config --with-projdir=/usr/local/proj4
 
$make
$sudo make install

If everthing is successful, you should see files in /usr/local/pgsql/share/contrib/.

cd /usr/local/pgsql/share/contrib/postgis-2.0
- legacy.sql
- postgis.sql
- postgis_upgrade_20_minor.sql
- spatial_ref_sys.sql
- uninstall_legacy.sql
- uninstall_postgis.sql

5. Starting postgres on boot. Download postgresstartup.tar.gz then extract to your /Library/StartupItems

6. Creating the database

createdb -E utf8 template_postgis
createlang plpgsql template_postgis
 
8.3:
psql -d template_postgis -f /usr/local/pgsql/share/lwpostgis.sql
psql -d template_postgis -f /usr/local/pgsql/share/spatial_ref_sys.sql
 
8.4:
psql -d template_postgis -f /usr/local/pgsql/share/contrib/postgis.sql 
psql -d template_postgis -f /usr/local/pgsql/share/contrib/spatial_ref_sys.sql
 
9.0 + postgis2.0:
psql -d template_postgis -f /usr/local/pgsql/share/contrib/postgis-2.0/postgis.sql 
psql -d template_postgis -f /usr/local/pgsql/share/contrib/postgis-2.0/spatial_ref_sys.sql

7. Install and download pgAdmin3 for MacOS X

http://www.postgresql.org/ftp/pgadmin3/release/

Update Feb 15, 2011: Use pgadmin1.12 to connect with postgres9. Other versions (1.8.4, 1.6.3 don’t work)

8. Startup pgadmin3. You will notice there is a window stating…

Server instrumentation
The server lacks instrumentation functions.
pgAdmin III uses some support functions that are not available by default in all PostgreSQL versions. These enable some tasks that make life easier when dealing with log files and configuration files.

9. Compile the adminpack. Go to $postgresql_install_directory/contrib/adminpack

$ make
gcc -no-cpp-precomp -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Winline -Wdeclaration-after-statement -Wendif-labels -fno-strict-aliasing -fwrapv  -bundle -multiply_defined suppress  adminpack.o  -L../../src/port -bundle_loader ../../src/backend/postgres  -o libadminpack.0.0.so
rm -f libadminpack.0.so
ln -s libadminpack.0.0.so libadminpack.0.so
rm -f libadminpack.so
ln -s libadminpack.0.0.so libadminpack.so
 
rupert:adminpack rupert$ sudo make install
Password:
mkdir /usr/local/pgsql/share/contrib
/bin/sh ../../config/install-sh -c -m 644 ./uninstall_adminpack.sql '/usr/local/pgsql/share/contrib'
/bin/sh ../../config/install-sh -c -m 644 adminpack.sql '/usr/local/pgsql/share/contrib'
/bin/sh ../../config/install-sh -c -m 755  libadminpack.0.0.so '/usr/local/pgsql/lib/adminpack.so'

10. Load the adminpack.sql into your maintenance and template database

$ psql -U postgres -d postgres -f /usr/local/pgsql/share/contrib/adminpack.sql 
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
...

11. Disconnect and Reconnect from pgAdmin3. You shouldn’t see the window again.

Continue only if you want pgRouting

12. Essentially we would need Boost Graph Library (BGL) a.k.a boost, Genetic Algorithm Utility Library (or, GAUL for short), and GEOS (which we installed earlier when we installed postgis).

13. The easiest way to install boost is using MacPorts.

$ sudo port install boost-jam
--->  Fetching boost-jam
--->  Attempting to fetch boost-jam-3.1.16.tgz from http://downloads.sourceforge.net/boost
--->  Verifying checksum(s) for boost-jam
--->  Extracting boost-jam
--->  Configuring boost-jam
--->  Building boost-jam
--->  Staging boost-jam into destroot
--->  Installing boost-jam 3.1.16_0
--->  Activating boost-jam 3.1.16_0
--->  Cleaning boost-jam
--->  Fetching gmake
--->  Attempting to fetch make-3.81.tar.bz2 from http://ftp.gnu.org/gnu/make
--->  Verifying checksum(s) for gmake
--->  Extracting gmake
--->  Configuring gmake
--->  Building gmake with target all
--->  Staging gmake into destroot
--->  Installing gmake 3.81_0
--->  Activating gmake 3.81_0
--->  Cleaning gmake
--->  Fetching boost
--->  Attempting to fetch boost_1_34_1.tar.bz2 from http://downloads.sourceforge.net/boost
...
$ sudo port install boost

Note: I encountered an error when I directly installed “sudo port install boost” on my first run. A quick workaround is to install boost-jam, then install boost afterwards. For more details:

Error: Target org.macports.build returned: shell command ” cd “/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_devel_boost/work/boost_1_34_1″ && gmake all ” returned error 2 Command output: Makefile:2: *** missing separator. Stop.

14. You can check if boost was successfully installed by…

$ sudo port -v installed boost boost-jam
The following ports are currently installed:
  boost @1.34.1_3+darwin_9 (active)
  boost-jam @3.1.16_0 (active)

15. For GAUL, we don’t need slang base on http://pgrouting.postlbs.org/wiki/1.x/InstallationManual.

$ ./configure --enable-slang=no
$ make
$ make install

16. Ok, so this is the heartbreaker. I was able to get pass cmake on pgRouting on version 1.02 however, I received “undefined symbols” when linking the librouting.dylib

Linking CXX shared library ../../lib/librouting.dylib
Undefined symbols:
  "_errcode", referenced from:
      _shortest_path in dijkstra.o
      _shortest_path_astar in astar.o
      _shortest_path_shooting_star in shooting_star.o

For the complete error details, see pgrouting_problem.txt

17. Thanks to www.kyngchaos.com, we can install http://www.kyngchaos.com/files/software/unixport/pgRouting-1.0.1-4.dmg binary from http://www.kyngchaos.com/wiki/software:postgres

18. Now we can load the pgRouting functions to our template database

psql -U postgres -d template_postgis -f /usr/local/pgsql/share/contrib/routing_core.sql 
psql -U postgres -d template_postgis -f /usr/local/pgsql/share/contrib/routing_core_wrappers.sql 
psql -U postgres -d template_postgis -f /usr/local/pgsql/share/contrib/routing_dd.sql
psql -U postgres -d template_postgis -f /usr/local/pgsql/share/contrib/routing_dd_wrappers.sql 
psql -U postgres -d template_postgis -f /usr/local/pgsql/share/contrib/routing_dd_tsp.sql
psql -U postgres -d template_postgis -f /usr/local/pgsql/share/contrib/routing_tsp.sql
psql -U postgres -d template_postgis -f /usr/local/pgsql/share/contrib/routing_tsp_wrappers.sql

References:

http://developer.apple.com/internet/opensource/postgres.html

http://www2.russbrooks.com:8080/2007/11/4/install-postgresql-on-mac-os-x-10-5-leopard

Migrating postgres/postgis data (updated)

February 15th, 2011 rupert Comments off

One of my hurdles recently is migrating data from my Debian Lenny Desktop Box (Postgres8.2.7/Postgis1.3.1) to my new MacBookPro Leopard(Postgres8.3.1/Postgis1.3.3). I found it out the hard way by inspecting the dump files manually.

1. pg_dump is your friend.

pg_dump --help

2. I strongly suggest if you have a big dump file (mine is 500MB) to split the schema from the data.

Add “-s” to create the schema:

pg_dump -C -s -h 127.0.0.1 -U lbs -W beijing > beijing_20080507_schema.sql

Add “-a” to dump the data only:

pg_dump -a -h 127.0.0.1 -U lbs -W beijing > beijing_20080507_schema.sql

3. pg_dump from an 8.2 would have statically linked liblwgeom to /usr/lib/postgresql/8.2/liblwgeom. You should change that to whereever your liblwgeom resides, mine is on /usr/local/pgsql/lib/liblwgeom. Just do a simple search and replace using vim on your file_schema.sql

4. After editing the schema, we can now restore the structure of the database. Check for errors and manually update the schema if needs be.

psql -h 127.0.0.1 -U beijing_4326 < beijing_20080507_schema.sql

5. Ok, so now we have the structure ready, we can also check this from pgAdmin3. Have a good look on the functions and table structures if they are fully restored.

6. Let’s load the data.

psql -h 127.0.0.1 -U beijing_4326 < beijing_20080507_data.sql
Categories: postgis, postgres Tags: ,