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