#! /usr/bin/env python3 import argparse from fnmatch import fnmatch import os from news import write_if_changed parser = argparse.ArgumentParser(description='Generate sitemap') parser.add_argument('-x', '--exclude', action='append', help='exclude directories (pattern)') parser.add_argument('root_dir', help='Root dicrectory') args = parser.parse_args() exclude = [] if args.exclude: for pat in args.exclude: exclude.append(pat) if not pat.endswith('/*'): exclude.append(pat + '/*') os.chdir(args.root_dir) fullpath = os.getcwd() fp_len = len(fullpath) tree = ('', []) current_subtree = tree[1] save_level = 0 for dirpath, dirs, files in sorted(os.walk(fullpath)): try: dirpath = dirpath[fp_len:] if not dirpath: continue for pat in exclude: if fnmatch(dirpath, pat): raise StopIteration parts = dirpath.split('/') level = len(parts) - 2 if level < save_level: current_subtree = tree[1] for i in range(level): current_subtree = current_subtree[-1][1] elif level > save_level: assert level - save_level == 1 current_subtree = current_subtree[-1][1] # else: # level == save_level: current_subtree.append((parts[-1], [])) save_level = level except StopIteration: pass def _tree2html(tree, path='', level=0): subparts = [] indent = " " * (level + 1) for title, subtree in tree: subpath = "%s/%s" % (path, title) if subpath.startswith('/'): subpath = subpath[1:] href = '%s' % (subpath, title) if subtree: subparts.append(indent + "