169560bf6SCharles Paul Hofer#!/usr/bin/env python 269560bf6SCharles Paul Hofer 369560bf6SCharles Paul Hoferr""" 469560bf6SCharles Paul HoferCreate index files that can be displayed as web pages in a given directory 569560bf6SCharles Paul Hoferand all its sub-directories. There are options to exclude certain files and 669560bf6SCharles Paul Hofersub-directories. 769560bf6SCharles Paul Hofer""" 869560bf6SCharles Paul Hofer 969560bf6SCharles Paul Hoferimport argparse 1069560bf6SCharles Paul Hoferimport os 1169560bf6SCharles Paul Hoferimport sys 1269560bf6SCharles Paul Hofer 1369560bf6SCharles Paul Hofer 1469560bf6SCharles Paul Hoferdef main(i_raw_args): 1569560bf6SCharles Paul Hofer l_args = parse_args(i_raw_args) 16*e08ffba8SPatrick Williams create_index_file(l_args.logs_dir_path, "/", l_args.exclude) 1769560bf6SCharles Paul Hofer 1869560bf6SCharles Paul Hofer 1969560bf6SCharles Paul Hoferdef create_index_file(i_dir_path, i_pretty_dir_path, i_exclude_list): 2069560bf6SCharles Paul Hofer r""" 2169560bf6SCharles Paul Hofer Create HTML index files for a given directory and all its sub-directories. 2269560bf6SCharles Paul Hofer 2369560bf6SCharles Paul Hofer Description of argument(s): 2469560bf6SCharles Paul Hofer i_dir_path The directory to generate an index file for. 2569560bf6SCharles Paul Hofer i_pretty_dir_path A pretty version of i_dir_path that can be shown to 2669560bf6SCharles Paul Hofer readers of the HTML page. For example, if i_dir_path 2769560bf6SCharles Paul Hofer is set to '/home/johndoe/logs/`, the caller may wish 2869560bf6SCharles Paul Hofer to only show '/logs/' in the HTML index pages. 2969560bf6SCharles Paul Hofer i_exclude_list A Python list of files and directories to exclude from 3069560bf6SCharles Paul Hofer """ 3169560bf6SCharles Paul Hofer 32*e08ffba8SPatrick Williams l_index_file_path = os.path.join(i_dir_path, "index.html") 3369560bf6SCharles Paul Hofer l_sub_dir_list = os.listdir(i_dir_path) 3469560bf6SCharles Paul Hofer 3569560bf6SCharles Paul Hofer # Created a sorted list of sub-directories in this directory 3669560bf6SCharles Paul Hofer l_dirs = sorted( 37*e08ffba8SPatrick Williams [ 38*e08ffba8SPatrick Williams d 39*e08ffba8SPatrick Williams for d in l_sub_dir_list 4069560bf6SCharles Paul Hofer if os.path.isdir(os.path.join(i_dir_path, d)) 41*e08ffba8SPatrick Williams and d not in i_exclude_list 42*e08ffba8SPatrick Williams ] 43*e08ffba8SPatrick Williams ) 4469560bf6SCharles Paul Hofer 4569560bf6SCharles Paul Hofer # Create a sorted list of files in this directory 4669560bf6SCharles Paul Hofer l_files = sorted( 47*e08ffba8SPatrick Williams [ 48*e08ffba8SPatrick Williams f 49*e08ffba8SPatrick Williams for f in l_sub_dir_list 5069560bf6SCharles Paul Hofer if not os.path.isdir(os.path.join(i_dir_path, f)) 51*e08ffba8SPatrick Williams and f not in i_exclude_list 52*e08ffba8SPatrick Williams ] 53*e08ffba8SPatrick Williams ) 5469560bf6SCharles Paul Hofer 5569560bf6SCharles Paul Hofer # Open up the index file we're going to write to. 56*e08ffba8SPatrick Williams with open(l_index_file_path, "w+") as l_index_file: 5769560bf6SCharles Paul Hofer l_index_file.write( 58*e08ffba8SPatrick Williams "<html>\n<head><title>" 59*e08ffba8SPatrick Williams + i_pretty_dir_path 60*e08ffba8SPatrick Williams + "</title></head>\n<body>\n<h2>OpenBMC Logs</h2>\n<h3>" 61*e08ffba8SPatrick Williams + i_pretty_dir_path 62*e08ffba8SPatrick Williams + "</h3>\n" 63*e08ffba8SPatrick Williams ) 6469560bf6SCharles Paul Hofer 6569560bf6SCharles Paul Hofer # Only show the link to go up a directory if this is not the root. 66*e08ffba8SPatrick Williams if not i_pretty_dir_path == "/": 67*e08ffba8SPatrick Williams l_index_file.write( 68*e08ffba8SPatrick Williams '<a href=".."><img src="/dir.png"> ..</a><br>\n' 69*e08ffba8SPatrick Williams ) 7069560bf6SCharles Paul Hofer 7169560bf6SCharles Paul Hofer # List directories first. 7269560bf6SCharles Paul Hofer for l_dir in l_dirs: 7369560bf6SCharles Paul Hofer l_index_file.write( 7469560bf6SCharles Paul Hofer '<a href="%s"><img src="/dir.png"> %s</a><br>\n' 75*e08ffba8SPatrick Williams % (l_dir, l_dir) 76*e08ffba8SPatrick Williams ) 7769560bf6SCharles Paul Hofer create_index_file( 7869560bf6SCharles Paul Hofer os.path.join(i_dir_path, l_dir), 79*e08ffba8SPatrick Williams i_pretty_dir_path + l_dir + "/", 80*e08ffba8SPatrick Williams i_exclude_list, 81*e08ffba8SPatrick Williams ) 8269560bf6SCharles Paul Hofer 8369560bf6SCharles Paul Hofer # List files second. 8469560bf6SCharles Paul Hofer for l_file in l_files: 85*e08ffba8SPatrick Williams l_index_file.write( 86*e08ffba8SPatrick Williams '<a href="%s"><img src="/file.png"> %s</a><br>\n' 87*e08ffba8SPatrick Williams % (l_file, l_file) 88*e08ffba8SPatrick Williams ) 8969560bf6SCharles Paul Hofer 90*e08ffba8SPatrick Williams l_index_file.write("</body>\n</html>") 9169560bf6SCharles Paul Hofer 9269560bf6SCharles Paul Hofer 9369560bf6SCharles Paul Hoferdef parse_args(i_raw_args): 9469560bf6SCharles Paul Hofer r""" 9569560bf6SCharles Paul Hofer Parse the given list as command-line arguments and return an object with 9669560bf6SCharles Paul Hofer the argument values. 9769560bf6SCharles Paul Hofer 9869560bf6SCharles Paul Hofer Description of argument(s): 9969560bf6SCharles Paul Hofer i_raw_args A list of command-line arguments, usually taken from 10069560bf6SCharles Paul Hofer sys.argv[1:]. 10169560bf6SCharles Paul Hofer """ 10269560bf6SCharles Paul Hofer 10369560bf6SCharles Paul Hofer parser = argparse.ArgumentParser( 104*e08ffba8SPatrick Williams description=( 105*e08ffba8SPatrick Williams "%(prog)s will create index files that can be displayed " 10669560bf6SCharles Paul Hofer "as web pages in a given directory and all its " 107*e08ffba8SPatrick Williams "sub-directories." 108*e08ffba8SPatrick Williams ), 109*e08ffba8SPatrick Williams formatter_class=argparse.ArgumentDefaultsHelpFormatter, 110*e08ffba8SPatrick Williams ) 11169560bf6SCharles Paul Hofer parser.add_argument( 112*e08ffba8SPatrick Williams "logs_dir_path", 113*e08ffba8SPatrick Williams help="Directory containing the logs that should be uploaded.", 114*e08ffba8SPatrick Williams ) 11569560bf6SCharles Paul Hofer parser.add_argument( 116*e08ffba8SPatrick Williams "--exclude", 117*e08ffba8SPatrick Williams nargs="+", 118*e08ffba8SPatrick Williams default=[".git", "index.html"], 119*e08ffba8SPatrick Williams help="A space-delimited list of files to exclude from the index.", 12069560bf6SCharles Paul Hofer ) 12169560bf6SCharles Paul Hofer return parser.parse_args(i_raw_args) 12269560bf6SCharles Paul Hofer 12369560bf6SCharles Paul Hofer 124*e08ffba8SPatrick Williamsif __name__ == "__main__": 12569560bf6SCharles Paul Hofer main(sys.argv[1:]) 126