PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/doc/tool/makeref.py

https://bitbucket.org/kcr/pypy
Python | 64 lines | 54 code | 10 blank | 0 comment | 13 complexity | c23cd13df3ed41640fb647d4fc7a4b4c MD5 | raw file
Possible License(s): Apache-2.0
  1. import py
  2. import pypy
  3. pypydir = py.path.local(pypy.__file__).join('..')
  4. distdir = pypydir.dirpath()
  5. issue_url = 'http://bugs.pypy.org/issue/pypy-dev/'
  6. bitbucket_url = 'https://bitbucket.org/pypy/pypy/src/default/'
  7. import urllib2, posixpath
  8. def makeref(docdir):
  9. reffile = docdir.join('_ref.txt')
  10. linkrex = py.std.re.compile('`(\S+)`_')
  11. name2target = {}
  12. def addlink(linkname, linktarget):
  13. assert linkname and linkname != '/'
  14. if linktarget in name2target:
  15. if linkname in name2target[linktarget]:
  16. return
  17. name2target.setdefault(linktarget, []).append(linkname)
  18. for textfile in sorted(docdir.listdir()): # for subdirs, see below
  19. if textfile.ext != '.rst':
  20. continue
  21. content = textfile.read()
  22. found = False
  23. for linkname in linkrex.findall(content):
  24. if '/' in linkname:
  25. found = True
  26. assert distdir.join(linkname).check(), "link %s in %s is dead" % (linkname, textfile)
  27. url = bitbucket_url + linkname
  28. if not linkname.endswith("/") and distdir.join(linkname).check(dir=1):
  29. url += "/"
  30. addlink(linkname, url)
  31. elif linkname.startswith('issue'):
  32. found = True
  33. addlink(linkname, issue_url+linkname)
  34. if found:
  35. assert ".. include:: _ref.txt" in content, "you need to include _ref.txt in %s" % (textfile, )
  36. items = name2target.items()
  37. items.sort()
  38. lines = []
  39. for linktarget, linknamelist in items:
  40. linknamelist.sort()
  41. for linkname in linknamelist[:-1]:
  42. lines.append(".. _`%s`:" % linkname)
  43. lines.append(".. _`%s`: %s" %(linknamelist[-1], linktarget))
  44. lines.append('')
  45. reffile.write("\n".join(lines))
  46. print "wrote %d references to %r" %(len(lines), reffile)
  47. #print "last ten lines"
  48. #for x in lines[-10:]: print x
  49. # We need to build a new _ref.txt for each directory that uses it, because
  50. # they differ in the number of "../" that they need in the link targets...
  51. makeref(pypydir.join('doc'))
  52. makeref(pypydir.join('doc').join('jit'))