blob: a35152cef10e2fa896d30ca70c8b85d2f5082030 [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):
Collin Winter828f04a2007-08-31 00:04:24 +000062 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':
Collin Wintere7bf59f2007-08-30 18:39:28 +0000103 if self.verbose: print('hacking %s to %s' % (origPath,outPath))
Tim Peters182b5ac2004-07-18 06:16:08 +0000104 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>')
Alexandre Vassalotti4e6531e2008-05-09 20:00:17 +0000112 os.walk(self.build_html, self.visit, None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000113
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)
Collin Wintere7bf59f2007-08-30 18:39:28 +0000121 print("Please wait until Apple Help Indexing Tool finishes before installing")
Tim Peters182b5ac2004-07-18 06:16:08 +0000122
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):
Collin Winter828f04a2007-08-31 00:04:24 +0000149 raise RuntimeError("Can't find source folder for documentation.")
Tim Peters182b5ac2004-07-18 06:16:08 +0000150 self.mkpath(self.build_dest)
151 if dep_util.newer(os.path.join(self.build_html,'index.html'), os.path.join(self.build_dest,'index.html')):
152 self.mkpath(self.build_dest)
153 self.hackHtml()
154 self.hackIndex()
155 self.makeHelpIndex()
Jack Jansen278a3a22002-08-28 22:22:10 +0000156
Jack Jansend2c684f2003-02-14 23:46:22 +0000157class AHVDocInstall(Command):
Tim Peters182b5ac2004-07-18 06:16:08 +0000158 description = "install Apple Help Viewer html files"
159 user_options = [('install-doc=', 'd',
160 'directory to install HTML tree'),
161 ('root=', None,
162 "install everything relative to this alternate root directory"),
163 ]
164
165 def initialize_options(self):
166 self.build_dest = None
167 self.install_doc = None
168 self.prefix = None
169 self.root = None
170
171 def finalize_options(self):
172 self.set_undefined_options('install',
173 ('prefix', 'prefix'),
174 ('root', 'root'))
175# import pdb ; pdb.set_trace()
176 build_cmd = self.get_finalized_command('build')
Benjamin Peterson2a691a82008-03-31 01:51:45 +0000177 if self.build_dest is None:
Tim Peters182b5ac2004-07-18 06:16:08 +0000178 build_cmd = self.get_finalized_command('build')
179 self.build_dest = build_cmd.build_dest
Benjamin Peterson2a691a82008-03-31 01:51:45 +0000180 if self.install_doc is None:
Jack Jansen6d22b562005-04-17 21:30:52 +0000181 self.install_doc = os.path.join(self.prefix, DESTDIR)
Collin Wintere7bf59f2007-08-30 18:39:28 +0000182 print('INSTALL', self.build_dest, '->', self.install_doc)
Tim Peters182b5ac2004-07-18 06:16:08 +0000183
184 def run(self):
185 self.finalize_options()
186 self.ensure_finalized()
Collin Wintere7bf59f2007-08-30 18:39:28 +0000187 print("Running Installer")
Tim Peters182b5ac2004-07-18 06:16:08 +0000188 instloc = self.install_doc
189 if self.root:
190 instloc = change_root(self.root, instloc)
191 self.mkpath(instloc)
192 copy_tree(self.build_dest, instloc)
Collin Wintere7bf59f2007-08-30 18:39:28 +0000193 print("Installation complete")
Tim Peters182b5ac2004-07-18 06:16:08 +0000194
Jack Jansen278a3a22002-08-28 22:22:10 +0000195def mungeVersion(infile, outfile):
Tim Peters182b5ac2004-07-18 06:16:08 +0000196 i = file(infile,'r')
197 o = file(outfile,'w')
198 o.write(re.sub('\$\(VERSION\)',sysconfig.get_config_var('VERSION'),i.read()))
199 i.close()
200 o.close()
201
Jack Jansen278a3a22002-08-28 22:22:10 +0000202def main():
Tim Peters182b5ac2004-07-18 06:16:08 +0000203 # turn off warnings when deprecated modules are imported
204## import warnings
205## warnings.filterwarnings("ignore",category=DeprecationWarning)
206 setup(name = 'Documentation',
207 version = '%d.%d' % sys.version_info[:2],
208 cmdclass = {'install_data':AHVDocInstall, 'build':DocBuild},
209 data_files = ['dummy'],
210 )
Jack Jansen278a3a22002-08-28 22:22:10 +0000211
212if __name__ == '__main__':
Tim Peters182b5ac2004-07-18 06:16:08 +0000213 main()