<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Singletoned &#187; mac</title>
	<atom:link href="http://blog.singletoned.net/category/mac/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.singletoned.net</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 21 Jan 2010 20:50:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Connecting to MS SQL Server from Python on Mac OS X Leopard</title>
		<link>http://blog.singletoned.net/2009/07/connecting-to-ms-sql-server-from-python-on-mac-os-x-leopard/</link>
		<comments>http://blog.singletoned.net/2009/07/connecting-to-ms-sql-server-from-python-on-mac-os-x-leopard/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 19:00:51 +0000</pubDate>
		<dc:creator>Ed Singleton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[freetds]]></category>
		<category><![CDATA[leopard]]></category>
		<category><![CDATA[sqlalchemy]]></category>
		<category><![CDATA[unixodbc]]></category>

		<guid isPermaLink="false">http://blog.singletoned.net/?p=19</guid>
		<description><![CDATA[This is a draft document, and will be updated as and when I discover
more.  The Mac instructions are very draft as I haven&#8217;t had a chance
to repeat them (I haven&#8217;t worked out how to run OS X Leopard in a virtual
machine yet).

I have unfortunately had to work with a legacy SQL Server database,
and was [...]]]></description>
			<content:encoded><![CDATA[<p>This is a draft document, and will be updated as and when I discover
more.  The Mac instructions are very draft as I haven&#8217;t had a chance
to repeat them (I haven&#8217;t worked out how to run OS X Leopard in a virtual
machine yet).</p>

<p>I have unfortunately had to work with a legacy SQL Server database,
and was reluctant to use Windows to do the development.  As such I
have been trying to get my Mac development machine and a Linux server
to connect to the database.  It has been quite an adventure, and this
is what I think I have worked out so far.</p>

<h1>Installing freetds on Mac</h1>

<p>I used MacPorts for installing packages.  It&#8217;s not perfect
but it&#8217;s a lot easier than installing from source when there is a lot
of dependencies, and quite a bit of complex configuration has often
been done for you.</p>

<p>Install freetds with the +mssql and +odbc variants.</p>

<pre><code>sudo port install freetds+mssql+odbc
</code></pre>

<p>Also install <code>unixODBC</code></p>

<pre><code>sudo port install unixODBC
</code></pre>

<p>You now have a folder <code>/opt/local/etc/freetds/</code> which should contain
two templates <code>tds.driver.template</code> and <code>tds.dsn.template</code> as well as
<code>freetds.conf</code>.  You shouldn&#8217;t need to edit <code>freetds.conf</code>, although
you can choose to specify the global tds version and charset (I have
done neither of those and it works for me, so best leave it alone
unless you have specific problems).</p>

<p>If you don&#8217;t have <code>tds.driver.template</code> or <code>tds.dsn.template</code>, create
them.</p>

<pre><code>sudo touch tds.driver.template
sudo touch tds.dsn.template
</code></pre>

<p>Edit them and add the following, adjusted for your servername, dbname etc.</p>

<pre><code># tds.driver.template
[TDS]
Description     = FreeTDS Driver for Linux &amp; MSSQL on Win32
Driver          = /opt/local/lib/libtdsodbc.so
Setup           = /opt/local/lib/libtdsS.so

# tds.dsn.template
[my_dsn]
Description     = Connection to windows virtual machine
Driver          = TDS
Trace           = No
Database        = my_database_name
Server          = MY-SERVER
Port            = 1433
TDS_Version     = 8.0
</code></pre>

<p>Note that in the driver template you are naming a driver.  You then
use that name in the dsn template as the value of the driver.  This
can be any name you want instead of <code>TDS</code>, but they must match.
Alternatively, you can specify the path to the driver in the dsn
template.</p>

<p>Now you are ready to install the two templates.  You&#8217;ll need to run
<code>odbcinst</code> for each template.  Running <code>odbcinst</code> without any
arguments will give you some useful info, but I&#8217;ll summarise what you
need to know to get it working.</p>

<pre><code> sudo odbcinst -i -d -f tds.driver.template
 # -i says you want to install
 # -d says you are installing the driver
 # -f says what template you are using

 sudo odbcinst -i -s -l -f tds.dsn.template
 # -i says you want to install
 # -s says you want to install dsn
 # -l says you want to install system dsn
 # -f says what template you are using
</code></pre>

<p>These will copy the contents of the templates to
<code>/Library/ODBC/odbcinst.ini</code> and <code>/Library/ODBC/odbc.ini</code>
respectively.  They also perform some piece of magic that I haven&#8217;t
worked out yet (eg. if you duplicate the entry it adds to <code>odbc.ini</code> and
change the name slightly, it won&#8217;t work).  However adding a new entry
using <code>odbcinst</code> does work.</p>

<p>With the second call to <code>odbcinst</code>, you have a choice between
installing system dsn to <code>/Library/ODBC/odbc.ini</code> using <code>-l</code> or user
dsn in <code>~/.odbc</code> using <code>-h</code>.  I recommend system dsn for development,
and doing more research for production.</p>

<p>Now you should be able to run queries on your database, using <code>isql</code>.</p>

<pre><code>isql my_dsn username password
</code></pre>

<p>This should bring up an SQL prompt that allows you to execute queries
on the server.  Try a couple of queries to test it.  If you don&#8217;t have
a legacy database with data already in it, why the hell are you using
SQL Server?</p>

<h1>Connecting Python to SQL Server through freetds on Mac</h1>

<p>You should now be ready to get python sorted out.  If you&#8217;ve had any
problems so far, let me know and I&#8217;ll try to help.</p>

<p>First, you need a python installed.  I&#8217;m using python2.6, but 2.5
should work fine, and maybe 2.4.  I&#8217;m not going to go into installing
python in too much depth, but I recommend not using the Mac system
python, and also not using MacPorts to install python.  I also
strongly recommend using virtualenv and virtualenvwrapper.  Create a
virtualenv as soon as you have installed python and always create your
other virtualenvs from that, so that you can easily get back to a
clean python if you need to.</p>

<p>Install <code>pyodbc</code> into your virtualenv:</p>

<pre><code>    easy_install pyodbc
</code></pre>

<p>Pyodbc needs to be compiled, so you will need a version of GCC.
Either install the latest version through MacPorts (this takes a very
long time) or install XCode which includes Apple&#8217;s own version of
GCC.  You may also need the python-dev headers and/or the unixodbc-dev
headers, depending on how you installed python and unixodbc.</p>

<p>If you have gcc errors and they include the string &#8220;-arch i386 -arch
ppc&#8221; somewhere in them, then python is probably trying to build a universal
build but doesn&#8217;t have the ppc headers.  Edit <your
python>/lib/python2.x/config/MakeFile and delete every instance of
&#8220;-arch ppc&#8221;.  (This is assuming you are running an Intel Mac of
course).</p>

<p>Now checkout the 0.6 branch of SQLAlchemy.  This is currently a branch
but should soon move to trunk.  It&#8217;s fine for development.  If you are
using this as a guide to setting up your production server, you are
insane.</p>

<pre><code>svn checkout http://svn.sqlalchemy.org/sqlalchemy/branches/rel_0_6
# Make sure your virtualenv is activated then:
cd rel_0_6
python setup.py develop
</code></pre>

<p>You should now be able to run some basic tests using SQLAlchemy:</p>

<pre><code>&gt;&gt;&gt; import sqlalchemy as sa
&gt;&gt;&gt; uri = "mssql://username:password@my_dsn"
&gt;&gt;&gt; engine = sa.create_engine(uri)
&gt;&gt;&gt; select_query = sa.text("SELECT * FROM MyTable")
&gt;&gt;&gt; result = engine.execute(select_query)
&gt;&gt;&gt; result.fetchall()
[]
&gt;&gt;&gt; insert_query = sa.text(
    "INSERT INTO MyTable (id, text_field) VALUES (:id, :text_field)")
&gt;&gt;&gt; result = engine.execute(insert_query, 
        id="Brian", text_field="Naughty Boy")
&gt;&gt;&gt; result = engine.execute(select_query)
&gt;&gt;&gt; result.fetchall()
[('Brian', 'Naughty Boy')]
</code></pre>

<p>That should all work.  At this point you should probably try running
some other tests.  Particularly ones involving non-ascii chars.  Then
you can watch it crumble and die.</p>

<p>The UK Pound Sign (£) is a particularly good symbol to use as it has a
different byte representation between latin-1 and utf-8.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.singletoned.net/2009/07/connecting-to-ms-sql-server-from-python-on-mac-os-x-leopard/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Blogging from an iPhone</title>
		<link>http://blog.singletoned.net/2009/07/blogging-from-an-iphone/</link>
		<comments>http://blog.singletoned.net/2009/07/blogging-from-an-iphone/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 07:15:20 +0000</pubDate>
		<dc:creator>Ed Singleton</dc:creator>
				<category><![CDATA[mac]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://blog.singletoned.net/?p=13</guid>
		<description><![CDATA[I&#8217;ve recently bought an iPhone, and have discovered the WordPress app for it.  I thought this might be the encouragement I need to start blogging again, and it could let me blog during those small, previously wasted moments such as meetings, on the toilet, whilst smoking, etc.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently bought an iPhone, and have discovered the <a href="http://iphone.wordpress.org/">WordPress app</a> for it.  I thought this might be the encouragement I need to start blogging again, and it could let me blog during those small, previously wasted moments such as meetings, on the toilet, whilst smoking, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.singletoned.net/2009/07/blogging-from-an-iphone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lock a Mac with a keyboard shortcut</title>
		<link>http://blog.singletoned.net/2008/05/lock-a-mac-with-a-keyboard-shortcut/</link>
		<comments>http://blog.singletoned.net/2008/05/lock-a-mac-with-a-keyboard-shortcut/#comments</comments>
		<pubDate>Tue, 06 May 2008 09:02:48 +0000</pubDate>
		<dc:creator>Ed Singleton</dc:creator>
				<category><![CDATA[mac]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[keyboard]]></category>
		<category><![CDATA[leopard]]></category>
		<category><![CDATA[macosx]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://singletoned.net/2008/05/06/lock-a-mac-with-a-keyboard-shortcut/</guid>
		<description><![CDATA[At last after a very long time searching, I&#8217;ve found a keyboard shortcut that allows you to lock a Mac in a similar style to (&#x2303;&#x2325;Del, L) on Windows.

In iChat options set Show status in menu bar, and in Keychain also set Show status in menu bar.

Now when you press (&#x2303;F8) the iChat status in [...]]]></description>
			<content:encoded><![CDATA[<p>At last after a very long time searching, I&#8217;ve found a keyboard shortcut that allows you to lock a Mac in a similar style to (&#x2303;&#x2325;Del, L) on Windows.</p>

<p>In iChat options set <code>Show status in menu bar</code>, and in Keychain also set <code>Show status in menu bar</code>.</p>

<p>Now when you press (&#x2303;F8) the iChat status in your menu bar will get the focus.  Then you can just press (&#x2190;) once to highlight the Keychain status, (&#x2193;) to highlight <code>Lock Screen</code> and (&#x21A9;) to select it.</p>

<p>This gives you (&#x2303;F8, &#x2190;, &#x2193;, &#x21A9;) as a rather convoluted and excessively long keyboard shortcut for locking your computer.  It is however very handy when you are working in an office, need to go make coffee and want to <a href="http://www.google.co.uk/search?q=%22every+time+you+touch+the+mouse+god+kills+a+kitten%22">save on kittens</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.singletoned.net/2008/05/lock-a-mac-with-a-keyboard-shortcut/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Installing CouchDB on Mac OS 10.5</title>
		<link>http://blog.singletoned.net/2008/04/installing-couchdb-on-mac-os-105/</link>
		<comments>http://blog.singletoned.net/2008/04/installing-couchdb-on-mac-os-105/#comments</comments>
		<pubDate>Thu, 10 Apr 2008 09:29:58 +0000</pubDate>
		<dc:creator>Ed Singleton</dc:creator>
				<category><![CDATA[couchdb]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[leopard]]></category>
		<category><![CDATA[macosx]]></category>

		<guid isPermaLink="false">http://www.singletoned.net/2008/04/10/installing-couchdb-on-mac-os-105/</guid>
		<description><![CDATA[I&#8217;ve been reading about CouchDB on and off for a few months now, and recently decided to properly evaluate it as an option for my new website.  This decision weirdly coincided with the release of Google AppEngine which uses their Bigtable system, which is vaguely similar to CouchDB (non-relational, document based, flexible and scalable).

Anyway, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been reading about <a href="http://couchdb.org/" title="Apache CouchDB: The CouchDB Project">CouchDB</a> on and off for a few months now, and recently decided to properly evaluate it as an option for my new website.  This decision weirdly coincided with the release of <a href="http://code.google.com/appengine/" title="Google App Engine - Google Code">Google AppEngine</a> which uses their Bigtable system, which is vaguely similar to CouchDB (non-relational, document based, flexible and scalable).</p>

<p>Anyway, I tried to install CouchDB on Leopard, and hit a few minor issues which I thought I&#8217;d document for others trying to install it.</p>

<p>I installed from Macports (always a good first stop).  There appears to be a missing dependency on SeaMonkey, so you need to do:</p>

<p><pre><code>sudo port install seamonkey
sudo port install couchdb
</code></pre></p>

<p>When you first start couchdb it complians that: <code>CouchDB needs write permission on the data directory: /opt/local/var/lib/couchdb</code> or <code>CouchDB needs write permission on the log directory: /opt/local/var/log/couchdb</code>.</p>

<p>I tried doing <code>sudo couchdb</code> to get over that, and couchdb seemed to start happily, and there was a response from <code>http://localhost:5984/</code>.  However, when I inserted my first document using couchdb-python, python hung completely with no repsonse (I left it for 10 minutes while I made a coffee).  In retrospect, <code>sudo couchdb</code> probably wasn&#8217;t the correct way to go (particularly as those directories don&#8217;t even exist), but I was keen to start playing.  The correct thing to do is probably to create a couchdb user that runs the database and do <code>sudo -u couchdb couchdb</code> to start the database.  Unfortunately I&#8217;m quite lazy, so instead I just did:</p>

<p><pre><code>sudo mkdir -p /opt/local/var/lib/couchdb
sudo chown ed /opt/local/var/lib/couchdb
sudo mkdir -p /opt/local/var/log/couchdb
sudo chown ed /opt/local/var/log/couchdb
</code></pre></p>

<p>Where <code>ed</code> should be replaced by your username.  <code>mkdir -p</code> recursively makes directories (incase you don&#8217;t have <code>/opt/local/var/lib/</code> or <code>/opt/local/var/log</code>) yet.</p>

<p>Anyway, hope this helps someone else, if they have problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.singletoned.net/2008/04/installing-couchdb-on-mac-os-105/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
