blob: 7c71c681308cd256823c7a8fc68e5cfd64b62a3c [file] [log] [blame]
Torsten Rasmussenf38e3882018-06-07 15:50:31 +02001#!/usr/bin/env python3
2
3import os
4import argparse
5
6def touch(trigger):
7 # If no trigger file is provided then do a return.
8 if(trigger is None):
9 return
10
11 if os.path.exists(trigger):
12 os.utime(trigger, None)
13 else:
14 with open(trigger, 'w') as fp:
15 fp.write("")
16
17
18def main():
19 parser = argparse.ArgumentParser(
20 description='This script will walk the specified directory and write the file specified \
21 with the list of all sub-directories found. If to the output file already \
22 exists, the file will only be updated in case sub-directories has been added \
23 or removed since previous invocation.')
24
25 parser.add_argument('-d', '--directory', required=True,
26 help='Directory to walk for sub-directory discovery')
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020027 parser.add_argument('-c', '--create-links', required=False,
28 help='Create links for each directory found in directory given')
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020029 parser.add_argument('-o', '--out-file', required=True,
30 help='File to write containing a list of all directories found')
31 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
36 dirlist = []
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020037 if(args.create_links is not None):
38 if not os.path.exists(args.create_links):
39 os.makedirs(args.create_links)
40 directory = args.directory
41 symlink = args.create_links + os.path.sep + directory.replace(os.path.sep, '_')
42 if not os.path.exists(symlink):
43 os.symlink(directory, symlink)
44 dirlist.extend(symlink)
45 else:
46 dirlist.extend(args.directory)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020047 dirlist.extend(os.linesep)
48 for root, dirs, files in os.walk(args.directory):
49 for subdir in dirs:
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020050 if(args.create_links is not None):
51 directory = os.path.join(root, subdir)
52 symlink = args.create_links + os.path.sep + directory.replace(os.path.sep, '_')
53 if not os.path.exists(symlink):
54 os.symlink(directory, symlink)
55 dirlist.extend(symlink)
56 else:
57 dirlist.extend(os.path.join(root, subdir))
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020058 dirlist.extend(os.linesep)
59
60 new = ''.join(dirlist)
61 existing = ''
62
63 if os.path.exists(args.out_file):
64 with open(args.out_file, 'r') as fp:
65 existing = fp.read()
66
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020067 if new != existing:
68 with open(args.out_file, 'w') as fp:
69 fp.write(new)
70 else:
71 with open(args.out_file, 'w') as fp:
72 fp.write(new)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020073
Torsten Rasmussen080e32e2018-06-14 22:27:17 +020074 # Always touch trigger file to ensure json files are updated
75 touch(args.trigger_file)
Torsten Rasmussenf38e3882018-06-07 15:50:31 +020076
77if __name__ == "__main__":
78 main()