#!/usr/bin/env python r""" Create index files that can be displayed as web pages in a given directory and all its sub-directories. There are options to exclude certain files and sub-directories. """ import argparse import os import sys def main(i_raw_args): l_args = parse_args(i_raw_args) create_index_file(l_args.logs_dir_path, '/', l_args.exclude) def create_index_file(i_dir_path, i_pretty_dir_path, i_exclude_list): r""" Create HTML index files for a given directory and all its sub-directories. Description of argument(s): i_dir_path The directory to generate an index file for. i_pretty_dir_path A pretty version of i_dir_path that can be shown to readers of the HTML page. For example, if i_dir_path is set to '/home/johndoe/logs/`, the caller may wish to only show '/logs/' in the HTML index pages. i_exclude_list A Python list of files and directories to exclude from """ l_index_file_path = os.path.join(i_dir_path, 'index.html') l_sub_dir_list = os.listdir(i_dir_path) # Created a sorted list of sub-directories in this directory l_dirs = sorted( [d for d in l_sub_dir_list if os.path.isdir(os.path.join(i_dir_path, d)) and d not in i_exclude_list]) # Create a sorted list of files in this directory l_files = sorted( [f for f in l_sub_dir_list if not os.path.isdir(os.path.join(i_dir_path, f)) and f not in i_exclude_list]) # Open up the index file we're going to write to. with open(l_index_file_path, 'w+') as l_index_file: l_index_file.write( '\n' '