Monday, 21 July 2008

Scheduling Tasks in OS X

One common task is to schedule scripts to run at various times. Coming from windows environment and task scheduler I wasn't really sure what the accepted OS X way of doing things was. In my Ubuntu Server development environment it seems the crontab is still the accepted way, but evidently OS X seems to be pushing towards using launchd instead. It seems possible to use ical to run your scripts as documented on tuaw but I prefer to use ical to launch human tasks rather than scripted ones :)

The task I needed to schedule is an applescript that checks several svn repositories and either runs svn update or hgpullsvn and then logs what has been updated or changes made. It would be nice if there was a package that does this for you but I haven't found one.

For a more indepth examination of launchd have a look at this post, but I really just wanted some simple way to setup my scheduled task. Enter Lingon which seems to fit the bill.

A simple setup to run a script every 6 hours. The only catch was to run osascript to actually run the script, and watch the console for errors.


Having not used applescript before it seems straightforward though effectively I just wanted to use it as an error handler around simple bash commands. This bash/applescript usage is covered by apple in their documentation on the do shell script command. The main things to watch out for are to remember that your env is not the same as your terminal so you might need multiple semi-colon separated bash commands like
PATH=$PATH:/usr/local/bin;export PATH;hgpullsvn
to get the result you want.

Slow Fowl Dinners

In a confluence of related events, I finished reading The Omnivores Dilemma, was intrigued by a former colleagues facebook profile image (hi Sally), and read that Jamie Oliver's Fowl dinners is screening here in Australia this week. I guess the TV execs could see how well the swearing chef Ramsey rated and thought Jamie executing chickens in-studio would be a sure bet to get the punters appalled, and the ratings up.

My awareness about the food chain has been limited to organic + free range = good and battery hen + fast food = bad, and a vague childhood memory of a chicken being killed in someones backyard. I've wanted but have never read Peter Singer's Animal Liberation - despite being a fan of his ideas. Probably I didn't want to reconcile the moral idea of not eating meat with my enjoyment of it. But the food chain isn't all about killing things, and in terms of the other great genetically modified debate, I've probably leaned more towards the GM arguments for food production in truth because of the disease resistance and nutrition enhancement arguments.

Michael Pollan's The Omnivores Dilemma is a direct assault on the american diet and does the heavy lifting in connecting the reader to the food chain that makes him an ideal poster boy for the Slow Food movement. The first part of the book is a natural history of corn and how much of the American food chain is based on this humble ingredient, and how horrifyingly, animals are forced consumers of it. Australians might have a little more wheat in their diet, but by the time I'd finished reading about corn I'd had my illusions about GM food dispelled, though the arguments are not really specific to GM crops but rather industrial non-organic farming in general. I think the arguments could be summarised as follows:
Agri-business aims to maximise profit by increasing yields, reducing cost, and protecting intellectual property by creating crops that do not reproduce naturally. The pressure to increase yield leads to increased land degradation, increased use of energy intensive non-sustainable inputs, monoculture, and an economic bind for the struggling farmer.
The historical reasons that America has arrived as a nation of corn is a salient warning to governments subsidising - directly or indirectly - agriculture (ethanol anyone?) with the goal of reducing price at the cash-register and sustaining farmers through hard times. It seems self-evident that the economic pressure on farms from drought and the market can lead to the food chain equivalent of 'work choices' with a race to the bottom in which no-one wins least of all the consumer who ends up with cereal that has to have it's nutrition sprayed on to keep it's place in people's diets.

At this point of the book my own basic organic pre-dispositions have been re-inforced but the only real change to my view is to become more sceptical about the promise of GM food. In principle I don't really have a problem against modifying food by natural or GM methods, but I would question what I'm getting in the bargain as consumer. Still it seems to me that any crop farming is problematic if the land is not carefully managed. Organic farming cereal crops is like driving a Toyota Prius. It reduces the your consumer footprint, but in the end you're still driving something that emits 1.5 tonnes of carbon annually.  Anything that can reduce the environmental footprint of a crop on the land - GM or otherwise would seem advantageous, and given Pollan's view of America's dependancy on corn I would view GM crops as secondary issue to that of monoculture and conventional fertilizers and pesticides.

The next part of the book is on the organic industry or as I read it, how the baby boomers turned reactionary, rejected industrial food, and then sold their soul anyway. It documents how the collective hobby farms took the leap into mainstream industrial and somehow transmogrified into a marketing slogan for people with money and a conscience. Maybe that's a bit harsh and I dread to think where we'd be without that movement, but Pollan reminds you of the costs to market of the industrial organic, and how the same agri-businesses who produce the conventional product now own the bulk of the supermarket organic. This brings us to the chickens from Jamie Oliver's Fowl Dinners.

Most Free Range and Organic meat marketing (and conventional) is designed to insulate you from the idea that you are killing something, which a lot of people find naturally distressing. In the 2006 film Babel, American kids are taken from their insulated world into the rawness of Mexico and are shocked as a chicken is killed in front of them - something which their Mexican hosts are clearly used to. Free Range and Organic marketers as Pollan illustrates try their best to insulate you from moral choice by portraying the 'free' and 'happy' conditions under which they live, failing to mention the short life span and sudden exit. I'm inclined to believe that you need to understand and come to terms with the death of the animals you are to eat, which leads me to Pollan's investigation into the arguments of Peter Singer in the later part of the book.

I'm sure I haven't fully digested (sorry ;) Peter Singer's arguments but I was delighted to see them here. I guess the nub of it is if you believe in the equality of our species and the idea of reducing suffering then you should also extend that to other animals that have a capacity to suffer. He also believes that it is better for a domestic species (the pig or chicken for example) not to exist than to exist and be killed by us.

In essence I have a difficulty with the concept of equality as a realistic option for our own species. In principle it sounds nice, but in practice the old adage of 'some are more equal than others' applies and everyone puts themselves in some social pecking order which isn't equality. Round the camp fire friends once asked the question: would you sacrifice yourself to save a group of others? The question could be re-phrased, how much suffering are you willing to take to reduce the suffering of others, because we compete for resources with animals and other humans. In practice you need to take some serious steps to reduce suffering which would include not having babies (which my partner finds perfectly reasonable). I'm happy to at least support Jamie Oliver's attempt to highlight the plight of factory hens to an audience who might not have ever thought about where their chicken nuggets have come from.

I'm going to brush over the dilemma of Pollan hunting wild boar in the last part of the book. I don't really object to being involved in the killing of the animal and appreciate his clear allusion to the natural predator instinct that this elicits, but I think that it makes sense to make sure you can have a fair chance of a clean kill rather than going out blasting away with minimal training. Surely he could have left it to a professional shooter, but I guess he wanted to pull the trigger, even though there is a question mark hanging over whose bullet actually killed the boar.

I can't help but think I need to be aware that I'm still a predator that likes to eat meat but at the same time make a conscious choice not to needlessly exacerbate the suffering of my prey, unlike other predators like those in David Attenborough's Planet Earth whose life is a competition to feed. Up until recently we needed meat in our diet, and just because we can now exist without meat doesn't make it correct not to eat it, any more than asking people not to have children though both are rational choices. For me it comes back to being aware and respecting that you're killing animals. Of course if you have made the choice to be vegetarian or a level 5 vegan you're guaranteed to have reduced an animals suffering.

When he starts to write about Polyface farm after the industrial organic it seems that this is where he delights in taking the reader. This is collective farming evolved into something truly sustainable but still a farm in the true sense of the word with rabbits, chickens, cows and other livestock. Polyface is not really a model so much as a philosophy, because you have to adapt the farm to the local environs and what grasses and trees are suited to the locale. Polyface seeks to opt out of the grid. Maybe this is what The Freedom Manifesto exhorts whose advertisement with the headline statements 'Death to the Supermarkets' & 'Back to the Land' caught my eye. My friend and former colleague has used the image on her facebook profile.



As a romantic ideal the Freedom Manifesto sounded great, but I can't help but think that these statements cannot be adopted but for a short moment, and some self appointed leader is probably going to excommunicate you for not playing the ukulele. Interestingly Polyface's roots were in Venezuela 'off the grid' but they had their land hijacked by guerilla's after a leftist coup. The idea of Polyface, and sum of The Omnivores Dilemma doesn't really answer the hard questions about sustainable farming cereal crops for the greater population, but I guess the point is that you start asking the right questions of the food you eat, and making conscious decisions. That's seems like a reasonable start.

Saturday, 19 July 2008

VMWARE Fusion + Ubuntu Server 8.04

I use vmware fusion on my macbookpro and recently downloaded a pre-built vm for Ubuntu Server 8.04 from Chrysaor. This needs a little work to make it comfy even with the prebuilt vm. I don't have *nix coursing through my veins, so it took me a little while to fit the pieces together but here it is.

The first thing you need to do is read the FAQs on the Chysaor website about installing the open source vmware tools using the custom ubuntu804vmtools.sh script provided which takes most of the pain away. Some other tips:

1. When opening the image in Fusion chose the moved option instead of copy unless you are creating a duplicate. That way you will have a lot less configuring to do on the network options. Do the rest via the console to avoid problems when it re-configures your network.

2. When installing the vmware tools (the FAQ above) and choosing Install Vmware Tools from the Fusion menu you need to check that your vmware tools iso is actually mounted at /media/cdrom before you run ubuntu804vmtools.sh. If not you'll need to manually mount it.
sudo mount /dev/cdrom /media/cdrom
alter your /etc/fstab file to auto mount it (which is probably how you want it anyway)
/dev/cdrom       /media/cdrom   udf,iso9660 user,auto,exec,utf8 0       0
For some reason I couldn't get it to mount the linux.iso from the vm library files (which is what is supposed to happen behind the scenes) so I ended up copying it elsewhere and attaching it directly to the guest cdrom. I have also needed the linux-headers-X.X.XX-XX package when using my own ubuntu server package.

3. If you did copy the vm instead of moving it then you'll need to alter the ethernet adapter settings
ifconfig #shows the mac addresses etc
sudo nano /etc/udev/rules.d/70-persistent-net.rules
# Remove extra entries of the following and change the last one with the correct mac address and NAME to eth0.
# PCI device 0x1022:0x2000 (pcnet32)
SUBSYSTEM=="net", DRIVERS=="?*", ATTRS{address}=="00:0c:29:b0:1a:59", NAME="eth0"
# Edit with the new ipaddress/subnetmask/gateway
sudo nano /etc/network/interfaces
# The primary network interface
auto eth0
iface eth0 inet dhcp
 4. Once you have your vmware tools installed and you enable shared folders for the vm and add a shared folder, the other issue I have run across is that the permissions within the guest need sudo to create delete or modify files in the shared folder. To get around this you find out what your uid and gid is in the etc/passwd and etc/groups files. In my case the uid and gid are both 1000. Edit the fstab to mount the shared folder as the user you login as.
host:/ /mnt/hgfs vmhgfs defaults,ttl=5,uid=1000,gid=1000 0 0
5. Lastly reconfigure the timezone
sudo dpkg-reconfigure tzdata
6. Changing the hostname
Edit the file /etc/hosts and add an additional name for the entry for 127.0.1.1 to the new hostname then edit /etc/hostname and change the name of the system. Restart the server.

7. Adding a virtual data drive
https://help.ubuntu.com/community/InstallingANewHardDrive

I use this just for development so thats really it aside from the usual install of packages and updating.

Friday, 18 July 2008

SyntaxHighlighter

Ok so I've given in and blogged. I needed somewhere to leave a trail of breadcrumbs so I can find my way back. So here I am. This is more of a personal blog rather than a platform - sometimes it's easier to link to a posting than post a long answer in a forum for the technical. Other times I just want to order my thoughts outside my head, and my handwritting is excruciating.

One of the requirements of the blog was to be able to post highlighted code syntax particularly in python. For this I'm using SyntaxHighlighter 1.5.1 which is excellent for this purpose but true to the spirit of the blog needs a little tweaking especially to use with blogger.
  1.  Download and unrar the code from the SyntaxHighlighter website
  2. To make the code xhtml compatible alter line 145 in shCore.js: change "if(tags[i].getAttribute('name')==name)" to "if(tags[i].getAttribute('title')==name)"
  3. The python highlighter doesn't work in safari so get my shBrushPython file and use that in place.
  4. Upload your files somewhere (I added mine to Google Pages as others have suggested - which I had to do in IE 7 as Safari doesn't show the upload function on Google Pages.
  5. Change your Blog template as described in this post.
  6. Use the newer Blogger in draft post editor to add your posts to avoid the annoying < br / > insertions by the default editor (at July 08). You should be able to add something like this to your posts!
<pre class="python" title="code">#A module
import something
</pre>

Which will give you hopefully highlighted code like this:

#A Module
import something

Some other tests with the syntax highlighter. Its pretty basic but good enough for my purposes.

a_single_quoted_string = 'single'
a_double_quoted_string = "double"
triple_quoted = """ triple
across lines"""
def somefunction(some_arg=None):
   pass
   return
somefunction()
class someclass(object):
   """Code here"""
   def __init__():
      pass

Hedged Down