summaryrefslogtreecommitdiff
path: root/util/file_types.py
diff options
context:
space:
mode:
authorNathan Binkert <nate@binkert.org>2011-04-15 10:43:06 -0700
committerNathan Binkert <nate@binkert.org>2011-04-15 10:43:06 -0700
commite5ecfde222d6b76de7320750c219960e6f6ec3ca (patch)
treeabc2266d778fe470dcbabd090cc353f42c53127f /util/file_types.py
parent07815c3379d26a5d132696b41a5f1efc618cb0e6 (diff)
downloadgem5-e5ecfde222d6b76de7320750c219960e6f6ec3ca.tar.xz
util: python implementation of a routine that will sort includes
I didn't realize that the perl version existed when I started this, this version has a lot more features than the previous one since it will sort and separate python, system, and m5 headers in separate groups, it will remove duplicates, it will also convert c headers to stl headers
Diffstat (limited to 'util/file_types.py')
-rw-r--r--util/file_types.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/util/file_types.py b/util/file_types.py
index 8fc2b1af4..85e058db0 100644
--- a/util/file_types.py
+++ b/util/file_types.py
@@ -87,3 +87,85 @@ def lang_type(filename, firstline=None, openok=True):
# sorry, we couldn't detect the language
return None
+
+# directories and files to ignore by default
+default_dir_ignore = frozenset(('.hg', '.svn', 'build', 'ext'))
+default_file_ignore = frozenset(('parsetab.py', ))
+
+def find_files(base, languages=all_languages,
+ dir_ignore=default_dir_ignore,
+ file_ignore=default_file_ignore):
+ '''find all files in a directory and its subdirectories based on a
+ set of languages, ignore directories specified in dir_ignore and
+ files specified in file_ignore'''
+ if base[-1] != '/':
+ base += '/'
+
+ def update_dirs(dirs):
+ '''strip the ignored directories out of the provided list'''
+ index = len(dirs) - 1
+ for i,d in enumerate(reversed(dirs)):
+ if d in dir_ignore:
+ del dirs[index - i]
+
+ # walk over base
+ for root,dirs,files in os.walk(base):
+ root = root.replace(base, '', 1)
+
+ # strip ignored directories from the list
+ update_dirs(dirs)
+
+ for filename in files:
+ if filename in file_ignore:
+ # skip ignored files
+ continue
+
+ # try to figure out the language of the specified file
+ fullpath = os.path.join(base, root, filename)
+ language = lang_type(fullpath)
+
+ # if the file is one of the langauges that we want return
+ # its name and the language
+ if language in languages:
+ yield fullpath, language
+
+def update_file(dst, src, language, mutator):
+ '''update a file of the specified language with the provided
+ mutator generator. If inplace is provided, update the file in
+ place and return the handle to the updated file. If inplace is
+ false, write the updated file to cStringIO'''
+
+ # if the source and destination are the same, we're updating in place
+ inplace = dst == src
+
+ if isinstance(src, str):
+ # if a filename was provided, open the file
+ mode = 'r+' if inplace else 'r'
+ src = file(src, mode)
+
+ orig_lines = []
+
+ # grab all of the lines of the file and strip them of their line ending
+ old_lines = list(line.rstrip('\r\n') for line in src.xreadlines())
+ new_lines = list(mutator(old_lines, src.name, language))
+
+ for line in src.xreadlines():
+ line = line
+
+ if inplace:
+ # if we're updating in place and the file hasn't changed, do nothing
+ if old_lines == new_lines:
+ return
+
+ # otherwise, truncate the file and seek to the beginning.
+ dst = src
+ dst.truncate(0)
+ dst.seek(0)
+ elif isinstance(dst, str):
+ # if we're not updating in place and a destination file name
+ # was provided, create a file object
+ dst = file(dst, 'w')
+
+ for line in new_lines:
+ dst.write(line)
+ dst.write('\n')