# Build and install an Apple Help Viewer compatible version of the Python # documentation into the framework. # Code by Bill Fancher, with some modifications by Jack Jansen. # # You must run this as a two-step process # 1. python setupDocs.py build # 2. Wait for Apple Help Indexing Tool to finish # 3. python setupDocs.py install # # To do: # - test whether the docs are available locally before downloading # - fix buildDocsFromSource # - Get documentation version from sys.version, fallback to 2.2.1 # - See if we can somehow detect that Apple Help Indexing Tool is finished # - data_files to setup() doesn't seem the right way to pass the arguments # import sys, os, re from distutils.cmd import Command from distutils.command.build import build from distutils.core import setup from distutils.file_util import copy_file from distutils.dir_util import copy_tree from distutils.log import log from distutils.spawn import spawn from distutils import sysconfig, dep_util from distutils.util import change_root class DocBuild(build): def initialize_options(self): build.initialize_options(self) self.build_html = None self.build_dest = None self.download = 1 self.doc_version = '2.2.2' def finalize_options(self): build.finalize_options(self) if self.build_html is None: self.build_html = os.path.join(self.build_base, 'html') if self.build_dest is None: self.build_dest = os.path.join(self.build_base, 'processed-html') def spawn(self, *args): spawn(args, 1, self.verbose, self.dry_run) def downloadDocs(self): workdir = os.getcwd() url = 'http://www.python.org/ftp/python/doc/%s/html-%s.tgz' % \ (self.doc_version,self.doc_version) os.chdir(self.build_base) self.spawn('curl','-O', url) os.chdir(workdir) tarfile = 'html-%s.tgz' % self.doc_version ## This no longer works due to name changes ## self.mkpath(self.build_html) ## os.chdir(self.build_html) ## self.spawn('tar', '-xzf', '../' + tarfile) ## os.chdir(workdir) print "** Please unpack %s" % os.path.join(self.build_base, tarfile) print "** Unpack the files into %s" % self.build_html raise RuntimeError, "You need to unpack the docs manually" def buildDocsFromSource(self): srcdir = '../../..' docdir = os.path.join(srcdir, 'Doc') htmldir = os.path.join(docdir, 'html') spawn(('make','--directory', docdir, 'html'), 1, self.verbose, self.dry_run) self.mkpath(self.build_html) copy_tree(htmldir, self.build_html) def ensureHtml(self): if not os.path.exists(self.build_html): if self.download: self.downloadDocs() else: self.buildDocsFromSource() def hackIndex(self): ind_html = 'index.html' #print 'self.build_dest =', self.build_dest hackedIndex = file(os.path.join(self.build_dest, ind_html),'w') origIndex = file(os.path.join(self.build_html,ind_html)) r = re.compile('', re.DOTALL) hackedIndex.write(r.sub('',origIndex.read())) def hackFile(self,d,f): origPath = os.path.join(d,f) assert(origPath[:len(self.build_html)] == self.build_html) outPath = os.path.join(self.build_dest, d[len(self.build_html)+1:], f) (name, ext) = os.path.splitext(f) if os.path.isdir(origPath): self.mkpath(outPath) elif ext == '.html': if self.verbose: print 'hacking %s to %s' % (origPath,outPath) hackedFile = file(outPath, 'w') origFile = file(origPath,'r') hackedFile.write(self.r.sub('