Statistics
| Branch: | Revision:

root / scout / libscout / generate_behavior_lists.py @ 6d0836d4

History | View | Annotate | Download (4.04 KB)

1
#!/usr/bin/python
2

    
3
# Usage:
4
#
5
# python generate_behavior_lists.py
6
#
7
# Parses to use only the final folder and file name for each source file.
8

    
9
import sys
10
import os
11

    
12
AUTOGEN_HEADER = '''/**
13
 ******************************************************************************
14
 * WARNING: THIS IS AN AUTOGENERATED FILE!
15
 *
16
 * Editing this file is USELESS. It will be overwritten during the build.
17
 * To properly edit this file, edit its corresponding *.template.* file, which
18
 * a script run by CMake uses to generate this file.
19
 *
20
 * This file was generated via rules in src/generate_behavior_lists.py.
21
 *
22
 * Please see that file for a description of syntax and procedure for
23
 * creating auto-generated files.
24
 *
25
 * THIS WARNING IS LONG TO MAKE SURE IT GETS YOUR ATTENTION!
26
 * THANK YOU.
27
 *****************************************************************************
28
 */
29

30
'''
31

    
32
# These are the codes this script will replace in template files.
33
FULL_PATH_CODE = 'AUTOGEN_BEHAVIOR_FULL_PATH'
34
LAST_PATH_CODE = 'AUTOGEN_BEHAVIOR_LAST_PATH'
35
BASE_NAME_CODE = 'AUTOGEN_BEHAVIOR_BASE_NAME'
36

    
37
# Different ways to refer to files in behaviors/ and test_behaviors/.
38
full_paths = []
39
last_paths = []
40
base_names = []
41

    
42
# Mapping of generated files to their templates
43
R_FILES = {'src/BehaviorList.cpp' : 'src/BehaviorList.template.cpp',
44
           'src/BehaviorList.h'   : 'src/BehaviorList.template.h'}
45

    
46
# Parse the src/behaviors and src/test_behaviors folders
47
cur_dir = os.getcwd()
48

    
49
all_paths = []
50
all_fnames = []
51
all_lasts = []
52

    
53
# Extract all the header files for behaviors we want
54
for path, dirname, fnames in os.walk('src'):
55
    if path == 'src/behaviors' or path == 'src/test_behaviors':
56
        path_parts = path.split('/')
57
        for f in fnames:
58
            if f.endswith('.h'):
59
                all_paths.append(path)
60
                all_fnames.append(f)
61
                all_lasts.append(path_parts[-1])
62

    
63
# Sort these headers into a good order and add them to our lists of paths/names
64
all_fnames, all_paths, all_lasts = (list(t) for t in zip(*sorted(zip(all_fnames, all_paths, all_lasts), key=lambda x: x[0].lower())))
65

    
66
for f, path, last in zip(all_fnames, all_paths, all_lasts):
67
    # Pause goes first
68
    if 'pause_scout' in f:
69
        full_paths.insert(0, os.path.join(cur_dir, path, f))
70
        last_paths.insert(0, os.path.join(last, f))
71
        base_names.insert(0, f.split('.')[0])
72
    # Everything else goes in alphabetical order
73
    else:
74
        full_paths.append(os.path.join(cur_dir, path, f))
75
        last_paths.append(os.path.join(last, f))
76
        base_names.append(f.split('.')[0])
77

    
78
# Now, a dictionary of codes to replace -> things to replace them with.
79
replacements = {FULL_PATH_CODE : full_paths,
80
                LAST_PATH_CODE : last_paths,
81
                BASE_NAME_CODE : base_names}
82

    
83
# also simultaneously write a txt file for the GUI to read from
84
with open("../scoutsim/behaviorList.txt", "w") as listFile:
85
    # Write all the replacements in R_FILES
86
    for outfile_name in sorted(R_FILES.keys()):
87
        with open(outfile_name, 'w') as outfile:
88
            with open(R_FILES[outfile_name], 'r') as infile:
89
                
90
                # Write an obvious header so no one will edit our nicely
91
                # generated file
92
                outfile.write(AUTOGEN_HEADER)
93

    
94
                for line in infile.readlines():
95
                    # If the line needs replacing, don't write the orgininal
96
                    # to the output file; rather, write many versions of the
97
                    # line replaced with each replacement value.
98
                    replaced = False
99
                    for r_key in replacements:
100
                        if r_key in line:
101
                            replaced = True
102
                            for b in replacements[r_key]:
103
                                outfile.write(line.replace(r_key, b))
104
                                if (r_key == BASE_NAME_CODE):
105
                                    listFile.write(b+"\n")
106
                    # If it doesn't need replacing, just write the original
107
                    if not replaced:
108
                        outfile.write(line)
109

    
110