blob: 2570ebe8534daf6b0fbdf761355c71fc5c440a22 [file] [log] [blame]
Torsten Rasmussenf38e3882018-06-07 15:50:31 +02001#!/usr/bin/env python3
Anas Nashif3ae52622019-04-06 09:08:09 -04002# SPDX-License-Identifier: Apache-2.0
Torsten Rasmussenf38e3882018-06-07 15:50:31 +02003
Ruslan Mstoiab206412020-07-01 16:11:08 +03004"""Write subfolder list to a file
5
6This script will walk the specified directory and write the file specified with
7the list of all sub-directories found. If the output file already exists, the
8file will only be updated in case sub-directories have been added or removed
9since the previous invocation.
10
11"""
12
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020013import os
14import argparse
15
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +020016
Ruslan Mstoiab206412020-07-01 16:11:08 +030017def parse_args():
18 """Parse command line arguments and options"""
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020019 parser = argparse.ArgumentParser(
Ruslan Mstoiab206412020-07-01 16:11:08 +030020 description=__doc__,
Jamie McCraeec704442023-01-04 16:08:36 +000021 formatter_class=argparse.RawDescriptionHelpFormatter,
22 allow_abbrev=False)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020023
24 parser.add_argument('-d', '--directory', required=True,
25 help='Directory to walk for sub-directory discovery')
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020026 parser.add_argument('-c', '--create-links', required=False,
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +020027 help='Create links for each directory found in \
28 directory given')
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020029 parser.add_argument('-o', '--out-file', required=True,
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +020030 help='File to write containing a list of all \
31 directories found')
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020032 parser.add_argument('-t', '--trigger-file', required=False,
Lingao Meng302422a2024-06-22 14:28:05 +080033 help='Trigger file to be touched to re-run CMake')
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020034
35 args = parser.parse_args()
36
Ruslan Mstoiab206412020-07-01 16:11:08 +030037 return args
38
39
40def get_subfolder_list(directory, create_links=None):
41 """Return subfolder list of a directory"""
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020042 dirlist = []
Ruslan Mstoiab206412020-07-01 16:11:08 +030043
44 if create_links is not None:
45 if not os.path.exists(create_links):
46 os.makedirs(create_links)
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +020047 symbase = os.path.basename(directory)
Ruslan Mstoiab206412020-07-01 16:11:08 +030048 symlink = create_links + os.path.sep + symbase
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020049 if not os.path.exists(symlink):
50 os.symlink(directory, symlink)
Ruslan Mstoiab206412020-07-01 16:11:08 +030051 dirlist.append(symlink)
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020052 else:
Ruslan Mstoiab206412020-07-01 16:11:08 +030053 dirlist.append(directory)
54
55 for root, dirs, _ in os.walk(directory, topdown=True):
Marc Herbertd5b28342019-02-15 18:56:57 -080056 dirs.sort()
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020057 for subdir in dirs:
Ruslan Mstoiab206412020-07-01 16:11:08 +030058 if create_links is not None:
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +020059 targetdirectory = os.path.join(root, subdir)
60 reldir = os.path.relpath(targetdirectory, directory)
61 linkname = symbase + '_' + reldir.replace(os.path.sep, '_')
Ruslan Mstoiab206412020-07-01 16:11:08 +030062 symlink = create_links + os.path.sep + linkname
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020063 if not os.path.exists(symlink):
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +020064 os.symlink(targetdirectory, symlink)
Ruslan Mstoiab206412020-07-01 16:11:08 +030065 dirlist.append(symlink)
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020066 else:
Ruslan Mstoiab206412020-07-01 16:11:08 +030067 dirlist.append(os.path.join(root, subdir))
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020068
Ruslan Mstoiab206412020-07-01 16:11:08 +030069 return dirlist
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020070
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020071
Ruslan Mstoiab206412020-07-01 16:11:08 +030072def gen_out_file(out_file, dirs):
73 """Generate file with the list of directories
74
75 File won't be updated if it already exists and has the same content
76
77 """
78 dirs_nl = "\n".join(dirs) + "\n"
79
80 if os.path.exists(out_file):
81 with open(out_file, 'r', encoding="utf-8") as out_file_fo:
82 out_file_dirs = out_file_fo.read()
83
84 if out_file_dirs == dirs_nl:
85 return
86
87 with open(out_file, 'w', encoding="utf-8") as out_file_fo:
88 out_file_fo.writelines(dirs_nl)
89
90
91def touch(trigger):
92 """Touch the trigger file
93
94 If no trigger file is provided then do a return.
95
96 """
97 if trigger is None:
98 return
99
100 if os.path.exists(trigger):
101 os.utime(trigger, None)
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200102 else:
Ruslan Mstoiab206412020-07-01 16:11:08 +0300103 with open(trigger, 'w') as trigger_fo:
104 trigger_fo.write("")
105
106
107def main():
108 """Parse command line arguments and take respective actions"""
109 args = parse_args()
110
111 dirs = get_subfolder_list(args.directory, args.create_links)
112 gen_out_file(args.out_file, dirs)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200113
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200114 # Always touch trigger file to ensure json files are updated
115 touch(args.trigger_file)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200116
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +0200117
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200118if __name__ == "__main__":
119 main()