Torsten Rasmussen | f38e388 | 2018-06-07 15:50:31 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import os |
| 4 | import argparse |
| 5 | |
| 6 | def 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 | |
| 18 | def 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 Rasmussen | 080e32e | 2018-06-14 22:27:17 +0200 | [diff] [blame] | 27 | parser.add_argument('-c', '--create-links', required=False, |
| 28 | help='Create links for each directory found in directory given') |
Torsten Rasmussen | f38e388 | 2018-06-07 15:50:31 +0200 | [diff] [blame] | 29 | 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 Rasmussen | 080e32e | 2018-06-14 22:27:17 +0200 | [diff] [blame] | 37 | 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 Rasmussen | f38e388 | 2018-06-07 15:50:31 +0200 | [diff] [blame] | 47 | dirlist.extend(os.linesep) |
| 48 | for root, dirs, files in os.walk(args.directory): |
| 49 | for subdir in dirs: |
Torsten Rasmussen | 080e32e | 2018-06-14 22:27:17 +0200 | [diff] [blame] | 50 | 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 Rasmussen | f38e388 | 2018-06-07 15:50:31 +0200 | [diff] [blame] | 58 | 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 Rasmussen | 080e32e | 2018-06-14 22:27:17 +0200 | [diff] [blame] | 67 | 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 Rasmussen | f38e388 | 2018-06-07 15:50:31 +0200 | [diff] [blame] | 73 | |
Torsten Rasmussen | 080e32e | 2018-06-14 22:27:17 +0200 | [diff] [blame] | 74 | # Always touch trigger file to ensure json files are updated |
| 75 | touch(args.trigger_file) |
Torsten Rasmussen | f38e388 | 2018-06-07 15:50:31 +0200 | [diff] [blame] | 76 | |
| 77 | if __name__ == "__main__": |
| 78 | main() |