2018-06-07 21:50:31 +08:00
|
|
|
#!/usr/bin/env python3
|
2019-04-06 21:08:09 +08:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2018-06-07 21:50:31 +08:00
|
|
|
|
|
|
|
import os
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
def touch(trigger):
|
|
|
|
# If no trigger file is provided then do a return.
|
2019-03-21 02:30:29 +08:00
|
|
|
if trigger is None:
|
2018-06-07 21:50:31 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
if os.path.exists(trigger):
|
|
|
|
os.utime(trigger, None)
|
|
|
|
else:
|
|
|
|
with open(trigger, 'w') as fp:
|
|
|
|
fp.write("")
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='This script will walk the specified directory and write the file specified \
|
|
|
|
with the list of all sub-directories found. If to the output file already \
|
|
|
|
exists, the file will only be updated in case sub-directories has been added \
|
|
|
|
or removed since previous invocation.')
|
|
|
|
|
|
|
|
parser.add_argument('-d', '--directory', required=True,
|
|
|
|
help='Directory to walk for sub-directory discovery')
|
2018-06-15 04:27:17 +08:00
|
|
|
parser.add_argument('-c', '--create-links', required=False,
|
|
|
|
help='Create links for each directory found in directory given')
|
2018-06-07 21:50:31 +08:00
|
|
|
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()
|
|
|
|
|
|
|
|
dirlist = []
|
2019-03-21 02:30:29 +08:00
|
|
|
if args.create_links is not None:
|
2018-06-15 04:27:17 +08:00
|
|
|
if not os.path.exists(args.create_links):
|
|
|
|
os.makedirs(args.create_links)
|
|
|
|
directory = args.directory
|
|
|
|
symlink = args.create_links + os.path.sep + directory.replace(os.path.sep, '_')
|
|
|
|
if not os.path.exists(symlink):
|
|
|
|
os.symlink(directory, symlink)
|
|
|
|
dirlist.extend(symlink)
|
|
|
|
else:
|
|
|
|
dirlist.extend(args.directory)
|
2018-06-07 21:50:31 +08:00
|
|
|
dirlist.extend(os.linesep)
|
2019-02-16 10:56:57 +08:00
|
|
|
for root, dirs, _ in os.walk(args.directory, topdown=True):
|
|
|
|
dirs.sort()
|
2018-06-07 21:50:31 +08:00
|
|
|
for subdir in dirs:
|
2019-03-21 02:30:29 +08:00
|
|
|
if args.create_links is not None:
|
2018-06-15 04:27:17 +08:00
|
|
|
directory = os.path.join(root, subdir)
|
|
|
|
symlink = args.create_links + os.path.sep + directory.replace(os.path.sep, '_')
|
|
|
|
if not os.path.exists(symlink):
|
|
|
|
os.symlink(directory, symlink)
|
|
|
|
dirlist.extend(symlink)
|
|
|
|
else:
|
|
|
|
dirlist.extend(os.path.join(root, subdir))
|
2018-06-07 21:50:31 +08:00
|
|
|
dirlist.extend(os.linesep)
|
|
|
|
|
|
|
|
new = ''.join(dirlist)
|
|
|
|
existing = ''
|
|
|
|
|
|
|
|
if os.path.exists(args.out_file):
|
|
|
|
with open(args.out_file, 'r') as fp:
|
|
|
|
existing = fp.read()
|
|
|
|
|
2018-06-15 04:27:17 +08:00
|
|
|
if new != existing:
|
|
|
|
with open(args.out_file, 'w') as fp:
|
|
|
|
fp.write(new)
|
|
|
|
else:
|
|
|
|
with open(args.out_file, 'w') as fp:
|
|
|
|
fp.write(new)
|
2018-06-07 21:50:31 +08:00
|
|
|
|
2018-06-15 04:27:17 +08:00
|
|
|
# Always touch trigger file to ensure json files are updated
|
|
|
|
touch(args.trigger_file)
|
2018-06-07 21:50:31 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|