3 # lanuage type for each file extension
37 # languages based on file prefix
42 ('Doxyfile', 'doxygen'),
45 # languages based on #! line of first file
52 # the list of all languages that we detect
53 all_languages
= frozenset(lang_types
.itervalues())
54 all_languages |
= frozenset(lang
for start
,lang
in lang_prefixes
)
55 all_languages |
= frozenset(lang
for start
,lang
in hash_bang
)
57 def 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
63 basename
= os
.path
.basename(filename
)
64 name
,extension
= os
.path
.splitext(basename
)
66 # first try to detect language based on file extension
68 return lang_types
[extension
]
72 # now try to detect language based on file prefix
73 for start
,lang
in lang_prefixes
:
74 if basename
.startswith(start
):
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()
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:
90 # sorry, we couldn't detect the language
93 # directories and files to ignore by default
94 default_dir_ignore
= frozenset(('.hg', '.svn', 'build', 'ext'))
95 default_file_ignore
= frozenset(('parsetab.py', ))
97 def 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'''
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
)):
114 for root
,dirs
,files
in os
.walk(base
):
115 root
= root
.replace(base
, '', 1)
117 # strip ignored directories from the list
120 for filename
in files
:
121 if filename
in file_ignore
:
125 # try to figure out the language of the specified file
126 fullpath
= os
.path
.join(base
, root
, filename
)
127 language
= lang_type(fullpath
)
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
134 def 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'''
140 # if the source and destination are the same, we're updating in place
143 if isinstance(src
, str):
144 # if a filename was provided, open the file
149 src
= file(src
, mode
)
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
))
157 for line
in src
.xreadlines():
161 # if we're updating in place and the file hasn't changed, do nothing
162 if old_lines
== new_lines
:
165 # otherwise, truncate the file and seek to the beginning.
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
174 for line
in new_lines
: