#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import argparse
import os.path
import csv

def main():
    parser = argparse.ArgumentParser(description='Convert CSV file to .htaccess directories for w3id.org')
    parser.add_argument('csv', metavar='CSV', nargs=1,
                       help='CSV file to parse')
    parser.add_argument("--output", default="build",
                        help="Path to populate with .htaccess directories")
    parser.add_argument("--no-header", action="store_false", dest="skip_header",
                        help="Do not skip first line of CSV")
    parser.add_argument("--header", action="store_true", dest="skip_header",
                        help="Skip first line of CSV")
    parser.set_defaults(skip_header=True)


    args = parser.parse_args()
    rows = None
    with open(args.csv[0]) as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            if rows is None:
                # First line
                rows = []
                if args.skip_header:
                    continue # don't add it
            (z_id,src,linktype,target,created,lastmodified,status,indexed) = row
            rows.append([src,linktype,target])
    rows.sort()
    rows.reverse() # /path/child before /path/ before /path

    for (src,linktype,target) in rows:
        print(src,"->",target)

if __name__ == "__main__":
    main()

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import argparse
import os.path
from pathlib import Path, PurePosixPath
import csv

def htaccess(output,src,linktype, target):
    srcpath = PurePosixPath(src).relative_to("/")
    htpath = output / srcpath / ".htaccess"
    if not htpath.parent.is_dir():
        htpath.parent.mkdir(parents=True)
    with htpath.open("w") as htfile:
        print("""
# Generated by cvs2w3id
RewriteEngine On
RewriteRule ^$ {0} [L,R]
""".format(target), file=htfile)

    print(srcpath,"->",target)

def delete_all(path):
    if not path.is_dir():
        path.unlink()
    else:
        for child in path.iterdir():
            delete_all(child)
        path.rmdir()

def main():
    parser = argparse.ArgumentParser(description='Convert CSV file to .htaccess directories for w3id.org')
    parser.add_argument('csv', metavar='CSV', nargs=1,
                       help='CSV file to parse')
    parser.add_argument("--output", default="build", type=Path,
                        help="Path to populate with .htaccess directories")
    parser.add_argument("--no-header", action="store_false", dest="skip_header",
                        help="Do not skip first line of CSV")
    parser.add_argument("--header", action="store_true", dest="skip_header",
                        help="Skip first line of CSV")
    parser.add_argument("--wipe", action="store_true", dest="wipe",
                        help="Wipe (any) existing directory")
    parser.set_defaults(skip_header=True, wipe=False)


    args = parser.parse_args()
    rows = None
    with open(args.csv[0]) as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            if rows is None:
                # First line
                rows = []
                if args.skip_header:
                    continue # don't add it
            (z_id,src,linktype,target,created,lastmodified,status,indexed) = row
            rows.append([src,linktype,target])
    rows.sort()
    rows.reverse() # /path/child before /path/ before /path

    if args.wipe and args.output.exists():
        delete_all(args.output)
    args.output.mkdir()
    for (src,linktype,target) in rows:
        htaccess(args.output,src,linktype,target)


if __name__ == "__main__":
    main()

Make .htaccess files