""" Simple script to help you keep your WingIDE project clean. To install, place in your "scripts" directory. The script adds cleaning items to the project view's context menu (right click). Version 0.2 Copyright (c) 2005, Ken Kinder All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ----------------------------- With minor modifications by Wingware """ import wingapi, glob, thread, os # XXX Could change this to use the file sets preference instead STOP_LIST = [ # Various backup files '*~', '*.bak', '*.tmp', '*.temp', '*temp', '*-old', '*.old', # Compiled Files '*.pyc', '*.pyo', '*.pyd', '*.pdb', '*.ilk', '*.dll', '*.lib', '*.a', '*.exe', '*.so', '*.o', '*.obj', '*.ctpl', # Version Control '*.svn', 'CVS', '#*#', '.#*', # Project Files '*.wpr', '*.wpu', '*.e3p', # Various Binary Files '*.tar.gz', '*.tgz', # Just don't care about... 'core', '*xvpics', '.Trashes', # Logs '*.log', 'log', ] # How often the background process should be invoked IDLE_TIMEOUT = 1000 class ProjectUpkeep: def __init__(self): self.app = wingapi.gApplication self._dir_walk_gen = None def start(self, update_path): print 'Project Upkeep: Start' self.app.SetStatusMessage('Project Upkeep: Start') self._dir_walk_gen = os.walk(update_path) self.added = 0 self.removed = 0 self.app.InstallTimeout(IDLE_TIMEOUT, self._idle) def stop(self): print 'Project Upkeep: Done' self.app.SetStatusMessage('Project Upkeep: Done') self._dir_walk_gen = None def _idle(self): if self._dir_walk_gen: try: root, dirs, files = self._dir_walk_gen.next() except StopIteration: self.stop() return print 'Project Upkeep: Scanning %s' % root exclude_list = [] # Remove directories we don't want to walk. for d in dirs: for stop_pattern in STOP_LIST: if glob.fnmatch.fnmatch(d, stop_pattern): if d in dirs: dirs.remove(d) # Move files matching the STOP_LIST patterns into exclude_list for stop_pattern in STOP_LIST: for exclude_file in glob.fnmatch.filter(files, stop_pattern): if exclude_file in files: files.remove(exclude_file) if exclude_file not in exclude_list: exclude_list.append(exclude_file) # Now files has a list of files we want to keep in the project, while # exclude_list has ones we do not want in the project. files_in_project = self.app.GetProject().GetAllFiles() add = [] remove = [] # # Build full-path add/remove lists for file in files: full_name = os.path.join(root, file) if full_name not in files_in_project: print 'Project Upkeep: Adding %s' % full_name self.added += 1 add.append(full_name) for file in exclude_list: full_name = os.path.join(root, file) if full_name in files_in_project: print 'Project Upkeep: Removing %s' % full_name self.removed += 1 remove.append(full_name) self.app.GetProject().AddFiles(add) self.app.GetProject().RemoveFiles(remove) self.app.InstallTimeout(IDLE_TIMEOUT, self._idle) self.app.SetStatusMessage('Project Upkeep: %s Added / %s Removed' % (self.added, self.removed)) _walker = ProjectUpkeep() def scan_for_new_files(update_path=wingapi.kArgFilename): update_path = update_path[0] if not os.path.isdir(update_path): update_path = os.path.dirname(update_path) _walker.start(update_path) def _scan_for_new_files_available(update_path=wingapi.kArgFilename): return _walker._dir_walk_gen is None and len(update_path) > 0 scan_for_new_files.contexts = [ wingapi.kContextProject(10), ] scan_for_new_files.available = _scan_for_new_files_available def stop_scanning(): _walker.stop() def _stop_scanning_available(): return _walker._dir_walk_gen is not None def remove_deleted_files(): # Start out by removing deleted files for file in wingapi.gApplication.GetProject().GetAllFiles(): if not os.path.exists(file): print 'Removing %s' % file wingapi.gApplication.GetProject().RemoveFiles([file]) stop_scanning.contexts = [ wingapi.kContextProject(10), ] remove_deleted_files.contexts = [ wingapi.kContextProject(10), ] stop_scanning.available = _stop_scanning_available