blob: e4ea8c333cafff986434aa88c4cef0b619685d68 [file] [log] [blame]
Charles Paul Hofer69560bf2018-08-21 14:04:35 -05001#!/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)
Patrick Williamse08ffba2022-12-05 10:33:46 -060016 create_index_file(l_args.logs_dir_path, "/", l_args.exclude)
Charles Paul Hofer69560bf2018-08-21 14:04:35 -050017
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
Patrick Williamse08ffba2022-12-05 10:33:46 -060032 l_index_file_path = os.path.join(i_dir_path, "index.html")
Charles Paul Hofer69560bf2018-08-21 14:04:35 -050033 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 Williamse08ffba2022-12-05 10:33:46 -060037 [
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 Hofer69560bf2018-08-21 14:04:35 -050044
45 # Create a sorted list of files in this directory
46 l_files = sorted(
Patrick Williamse08ffba2022-12-05 10:33:46 -060047 [
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 Hofer69560bf2018-08-21 14:04:35 -050054
55 # Open up the index file we're going to write to.
Patrick Williamse08ffba2022-12-05 10:33:46 -060056 with open(l_index_file_path, "w+") as l_index_file:
Charles Paul Hofer69560bf2018-08-21 14:04:35 -050057 l_index_file.write(
Patrick Williamse08ffba2022-12-05 10:33:46 -060058 "<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 Hofer69560bf2018-08-21 14:04:35 -050064
65 # Only show the link to go up a directory if this is not the root.
Patrick Williamse08ffba2022-12-05 10:33:46 -060066 if not i_pretty_dir_path == "/":
67 l_index_file.write(
68 '<a href=".."><img src="/dir.png"> ..</a><br>\n'
69 )
Charles Paul Hofer69560bf2018-08-21 14:04:35 -050070
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 Williamse08ffba2022-12-05 10:33:46 -060075 % (l_dir, l_dir)
76 )
Charles Paul Hofer69560bf2018-08-21 14:04:35 -050077 create_index_file(
78 os.path.join(i_dir_path, l_dir),
Patrick Williamse08ffba2022-12-05 10:33:46 -060079 i_pretty_dir_path + l_dir + "/",
80 i_exclude_list,
81 )
Charles Paul Hofer69560bf2018-08-21 14:04:35 -050082
83 # List files second.
84 for l_file in l_files:
Patrick Williamse08ffba2022-12-05 10:33:46 -060085 l_index_file.write(
86 '<a href="%s"><img src="/file.png"> %s</a><br>\n'
87 % (l_file, l_file)
88 )
Charles Paul Hofer69560bf2018-08-21 14:04:35 -050089
Patrick Williamse08ffba2022-12-05 10:33:46 -060090 l_index_file.write("</body>\n</html>")
Charles Paul Hofer69560bf2018-08-21 14:04:35 -050091
92
93def 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 Williamse08ffba2022-12-05 10:33:46 -0600104 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 Hofer69560bf2018-08-21 14:04:35 -0500111 parser.add_argument(
Patrick Williamse08ffba2022-12-05 10:33:46 -0600112 "logs_dir_path",
113 help="Directory containing the logs that should be uploaded.",
114 )
Charles Paul Hofer69560bf2018-08-21 14:04:35 -0500115 parser.add_argument(
Patrick Williamse08ffba2022-12-05 10:33:46 -0600116 "--exclude",
117 nargs="+",
118 default=[".git", "index.html"],
119 help="A space-delimited list of files to exclude from the index.",
Charles Paul Hofer69560bf2018-08-21 14:04:35 -0500120 )
121 return parser.parse_args(i_raw_args)
122
123
Patrick Williamse08ffba2022-12-05 10:33:46 -0600124if __name__ == "__main__":
125 main(sys.argv[1:])