import hashlib
import os
import shutil
import sys

from subprocess import check_call
from urllib2 import urlopen
from zipfile import ZipFile


YUIDOC_URL = 'http://yuilibrary.com/downloads/yuidoc/yuidoc_1.0.0b1.zip'
YUIDOC_MD5 = 'cd5545d2dec8f7afe3d18e793538162c'

class BuildError(Exception):
    pass

class BuildApidocsCommand(object):
    def __init__(self, stdout=sys.stdout, stderr=sys.stderr):
        self.stdout = stdout
        self.stderr = stderr

    def log(self, message, newline=os.linesep):
        self.stderr.write(''.join((message, newline)))

    def main(self, argv=sys.argv):
        workingbranch_dir = os.path.join(os.path.dirname(__file__), '..')

        # setup working dir
        tmpdir = os.path.join(workingbranch_dir, 'build')
        if not os.path.isdir(tmpdir):
            self.log('Creating working dir: %s' % (workingbranch_dir,))
            os.mkdir(tmpdir)
        else:
            self.log('Using working dir: %s' % (workingbranch_dir,))

        # download and cache yuidoc
        yuizip_path = os.path.join(tmpdir, os.path.basename(YUIDOC_URL))
        if os.path.exists(yuizip_path):
            def producer():
                self.log('Using cached YUI doc')
                yield open(yuizip_path).read()
        else:
            def producer():
                with open(yuizip_path, 'w') as yuizip:
                    self.log('Downloading YUI Doc', newline='')
                    download = urlopen(YUIDOC_URL)
                    while True:
                        bytes = download.read(1024*10)
                        if not bytes:
                            self.log('')
                            break
                        else:
                            yuizip.write(bytes)
                            self.log('.', newline='')
                            yield bytes

        checksum = hashlib.md5()
        for bytes in producer():
            checksum.update(bytes)

        actual_md5 = checksum.hexdigest()
        if actual_md5 != YUIDOC_MD5:
            raise BuildError(
                'YUI Doc checksum error. '
                'Expected: %s, Got: %s' % (YUIDOC_MD5, actual_md5))
        else:
            self.log('YUI Doc checksum verified')

        # extract yuidoc folder from the downloaded zip file
        zip = ZipFile(yuizip_path)
        self.log('Extracting YUI Doc')
        zip.extractall(
            tmpdir, (m for m in zip.namelist() if m.startswith('yuidoc')))

        # Remove any existing apidocs so that we can track removed files
        shutil.rmtree(os.path.join(workingbranch_dir, 'docs', 'apidocs'))

        # Use the yuidoc script that we just extracted to generate new docs
        self.log('Running YUI Doc')
        check_call((
            sys.executable,
            os.path.join(tmpdir, 'yuidoc', 'bin', 'yuidoc.py'),
            workingbranch_dir,
            '-p', os.path.join(workingbranch_dir, 'docs', 'apidocs'),
            '-o', os.path.join(workingbranch_dir, 'docs', 'apidocs'),
            '-t', os.path.join(
                        workingbranch_dir, 'jarmonbuild', 'yuidoc_template'),
            '-v', '10.8',
            '-Y', '2',
            '--project=Jarmon',
            '--projecturl=http://www.launchpad.net/jarmon'
        ))

        self.log('Removing working dir: %s' % (tmpdir,))
        shutil.rmtree(tmpdir)

@@ -1,12 +1,142 @@
+# Copyright (c) 2010 Richard Wall %3Crichard (at) the-moon.net%3E%0A%22%22%22%0AFunctions and Classes for automating the release of Jarmon%0A%22%22%22%0A%0A
 import hashl
@@ -388,16 +388,17 @@
 8162c'%0A%0A
+%0A
 class Bu
@@ -418,16 +418,84 @@
 ption):%0A
+    %22%22%22%0A    A base Exception for errors in the build system%0A    %22%22%22%0A
     pass
@@ -496,16 +496,17 @@
   pass%0A%0A
+%0A
 class Bu
@@ -532,16 +532,96 @@
 bject):%0A
+    %22%22%22%0A    Download YUI Doc and use it to generate apidocs for jarmon%0A    %22%22%22%0A%0A
     def 
@@ -635,16 +635,17 @@
 _(self, 
+_
 stdout=s
@@ -655,16 +655,17 @@
 stdout, 
+_
 stderr=s
@@ -698,16 +698,17 @@
 tdout = 
+_
 stdout%0A 
@@ -728,16 +728,17 @@
 tderr = 
+_
 stderr%0A%0A
@@ -797,90 +797,442 @@
     
-self.stderr.write(''.join((message, newline)))%0A%0A    def main(self, argv=sys.argv):
+%22%22%22%0A        @param message: A message to be logged%0A        @param newline: The newline string to be appended to the message. Use%0A            '' to prevent a newline%0A        %22%22%22%0A        self.stderr.write(''.join((message, newline)))%0A%0A    def main(self, argv=sys.argv%5B1:%5D):%0A        %22%22%22%0A        The main entry point for the build-apidocs command%0A%0A        @param argv: The list of arguments passed to the build-apidocs command%0A        %22%22%22
 %0A   
@@ -2853,24 +2853,226 @@
 verified')%0A%0A
+        yuidoc_dir = os.path.join(tmpdir, 'yuidoc')%0A        if os.path.isdir(yuidoc_dir):%0A            self.log('Removing existing yuidoc dir: %25s' %25 (yuidoc_dir,))%0A            shutil.rmtree(yuidoc_dir)%0A%0A
         # ex
@@ -4103,92 +4103,4 @@
  ))%0A
-%0A        self.log('Removing working dir: %25s' %25 (tmpdir,))%0A        shutil.rmtree(tmpdir)%0A

Add docstrings and don't remove the build dir on completion