blob: 8f5288928ad13ebdbb87b49aa1c4216fa8dbd994 [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__,
21 formatter_class=argparse.RawDescriptionHelpFormatter)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020022
23 parser.add_argument('-d', '--directory', required=True,
24 help='Directory to walk for sub-directory discovery')
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020025 parser.add_argument('-c', '--create-links', required=False,
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +020026 help='Create links for each directory found in \
27 directory given')
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020028 parser.add_argument('-o', '--out-file', required=True,
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +020029 help='File to write containing a list of all \
30 directories found')
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020031 parser.add_argument('-t', '--trigger-file', required=False,
32 help='Trigger file to be be touched to re-run CMake')
33
34 args = parser.parse_args()
35
Ruslan Mstoiab206412020-07-01 16:11:08 +030036 return args
37
38
39def get_subfolder_list(directory, create_links=None):
40 """Return subfolder list of a directory"""
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020041 dirlist = []
Ruslan Mstoiab206412020-07-01 16:11:08 +030042
43 if create_links is not None:
44 if not os.path.exists(create_links):
45 os.makedirs(create_links)
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +020046 symbase = os.path.basename(directory)
Ruslan Mstoiab206412020-07-01 16:11:08 +030047 symlink = create_links + os.path.sep + symbase
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020048 if not os.path.exists(symlink):
49 os.symlink(directory, symlink)
Ruslan Mstoiab206412020-07-01 16:11:08 +030050 dirlist.append(symlink)
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020051 else:
Ruslan Mstoiab206412020-07-01 16:11:08 +030052 dirlist.append(directory)
53
54 for root, dirs, _ in os.walk(directory, topdown=True):
Marc Herbertd5b28342019-02-15 18:56:57 -080055 dirs.sort()
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020056 for subdir in dirs:
Ruslan Mstoiab206412020-07-01 16:11:08 +030057 if create_links is not None:
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +020058 targetdirectory = os.path.join(root, subdir)
59 reldir = os.path.relpath(targetdirectory, directory)
60 linkname = symbase + '_' + reldir.replace(os.path.sep, '_')
Ruslan Mstoiab206412020-07-01 16:11:08 +030061 symlink = create_links + os.path.sep + linkname
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020062 if not os.path.exists(symlink):
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +020063 os.symlink(targetdirectory, symlink)
Ruslan Mstoiab206412020-07-01 16:11:08 +030064 dirlist.append(symlink)
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020065 else:
Ruslan Mstoiab206412020-07-01 16:11:08 +030066 dirlist.append(os.path.join(root, subdir))
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020067
Ruslan Mstoiab206412020-07-01 16:11:08 +030068 return dirlist
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020069
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020070
Ruslan Mstoiab206412020-07-01 16:11:08 +030071def gen_out_file(out_file, dirs):
72 """Generate file with the list of directories
73
74 File won't be updated if it already exists and has the same content
75
76 """
77 dirs_nl = "\n".join(dirs) + "\n"
78
79 if os.path.exists(out_file):
80 with open(out_file, 'r', encoding="utf-8") as out_file_fo:
81 out_file_dirs = out_file_fo.read()
82
83 if out_file_dirs == dirs_nl:
84 return
85
86 with open(out_file, 'w', encoding="utf-8") as out_file_fo:
87 out_file_fo.writelines(dirs_nl)
88
89
90def touch(trigger):
91 """Touch the trigger file
92
93 If no trigger file is provided then do a return.
94
95 """
96 if trigger is None:
97 return
98
99 if os.path.exists(trigger):
100 os.utime(trigger, None)
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200101 else:
Ruslan Mstoiab206412020-07-01 16:11:08 +0300102 with open(trigger, 'w') as trigger_fo:
103 trigger_fo.write("")
104
105
106def main():
107 """Parse command line arguments and take respective actions"""
108 args = parse_args()
109
110 dirs = get_subfolder_list(args.directory, args.create_links)
111 gen_out_file(args.out_file, dirs)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200112
Torsten Rasmussen080e32e2018-06-14 22:27:17 +0200113 # Always touch trigger file to ensure json files are updated
114 touch(args.trigger_file)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200115
Torsten Rasmussen2cf53b62020-04-22 08:06:07 +0200116
Torsten Rasmussenf38e3882018-06-07 15:50:31 +0200117if __name__ == "__main__":
118 main()