blob: bd86a20a8386421148297056e7919d282e6af195 [file] [log] [blame]
Jack Jansen278a3a22002-08-28 22:22:10 +00001# Build and install an Apple Help Viewer compatible version of the Python
2# documentation into the framework.
3# Code by Bill Fancher, with some modifications by Jack Jansen.
4#
5# You must run this as a two-step process
6# 1. python setupDocs.py build
7# 2. Wait for Apple Help Indexing Tool to finish
8# 3. python setupDocs.py install
9#
10# To do:
11# - test whether the docs are available locally before downloading
12# - fix buildDocsFromSource
13# - Get documentation version from sys.version, fallback to 2.2.1
14# - See if we can somehow detect that Apple Help Indexing Tool is finished
15# - data_files to setup() doesn't seem the right way to pass the arguments
16#
17import sys, os, re
18from distutils.cmd import Command
19from distutils.command.build import build
20from distutils.core import setup
21from distutils.file_util import copy_file
22from distutils.dir_util import copy_tree
23from distutils.log import log
24from distutils.spawn import spawn
25from distutils import sysconfig, dep_util
Jack Jansend2c684f2003-02-14 23:46:22 +000026from distutils.util import change_root
Jack Jansenf7c82202003-05-27 22:47:55 +000027import HelpIndexingTool
28import Carbon.File
29import time
Jack Jansen278a3a22002-08-28 22:22:10 +000030
Jack Jansen6d22b562005-04-17 21:30:52 +000031MAJOR_VERSION='2.4'
32MINOR_VERSION='2.4.1'
33DESTDIR='/Applications/MacPython-%s/PythonIDE.app/Contents/Resources/English.lproj/PythonDocumentation' % MAJOR_VERSION
34
Jack Jansen278a3a22002-08-28 22:22:10 +000035class DocBuild(build):
Tim Peters182b5ac2004-07-18 06:16:08 +000036 def initialize_options(self):
37 build.initialize_options(self)
38 self.build_html = None
39 self.build_dest = None
Jack Jansen6d22b562005-04-17 21:30:52 +000040 self.download = 1
41 self.doc_version = MINOR_VERSION # Only needed if download is true
Jack Jansend2c684f2003-02-14 23:46:22 +000042
Tim Peters182b5ac2004-07-18 06:16:08 +000043 def finalize_options(self):
44 build.finalize_options(self)
45 if self.build_html is None:
46 self.build_html = os.path.join(self.build_base, 'html')
47 if self.build_dest is None:
48 self.build_dest = os.path.join(self.build_base, 'PythonDocumentation')
49
50 def spawn(self, *args):
51 spawn(args, 1, self.verbose, self.dry_run)
52
53 def downloadDocs(self):
54 workdir = os.getcwd()
Jack Jansen6d22b562005-04-17 21:30:52 +000055 # XXX Note: the next strings may change from version to version
56 url = 'http://www.python.org/ftp/python/doc/%s/html-%s.tar.bz2' % \
Tim Peters182b5ac2004-07-18 06:16:08 +000057 (self.doc_version,self.doc_version)
Jack Jansen6d22b562005-04-17 21:30:52 +000058 tarfile = 'html-%s.tar.bz2' % self.doc_version
59 dirname = 'Python-Docs-%s' % self.doc_version
Tim Peterse8906822005-04-20 17:45:13 +000060
Jack Jansen6d22b562005-04-17 21:30:52 +000061 if os.path.exists(self.build_html):
62 raise RuntimeError, '%s: already exists, please remove and try again' % self.build_html
Tim Peters182b5ac2004-07-18 06:16:08 +000063 os.chdir(self.build_base)
64 self.spawn('curl','-O', url)
Jack Jansen6d22b562005-04-17 21:30:52 +000065 self.spawn('tar', '-xjf', tarfile)
66 os.rename(dirname, 'html')
Tim Peters182b5ac2004-07-18 06:16:08 +000067 os.chdir(workdir)
Jack Jansen6d22b562005-04-17 21:30:52 +000068## print "** Please unpack %s" % os.path.join(self.build_base, tarfile)
69## print "** Unpack the files into %s" % self.build_html
70## raise RuntimeError, "You need to unpack the docs manually"
Tim Peters182b5ac2004-07-18 06:16:08 +000071
72 def buildDocsFromSource(self):
73 srcdir = '../../..'
74 docdir = os.path.join(srcdir, 'Doc')
75 htmldir = os.path.join(docdir, 'html')
76 spawn(('make','--directory', docdir, 'html'), 1, self.verbose, self.dry_run)
77 self.mkpath(self.build_html)
78 copy_tree(htmldir, self.build_html)
79
80 def ensureHtml(self):
81 if not os.path.exists(self.build_html):
82 if self.download:
83 self.downloadDocs()
84 else:
85 self.buildDocsFromSource()
86
87 def hackIndex(self):
88 ind_html = 'index.html'
89 #print 'self.build_dest =', self.build_dest
90 hackedIndex = file(os.path.join(self.build_dest, ind_html),'w')
91 origIndex = file(os.path.join(self.build_html,ind_html))
92 r = re.compile('<style type="text/css">.*</style>', re.DOTALL)
93 hackedIndex.write(r.sub('<META NAME="AppleTitle" CONTENT="Python Documentation">',origIndex.read()))
94
95 def hackFile(self,d,f):
96 origPath = os.path.join(d,f)
97 assert(origPath[:len(self.build_html)] == self.build_html)
98 outPath = os.path.join(self.build_dest, d[len(self.build_html)+1:], f)
99 (name, ext) = os.path.splitext(f)
100 if os.path.isdir(origPath):
101 self.mkpath(outPath)
102 elif ext == '.html':
103 if self.verbose: print 'hacking %s to %s' % (origPath,outPath)
104 hackedFile = file(outPath, 'w')
105 origFile = file(origPath,'r')
106 hackedFile.write(self.r.sub('<dl><dt><dd>', origFile.read()))
107 else:
108 copy_file(origPath, outPath)
109
110 def hackHtml(self):
111 self.r = re.compile('<dl><dd>')
112 os.path.walk(self.build_html, self.visit, None)
113
114 def visit(self, dummy, dirname, filenames):
115 for f in filenames:
116 self.hackFile(dirname, f)
117
118 def makeHelpIndex(self):
119 app = '/Developer/Applications/Apple Help Indexing Tool.app'
120 self.spawn('open', '-a', app , self.build_dest)
121 print "Please wait until Apple Help Indexing Tool finishes before installing"
122
123 def makeHelpIndex(self):
124 app = HelpIndexingTool.HelpIndexingTool(start=1)
125 app.open(Carbon.File.FSSpec(self.build_dest))
126 sys.stderr.write("Waiting for Help Indexing Tool to start...")
127 while 1:
128 # This is bad design in the suite generation code!
129 idle = app._get(HelpIndexingTool.Help_Indexing_Tool_Suite._Prop_idleStatus())
130 time.sleep(10)
131 if not idle: break
132 sys.stderr.write(".")
133 sys.stderr.write("\n")
134 sys.stderr.write("Waiting for Help Indexing Tool to finish...")
135 while 1:
136 # This is bad design in the suite generation code!
137 idle = app._get(HelpIndexingTool.Help_Indexing_Tool_Suite._Prop_idleStatus())
138 time.sleep(10)
139 if idle: break
140 sys.stderr.write(".")
141 sys.stderr.write("\n")
142
143
144 def run(self):
145 self.ensure_finalized()
146 self.mkpath(self.build_base)
147 self.ensureHtml()
148 if not os.path.isdir(self.build_html):
149 raise RuntimeError, \
150 "Can't find source folder for documentation."
151 self.mkpath(self.build_dest)
152 if dep_util.newer(os.path.join(self.build_html,'index.html'), os.path.join(self.build_dest,'index.html')):
153 self.mkpath(self.build_dest)
154 self.hackHtml()
155 self.hackIndex()
156 self.makeHelpIndex()
Jack Jansen278a3a22002-08-28 22:22:10 +0000157
Jack Jansend2c684f2003-02-14 23:46:22 +0000158class AHVDocInstall(Command):
Tim Peters182b5ac2004-07-18 06:16:08 +0000159 description = "install Apple Help Viewer html files"
160 user_options = [('install-doc=', 'd',
161 'directory to install HTML tree'),
162 ('root=', None,
163 "install everything relative to this alternate root directory"),
164 ]
165
166 def initialize_options(self):
167 self.build_dest = None
168 self.install_doc = None
169 self.prefix = None
170 self.root = None
171
172 def finalize_options(self):
173 self.set_undefined_options('install',
174 ('prefix', 'prefix'),
175 ('root', 'root'))
176# import pdb ; pdb.set_trace()
177 build_cmd = self.get_finalized_command('build')
178 if self.build_dest == None:
179 build_cmd = self.get_finalized_command('build')
180 self.build_dest = build_cmd.build_dest
181 if self.install_doc == None:
Jack Jansen6d22b562005-04-17 21:30:52 +0000182 self.install_doc = os.path.join(self.prefix, DESTDIR)
Tim Peters182b5ac2004-07-18 06:16:08 +0000183 print 'INSTALL', self.build_dest, '->', self.install_doc
184
185 def run(self):
186 self.finalize_options()
187 self.ensure_finalized()
188 print "Running Installer"
189 instloc = self.install_doc
190 if self.root:
191 instloc = change_root(self.root, instloc)
192 self.mkpath(instloc)
193 copy_tree(self.build_dest, instloc)
194 print "Installation complete"
195
Jack Jansen278a3a22002-08-28 22:22:10 +0000196def mungeVersion(infile, outfile):
Tim Peters182b5ac2004-07-18 06:16:08 +0000197 i = file(infile,'r')
198 o = file(outfile,'w')
199 o.write(re.sub('\$\(VERSION\)',sysconfig.get_config_var('VERSION'),i.read()))
200 i.close()
201 o.close()
202
Jack Jansen278a3a22002-08-28 22:22:10 +0000203def main():
Tim Peters182b5ac2004-07-18 06:16:08 +0000204 # turn off warnings when deprecated modules are imported
205## import warnings
206## warnings.filterwarnings("ignore",category=DeprecationWarning)
207 setup(name = 'Documentation',
208 version = '%d.%d' % sys.version_info[:2],
209 cmdclass = {'install_data':AHVDocInstall, 'build':DocBuild},
210 data_files = ['dummy'],
211 )
Jack Jansen278a3a22002-08-28 22:22:10 +0000212
213if __name__ == '__main__':
Tim Peters182b5ac2004-07-18 06:16:08 +0000214 main()