Remove unused links from docs by jdufresne · Pull Request #5735 · encode/django-rest-framework (original) (raw)

I noticed some of these links were unused while working on #5729. I wrote the following script to find them all:

#!/usr/bin/env python3

import os import re import subprocess

link_re = re.compile(r'^([.*]):')

prune_name = [ '.git', '.hg', '.tox', 'pycache', 'LC_MESSAGES', 'node_modules', ]

root = os.getcwd() for dirpath, dirnames, filenames in os.walk(root): for name in prune_name: try: dirnames.remove(name) except ValueError: pass

for fn in filenames:
    _, ext = os.path.splitext(fn)
    if ext == '.md':
        links = []
        path = os.path.join(dirpath, fn)
        with open(path) as fp:
            for line in fp:
                match = link_re.match(line)
                if match:
                    links.append(match.group(1))
        for link in links:
            cmd = [
                'grep',
                '-c',
                '-F', link,
                path,
            ]
            out = subprocess.check_output(cmd)
            count = int(out.strip())
            if count < 2:
                print(path, link, count)