1#!/usr/bin/env python 2 3r""" 4Create index files that can be displayed as web pages in a given directory 5and all its sub-directories. There are options to exclude certain files and 6sub-directories. 7""" 8 9import argparse 10import os 11import sys 12 13 14def main(i_raw_args): 15 l_args = parse_args(i_raw_args) 16 create_index_file(l_args.logs_dir_path, '/', l_args.exclude) 17 18 19def create_index_file(i_dir_path, i_pretty_dir_path, i_exclude_list): 20 r""" 21 Create HTML index files for a given directory and all its sub-directories. 22 23 Description of argument(s): 24 i_dir_path The directory to generate an index file for. 25 i_pretty_dir_path A pretty version of i_dir_path that can be shown to 26 readers of the HTML page. For example, if i_dir_path 27 is set to '/home/johndoe/logs/`, the caller may wish 28 to only show '/logs/' in the HTML index pages. 29 i_exclude_list A Python list of files and directories to exclude from 30 """ 31 32 l_index_file_path = os.path.join(i_dir_path, 'index.html') 33 l_sub_dir_list = os.listdir(i_dir_path) 34 35 # Created a sorted list of sub-directories in this directory 36 l_dirs = sorted( 37 [d for d 38 in l_sub_dir_list 39 if os.path.isdir(os.path.join(i_dir_path, d)) 40 and d not in i_exclude_list]) 41 42 # Create a sorted list of files in this directory 43 l_files = sorted( 44 [f for f 45 in l_sub_dir_list 46 if not os.path.isdir(os.path.join(i_dir_path, f)) 47 and f not in i_exclude_list]) 48 49 # Open up the index file we're going to write to. 50 with open(l_index_file_path, 'w+') as l_index_file: 51 l_index_file.write( 52 '<html>\n' 53 '<head><title>' + i_pretty_dir_path + '</title></head>\n' 54 '<body>\n' 55 '<h2>OpenBMC Logs</h2>\n' 56 '<h3>' + i_pretty_dir_path + '</h3>\n') 57 58 # Only show the link to go up a directory if this is not the root. 59 if not i_pretty_dir_path == '/': 60 l_index_file.write('<a href=".."><img src="/dir.png"> ..</a><br>\n') 61 62 # List directories first. 63 for l_dir in l_dirs: 64 l_index_file.write( 65 '<a href="%s"><img src="/dir.png"> %s</a><br>\n' 66 % (l_dir, l_dir)) 67 create_index_file( 68 os.path.join(i_dir_path, l_dir), 69 i_pretty_dir_path + l_dir + '/', 70 i_exclude_list) 71 72 # List files second. 73 for l_file in l_files: 74 l_index_file.write('<a href="%s"><img src="/file.png"> %s</a><br>\n' 75 % (l_file, l_file)) 76 77 l_index_file.write('</body>\n</html>') 78 79 80def parse_args(i_raw_args): 81 r""" 82 Parse the given list as command-line arguments and return an object with 83 the argument values. 84 85 Description of argument(s): 86 i_raw_args A list of command-line arguments, usually taken from 87 sys.argv[1:]. 88 """ 89 90 parser = argparse.ArgumentParser( 91 description="%(prog)s will create index files that can be displayed " 92 "as web pages in a given directory and all its " 93 "sub-directories.", 94 formatter_class=argparse.ArgumentDefaultsHelpFormatter) 95 parser.add_argument( 96 'logs_dir_path', 97 help='Directory containing the logs that should be uploaded.') 98 parser.add_argument( 99 '--exclude', 100 nargs='+', 101 default=['.git', 'index.html'], 102 help='A space-delimited list of files to exclude from the index.' 103 ) 104 return parser.parse_args(i_raw_args) 105 106 107if __name__ == '__main__': 108 main(sys.argv[1:])