def build_database(self, dirname=None, suffix="rst"): """Build the links database by searching all the files with the specified suffix recursively. if dirname is None, then find the current directory """ DB_NAME = "links_database.py" import os dirname = os.getcwd() if dirname is None else dirname for obj in os.listdir(dirname): curobj = os.path.join(dirname, obj) if os.path.isdir(curobj): self.build_database(curobj, suffix) elif curobj.split(".")[-1] == suffix: # update the database try: fh = open(DB_NAME) old = fh.read() fh.close() exec(old) except IOError: LINKS = {} matches = self.extract_links(curobj) for m in matches: link_name = m[0] link = m[1] if link in ["http://", "https://"]: continue if link_name not in LINKS: key = u"%s" % link_name.decode("utf-8") LINKS.update({ key : link }) from django.utils import simplejson tpl = """# coding: utf-8\nLINKS = %s""" new = tpl % simplejson.dumps(LINKS, indent=4, ensure_ascii=False) new = new.encode("utf-8") fh = open(DB_NAME, "w") fh.write(new) fh.close()