Tuesday, December 23, 2014

Trust your auto indentation... really

I knew I liked my editor's autoindent feature, but I didn't realize how valuable it was until this situation popped up. I was reading the book Rebuilding Rails (worth a look) and happened to code a simple method as part of one example.
def call(env)
  (klass, act) = get_controller_and_action(env)
       controller = klass.new(env)
       text = controller.send(act)
  [200, {"Content-Type" => "text/html"},
...
As you can see the indentation isn't correct for the 'controller =' and 'text =' lines and try as I might
vim gg=G
it wouldn't indent properly.  I chalked it up as low priority weirdness to be dealt with later until I loaded up the program and got this error:

undefined local variable or method `controller' for ...

I finally dropped into HexDump mode using vim's nifty little xxd utility
:%!xxd
and got this output for the lines that wouldn't indent properly.
0000060: 6c28 656e 7629 0a20 2020 2020 2028 6b6c  l(env).      (kl
0000070: 6173 732c 2061 6374 2920 3d20 6765 745f  ass, act) = get_
0000080: 636f 6e74 726f 6c6c 6572 5f61 6e64 5f61  controller_and_a
0000090: 6374 696f 6e28 656e 7629 0a20 2020 2020  ction(env).    
00000a0: 20c2 a0c2 a0c2 a0c2 a0c2 a063 6f6e 7472   ..........contr
00000b0: 6f6c 6c65 7220 3d20 6b6c 6173 732e 6e65  oller = klass.ne
00000c0: 7728 656e 7629 0a20 2020 2020 20c2 a0c2  w(env).      ...
00000d0: a0c2 a0c2 a0c2 a074 6578 7420 3d20 636f  .......text = co
00000e0: 6e74 726f 6c6c 6572 2e73 656e 6428 6163  ntroller.send(ac
Notice the 'c2a0' sequence repeated five times in front of the 'controller =' and 'text =' lines. Those non-printing characters were the culprit: the actual variable name was 0x'c2a0c2a0c2a0c2a0c2a0'controller, for example so when I tried to use 'controller', that was still undefined. How those byte pairs got inserted remains a mystery. But I am guessing from this post that it was a non-breaking Unicode character.

Saturday, May 24, 2014

Bash regular expression idosyncracies

As a scripting language, bash can certainly be considered a bit unusual, but it's got the power and the later versions keep adding functionality which makes it easier to get things done. However, even with the latest additions, it's still got its quirks.

Consider the regular expression matching feature (added in version 3)

Given a value
MY_STRING='/usr/local/heroku/bin:/Users/jseidel/bin' 
let's say you want to check for the occurrence of the string 'jseidel/bin'. You might try the following script just to check things out in advance.

#!/bin/bash

export MY_STRING=/usr/local/heroku/bin:/Users/jseidel/bin

if [[ "$MY_STRING" =~ /.*jseidel\/bin.*/ ]]; then
  echo "Found .* version"
fi
if [[ "$MY_STRING" =~ /.*jseidel\/bin.+/ ]]; then
  echo "Found .+ version"
fi
if [[ "$MY_STRING" =~ 'jseidel\/bin' ]]; then
  echo "Found single-quoted version"
fi
if [[ "$MY_STRING" =~ "jseidel\/bin" ]]; then
  echo "Found double-quoted version"
fi
if [[ "$MY_STRING" =~ jseidel\/bin ]]; then
  echo "Found un-quoted version"
fi
What you would find (even in bash 4.3.11) is that only the last one works. 
  1. The first one fails because there are no characters after the 'bin' at the end of the string. This is actually pilot error in that there are no characters following that last 'n' so 'one or more characters' is false.
  2. The second one fails because bash apparently doesn't properly support the '+' operator.
  3. The third and fourth ones fail because bash apparently doesn't properly handle REs in a string. Various docs say the quotes are optional, but that doesn't seem to be the case, either for single- or double-quoted strings.

Took me quite a bit to figure this one out; hope it helps someone else.

Saturday, March 1, 2014

I love Linux, but not Nvidia!

I am really beginning to hate Nvidia! I just spent another 4 hours recovering from an update which confused the Nvidia drivers to no end.  After lots of Googling, rebooting, and testing, here's what worked for me; hopefully it will help others (there certainly are lots of posts out there with similar problems).

For the record, I'm running Kubuntu 13.10, 64-bit, with a GeForce GT630 Nvidia card and have two monitors configured side-by-side through nvidia-settings. Also for the record, the later versions of nvidia-settings actually work very well with multiple monitors, unlike earlier versions which required a great deal of hackery.

Initial symptoms
 After updating some applications I decided to reboot (I am rather paranoid these days about updates and rebooting as I have had numerous Nividia issues) I am able to load KDM (actually lightdm for my version) and after attempting to start my account, KDM drops back to the basic GUI login prompt... which repeatedly keeps being interrupted and returning to login.

The first problem I dealt with was finding and fixing any files in my home directory that were owned by root. This is a well-known problem on Ubuntu (maybe other distributions?) wherein root owns a few files and interrupts KDM loading because of permission issues. To resolve these issues, I ran
find -user root
in my home directory and it listed all files owned by root. Then I ran:
sudo chown jseidel:jseidel *
on the root-owned files to make them mine. If you have links, you may have to use the -h option on chown.

One particular problem file is .Xauthority, which sometimes, some way, gets owned by root and will certainly stop your desktop from loading, either in KDM or Gnome.

What finally worked for me
As I progressed through numerous tests, I noticed that when I dropped into a shell (Ctrl-Alt-F1), I got the message
initctl: Event failed
which can indicate an Nvidia driver issue (I've had these many times before -- always a PITA!)

When I looked in the Kernel log after a failure:
less /var/log/kern.log
I found a series of messages like:
API mismatch...
Nvidia client is version 331.49
Kernel is version 319.60
So... the Nvidia installer goofed! What finally worked for me in this situation was the following:
sudo apt-get remove --purge nvidia*
followed by reinstalling the appropriate Nvidia driver, V331.20 in my case. I do my installations from the command line like so, after downloading the desired Nvidia driver:
cd ~/Drivers
chmod +x NVIDIA-Linux-x86_64-331.20.run
sudo ./NVIDIA-Linux-x86_64-331.20.run

Some useful commands:
less /proc/driver/nvidia/version     # Show installed version

Some possibly useful links:
http://askubuntu.com/questions/41681/blank-screen-after-installing-nvidia-restricted-driver

http://news.softpedia.com/news/How-to-Install-the-Latest-NVIDIA-331-20-Drivers-in-Ubuntu-13-10-399182.shtml

http://ubuntugenius.wordpress.com/2013/11/29/ubuntu-13-10-permissions-fix-this-operation-cannot-continue-since-proper-authorization-was-not-provided-halts-software-updater-shutdown-drive-mounting-dvd-playback-etc/

http://ubuntuforums.org/showthread.php?t=1745841


Monday, January 13, 2014

Getting Phusion Passenger to update the site...

First time using Phusion Passenger on a Rails site that I'm supporting for a new client, hosted on dreamhost.com.

I made a very simple upgrade to a view but it wouldn't display the updated content. If you've worked with Passenger before, you probably know this, but Passenger won't reload any new content unless you update the modification date of a special file "tmp/restart.txt", as in:
touch tmp/restart.txt
This information is buried fairly deep in the documentation so I didn't notice it immediately. Hopefully this will save someone else a bit of head-scratching.