Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | Create index files that can be displayed as web pages in a given directory |
| 5 | and all its sub-directories. There are options to exclude certain files and |
| 6 | sub-directories. |
| 7 | """ |
| 8 | |
| 9 | import argparse |
| 10 | import os |
| 11 | import sys |
| 12 | |
| 13 | |
| 14 | def main(i_raw_args): |
| 15 | l_args = parse_args(i_raw_args) |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 16 | create_index_file(l_args.logs_dir_path, "/", l_args.exclude) |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 17 | |
| 18 | |
| 19 | def 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 | |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 32 | l_index_file_path = os.path.join(i_dir_path, "index.html") |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 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( |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 37 | [ |
| 38 | d |
| 39 | for d in l_sub_dir_list |
| 40 | if os.path.isdir(os.path.join(i_dir_path, d)) |
| 41 | and d not in i_exclude_list |
| 42 | ] |
| 43 | ) |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 44 | |
| 45 | # Create a sorted list of files in this directory |
| 46 | l_files = sorted( |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 47 | [ |
| 48 | f |
| 49 | for f in l_sub_dir_list |
| 50 | if not os.path.isdir(os.path.join(i_dir_path, f)) |
| 51 | and f not in i_exclude_list |
| 52 | ] |
| 53 | ) |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 54 | |
| 55 | # Open up the index file we're going to write to. |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 56 | with open(l_index_file_path, "w+") as l_index_file: |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 57 | l_index_file.write( |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 58 | "<html>\n<head><title>" |
| 59 | + i_pretty_dir_path |
| 60 | + "</title></head>\n<body>\n<h2>OpenBMC Logs</h2>\n<h3>" |
| 61 | + i_pretty_dir_path |
| 62 | + "</h3>\n" |
| 63 | ) |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 64 | |
| 65 | # Only show the link to go up a directory if this is not the root. |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 66 | if not i_pretty_dir_path == "/": |
| 67 | l_index_file.write( |
| 68 | '<a href=".."><img src="/dir.png"> ..</a><br>\n' |
| 69 | ) |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 70 | |
| 71 | # List directories first. |
| 72 | for l_dir in l_dirs: |
| 73 | l_index_file.write( |
| 74 | '<a href="%s"><img src="/dir.png"> %s</a><br>\n' |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 75 | % (l_dir, l_dir) |
| 76 | ) |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 77 | create_index_file( |
| 78 | os.path.join(i_dir_path, l_dir), |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 79 | i_pretty_dir_path + l_dir + "/", |
| 80 | i_exclude_list, |
| 81 | ) |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 82 | |
| 83 | # List files second. |
| 84 | for l_file in l_files: |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 85 | l_index_file.write( |
| 86 | '<a href="%s"><img src="/file.png"> %s</a><br>\n' |
| 87 | % (l_file, l_file) |
| 88 | ) |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 89 | |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 90 | l_index_file.write("</body>\n</html>") |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 91 | |
| 92 | |
| 93 | def parse_args(i_raw_args): |
| 94 | r""" |
| 95 | Parse the given list as command-line arguments and return an object with |
| 96 | the argument values. |
| 97 | |
| 98 | Description of argument(s): |
| 99 | i_raw_args A list of command-line arguments, usually taken from |
| 100 | sys.argv[1:]. |
| 101 | """ |
| 102 | |
| 103 | parser = argparse.ArgumentParser( |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 104 | description=( |
| 105 | "%(prog)s will create index files that can be displayed " |
| 106 | "as web pages in a given directory and all its " |
| 107 | "sub-directories." |
| 108 | ), |
| 109 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 110 | ) |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 111 | parser.add_argument( |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 112 | "logs_dir_path", |
| 113 | help="Directory containing the logs that should be uploaded.", |
| 114 | ) |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 115 | parser.add_argument( |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 116 | "--exclude", |
| 117 | nargs="+", |
| 118 | default=[".git", "index.html"], |
| 119 | help="A space-delimited list of files to exclude from the index.", |
Charles Paul Hofer | 69560bf | 2018-08-21 14:04:35 -0500 | [diff] [blame] | 120 | ) |
| 121 | return parser.parse_args(i_raw_args) |
| 122 | |
| 123 | |
Patrick Williams | e08ffba | 2022-12-05 10:33:46 -0600 | [diff] [blame^] | 124 | if __name__ == "__main__": |
| 125 | main(sys.argv[1:]) |