PageRenderTime 2760ms CodeModel.GetById 32ms RepoModel.GetById 9ms app.codeStats 0ms

/pypy/doc/tool/makeref.py

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