forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubfolder_list.py
118 lines (86 loc) · 3.53 KB
/
subfolder_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
"""Write subfolder list to a file
This script will walk the specified directory and write the file specified with
the list of all sub-directories found. If the output file already exists, the
file will only be updated in case sub-directories have been added or removed
since the previous invocation.
"""
import os
import argparse
def parse_args():
"""Parse command line arguments and options"""
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-d', '--directory', required=True,
help='Directory to walk for sub-directory discovery')
parser.add_argument('-c', '--create-links', required=False,
help='Create links for each directory found in \
directory given')
parser.add_argument('-o', '--out-file', required=True,
help='File to write containing a list of all \
directories found')
parser.add_argument('-t', '--trigger-file', required=False,
help='Trigger file to be be touched to re-run CMake')
args = parser.parse_args()
return args
def get_subfolder_list(directory, create_links=None):
"""Return subfolder list of a directory"""
dirlist = []
if create_links is not None:
if not os.path.exists(create_links):
os.makedirs(create_links)
symbase = os.path.basename(directory)
symlink = create_links + os.path.sep + symbase
if not os.path.exists(symlink):
os.symlink(directory, symlink)
dirlist.append(symlink)
else:
dirlist.append(directory)
for root, dirs, _ in os.walk(directory, topdown=True):
dirs.sort()
for subdir in dirs:
if create_links is not None:
targetdirectory = os.path.join(root, subdir)
reldir = os.path.relpath(targetdirectory, directory)
linkname = symbase + '_' + reldir.replace(os.path.sep, '_')
symlink = create_links + os.path.sep + linkname
if not os.path.exists(symlink):
os.symlink(targetdirectory, symlink)
dirlist.append(symlink)
else:
dirlist.append(os.path.join(root, subdir))
return dirlist
def gen_out_file(out_file, dirs):
"""Generate file with the list of directories
File won't be updated if it already exists and has the same content
"""
dirs_nl = "\n".join(dirs) + "\n"
if os.path.exists(out_file):
with open(out_file, 'r', encoding="utf-8") as out_file_fo:
out_file_dirs = out_file_fo.read()
if out_file_dirs == dirs_nl:
return
with open(out_file, 'w', encoding="utf-8") as out_file_fo:
out_file_fo.writelines(dirs_nl)
def touch(trigger):
"""Touch the trigger file
If no trigger file is provided then do a return.
"""
if trigger is None:
return
if os.path.exists(trigger):
os.utime(trigger, None)
else:
with open(trigger, 'w') as trigger_fo:
trigger_fo.write("")
def main():
"""Parse command line arguments and take respective actions"""
args = parse_args()
dirs = get_subfolder_list(args.directory, args.create_links)
gen_out_file(args.out_file, dirs)
# Always touch trigger file to ensure json files are updated
touch(args.trigger_file)
if __name__ == "__main__":
main()