File: find_rst.py

package info (click to toggle)
cmake-format 0.6.13-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,436 kB
  • sloc: python: 16,990; makefile: 14
file content (68 lines) | stat: -rw-r--r-- 1,936 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Find all restructured text files and write them out to a manifest
"""

from __future__ import print_function

import argparse
import os

EXCLUDE_DIRS = [
    ".build",
    ".git"
]


def main():
  parser = argparse.ArgumentParser(description=__doc__)
  parser.add_argument("-m", "--manifest-path",
                      help="Path to the manifest file to create/update")
  parser.add_argument("-t", "--touch", action="store_true",
                      help="Touch the manifest if any rst files are newer")
  parser.add_argument("rootdir")
  args = parser.parse_args()

  rootdir = os.path.realpath(args.rootdir)

  latest_mtime = 0

  fileset = set()
  for parent, dirnames, filenames in os.walk(rootdir):
    dirnames[:] = sorted(dirnames)
    relpath_parent = os.path.relpath(parent, rootdir)
    if relpath_parent in EXCLUDE_DIRS:
      dirnames[:] = []
      continue

    for filename in filenames:
      if filename.endswith(".rst"):
        if relpath_parent == ".":
          relpath_file = filename
        else:
          relpath_file = os.path.join(relpath_parent, filename)
        file_mtime = os.path.getmtime(os.path.join(rootdir, relpath_file))
        latest_mtime = max(latest_mtime, file_mtime)
        fileset.add(relpath_file)

  manifest_mtime = 0
  manifest_path = os.path.realpath(args.manifest_path)
  manifest_set = set()
  if os.path.exists(manifest_path):
    manifest_mtime = os.path.getmtime(manifest_path)
    with open(manifest_path, "r") as infile:
      for line in infile:
        manifest_set.add(line.strip())

  if manifest_set != fileset:
    print("RST manifest has changed")
    with open(manifest_path, "w") as outfile:
      for relpath_file in sorted(fileset):
        outfile.write(relpath_file)
        outfile.write("\n")
  elif args.touch and latest_mtime > manifest_mtime:
    print("An RST file has changed")
    os.utime(manifest_path, None)


if __name__ == "__main__":
  main()