<?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>ptone &#187; python</title>
	<atom:link href="http://ptone.com/dablog/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://ptone.com/dablog</link>
	<description>Hodgepodge of thoughts, technical notes, and random observations</description>
	<lastBuildDate>Sat, 04 Jun 2011 14:42:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Making iPhone iTunes remote more useful</title>
		<link>http://ptone.com/dablog/2009/09/making-iphone-itunes-remote-more-useful/</link>
		<comments>http://ptone.com/dablog/2009/09/making-iphone-itunes-remote-more-useful/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 00:10:17 +0000</pubDate>
		<dc:creator>ptone</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[itunes]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ptone.com/dablog/2009/09/making-iphone-itunes-remote-more-useful/</guid>
		<description><![CDATA[The iTunes remote is pretty slick &#8211; if only it could turn on the stereo I have a mac mini set up as a HTPC. The music end of this was a bit neglected as it was originally set up to use a custom onscreen menu control to switch between several different iTunes libraries and [...]]]></description>
			<content:encoded><![CDATA[<p>The iTunes remote is pretty slick &#8211; if only it could turn on the stereo</p>

<p><span id="more-85"></span></p>

<p>I have a mac mini set up as a HTPC.  The music end of this was a bit neglected as it was originally set up to use a custom onscreen menu control to switch between several different iTunes libraries and then have basic control within them.</p>

<p>Now that we have iPhones in the house &#8211; I consolidated the decade + worth of iTunes libraries into one.</p>

<p>I wanted a way to control the speakers based on iTunes state.</p>

<p>So this little python script runs every 10 seconds, here is a quick outline of what it does.</p>

<p>If iTunes is playing, but the speakers are off &#8211; then adjust some volumes on the speakers, turn them on, then rewind the track.</p>

<p>If iTunes is paused for more than 2 minutes, and the speakers are on &#8211; and they were turned on by this autodetect system, then turn the speakers off.</p>

<p>The turning on and off is done by my original HTPC applescript which controls an <a href="http://www.irtrans.de/en/shop/usb.php">irTrans</a> USB IR transceiver through the iRed software.</p>

<p>Now we just fire up the remote app on the phone, and within a few seconds the speakers are on and the music is playing.</p>

<pre><code>#!/usr/bin/env python
# encoding: utf-8
"""

Created by Preston Holmes on 2009-09-05.
preston@ptone.com
Copyright (c) 2009

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

import sys
import os
from appscript import *
from subprocess import Popen, call, STDOUT, PIPE
import plistlib
import time

prefs = '/Library/Preferences/com.ptone.tuneswatcher.plist'
vol_target = '/Users/Preston/Documents/HTPC/targetvolume'

def sh(cmd):
    return Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE).communicate()[0]

def itunes_running_():
    for p in psi.process.ProcessTable().items():
        try:
            if "Applications/iTunes" in p.command:
                return True
        except:
            pass
    return False

def init_prefs ():
    d = {'state':'unknown','last_state_change':time.time(),'auto_on':False}
    plistlib.writePlist (d,prefs)

def itunes_running():
    pcount = sh('ps -xa | grep -c iTunes').strip()
    return int(pcount) &gt; 3

def getSettings():
    settings_file = '/Users/preston/Documents/HTPC/settings'
    f = open(settings_file,'r')
    flines = f.readlines()
    f.close()
    settingsDict = {}
    for aLine in flines:
        aLine = aLine.rstrip('\n')
        #print aLine
        if aLine != '':
            apair = aLine.split(' ')
            settingsDict[apair[0]] = apair[1]
    return settingsDict

def read_prefs ():
    if not os.path.exists (prefs):
        init_prefs()
    d = plistlib.readPlist (prefs)
    htpc_settings = getSettings()
    d.update(htpc_settings)
    return d

def save_prefs(d):
    plistlib.writePlist (d,prefs)

def main():
    d = read_prefs()
    # print d
    is_running = itunes_running()
    if is_running:
        tunes = app("iTunes")
        # print "tunes.running"
        playing = (tunes.player_state() == k.playing)
        # print tunes.player_state() 
        # print playing
        if playing:  
            # print "tunes.playing"
            if d['speakers'] == 'off':
                # turn on speakers
                tunes.pause()
                sh ("""osascript -e 'tell application "HTPC" to power_speakers()'""")
                if int(d['volume']) &lt; 40:
                    open(vol_target,'w').write("50")
                    sh ("""osascript -e 'tell application "HTPC" to setVolume()'""")
                tunes.sound_volume.set(60)
                tunes.player_position.set(0)
                tunes.play()
                d['auto_on'] = True
                save_prefs(d)
                # start streamer here?
            if d['state'] != 'playing':
                d['state'] = 'playing'
                d['last_state_change'] = time.time()
                save_prefs(d)


        else: # iTunes not playing
            if d['state'] == 'playing':
                d['state'] = 'stopped'
                d['last_state_change'] = time.time()
                save_prefs(d)
            # check if last change was 5 minutes ago and turn off speakers
            if d['auto_on'] and ((time.time() - d['last_state_change']) &gt; 10):
                if d['speakers'] == 'on':
                    # turn speakers off only if turned on automatically
                    sh ("""osascript -e 'tell application "HTPC" to power_speakers()'""")
                    sh ("""osascript -e 'tell application "HTPC" to calibrateVolume()'""")
                d['auto_on'] = False
                save_prefs(d)
        # print (tunes.player_state() == k.stopped)


if __name__ == '__main__':
    main()
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://ptone.com/dablog/2009/09/making-iphone-itunes-remote-more-useful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pre-fetch Apple downloads as dmg files</title>
		<link>http://ptone.com/dablog/2009/07/pre-fetch-apple-downloads-as-dmg-files/</link>
		<comments>http://ptone.com/dablog/2009/07/pre-fetch-apple-downloads-as-dmg-files/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 16:43:38 +0000</pubDate>
		<dc:creator>ptone</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ptone.com/dablog/2009/07/pre-fetch-apple-downloads-as-dmg-files/</guid>
		<description><![CDATA[In my quest to automate the workflow of managing Macs at work, I wanted a way to download disk image files from apple ahead of time &#8211; once downloaded the next will be to integrate with my watched install project from the previous post, and then auto lcreate the loadsets. I can then do all [...]]]></description>
			<content:encoded><![CDATA[<p>In my quest to automate the workflow of managing Macs at work, I wanted a way to download disk image files from apple ahead of time &#8211; once downloaded the next will be to integrate with my watched install project from the previous post, and then auto lcreate the loadsets.  I can then do all my management on the radmind server (picking and choosing loadsets ready to go).  </p>

<p>The script monitors http://images.apple.com/downloads/macosx/apple/recent.rss and if it has been updated since last check (stores the last check in a plist file) it will check the feed links for any dmg it can find and download it to /downloaded_dmgs/ (which is easy enough to change in the script source)</p>

<p>The script source is below the fold &#8211; or download <a href="/downloads/get_apple_updates.py.zip">here</a></p>

<p><span id="more-78"></span></p>

<div class="highlight" ><pre><span style="color: #60a0b0; font-style: italic">#!/usr/bin/env python</span>
<span style="color: #60a0b0; font-style: italic"># encoding: utf-8</span>
<span style="color: #4070a0; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #4070a0; font-style: italic">This script attempts to download any dmg files it can find in Apple&#39;s RSS feed of updates</span>
<span style="color: #4070a0; font-style: italic"></span>
<span style="color: #4070a0; font-style: italic">requires BeautifulSoup and feedparser modules</span>
<span style="color: #4070a0; font-style: italic"></span>
<span style="color: #4070a0; font-style: italic">Created by Preston Holmes on 2009-07-15.</span>
<span style="color: #4070a0; font-style: italic">preston@ptone.com</span>
<span style="color: #4070a0; font-style: italic">Copyright (c) 2009</span>
<span style="color: #4070a0; font-style: italic"></span>
<span style="color: #4070a0; font-style: italic">Permission is hereby granted, free of charge, to any person obtaining</span>
<span style="color: #4070a0; font-style: italic">a copy of this software and associated documentation files (the</span>
<span style="color: #4070a0; font-style: italic">&quot;Software&quot;), to deal in the Software without restriction, including</span>
<span style="color: #4070a0; font-style: italic">without limitation the rights to use, copy, modify, merge, publish,</span>
<span style="color: #4070a0; font-style: italic">distribute, sublicense, and/or sell copies of the Software, and to</span>
<span style="color: #4070a0; font-style: italic">permit persons to whom the Software is furnished to do so, subject to</span>
<span style="color: #4070a0; font-style: italic">the following conditions:</span>
<span style="color: #4070a0; font-style: italic"></span>
<span style="color: #4070a0; font-style: italic">The above copyright notice and this permission notice shall be included</span>
<span style="color: #4070a0; font-style: italic">in all copies or substantial portions of the Software.</span>
<span style="color: #4070a0; font-style: italic"></span>
<span style="color: #4070a0; font-style: italic">THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND,</span>
<span style="color: #4070a0; font-style: italic">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF</span>
<span style="color: #4070a0; font-style: italic">MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.</span>
<span style="color: #4070a0; font-style: italic">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY</span>
<span style="color: #4070a0; font-style: italic">CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,</span>
<span style="color: #4070a0; font-style: italic">TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE</span>
<span style="color: #4070a0; font-style: italic">SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</span>
<span style="color: #4070a0; font-style: italic">&quot;&quot;&quot;</span>

<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">sys</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">os</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">feedparser</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">urllib</span><span style="color: #666666">,</span> <span style="color: #0e84b5; font-weight: bold">urllib2</span>
<span style="color: #007020; font-weight: bold">from</span> <span style="color: #0e84b5; font-weight: bold">BeautifulSoup</span> <span style="color: #007020; font-weight: bold">import</span> BeautifulSoup
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">plistlib</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">re</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">pdb</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">time</span>

feed_url <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;http://images.apple.com/downloads/macosx/apple/recent.rss&#39;</span>
dmg_location <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;/downloaded_dmgs/&#39;</span>
prefs_file <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;/Library/Preferences/com.ptone.updatefetcher.plist&#39;</span>

last_done <span style="color: #666666">=</span> <span style="color: #40a070">0</span>
ticker <span style="color: #666666">=</span> <span style="color: #40a070">0</span>
current_download <span style="color: #666666">=</span> {}
last_msg <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;&#39;</span>
debug <span style="color: #666666">=</span> <span style="color: #007020">False</span>

<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">download_status</span>(block_ct,block_sz,total):
    <span style="color: #4070a0; font-style: italic">&quot;&quot;&quot;The hook will be passed three arguments; a count of blocks transferred so far, a block size in bytes, and the total size of the file.&quot;&quot;&quot;</span>
    <span style="color: #007020; font-weight: bold">global</span> last_done, current_download,ticker,last_msg
    now <span style="color: #666666">=</span> time<span style="color: #666666">.</span>time()
    done <span style="color: #666666">=</span> ((block_ct <span style="color: #666666">*</span> block_sz)<span style="color: #666666">/</span><span style="color: #007020">float</span>(total)) <span style="color: #666666">*</span> <span style="color: #40a070">100</span>
    <span style="color: #007020; font-weight: bold">if</span> done <span style="color: #666666">&gt;</span> last_done <span style="color: #666666">+</span> <span style="color: #40a070">5</span> <span style="color: #007020; font-weight: bold">and</span> now<span style="color: #666666">-</span>ticker <span style="color: #666666">&gt;</span> <span style="color: #40a070">1</span>:
        msg <span style="color: #666666">=</span> <span style="color: #4070a0">&quot;downloading </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">, </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">mb </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0"> </span><span style="color: #70a0d0; font-style: italic">%%</span><span style="color: #4070a0"> done&quot;</span> <span style="color: #666666">%</span> (current_download[<span style="color: #4070a0">&#39;entry&#39;</span>],total<span style="color: #666666">/</span>(<span style="color: #40a070">1024</span><span style="color: #666666">*</span><span style="color: #40a070">1024</span>),<span style="color: #007020">int</span>(done)) 
        sys<span style="color: #666666">.</span>stderr<span style="color: #666666">.</span>write(<span style="color: #4070a0">&#39;&#39;</span><span style="color: #666666">.</span>ljust(<span style="color: #007020">len</span>(last_msg),<span style="color: #4070a0">&#39;</span><span style="color: #4070a0; font-weight: bold">\b</span><span style="color: #4070a0">&#39;</span>))
        sys<span style="color: #666666">.</span>stderr<span style="color: #666666">.</span>write(msg)
        last_msg <span style="color: #666666">=</span> msg
        sys<span style="color: #666666">.</span>stderr<span style="color: #666666">.</span>flush()
        last_done <span style="color: #666666">=</span> done
        ticker <span style="color: #666666">=</span> now

<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">get_dmg_from_iframepage</span>(url):
    <span style="color: #4070a0; font-style: italic">&quot;&quot;&quot;Sleuths out the dmg url from pages that have an iFrame download form like iTunes and Safari&quot;&quot;&quot;</span>
    <span style="color: #60a0b0; font-style: italic"># pdb.set_trace()</span>
    <span style="color: #007020; font-weight: bold">if</span> <span style="color: #007020; font-weight: bold">not</span> url<span style="color: #666666">.</span>endswith(<span style="color: #4070a0">&#39;/&#39;</span>):
        <span style="color: #007020; font-weight: bold">return</span> <span style="color: #007020">False</span>
    soup <span style="color: #666666">=</span> BeautifulSoup(urllib2<span style="color: #666666">.</span>urlopen(url)<span style="color: #666666">.</span>read())
    iframe_url <span style="color: #666666">=</span> soup<span style="color: #666666">.</span>find(<span style="color: #4070a0">&#39;iframe&#39;</span>,src<span style="color: #666666">=</span>re<span style="color: #666666">.</span>compile(<span style="color: #4070a0">&#39;.*swdlp.apple.com.*&#39;</span>))
    <span style="color: #007020; font-weight: bold">if</span> <span style="color: #007020; font-weight: bold">not</span> iframe_url: 
        <span style="color: #007020; font-weight: bold">return</span> <span style="color: #007020">False</span>
    <span style="color: #007020; font-weight: bold">else</span>:
        iframe_url <span style="color: #666666">=</span> iframe_url[<span style="color: #4070a0">&#39;src&#39;</span>]
    <span style="color: #60a0b0; font-style: italic"># user agent is needed to get the mac version of safari</span>
    user_agent <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/4.0.1 Safari/530.18&#39;</span>
    headers <span style="color: #666666">=</span> { <span style="color: #4070a0">&#39;User-Agent&#39;</span> : user_agent }
    req <span style="color: #666666">=</span> urllib2<span style="color: #666666">.</span>Request(iframe_url, headers<span style="color: #666666">=</span>headers)
    page_data <span style="color: #666666">=</span> urllib2<span style="color: #666666">.</span>urlopen(req)<span style="color: #666666">.</span>read()
    soup <span style="color: #666666">=</span> BeautifulSoup(page_data)
    dmgs <span style="color: #666666">=</span> soup<span style="color: #666666">.</span>findAll(<span style="color: #4070a0">&#39;input&#39;</span>,attrs <span style="color: #666666">=</span> {<span style="color: #4070a0">&#39;type&#39;</span>:<span style="color: #4070a0">&#39;hidden&#39;</span>,<span style="color: #4070a0">&#39;name&#39;</span>:<span style="color: #4070a0">&#39;downloadURL&#39;</span>,<span style="color: #4070a0">&#39;value&#39;</span>:re<span style="color: #666666">.</span>compile(<span style="color: #4070a0">&#39;.*dmg$&#39;</span>)})
    <span style="color: #007020; font-weight: bold">if</span> <span style="color: #007020">len</span>(dmgs) <span style="color: #666666">&gt;</span> <span style="color: #40a070">0</span>:
        <span style="color: #007020; font-weight: bold">return</span> [i[<span style="color: #4070a0">&#39;value&#39;</span>] <span style="color: #007020; font-weight: bold">for</span> i <span style="color: #007020; font-weight: bold">in</span> dmgs]
    <span style="color: #007020; font-weight: bold">else</span>:
        <span style="color: #007020; font-weight: bold">return</span> <span style="color: #007020">False</span>

<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">clear_status</span>():
    <span style="color: #007020; font-weight: bold">global</span> current_download,last_done,last_msg
    <span style="color: #007020; font-weight: bold">if</span> last_done <span style="color: #666666">&gt;</span> <span style="color: #40a070">0</span>:
        sys<span style="color: #666666">.</span>stderr<span style="color: #666666">.</span>write(<span style="color: #4070a0">&#39;&#39;</span><span style="color: #666666">.</span>ljust(<span style="color: #007020">len</span>(last_msg),<span style="color: #4070a0">&#39;</span><span style="color: #4070a0; font-weight: bold">\b</span><span style="color: #4070a0">&#39;</span>))
        sys<span style="color: #666666">.</span>stderr<span style="color: #666666">.</span>write(<span style="color: #4070a0">&#39;&#39;</span><span style="color: #666666">.</span>ljust(<span style="color: #007020">len</span>(last_msg),<span style="color: #4070a0">&#39; &#39;</span>))
        sys<span style="color: #666666">.</span>stderr<span style="color: #666666">.</span>write(<span style="color: #4070a0">&#39;</span><span style="color: #4070a0; font-weight: bold">\r</span><span style="color: #4070a0">&#39;</span>)
        sys<span style="color: #666666">.</span>stdout<span style="color: #666666">.</span>flush()
    last_done <span style="color: #666666">=</span> <span style="color: #40a070">0</span>
    last_msg <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;&#39;</span>
    
<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">get_dmg</span>(dmg_url):
    <span style="color: #007020; font-weight: bold">global</span> current_download,last_done,last_msg
    <span style="color: #60a0b0; font-style: italic"># do fancy clearing of % done</span>
    clear_status()
    fname <span style="color: #666666">=</span> dmg_url<span style="color: #666666">.</span>split(<span style="color: #4070a0">&#39;/&#39;</span>)[<span style="color: #666666">-</span><span style="color: #40a070">1</span>]
    fpath <span style="color: #666666">=</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(dmg_location,fname)
    <span style="color: #007020; font-weight: bold">if</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(fpath): 
        <span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&quot;</span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0"> already downloaded&quot;</span> <span style="color: #666666">%</span> fname
    <span style="color: #007020; font-weight: bold">else</span>:
        <span style="color: #60a0b0; font-style: italic"># save the dmg</span>
        current_download[<span style="color: #4070a0">&#39;fname&#39;</span>] <span style="color: #666666">=</span> fname
        urllib<span style="color: #666666">.</span>urlretrieve (dmg_url,fpath,download_status)
        clear_status()
        <span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&#39;downloaded: </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&#39;</span> <span style="color: #666666">%</span> current_download[<span style="color: #4070a0">&#39;entry&#39;</span>]

<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">main</span>():
    <span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&quot;============ Script Run </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0"> ============&quot;</span> <span style="color: #666666">%</span> time<span style="color: #666666">.</span>asctime()
    prefs <span style="color: #666666">=</span> {}
    <span style="color: #007020; font-weight: bold">global</span> current_download, last_done, last_msg

    <span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&quot;Parsing Feed&quot;</span>
    feed <span style="color: #666666">=</span> feedparser<span style="color: #666666">.</span>parse(feed_url)
    feed_time <span style="color: #666666">=</span> time<span style="color: #666666">.</span>mktime (feed<span style="color: #666666">.</span>feed<span style="color: #666666">.</span>updated_parsed)
    <span style="color: #007020; font-weight: bold">if</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(prefs_file):
        prefs <span style="color: #666666">=</span> plistlib<span style="color: #666666">.</span>readPlist(prefs_file)
    <span style="color: #007020; font-weight: bold">else</span>:
        prefs <span style="color: #666666">=</span> {<span style="color: #4070a0">&#39;last_check&#39;</span>:feed_time <span style="color: #666666">-</span> <span style="color: #40a070">1000</span>}
    
    <span style="color: #007020; font-weight: bold">if</span> debug: prefs <span style="color: #666666">=</span> {<span style="color: #4070a0">&#39;last_check&#39;</span>:feed_time <span style="color: #666666">-</span> <span style="color: #40a070">1000</span>}
    
    now <span style="color: #666666">=</span> time<span style="color: #666666">.</span>mktime(time<span style="color: #666666">.</span>gmtime())
    <span style="color: #60a0b0; font-style: italic"># print feed_time</span>
    <span style="color: #60a0b0; font-style: italic"># print now</span>
    <span style="color: #007020; font-weight: bold">if</span> feed_time <span style="color: #666666">&lt;=</span> prefs[<span style="color: #4070a0">&#39;last_check&#39;</span>]:
        <span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&quot;Feed not updated since last check&quot;</span>
        prefs[<span style="color: #4070a0">&#39;last_check&#39;</span>] <span style="color: #666666">=</span> now
        plistlib<span style="color: #666666">.</span>writePlist(prefs, prefs_file)
        sys<span style="color: #666666">.</span>exit()
    <span style="color: #007020; font-weight: bold">else</span>:
        <span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&quot;feed updated </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&quot;</span> <span style="color: #666666">%</span> feed<span style="color: #666666">.</span>feed<span style="color: #666666">.</span>updated
    <span style="color: #007020; font-weight: bold">if</span> <span style="color: #007020; font-weight: bold">not</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(dmg_location): os<span style="color: #666666">.</span>makedirs(dmg_location)
    <span style="color: #60a0b0; font-style: italic"># loop over the entries</span>
    <span style="color: #007020; font-weight: bold">for</span> entry <span style="color: #007020; font-weight: bold">in</span> feed<span style="color: #666666">.</span>entries:
        current_download <span style="color: #666666">=</span> {<span style="color: #4070a0">&#39;entry&#39;</span>:entry<span style="color: #666666">.</span>title}
        dmg_found <span style="color: #666666">=</span> <span style="color: #007020">False</span>
        url <span style="color: #666666">=</span> entry<span style="color: #666666">.</span>link
        <span style="color: #60a0b0; font-style: italic"># load link with urllib2 into beautiful soup</span>
        soup <span style="color: #666666">=</span> BeautifulSoup(urllib2<span style="color: #666666">.</span>urlopen(url)<span style="color: #666666">.</span>read())
        <span style="color: #60a0b0; font-style: italic"># find sidecar link</span>
        downloads <span style="color: #666666">=</span> soup<span style="color: #666666">.</span>findAll(<span style="color: #4070a0">&#39;a&#39;</span>,href<span style="color: #666666">=</span>re<span style="color: #666666">.</span>compile(<span style="color: #4070a0">&#39;http://wsidecar.*&#39;</span>))
        <span style="color: #007020; font-weight: bold">if</span> downloads:
            download_url <span style="color: #666666">=</span> downloads[<span style="color: #40a070">0</span>][<span style="color: #4070a0">&#39;href&#39;</span>]
            <span style="color: #60a0b0; font-style: italic"># determine if link is dmg                </span>
            <span style="color: #007020; font-weight: bold">if</span> download_url<span style="color: #666666">.</span>endswith(<span style="color: #4070a0">&#39;.dmg&#39;</span>):
                dmg_found <span style="color: #666666">=</span> <span style="color: #007020">True</span>
                <span style="color: #60a0b0; font-style: italic"># make it a list for compatibility with pages that return multiple dmgs</span>
                dmg_urls <span style="color: #666666">=</span> [download_url]
            <span style="color: #007020; font-weight: bold">else</span>:
                dmg_found <span style="color: #666666">=</span> dmg_urls <span style="color: #666666">=</span> get_dmg_from_iframepage(download_url)            
            <span style="color: #007020; font-weight: bold">if</span> dmg_found:
                <span style="color: #007020; font-weight: bold">for</span> a_dmg <span style="color: #007020; font-weight: bold">in</span> dmg_urls:
                    get_dmg(a_dmg)
            <span style="color: #007020; font-weight: bold">else</span>:
                <span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&quot;no dmg found in </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&quot;</span> <span style="color: #666666">%</span> entry<span style="color: #666666">.</span>title
                
    prefs[<span style="color: #4070a0">&#39;last_check&#39;</span>] <span style="color: #666666">=</span> now
    plistlib<span style="color: #666666">.</span>writePlist(prefs, prefs_file)
    
<span style="color: #007020; font-weight: bold">if</span> __name__ <span style="color: #666666">==</span> <span style="color: #4070a0">&#39;__main__&#39;</span>:
    main()
</pre></div>
]]></content:encoded>
			<wfw:commentRss>http://ptone.com/dablog/2009/07/pre-fetch-apple-downloads-as-dmg-files/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>watchedinstall</title>
		<link>http://ptone.com/dablog/2009/07/watchedinstall/</link>
		<comments>http://ptone.com/dablog/2009/07/watchedinstall/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 17:33:50 +0000</pubDate>
		<dc:creator>ptone</dc:creator>
				<category><![CDATA[Random Observation]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ptone.com/dablog/2009/07/watchedinstall/</guid>
		<description><![CDATA[[update: project now on bitbucket] The Problem There is often the need in a deployment scenario to repackage an installer into some other format. This might be a radmind transcript, a simple payload only installer package or perhaps just a manifest of what exactly changed on the system. Apple installer style packages contain simple payloads, [...]]]></description>
			<content:encoded><![CDATA[<p>[update: project now on bitbucket]</p>

<p><strong>The Problem</strong></p>

<p>There is often the need in a deployment scenario to repackage an installer into some other format.  This might be a <a href="http://rsug.itd.umich.edu/software/radmind/">radmind</a> transcript, a simple payload only installer package or perhaps just a manifest of what exactly changed on the system.</p>

<p>Apple installer style packages contain simple payloads, but often contain pre or postflight scripts which can make additional and important changes to the filesystem.</p>

<p>There are a couple methods that have been used to try to deduce what exactly was installed:</p>

<p><span id="more-77"></span></p>

<p><em>The snapshot method:</em></p>

<p>This involves taking a snapshot of the filesystem before and after the install and diffing for changes &#8211; this is a common method of building radmind transcripts.  One disadvantage of this is that it requires two complete traverses of the entire filesystem to determine changes.  Not so horrible, but if you have 10 minor updates, it can add up.</p>

<p><em>The packagemaker watch:</em></p>

<p>From the packagemaker man page:</p>

<blockquote>
  <p>If the &#8211;watch ï¬‚ag is speciï¬ed, packagemaker will monitor ï¬lesystem changes until it receives the 
  SIGUSR1 signal. It will then construct a package of all ï¬les that were created/modiï¬ed while it was watching. </p>
</blockquote>

<p>The problem with these first two methods is that the filesystem is a noisy place, with all sort of changes being made by the various processes running.  These are captured with the above methods and then need to be sorted through by hand.  There can arise a lot of confusion over whether a given change was made by the installer or by some other process as all changes are captured without any note of the source of the change.</p>

<p><em>Install on non-boot volume:</em></p>

<p>Installing on a non-boot volume with the snapshot method resolves the noise issue, as generally most of the FS noise is happening on the boot volume.  The problem with this method is that a number of installers either expect or require, sometimes quietly and implicitly that they be run on the boot volume.</p>

<p><strong>Shooting for the best of both worlds</strong></p>

<p><a href="http://bitbucket.org/ptone/watchedinstall/downloads/">Download watchedinstall</a></p>

<p>watchedinstall is a tool composed of a FSEvents logging tool and a Python script that tries to determine exactly what gets installed by an installer.</p>

<p>The tool takes an apple installer package, or the process id (pid) of a 3rd party installer and then logs two sets of information while the installer runs:</p>

<ul>
<li>All spawned processes and their parent</li>
<li>All changes to the filesystem</li>
</ul>

<p>When the installation is done, watched install builds a list of all the processes involved in the installation. Even for something simple like the sound effects installer from iLife involves nearly a dozen descendent processes of the initial installer process.</p>

<p>This list of installer related pids is used to filter the list of FS changes to only those made by the installation process.</p>

<p>The filtered list of changes is then output in a number of formats, currently the choices are:</p>

<ul>
<li>A radmind transcript</li>
<li>A simple payload installer package</li>
<li>A text file manifest of the changes</li>
</ul>

<p><strong>Installation</strong></p>

<ol>
<li><p>Copy the fsewatcher tool somewhere on your path, for example /usr/local/bin and make sure its executable (chmod +x)</p></li>
<li><p>The watchedinstall.py python script can be run from anywhere.</p></li>
</ol>

<p><strong>Usage</strong></p>

<p>You must run watchedinstall.py as root &#8211; it will fire up the companion fsewatcher tool as long as it can be found in your path.</p>

<p>You can make the watchedinstall.py file executable, or just run it through the Python interpreter directly.</p>

<p>The script uses the following options:</p>

<ul>
<li>-h, &#8211;help              for a more concise form of this help</li>
<li>-v, &#8211;verbose           display all verbose installer output</li>
<li>-e, &#8211;english-only      filter out non english project files</li>
<li>-o PATH, &#8211;output=PATH</li>
</ul>

<p>file to save results in, if not specified use standard out.  If not specified verbosity will be disabled so that standard output is a clean representation of the output.  Standard out results only make sense for radmind and standard formats.  If the argument is a directory, the script will try to intelligently name the output file based on the installer source.  So iLifeUpdate02.pkg becomes a transcript iLifeUpdate02.T for radmind output, or iLifeUpdate02-repack.pkg for package format.</p>

<ul>
<li><p>-f [radmind | standard | package], &#8211;format=[radmind | standard | package]
Format for output file, default: radmind.</p>

<ul>
<li><p>Radmind format will create a radmind transcript.</p>

<p>Radmind format requires radmind be installed.</p></li>
<li>Standard output creates a text file manifest of changes. </li>
<li><p>Package format will create a simple payload package (no scripts) of the changes made.  </p>

<p>The way packagemaker works is it will use the permissions of the boot volume for those in the package.  </p>

<p>Also the &#8216;package&#8217; format requires the install target be the boot volume.  </p>

<p>The package format option requires that developer tools be installed.</p></li>
</ul></li>
<li><p>-i PID, &#8211;pid=PID     </p></li>
</ul>

<p>Manually specify the PID of the parent installer process.  If you are using a non-Apple installer style installer (ie installerVISE) you can fire up the installer, determine its pid from a tool like PS or Activity Monitor and pass that pid to watchedinstall.  Once watched install is running, you can then return to the installer and start the installation.  When the installer quits, watchedinstall will finish its work.</p>

<p><strong>Installer Options:</strong>
These options apply if you are choosing to invoke Apple&#8217;s installer with a package</p>

<ul>
<li>-p PATH, &#8211;package=PATH     package to install, can be an mpkg</li>
<li>-t PATH, &#8211;target=PATH      target of package install, defaults to /</li>
</ul>

<p><strong>Radmind Options:</strong>
These options only apply if the radmind format is used</p>

<ul>
<li>-K PATH             path to command file, default is standard command.K</li>
<li>-I                  use case insensitive sorting</li>
<li>-C [ . | / ], &#8211;comparison-path=[ . | / ]       comparison path to use, default is relative</li>
<li>-c [ only sha1 supported ]      checksum if any, only sha1 supported for -P option</li>
<li>-P      enable experimental pure python fsdiff output (a bit faster &#8211; but not recommended)</li>
</ul>

<p>Note that any radmind excludes are parsed and respected.</p>

<p><strong>Usage Examples</strong></p>

<pre><code>sudo python path/to/watchedinstall.py -ve -p path/to/AppleIntermediateCodec.pkg -o /transcripts/ -I
</code></pre>

<p>will create /transcripts/AppleIntermediateCodec.T without english support files</p>

<pre><code>sudo python path/to/watchedinstall.py -K /var/radmind/client/clientUpdate.K -I -p path/to/AppleIntermediateCodec.pkg &gt; /AIC.T
</code></pre>

<p>will create /AIC.T</p>

<pre><code>sudo python path/to/watchedinstall.py --format standard -p path/to/AppleIntermediateCodec.pkg &gt; /AIC.txt
</code></pre>

<p>creates a textfile manifest of filesystem changes</p>

<pre><code>sudo python path/to/watchedinstall.py -e -o /Packages/ --format package -p path/to/AppleIntermediateCodec.pkg
</code></pre>

<p>repacks the installer into payload only package without non-english files at /Packages/AppleIntermediateCodec-repack.pkg</p>
]]></content:encoded>
			<wfw:commentRss>http://ptone.com/dablog/2009/07/watchedinstall/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Saving time (and wrists) scripting Illustrator</title>
		<link>http://ptone.com/dablog/2009/06/saving-time-and-wrists-scripting-illustrator/</link>
		<comments>http://ptone.com/dablog/2009/06/saving-time-and-wrists-scripting-illustrator/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 19:04:11 +0000</pubDate>
		<dc:creator>ptone</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ptone.com/dablog/2009/06/saving-time-and-wrists-scripting-illustrator/</guid>
		<description><![CDATA[Sometimes doing tedious layout work in Illustrator is both time consuming and hard on the wrists. In this post I show a quick example of how one can script Illustrator very effectively I work at a school where I&#8217;m involved in putting together our yearbook. For several years now we have done a collage of [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes doing tedious layout work in Illustrator is both time consuming and hard on the wrists.  In this post I show a quick example of how one can script Illustrator very effectively</p>

<p><span id="more-75"></span></p>

<p>I work at a school where I&#8217;m involved in putting together our yearbook. For several years now we have done a collage of self portraits.</p>

<p>These can be a chore to layout in Illustrator.  You want to randomize the grades, scale everything to be in nice rows and line everything up.</p>

<p>Here is what I started with:</p>

<p><img src="http://ptone.com/dablog/wp-content/uploads/2009/06/picture-46.png" alt="Picture 46" /></p>

<p>Each grade is a different layer (and highlight color)</p>

<p>Its hard to know what size to standardize on to flow these out best onto the cover layout (its a front and back deal)
Also I don&#8217;t want all of one grade next to another, so they need some shuffling.</p>

<p>So I whipped up this python script to do the grunt work for me.</p>

<p><em>quick digression on scripting language</em>
Note I could have done this in Applescript &#8211; and for most of you that would be easier to use. I think that for quick scripts where the bulk of the script is generating apple events &#8211; Applescript is generally easier.  But as soon as you involve much general programming, Applescript is sorely lacking.  It has an awkward and verbose syntax, and has a very limited standard library &#8211; so you often have to write all your own utilities.  <a href="http://appscript.sourceforge.net/py-appscript/">appscript</a> is a Python module that provides access to Apples OSA system and allows you to do anything you can do in Applescript.  The syntax takes some getting used to, but if you need to do other things in your script &#8211; its a win.  For example the &#8220;Shuffle&#8221; bit in the code below was a single line of python, but would have required a PITA sub-routine in Applescript.</p>

<div class="highlight" ><pre><span style="color: #60a0b0; font-style: italic">#!/usr/bin/env python</span>
<span style="color: #60a0b0; font-style: italic"># encoding: utf-8</span>
<span style="color: #4070a0; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #4070a0; font-style: italic">Created by Preston Holmes on 2009-06-04.</span>
<span style="color: #4070a0; font-style: italic">&quot;&quot;&quot;</span>

<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">sys</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">os</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">appscript</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">random</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">time</span>
<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">main</span>():
    il <span style="color: #666666">=</span> appscript<span style="color: #666666">.</span>app(<span style="color: #4070a0">&quot;Adobe Illustrator&quot;</span>)
    page_items <span style="color: #666666">=</span> il<span style="color: #666666">.</span>current_document<span style="color: #666666">.</span>page_items()
    random_index <span style="color: #666666">=</span> <span style="color: #007020">range</span>(<span style="color: #007020">len</span>(page_items))
    random<span style="color: #666666">.</span>shuffle(random_index)
    left_edge <span style="color: #666666">=</span> <span style="color: #40a070">15</span>
    curr_x <span style="color: #666666">=</span> left_edge
    curr_y <span style="color: #666666">=</span> <span style="color: #40a070">775</span>
    x_max <span style="color: #666666">=</span> <span style="color: #40a070">600</span>
    row_height <span style="color: #666666">=</span> <span style="color: #40a070">80</span>
    <span style="color: #007020; font-weight: bold">print</span> <span style="color: #007020">len</span>(random_index)
    <span style="color: #007020; font-weight: bold">for</span> i <span style="color: #007020; font-weight: bold">in</span> random_index:
        item <span style="color: #666666">=</span> page_items[i]
        <span style="color: #007020; font-weight: bold">if</span> item<span style="color: #666666">.</span>locked() <span style="color: #007020; font-weight: bold">or</span> item<span style="color: #666666">.</span>layer<span style="color: #666666">.</span>locked():
            <span style="color: #007020; font-weight: bold">continue</span>
        h <span style="color: #666666">=</span> item<span style="color: #666666">.</span>height()
        w <span style="color: #666666">=</span> item<span style="color: #666666">.</span>width()
        <span style="color: #007020; font-weight: bold">if</span> h <span style="color: #666666">==</span> <span style="color: #40a070">0</span> <span style="color: #007020; font-weight: bold">or</span> w <span style="color: #666666">==</span><span style="color: #40a070">0</span>:
            <span style="color: #007020; font-weight: bold">continue</span>
        s <span style="color: #666666">=</span> <span style="color: #007020">int</span> ((row_height<span style="color: #666666">/</span>h) <span style="color: #666666">*</span> <span style="color: #40a070">100</span>)
        <span style="color: #60a0b0; font-style: italic"># s2 = row_height/h</span>
        <span style="color: #007020; font-weight: bold">if</span> (curr_x <span style="color: #666666">+</span> w) <span style="color: #666666">&gt;</span> x_max:
            curr_x <span style="color: #666666">=</span> left_edge
            curr_y <span style="color: #666666">=</span> curr_y <span style="color: #666666">-</span> row_height
        sm <span style="color: #666666">=</span> il<span style="color: #666666">.</span>get_scale_matrix(horizontal_scale<span style="color: #666666">=</span>s,vertical_scale<span style="color: #666666">=</span>s)
        <span style="color: #60a0b0; font-style: italic"># Having some real problems with scale transforms screwing up stroke width - but I&#39;m just working with placed items</span>
        il<span style="color: #666666">.</span>transform(item,using<span style="color: #666666">=</span>sm,line_scale<span style="color: #666666">=</span><span style="color: #40a070">100</span>,transforming_stroke_patterns<span style="color: #666666">=</span><span style="color: #007020">False</span>)
        <span style="color: #60a0b0; font-style: italic"># item.stroked_width.set(1)</span>
        <span style="color: #60a0b0; font-style: italic"># alternate scale approach (also seems to affect stroke width...)</span>
        <span style="color: #60a0b0; font-style: italic"># item.width.set(w*s2)</span>
        <span style="color: #60a0b0; font-style: italic"># item.height.set(h*s2)</span>
        left,top,right,bottom <span style="color: #666666">=</span> item<span style="color: #666666">.</span>geometric_bounds()
        dx <span style="color: #666666">=</span> curr_x <span style="color: #666666">-</span> left
        dy <span style="color: #666666">=</span> curr_y <span style="color: #666666">-</span> top
        m <span style="color: #666666">=</span> il<span style="color: #666666">.</span>get_translation_matrix(delta_x<span style="color: #666666">=</span>dx,delta_y<span style="color: #666666">=</span>dy)
        <span style="color: #60a0b0; font-style: italic"># can&#39;t use concatenated transform as scale will change the amount to move...</span>
        <span style="color: #60a0b0; font-style: italic"># full_m = il.concatenate_scale_matrix(m,horizontal_scale=s,vertical_scale=s)</span>
        il<span style="color: #666666">.</span>transform(item,using<span style="color: #666666">=</span>m)
        curr_x <span style="color: #666666">=</span> curr_x <span style="color: #666666">+</span> (right <span style="color: #666666">-</span> left)

    

<span style="color: #007020; font-weight: bold">if</span> __name__ <span style="color: #666666">==</span> <span style="color: #4070a0">&#39;__main__&#39;</span>:
    main()
</pre></div>

<p>So what does this get me?</p>

<p><img src="http://ptone.com/dablog/wp-content/uploads/2009/06/picture-47.png" alt="Picture 47" /></p>

<p>I made this to generate one page width &#8211; and its easy to split up across pages:
<img src="http://ptone.com/dablog/wp-content/uploads/2009/06/picture-48.png" alt="Picture 48" /></p>

<p>And as you can see, it also shuffled the source layers:
<img src="http://ptone.com/dablog/wp-content/uploads/2009/06/picture-49.png" alt="Picture 49" /></p>

<p>Now if I want to change how it flows &#8211; or add or subtract images I can just change the single row_height parameter of the script and get variations like this:
<img src="http://ptone.com/dablog/wp-content/uploads/2009/06/picture-50.png" alt="Picture 50" /></p>

<p><img src="http://ptone.com/dablog/wp-content/uploads/2009/06/picture-51.png" alt="Picture 51" /></p>

<p>If you realize an image needs 90 degree rotation, it no longer requires having to entirely re-layout the whole project</p>

<p>Of course there is still some massaging needed by hand &#8211; but it is a fraction of the effort.</p>
]]></content:encoded>
			<wfw:commentRss>http://ptone.com/dablog/2009/06/saving-time-and-wrists-scripting-illustrator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Time Machine: poor man&#8217;s version control</title>
		<link>http://ptone.com/dablog/2009/02/time-machine-poor-mans-version-control/</link>
		<comments>http://ptone.com/dablog/2009/02/time-machine-poor-mans-version-control/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 01:09:52 +0000</pubDate>
		<dc:creator>ptone</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ptone.com/dablog/2009/02/time-machine-poor-mans-version-control/</guid>
		<description><![CDATA[There are a has been a number of version control systems en vogue over time, CVS, SVN, Git etc. I try to keep up with them, use them where possible, but don&#8217;t put EVERYTHING I do in version control. Since I am on a Mac running Leopard, I do have, and use Time Machine and [...]]]></description>
			<content:encoded><![CDATA[<p>There are a has been a number of version control systems en vogue over time, CVS, SVN, Git etc.</p>

<p>I try to keep up with them, use them where possible, but don&#8217;t put EVERYTHING I do in version control.  Since I am on a Mac running Leopard, I do have, and use <a href="http://arstechnica.com/apple/reviews/2007/10/mac-os-x-10-5.ars/14">Time Machine</a> and so wanted to see if it would be pretty easy to use that to do some quick diffs between some source files.</p>

<p>The result is a quick and dirty python script.  In the unlikely event that I ever have time, this would be a cool pyObjC project, a file browser panel, date versions picker, and a webkit view (with some better css).</p>

<p>The source to the script is below the fold &#8211; it will look for the first attached volume that has time machine backups for the current machine.  It will not work with network based time machine backups that are on disk images. This script will run on a stock Leopard install without any extra python modules needed.</p>

<p><span id="more-54"></span></p>

<div class="highlight" ><pre><span style="color: #60a0b0; font-style: italic">#!/usr/bin/env python</span>
<span style="color: #60a0b0; font-style: italic"># encoding: utf-8</span>
<span style="color: #4070a0; font-style: italic">&quot;&quot;&quot;</span>
<span style="color: #4070a0; font-style: italic">Created by Preston Holmes on 2009-02-23.</span>
<span style="color: #4070a0; font-style: italic">Copyright (c) 2009 __MyCompanyName__. All rights reserved.</span>
<span style="color: #4070a0; font-style: italic">&quot;&quot;&quot;</span>

<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">sys</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">os</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">getopt</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">difflib</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">time</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">pdb</span>
<span style="color: #007020; font-weight: bold">from</span> <span style="color: #0e84b5; font-weight: bold">subprocess</span> <span style="color: #007020; font-weight: bold">import</span> Popen, PIPE

<span style="color: #60a0b0; font-style: italic"># if you set time_machine_path, it should be to the full path of this machines backup drive:</span>
<span style="color: #60a0b0; font-style: italic"># ie &#39;/Volumes/TM_Drive/Backups.backupsdb/Joes-Mac/&#39;</span>
<span style="color: #60a0b0; font-style: italic"># if not set explicitly - the script will use the first TM drive it finds, </span>
<span style="color: #60a0b0; font-style: italic"># and the first host folder it finds - which should work for most cases</span>
<span style="color: #60a0b0; font-style: italic"># Will Not work with network or disk image based time machine backups</span>

time_machine_path <span style="color: #666666">=</span> <span style="color: #007020">None</span>

cmd <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;tell application &quot;Finder&quot; to name of (path to startup disk)&#39;</span>
boot_volume <span style="color: #666666">=</span> Popen(<span style="color: #4070a0">&#39;osascript -e </span><span style="color: #4070a0; font-weight: bold">\&#39;</span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0; font-weight: bold">\&#39;</span><span style="color: #4070a0">&#39;</span> <span style="color: #666666">%</span> cmd,shell<span style="color: #666666">=</span><span style="color: #007020">True</span>,stdout<span style="color: #666666">=</span>PIPE,stderr<span style="color: #666666">=</span>PIPE)<span style="color: #666666">.</span>communicate()[<span style="color: #40a070">0</span>][<span style="color: #40a070">0</span>:<span style="color: #666666">-</span><span style="color: #40a070">1</span>]



help_message <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;&#39;&#39;</span>
<span style="color: #4070a0">Call this script with the path to one or more text based files as arguments</span>
<span style="color: #4070a0">&#39;&#39;&#39;</span>
header <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;&#39;&#39;</span>
<span style="color: #4070a0"></span>
<span style="color: #4070a0">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
<span style="color: #4070a0">          &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #4070a0"></span>
<span style="color: #4070a0">&lt;html&gt;</span>
<span style="color: #4070a0"></span>
<span style="color: #4070a0">&lt;head&gt;</span>
<span style="color: #4070a0">    &lt;meta http-equiv=&quot;Content-Type&quot;</span>
<span style="color: #4070a0">          content=&quot;text/html; charset=ISO-8859-1&quot; /&gt;</span>
<span style="color: #4070a0">    &lt;title&gt;&lt;/title&gt;</span>
<span style="color: #4070a0">    &lt;style type=&quot;text/css&quot;&gt;</span>
<span style="color: #4070a0">        table.diff {font-family:Courier; border:medium;}</span>
<span style="color: #4070a0">        .diff_header {background-color:#e0e0e0}</span>
<span style="color: #4070a0">        td.diff_header {text-align:right}</span>
<span style="color: #4070a0">        .diff_next {background-color:#c0c0c0}</span>
<span style="color: #4070a0">        .diff_add {background-color:#aaffaa}</span>
<span style="color: #4070a0">        .diff_chg {background-color:#ffff77}</span>
<span style="color: #4070a0">        .diff_sub {background-color:#ffaaaa}</span>
<span style="color: #4070a0">    &lt;/style&gt;</span>
<span style="color: #4070a0">&lt;/head&gt;</span>
<span style="color: #4070a0"></span>
<span style="color: #4070a0">&lt;body&gt;</span>
<span style="color: #4070a0">&#39;&#39;&#39;</span>
footer <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;&#39;&#39;</span>
<span style="color: #4070a0">    &lt;table class=&quot;diff&quot; summary=&quot;Legends&quot;&gt;</span>
<span style="color: #4070a0">        &lt;tr&gt; &lt;th colspan=&quot;2&quot;&gt; Legends &lt;/th&gt; &lt;/tr&gt;</span>
<span style="color: #4070a0">        &lt;tr&gt; &lt;td&gt; &lt;table border=&quot;&quot; summary=&quot;Colors&quot;&gt;</span>
<span style="color: #4070a0">                      &lt;tr&gt;&lt;th&gt; Colors &lt;/th&gt; &lt;/tr&gt;</span>
<span style="color: #4070a0">                      &lt;tr&gt;&lt;td class=&quot;diff_add&quot;&gt;&amp;nbsp;Added&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;</span>
<span style="color: #4070a0">                      &lt;tr&gt;&lt;td class=&quot;diff_chg&quot;&gt;Changed&lt;/td&gt; &lt;/tr&gt;</span>
<span style="color: #4070a0">                      &lt;tr&gt;&lt;td class=&quot;diff_sub&quot;&gt;Deleted&lt;/td&gt; &lt;/tr&gt;</span>
<span style="color: #4070a0">                  &lt;/table&gt;&lt;/td&gt;</span>
<span style="color: #4070a0">             &lt;td&gt; &lt;table border=&quot;&quot; summary=&quot;Links&quot;&gt;</span>
<span style="color: #4070a0">                      &lt;tr&gt;&lt;th colspan=&quot;2&quot;&gt; Links &lt;/th&gt; &lt;/tr&gt;</span>
<span style="color: #4070a0">                      &lt;tr&gt;&lt;td&gt;(f)irst change&lt;/td&gt; &lt;/tr&gt;</span>
<span style="color: #4070a0">                      &lt;tr&gt;&lt;td&gt;(n)ext change&lt;/td&gt; &lt;/tr&gt;</span>
<span style="color: #4070a0">                      &lt;tr&gt;&lt;td&gt;(t)op&lt;/td&gt; &lt;/tr&gt;</span>
<span style="color: #4070a0">                  &lt;/table&gt;&lt;/td&gt; &lt;/tr&gt;</span>
<span style="color: #4070a0">    &lt;/table&gt;</span>
<span style="color: #4070a0">&lt;/body&gt;</span>
<span style="color: #4070a0"></span>
<span style="color: #4070a0">&lt;/html&gt;</span>
<span style="color: #4070a0">&#39;&#39;&#39;</span>
<span style="color: #007020; font-weight: bold">class</span> <span style="color: #0e84b5; font-weight: bold">Usage</span>(<span style="color: #007020">Exception</span>):
    <span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">__init__</span>(<span style="color: #007020">self</span>, msg):
        <span style="color: #007020">self</span><span style="color: #666666">.</span>msg <span style="color: #666666">=</span> msg

<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">find_versions</span>(path):
    <span style="color: #60a0b0; font-style: italic">#pdb.set_trace()</span>
    <span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&#39;looking for versions of </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&#39;</span> <span style="color: #666666">%</span> path
    <span style="color: #60a0b0; font-style: italic">#print time.strftime(&quot;%m/%d/%Y %I:%M:%S %p&quot;,time.localtime(os.path.getmtime(fname)))</span>
    backups <span style="color: #666666">=</span> os<span style="color: #666666">.</span>listdir(time_machine_path)
    backups<span style="color: #666666">.</span>sort()
    pathlist <span style="color: #666666">=</span> [os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(time_machine_path,b,boot_volume,path[<span style="color: #40a070">1</span>:]) <span style="color: #007020; font-weight: bold">for</span> b <span style="color: #007020; font-weight: bold">in</span> backups]
    pathlist<span style="color: #666666">.</span>append(path)
    versions <span style="color: #666666">=</span> []
    mod_times <span style="color: #666666">=</span> []
    <span style="color: #007020; font-weight: bold">for</span> f <span style="color: #007020; font-weight: bold">in</span> pathlist:
        <span style="color: #007020; font-weight: bold">if</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(f):
            mod_time <span style="color: #666666">=</span> time<span style="color: #666666">.</span>localtime(os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>getmtime(f))
            <span style="color: #007020; font-weight: bold">if</span> <span style="color: #007020; font-weight: bold">not</span> mod_time <span style="color: #007020; font-weight: bold">in</span> mod_times:
                mod_times<span style="color: #666666">.</span>append(mod_time)
                versions<span style="color: #666666">.</span>append({mod_time:f})
    <span style="color: #007020; font-weight: bold">return</span> versions
    
<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">getTMLocation</span>():
    <span style="color: #007020; font-weight: bold">global</span> time_machine_path
    <span style="color: #007020; font-weight: bold">if</span> time_machine_path <span style="color: #007020; font-weight: bold">and</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(time_machine_path):
        <span style="color: #007020; font-weight: bold">return</span> <span style="color: #007020">True</span>
    volumes <span style="color: #666666">=</span> os<span style="color: #666666">.</span>listdir(<span style="color: #4070a0">&#39;/Volumes&#39;</span>)
    <span style="color: #60a0b0; font-style: italic">#hostname = os.uname()[1].split(&#39;.&#39;)[0]</span>
    <span style="color: #60a0b0; font-style: italic">#cmd = &#39;scutil --get ComputerName&#39;</span>
    <span style="color: #60a0b0; font-style: italic">#machine_name = Popen(&#39;osascript -e \&#39;%s\&#39;&#39; % cmd,shell=True,stdout=PIPE,stderr=PIPE).communicate()[0][0:-1]</span>
    <span style="color: #007020; font-weight: bold">for</span> v <span style="color: #007020; font-weight: bold">in</span> volumes:
        <span style="color: #007020; font-weight: bold">if</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>exists(os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(<span style="color: #4070a0">&#39;/Volumes&#39;</span>,v,<span style="color: #4070a0">&#39;Backups.backupdb&#39;</span>)):
            backupsdb <span style="color: #666666">=</span> (os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(<span style="color: #4070a0">&#39;/Volumes&#39;</span>,v,<span style="color: #4070a0">&#39;Backups.backupdb&#39;</span>))
            time_machine_path <span style="color: #666666">=</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(backupsdb,os<span style="color: #666666">.</span>listdir(backupsdb)[<span style="color: #40a070">0</span>])
            <span style="color: #007020; font-weight: bold">return</span> <span style="color: #007020">True</span>
            <span style="color: #60a0b0; font-style: italic"># candidate_path = os.path.join(&#39;/Volumes&#39;,v,&#39;Backups.backupdb&#39;,machine_name)</span>
            <span style="color: #60a0b0; font-style: italic"># if os.path.exists(candidate_path):</span>
            <span style="color: #60a0b0; font-style: italic">#     time_machine_path = candidate_path</span>
            <span style="color: #60a0b0; font-style: italic">#     return True</span>
    <span style="color: #007020; font-weight: bold">return</span> <span style="color: #007020">False</span>
    
<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">main</span>(argv<span style="color: #666666">=</span><span style="color: #007020">None</span>):
    
    <span style="color: #007020; font-weight: bold">if</span> <span style="color: #007020; font-weight: bold">not</span> getTMLocation():
        <span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&#39;No Time Macine Backup Found&#39;</span>
        sys<span style="color: #666666">.</span>exit()
    <span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&#39;time machine path: &#39;</span> <span style="color: #666666">+</span> time_machine_path
    <span style="color: #007020; font-weight: bold">if</span> argv <span style="color: #007020; font-weight: bold">is</span> <span style="color: #007020">None</span>:
        argv <span style="color: #666666">=</span> sys<span style="color: #666666">.</span>argv
    <span style="color: #007020; font-weight: bold">try</span>:
        <span style="color: #007020; font-weight: bold">try</span>:
            opts, args <span style="color: #666666">=</span> getopt<span style="color: #666666">.</span>getopt(argv[<span style="color: #40a070">1</span>:], <span style="color: #4070a0">&quot;ho:v&quot;</span>, [<span style="color: #4070a0">&quot;help&quot;</span>, <span style="color: #4070a0">&quot;output=&quot;</span>])
        <span style="color: #007020; font-weight: bold">except</span> getopt<span style="color: #666666">.</span>error, msg:
            <span style="color: #007020; font-weight: bold">raise</span> Usage(msg)
    
        <span style="color: #60a0b0; font-style: italic"># option processing</span>
        <span style="color: #007020; font-weight: bold">for</span> option, value <span style="color: #007020; font-weight: bold">in</span> opts:
            <span style="color: #007020; font-weight: bold">if</span> option <span style="color: #666666">==</span> <span style="color: #4070a0">&quot;-v&quot;</span>:
                verbose <span style="color: #666666">=</span> <span style="color: #007020">True</span>
            <span style="color: #007020; font-weight: bold">if</span> option <span style="color: #007020; font-weight: bold">in</span> (<span style="color: #4070a0">&quot;-h&quot;</span>, <span style="color: #4070a0">&quot;â€“help&quot;</span>):
                <span style="color: #007020; font-weight: bold">raise</span> Usage(help_message)
            <span style="color: #007020; font-weight: bold">if</span> option <span style="color: #007020; font-weight: bold">in</span> (<span style="color: #4070a0">&quot;-o&quot;</span>, <span style="color: #4070a0">&quot;â€“output&quot;</span>):
                output <span style="color: #666666">=</span> value
        <span style="color: #007020; font-weight: bold">if</span> <span style="color: #007020; font-weight: bold">not</span> args:
            <span style="color: #007020; font-weight: bold">raise</span> Usage(help_message)
        differ <span style="color: #666666">=</span> difflib<span style="color: #666666">.</span>HtmlDiff(tabsize<span style="color: #666666">=</span><span style="color: #40a070">4</span>)
        html <span style="color: #666666">=</span> header
        <span style="color: #007020; font-weight: bold">for</span> path <span style="color: #007020; font-weight: bold">in</span> args:
            path <span style="color: #666666">=</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>join(os<span style="color: #666666">.</span>getcwd(),path)
            html <span style="color: #666666">+=</span> <span style="color: #4070a0">&#39;&lt;h1&gt;Changes for </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&lt;/h1&#39;</span> <span style="color: #666666">%</span> os<span style="color: #666666">.</span>path<span style="color: #666666">.</span>basename(path)
            versions <span style="color: #666666">=</span> find_versions(path)
            <span style="color: #007020; font-weight: bold">if</span> <span style="color: #007020">len</span>(versions) <span style="color: #666666">&lt;</span> <span style="color: #40a070">2</span>:
                html <span style="color: #666666">+=</span> <span style="color: #4070a0">&#39;&lt;h2&gt;Less than 2 Versions found on Time Machine Backup&lt;/h2&gt;&#39;</span>
            <span style="color: #007020; font-weight: bold">else</span>:
                html <span style="color: #666666">+=</span> <span style="color: #4070a0">&#39;&lt;h2&gt;</span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0"> versions found&lt;/h2&gt;&#39;</span> <span style="color: #666666">%</span> <span style="color: #007020">len</span>(versions)
            <span style="color: #007020; font-weight: bold">for</span> i <span style="color: #007020; font-weight: bold">in</span> <span style="color: #007020">range</span>(<span style="color: #40a070">0</span>,<span style="color: #007020">len</span>(versions)):
                <span style="color: #007020; font-weight: bold">if</span> i: <span style="color: #60a0b0; font-style: italic">#skip the first</span>
                    d1 <span style="color: #666666">=</span> time<span style="color: #666666">.</span>strftime(<span style="color: #4070a0">&quot;%m/</span><span style="color: #70a0d0; font-style: italic">%d</span><span style="color: #4070a0">/%Y %I:%M:%S %p&quot;</span>,versions[i<span style="color: #666666">-</span><span style="color: #40a070">1</span>]<span style="color: #666666">.</span>keys()[<span style="color: #40a070">0</span>])
                    d2 <span style="color: #666666">=</span> time<span style="color: #666666">.</span>strftime(<span style="color: #4070a0">&quot;%m/</span><span style="color: #70a0d0; font-style: italic">%d</span><span style="color: #4070a0">/%Y %I:%M:%S %p&quot;</span>,versions[i]<span style="color: #666666">.</span>keys()[<span style="color: #40a070">0</span>])
                    html <span style="color: #666666">=</span> html <span style="color: #666666">+</span> <span style="color: #4070a0">&#39;&lt;h2&gt;changes from </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0"> to </span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&lt;/h2&gt;&#39;</span> <span style="color: #666666">%</span> (d1,d2)
                    l1 <span style="color: #666666">=</span> <span style="color: #007020">open</span>(versions[i<span style="color: #666666">-</span><span style="color: #40a070">1</span>]<span style="color: #666666">.</span>values()[<span style="color: #40a070">0</span>])<span style="color: #666666">.</span>readlines()
                    l2 <span style="color: #666666">=</span> <span style="color: #007020">open</span>(versions[i]<span style="color: #666666">.</span>values()[<span style="color: #40a070">0</span>])<span style="color: #666666">.</span>readlines()
                    table <span style="color: #666666">=</span> differ<span style="color: #666666">.</span>make_table(l1,l2,context<span style="color: #666666">=</span><span style="color: #007020">True</span>,numlines<span style="color: #666666">=</span><span style="color: #40a070">3</span>)
                    html <span style="color: #666666">+=</span> table
        html <span style="color: #666666">+=</span> footer
        o <span style="color: #666666">=</span> <span style="color: #007020">open</span>(<span style="color: #4070a0">&#39;/tmp/diff.html&#39;</span>,<span style="color: #4070a0">&#39;w&#39;</span>)
        o<span style="color: #666666">.</span>write(html)
        o<span style="color: #666666">.</span>close()
        <span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">webbrowser</span>
        webbrowser<span style="color: #666666">.</span>open(<span style="color: #4070a0">&#39;/tmp/diff.html&#39;</span>)
    <span style="color: #007020; font-weight: bold">except</span> Usage, err:
        <span style="color: #007020; font-weight: bold">print</span> <span style="color: #666666">&gt;&gt;</span> sys<span style="color: #666666">.</span>stderr, sys<span style="color: #666666">.</span>argv[<span style="color: #40a070">0</span>]<span style="color: #666666">.</span>split(<span style="color: #4070a0">&quot;/&quot;</span>)[<span style="color: #666666">-</span><span style="color: #40a070">1</span>] <span style="color: #666666">+</span> <span style="color: #4070a0">&quot;: &quot;</span> <span style="color: #666666">+</span> <span style="color: #007020">str</span>(err<span style="color: #666666">.</span>msg)
        <span style="color: #007020; font-weight: bold">print</span> <span style="color: #666666">&gt;&gt;</span> sys<span style="color: #666666">.</span>stderr, <span style="color: #4070a0">&quot;</span><span style="color: #4070a0; font-weight: bold">\t</span><span style="color: #4070a0"> for help use â€“help&quot;</span>
        <span style="color: #007020; font-weight: bold">return</span> <span style="color: #40a070">2</span>


<span style="color: #007020; font-weight: bold">if</span> __name__ <span style="color: #666666">==</span> <span style="color: #4070a0">&quot;__main__&quot;</span>:
    sys<span style="color: #666666">.</span>exit(main())
</pre></div>
]]></content:encoded>
			<wfw:commentRss>http://ptone.com/dablog/2009/02/time-machine-poor-mans-version-control/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fixing PDB and TextMate for files with spaces in path</title>
		<link>http://ptone.com/dablog/2009/01/fixing-pdb-and-textmate-for-files-with-spaces-in-path/</link>
		<comments>http://ptone.com/dablog/2009/01/fixing-pdb-and-textmate-for-files-with-spaces-in-path/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 21:55:51 +0000</pubDate>
		<dc:creator>ptone</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ptone.com/dablog/?p=40</guid>
		<description><![CDATA[There is a cool little glue package out there called PdbTextMateSupport that allows you to highlight the current line of python code as you debug it in PDB, python&#8217;s debugger. It works by sending URLs to textmate &#8211; however the python code does not escape spaces in the path, and if you are on OS [...]]]></description>
			<content:encoded><![CDATA[<div>There is a cool little glue package out there called PdbTextMateSupport that allows you to highlight the current line of python code as you debug it in PDB, python&#8217;s debugger.  It works by sending URLs to textmate &#8211; however the python code does not escape spaces in the path, and if you are on OS X there is a decent chance that you have a folder somewhere along the way that might have a space&#8230;

Here is the fix:</div>

<p><span id="more-40"></span></p>

<div class="highlight" ><pre>om os<span style="color: #666666">.</span>path <span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">exists</span>
<span style="color: #007020; font-weight: bold">from</span> <span style="color: #0e84b5; font-weight: bold">os</span> <span style="color: #007020; font-weight: bold">import</span> system
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">urllib</span>

<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">mate</span>(<span style="color: #007020">self</span>):
    frame, lineno <span style="color: #666666">=</span> <span style="color: #007020">self</span><span style="color: #666666">.</span>stack[<span style="color: #007020">self</span><span style="color: #666666">.</span>curindex]
    filename <span style="color: #666666">=</span> <span style="color: #007020">self</span><span style="color: #666666">.</span>canonic(frame<span style="color: #666666">.</span>f_code<span style="color: #666666">.</span>co_filename)
    f2 <span style="color: #666666">=</span> urllib<span style="color: #666666">.</span>quote(filename)
    <span style="color: #007020; font-weight: bold">if</span> exists(filename):
        tm_url <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;txmt://open?url=file://</span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&amp;line=</span><span style="color: #70a0d0; font-style: italic">%d</span><span style="color: #4070a0">&amp;column=2&#39;</span> <span style="color: #666666">%</span> (f2, lineno)
        osa_cmd <span style="color: #666666">=</span> <span style="color: #4070a0">&#39;tell application &quot;TextMate&quot; to get url &quot;</span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0">&quot;&#39;</span> <span style="color: #666666">%</span> tm_url
        system(<span style="color: #4070a0">&#39;osascript -e </span><span style="color: #4070a0; font-weight: bold">\&#39;</span><span style="color: #70a0d0; font-style: italic">%s</span><span style="color: #4070a0; font-weight: bold">\&#39;</span><span style="color: #4070a0">&#39;</span> <span style="color: #666666">%</span> osa_cmd)

<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">preloop</span>(<span style="color: #007020">self</span>):
    mate(<span style="color: #007020">self</span>)

<span style="color: #007020; font-weight: bold">def</span> <span style="color: #06287e">precmd</span>(<span style="color: #007020">self</span>, line):
    mate(<span style="color: #007020">self</span>)
    <span style="color: #007020; font-weight: bold">return</span> line
</pre></div>
]]></content:encoded>
			<wfw:commentRss>http://ptone.com/dablog/2009/01/fixing-pdb-and-textmate-for-files-with-spaces-in-path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>quick python cgi to send email</title>
		<link>http://ptone.com/dablog/2008/12/quick-python-cgi-to-send-email/</link>
		<comments>http://ptone.com/dablog/2008/12/quick-python-cgi-to-send-email/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 17:43:40 +0000</pubDate>
		<dc:creator>ptone</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://ptone.com/dablog/?p=39</guid>
		<description><![CDATA[In the previous post I mentioned a python script I was using with curl to send me a SMS message. Its a pretty quick and dirty one, but here is the script I use. Since these are quick messages meant for my phone &#8211; I don&#8217;t do much with the body. And I just use [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous post I mentioned a python script I was using with curl to send me a SMS message.  Its a pretty quick and dirty one, but here is the script I use.  Since these are quick messages meant for my phone &#8211; I don&#8217;t do much with the body.  And I just use this with a GET style URL, although the python cgi lib could just as easily handle a POST.  So I call it like this:</p>

<p>http://www.domain.net/cgi-bin/sendmemail.py?to=number@txt.att.net&amp;subject=printing_done</p>

<p>I use this via curl in other scripts as a sort of notification system.
<span id="more-39"></span>
<div class="highlight" ><pre><span style="color: #60a0b0; font-style: italic">#! /usr/bin/env python</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">cgi</span><span style="color: #666666">,</span><span style="color: #0e84b5; font-weight: bold">cgitb</span>
cgitb<span style="color: #666666">.</span>enable()
<span style="color: #60a0b0; font-style: italic"># Required header that tells the browser how to render the text.</span>
<span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&quot;Content-Type: text/plain</span><span style="color: #4070a0; font-weight: bold">\n\n</span><span style="color: #4070a0">&quot;</span></p>

<p><span style="color: #60a0b0; font-style: italic"># Print a simple message to the display window.</span>
<span style="color: #007020; font-weight: bold">from</span> <span style="color: #0e84b5; font-weight: bold">email.MIMEText</span> <span style="color: #007020; font-weight: bold">import</span> MIMEText
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">smtplib</span><span style="color: #666666">,</span><span style="color: #0e84b5; font-weight: bold">sys</span></p>

<p>The_Form <span style="color: #666666">=</span> cgi<span style="color: #666666">.</span>FieldStorage()</p>

<p>body<span style="color: #666666">=</span><span style="color: #4070a0">&#39;&#39;&#39;blank</span>
<span style="color: #4070a0">&#39;&#39;&#39;</span></p>

<p>msg <span style="color: #666666">=</span> MIMEText(body)</p>

<p>mfrom <span style="color: #666666">=</span> <span style="color: #4070a0">&quot;email@example.com&quot;</span>
to <span style="color: #666666">=</span> The<em>Form[<span style="color: #4070a0">&#39;to&#39;</span>]<span style="color: #666666">.</span>value
msg[<span style="color: #4070a0">&#39;From&#39;</span>] <span style="color: #666666">=</span> mfrom
msg[<span style="color: #4070a0">&#39;To&#39;</span>] <span style="color: #666666">=</span> to
msg[<span style="color: #4070a0">&#39;Subject&#39;</span>] <span style="color: #666666">=</span> The</em>Form[<span style="color: #4070a0">&#39;subject&#39;</span>]<span style="color: #666666">.</span>value</p>

<p>server <span style="color: #666666">=</span> smtplib<span style="color: #666666">.</span>SMTP(<span style="color: #4070a0">&quot;smtp.gmail.com&quot;</span>,<span style="color: #40a070">587</span>)
server<span style="color: #666666">.</span>ehlo()
server<span style="color: #666666">.</span>starttls()
server<span style="color: #666666">.</span>ehlo()
server<span style="color: #666666">.</span>login(<span style="color: #666666">&lt;</span>smtp login<span style="color: #666666">&gt;</span>,<span style="color: #666666">&lt;</span>smtp password<span style="color: #666666">&gt;</span>)
server<span style="color: #666666">.</span>sendmail(mfrom,[to],msg<span style="color: #666666">.</span>as_string())
server<span style="color: #666666">.</span>quit
<span style="color: #007020; font-weight: bold">print</span> <span style="color: #4070a0">&#39;sent&#39;</span>
</pre></div></p>
]]></content:encoded>
			<wfw:commentRss>http://ptone.com/dablog/2008/12/quick-python-cgi-to-send-email/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

