1 # Copyright (c) 2010 Google Inc. All rights reserved. 2 # 3 # Redistribution and use in source and binary forms, with or without 4 # modification, are permitted provided that the following conditions are 5 # met: 6 # 7 # * Redistributions of source code must retain the above copyright 8 # notice, this list of conditions and the following disclaimer. 9 # * Redistributions in binary form must reproduce the above 10 # copyright notice, this list of conditions and the following disclaimer 11 # in the documentation and/or other materials provided with the 12 # distribution. 13 # * Neither the name of Google Inc. nor the names of its 14 # contributors may be used to endorse or promote products derived from 15 # this software without specific prior written permission. 16 # 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 # A home for file logic which should sit above FileSystem, but 30 # below more complicated objects. 31 32 import logging 33 import zipfile 34 35 from webkitpy.common.system.executive import ScriptError 36 37 38 _log = logging.getLogger(__name__) 39 40 41 class Workspace(object): 42 def __init__(self, filesystem, executive): 43 self._filesystem = filesystem 44 self._executive = executive # FIXME: Remove if create_zip is moved to python. 45 46 def find_unused_filename(self, directory, name, extension, search_limit=100): 47 for count in range(search_limit): 48 if count: 49 target_name = "%s-%s.%s" % (name, count, extension) 50 else: 51 target_name = "%s.%s" % (name, extension) 52 target_path = self._filesystem.join(directory, target_name) 53 if not self._filesystem.exists(target_path): 54 return target_path 55 # If we can't find an unused name in search_limit tries, just give up. 56 return None 57 58 def create_zip(self, zip_path, source_path, zip_class=zipfile.ZipFile): 59 # It's possible to create zips with Python: 60 # zip_file = ZipFile(zip_path, 'w') 61 # for root, dirs, files in os.walk(source_path): 62 # for path in files: 63 # absolute_path = os.path.join(root, path) 64 # zip_file.write(os.path.relpath(path, source_path)) 65 # However, getting the paths, encoding and compression correct could be non-trivial. 66 # So, for now we depend on the environment having "zip" installed (likely fails on Win32) 67 try: 68 self._executive.run_command(['zip', '-9', '-r', zip_path, '.'], cwd=source_path) 69 except ScriptError, e: 70 _log.error("Workspace.create_zip failed in %s:\n%s" % (source_path, e.message_with_output())) 71 return None 72 73 return zip_class(zip_path) 74