38 'MOD_LIST_ITER':
None,
49 _reAtAt = re.compile(
'@([a-zA-Z_]\w*)@|@([a-zA-Z_]\w*):(.+)@')
50 _Now = time.localtime()
53 'THIS_YEAR': repr(_Now.tm_year),
54 'THIS_DATE':
"%d.%02d.%02d" % (_Now.tm_year, _Now.tm_mon, _Now.tm_mday),
55 'THIS_TIME':
"%02d:%02d:%02d" % (_Now.tm_hour, _Now.tm_min, _Now.tm_sec),
59 def AtAtIdentifier(match):
60 if match.lastindex == 1:
61 return (match.group(1),
None)
62 if match.lastindex == 3:
63 return (match.group(2), match.group(3))
68 def AtAtWriteValAtomic(fp, val, fmt):
71 elif type(val) == str:
77 def AtAtWriteValList(fp, valList, fmt):
79 AtAtWriteValAtomic(fp, val, fmt)
84 def AtAtWriteVal(fp, id, valDict, fmt=None):
89 val(fp, id, valDict, fmt)
90 elif type(val) == list:
91 AtAtWriteValList(fp, val, fmt)
93 AtAtWriteValAtomic(fp, val, fmt)
96 def AtAtReplace(fileNameIn, userDict, bReplace=True):
97 fileNameTmp = fileNameIn +
'.tmp' 98 fpSrc = open(fileNameIn,
'r') 99 fpTmp = open(fileNameTmp, 'w')
101 line = fpSrc.readline()
105 for match
in _reAtAt.finditer(line):
107 fpTmp.write(line[m:match.start()])
108 id,fmt = AtAtIdentifier(match)
109 if userDict.has_key(id):
110 AtAtWriteVal(fpTmp, id, userDict, fmt)
111 elif AtAtBuiltIns.has_key(id):
112 AtAtWriteVal(fpTmp, id, AtAtBuiltIns, fmt)
114 AtAtWarning(fileNameIn, lineNum, match.start(), id,
115 "unknown identifier")
116 fpTmp.write(match.group(0))
119 fpTmp.write(line[m:])
121 line = fpSrc.readline()
125 os.rename(fileNameTmp, fileNameIn)
131 def AtAtWarning(fileName, lineNum, colNum, *args):
132 """ Print AtAt warning. 135 *args - List of warning message arguments. 137 wmsg =
"Warning: %s[%d,%d]" % (fileName, lineNum, colNum)
148 def AtAtCbModUrlListIter(fp, id, varDict, fmt):
150 for _ivar
in varDict[
'MOD_URL_LIST']:
156 def MakeEnv(docRoot, modSetup):
157 global DocRoot, PkgRoot, PyDocInfo, PkgInfo
158 dname = os.path.dirname(modSetup)
160 sys.path = [dname] + sys.path
162 sys.path = [
'.'] + sys.path
164 exec(
"import "+modSetup+
" as setup")
166 PrintUsageErr(
"%s: setup module not found - cannot import" % (modSetup))
168 now = time.localtime()
170 PkgRoot = setup.pkgroot
171 PyDocInfo = setup.PyDocInfo
172 PkgInfo = setup.PkgInfo
173 HtmlVars[
'PKG_NAME'] = PkgInfo[
'name']
174 HtmlVars[
'PKG_VER'] = PkgInfo[
'version']
175 HtmlVars[
'BRIEF'] = PkgInfo.get(
'description',
'')
176 HtmlVars[
'LONG_DESC'] = PkgInfo.get(
'long_description',
'')
177 HtmlVars[
'AUTHOR'] = PkgInfo.get(
'author',
'RoadNarrows')
178 HtmlVars[
'EMAIL'] = PkgInfo.get(
'author_email',
'oneway@roadnarrows.com')
179 HtmlVars[
'ORG_FQ'] = PkgInfo.get(
'maintainer',
'RoadNarrows LLC')
180 HtmlVars[
'ORG_INITIALS'] = PyDocInfo.get(
'org_initials',
'RN')
181 HtmlVars[
'ORG_URL'] = PkgInfo.get(
'url',
'http://www.roadnarrows.com/')
182 HtmlVars[
'MOD_LIST_ITER'] = AtAtCbModUrlListIter
183 for varname,imgfile
in PyDocInfo[
'images'].iteritems():
184 HtmlVars[varname] = imgfile
188 if not os.path.exists(DocRoot):
189 os.makedirs(DocRoot, mode=0775)
190 imgdir = DocRoot+os.sep+
'images' 191 if not os.path.exists(imgdir):
192 os.mkdir(DocRoot+os.sep+
'images', 0775)
196 srcdir = PyDocInfo[
'images_dir']
199 dstdir = DocRoot+os.sep+
'images'+os.sep
200 for varname,imgfile
in PyDocInfo[
'images'].iteritems():
201 shutil.copy(srcdir+os.sep+imgfile, dstdir)
202 print " Copied images." 205 def GetCbWalk(pyfilelist, dirname, fnames):
206 excludes = [
'.svn',
'.deps' 'obj']
208 if dirname.find(sEx) != -1:
210 pyfilelist += [dirname]
212 if len(f) > 3
and f[-3:] ==
'.py':
213 pyfilelist += [dirname+os.sep+f]
216 def GetPythonFileList(modname):
218 os.path.walk(modname, GetCbWalk, pyfilelist)
222 def MakeHtmlBatch(modbatch, htmlfilelist):
226 for modpath
in modbatch:
230 for modpath
in modbatch:
231 htmlfilelist += [modpath+
'.html']
235 def MakeHtmlPkgDocs():
237 for pkgdir
in PkgInfo[
'package_dir'].itervalues():
238 dname = os.path.dirname(pkgdir)
239 modname = os.path.basename(pkgdir)
240 HtmlVars[
'MOD_URL_LIST'] += [modname +
'.html']
248 pyfilelist = GetPythonFileList(modname)
250 for pyfile
in pyfilelist:
251 bname=os.path.basename(pyfile)
254 modpath = pyfile.replace(os.sep,
'.')
255 modpath = modpath.replace(
'.py',
'')
256 if len(modbatch) < 8:
257 modbatch += [modpath]
259 MakeHtmlBatch(modbatch, htmlfilelist)
261 if len(modbatch) > 0:
262 MakeHtmlBatch(modbatch, htmlfilelist)
265 for html
in htmlfilelist:
267 srcfile = dname + os.sep + html
270 dstfile = DocRoot+os.sep+html
272 os.rename(srcfile, DocRoot+os.sep+html)
274 print >>sys.stderr,
"%s -> %s: cannot copy" % (srcfile, dstfile)
278 postfile = AtAtReplace(PyDocInfo[
'index_template'], HtmlVars,
False)
279 os.rename(postfile, DocRoot+os.sep+
'index.html')
280 print ' Wrote index.html' 283 def MakePyDoc(docRoot, modSetup):
284 MakeEnv(docRoot, modSetup)
298 class Usage(Exception):
299 """ Command-Line Options Usage Exception Class. """ 300 def __init__(self, msg):
304 def PrintUsageErr(emsg):
305 """ Print Error Usage Message. """ 307 print "%s: %s" % (_Argv0, emsg)
309 print "%s: error" % (_Argv0)
310 print "Try '%s --help' for more information." % (_Argv0)
314 """ Print Command-Line Usage Message """ 316 usage: %s [OPTIONS] <setup_py_file> 319 """ % (_Argv0, _Argv0)
320 print """Options and arguments: 321 -d, --docroot=<dir> : Generated HTML documentation root directory 322 --vpath=<path> : Library virtual path 324 -h, --help : Display this help and exit. 328 def GetOptions(argv=None, **kwargs):
329 """ Get Main Options and Arguments """ 335 _Argv0 = kwargs.get(
'argv0', __file__)
338 kwargs[
'vpath'] =
None 344 opts, args = getopt.getopt(argv[1:],
"?hd:",
345 [
'help',
'docroot=',
'vpath=',
''])
346 except getopt.error, msg:
348 for opt, optarg
in opts:
349 if opt
in (
'-h',
'--help',
'-?'):
352 elif opt
in (
'-d',
'--docroot'):
353 kwargs[
'docroot'] = optarg
354 elif opt
in (
'--vpath'):
355 kwargs[
'vpath'] = optarg
357 PrintUsageErr(err.msg)
361 PrintUsageErr(
"No input setup.py file specified")
364 kwargs[
'setup'] = args[0]
369 def Main(argv=None, **kwargs):
372 kwargs = GetOptions(argv, **kwargs)
373 if kwargs[
'vpath']
is not None:
374 ldpath = os.getenv(
'LD_LIBRARY_PATH')
376 ldpath = kwargs[
'vpath'] +
':' + ldpath
378 ldpath = kwargs[
'vpath']
379 os.putenv(
'LD_LIBRARY_PATH', ldpath)
380 MakePyDoc(kwargs[
'docroot'], kwargs[
'setup'])