file_types.py (11403:e8949ea6961f) file_types.py (11508:46e5f3bf7f17)
1# Copyright (c) 2010 The Hewlett-Packard Development Company
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28
1import os
2
3# lanuage type for each file extension
4lang_types = {
5 '.c' : "C",
6 '.cl' : "C",
7 '.h' : "C",
8 '.cc' : "C++",
9 '.hh' : "C++",
10 '.cxx' : "C++",
11 '.hxx' : "C++",
12 '.cpp' : "C++",
13 '.hpp' : "C++",
14 '.C' : "C++",
15 '.H' : "C++",
16 '.i' : "swig",
17 '.py' : "python",
18 '.pl' : "perl",
19 '.pm' : "perl",
20 '.s' : "asm",
21 '.S' : "asm",
22 '.l' : "lex",
23 '.ll' : "lex",
24 '.y' : "yacc",
25 '.yy' : "yacc",
26 '.isa' : "isa",
27 '.sh' : "shell",
28 '.slicc' : "slicc",
29 '.sm' : "slicc",
30 '.awk' : "awk",
31 '.el' : "lisp",
32 '.txt' : "text",
33 '.tex' : "tex",
34 '.mk' : "make",
35 }
36
37# languages based on file prefix
38lang_prefixes = (
39 ('SCons', 'scons'),
40 ('Make', 'make'),
41 ('make', 'make'),
42 ('Doxyfile', 'doxygen'),
43 )
44
45# languages based on #! line of first file
46hash_bang = (
47 ('python', 'python'),
48 ('perl', 'perl'),
49 ('sh', 'shell'),
50 )
51
52# the list of all languages that we detect
53all_languages = frozenset(lang_types.itervalues())
54all_languages |= frozenset(lang for start,lang in lang_prefixes)
55all_languages |= frozenset(lang for start,lang in hash_bang)
56
57def lang_type(filename, firstline=None, openok=True):
58 '''identify the language of a given filename and potentially the
59 firstline of the file. If the firstline of the file is not
60 provided and openok is True, open the file and read the first line
61 if necessary'''
62
63 basename = os.path.basename(filename)
64 name,extension = os.path.splitext(basename)
65
66 # first try to detect language based on file extension
67 try:
68 return lang_types[extension]
69 except KeyError:
70 pass
71
72 # now try to detect language based on file prefix
73 for start,lang in lang_prefixes:
74 if basename.startswith(start):
75 return lang
76
77 # if a first line was not provided but the file is ok to open,
78 # grab the first line of the file.
79 if firstline is None and openok:
80 handle = file(filename, 'r')
81 firstline = handle.readline()
82 handle.close()
83
84 # try to detect language based on #! in first line
85 if firstline and firstline.startswith('#!'):
86 for string,lang in hash_bang:
87 if firstline.find(string) > 0:
88 return lang
89
90 # sorry, we couldn't detect the language
91 return None
92
93# directories and files to ignore by default
94default_dir_ignore = frozenset(('.hg', '.svn', 'build', 'ext'))
95default_file_ignore = frozenset(('parsetab.py', ))
96
97def find_files(base, languages=all_languages,
98 dir_ignore=default_dir_ignore,
99 file_ignore=default_file_ignore):
100 '''find all files in a directory and its subdirectories based on a
101 set of languages, ignore directories specified in dir_ignore and
102 files specified in file_ignore'''
103 if base[-1] != '/':
104 base += '/'
105
106 def update_dirs(dirs):
107 '''strip the ignored directories out of the provided list'''
108 index = len(dirs) - 1
109 for i,d in enumerate(reversed(dirs)):
110 if d in dir_ignore:
111 del dirs[index - i]
112
113 # walk over base
114 for root,dirs,files in os.walk(base):
115 root = root.replace(base, '', 1)
116
117 # strip ignored directories from the list
118 update_dirs(dirs)
119
120 for filename in files:
121 if filename in file_ignore:
122 # skip ignored files
123 continue
124
125 # try to figure out the language of the specified file
126 fullpath = os.path.join(base, root, filename)
127 language = lang_type(fullpath)
128
129 # if the file is one of the langauges that we want return
130 # its name and the language
131 if language in languages:
132 yield fullpath, language
133
134def update_file(dst, src, language, mutator):
135 '''update a file of the specified language with the provided
136 mutator generator. If inplace is provided, update the file in
137 place and return the handle to the updated file. If inplace is
138 false, write the updated file to cStringIO'''
139
140 # if the source and destination are the same, we're updating in place
141 inplace = dst == src
142
143 if isinstance(src, str):
144 # if a filename was provided, open the file
145 if inplace:
146 mode = 'r+'
147 else:
148 mode = 'r'
149 src = file(src, mode)
150
151 orig_lines = []
152
153 # grab all of the lines of the file and strip them of their line ending
154 old_lines = list(line.rstrip('\r\n') for line in src.xreadlines())
155 new_lines = list(mutator(old_lines, src.name, language))
156
157 for line in src.xreadlines():
158 line = line
159
160 if inplace:
161 # if we're updating in place and the file hasn't changed, do nothing
162 if old_lines == new_lines:
163 return
164
165 # otherwise, truncate the file and seek to the beginning.
166 dst = src
167 dst.truncate(0)
168 dst.seek(0)
169 elif isinstance(dst, str):
170 # if we're not updating in place and a destination file name
171 # was provided, create a file object
172 dst = file(dst, 'w')
173
174 for line in new_lines:
175 dst.write(line)
176 dst.write('\n')
29import os
30
31# lanuage type for each file extension
32lang_types = {
33 '.c' : "C",
34 '.cl' : "C",
35 '.h' : "C",
36 '.cc' : "C++",
37 '.hh' : "C++",
38 '.cxx' : "C++",
39 '.hxx' : "C++",
40 '.cpp' : "C++",
41 '.hpp' : "C++",
42 '.C' : "C++",
43 '.H' : "C++",
44 '.i' : "swig",
45 '.py' : "python",
46 '.pl' : "perl",
47 '.pm' : "perl",
48 '.s' : "asm",
49 '.S' : "asm",
50 '.l' : "lex",
51 '.ll' : "lex",
52 '.y' : "yacc",
53 '.yy' : "yacc",
54 '.isa' : "isa",
55 '.sh' : "shell",
56 '.slicc' : "slicc",
57 '.sm' : "slicc",
58 '.awk' : "awk",
59 '.el' : "lisp",
60 '.txt' : "text",
61 '.tex' : "tex",
62 '.mk' : "make",
63 }
64
65# languages based on file prefix
66lang_prefixes = (
67 ('SCons', 'scons'),
68 ('Make', 'make'),
69 ('make', 'make'),
70 ('Doxyfile', 'doxygen'),
71 )
72
73# languages based on #! line of first file
74hash_bang = (
75 ('python', 'python'),
76 ('perl', 'perl'),
77 ('sh', 'shell'),
78 )
79
80# the list of all languages that we detect
81all_languages = frozenset(lang_types.itervalues())
82all_languages |= frozenset(lang for start,lang in lang_prefixes)
83all_languages |= frozenset(lang for start,lang in hash_bang)
84
85def lang_type(filename, firstline=None, openok=True):
86 '''identify the language of a given filename and potentially the
87 firstline of the file. If the firstline of the file is not
88 provided and openok is True, open the file and read the first line
89 if necessary'''
90
91 basename = os.path.basename(filename)
92 name,extension = os.path.splitext(basename)
93
94 # first try to detect language based on file extension
95 try:
96 return lang_types[extension]
97 except KeyError:
98 pass
99
100 # now try to detect language based on file prefix
101 for start,lang in lang_prefixes:
102 if basename.startswith(start):
103 return lang
104
105 # if a first line was not provided but the file is ok to open,
106 # grab the first line of the file.
107 if firstline is None and openok:
108 handle = file(filename, 'r')
109 firstline = handle.readline()
110 handle.close()
111
112 # try to detect language based on #! in first line
113 if firstline and firstline.startswith('#!'):
114 for string,lang in hash_bang:
115 if firstline.find(string) > 0:
116 return lang
117
118 # sorry, we couldn't detect the language
119 return None
120
121# directories and files to ignore by default
122default_dir_ignore = frozenset(('.hg', '.svn', 'build', 'ext'))
123default_file_ignore = frozenset(('parsetab.py', ))
124
125def find_files(base, languages=all_languages,
126 dir_ignore=default_dir_ignore,
127 file_ignore=default_file_ignore):
128 '''find all files in a directory and its subdirectories based on a
129 set of languages, ignore directories specified in dir_ignore and
130 files specified in file_ignore'''
131 if base[-1] != '/':
132 base += '/'
133
134 def update_dirs(dirs):
135 '''strip the ignored directories out of the provided list'''
136 index = len(dirs) - 1
137 for i,d in enumerate(reversed(dirs)):
138 if d in dir_ignore:
139 del dirs[index - i]
140
141 # walk over base
142 for root,dirs,files in os.walk(base):
143 root = root.replace(base, '', 1)
144
145 # strip ignored directories from the list
146 update_dirs(dirs)
147
148 for filename in files:
149 if filename in file_ignore:
150 # skip ignored files
151 continue
152
153 # try to figure out the language of the specified file
154 fullpath = os.path.join(base, root, filename)
155 language = lang_type(fullpath)
156
157 # if the file is one of the langauges that we want return
158 # its name and the language
159 if language in languages:
160 yield fullpath, language
161
162def update_file(dst, src, language, mutator):
163 '''update a file of the specified language with the provided
164 mutator generator. If inplace is provided, update the file in
165 place and return the handle to the updated file. If inplace is
166 false, write the updated file to cStringIO'''
167
168 # if the source and destination are the same, we're updating in place
169 inplace = dst == src
170
171 if isinstance(src, str):
172 # if a filename was provided, open the file
173 if inplace:
174 mode = 'r+'
175 else:
176 mode = 'r'
177 src = file(src, mode)
178
179 orig_lines = []
180
181 # grab all of the lines of the file and strip them of their line ending
182 old_lines = list(line.rstrip('\r\n') for line in src.xreadlines())
183 new_lines = list(mutator(old_lines, src.name, language))
184
185 for line in src.xreadlines():
186 line = line
187
188 if inplace:
189 # if we're updating in place and the file hasn't changed, do nothing
190 if old_lines == new_lines:
191 return
192
193 # otherwise, truncate the file and seek to the beginning.
194 dst = src
195 dst.truncate(0)
196 dst.seek(0)
197 elif isinstance(dst, str):
198 # if we're not updating in place and a destination file name
199 # was provided, create a file object
200 dst = file(dst, 'w')
201
202 for line in new_lines:
203 dst.write(line)
204 dst.write('\n')