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
5# are met:
6#
7# 1.  Redistributions of source code must retain the above copyright
8#     notice, this list of conditions and the following disclaimer.
9# 2.  Redistributions in binary form must reproduce the above copyright
10#     notice, this list of conditions and the following disclaimer in the
11#     documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
14# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24from webkitpy.common.system.fileset import FileSetFileHandle
25from webkitpy.common.system.filesystem import FileSystem
26
27
28class DirectoryFileSet(object):
29    """The set of files under a local directory."""
30    def __init__(self, path, filesystem=None):
31        self._path = path
32        self._filesystem = filesystem or FileSystem()
33        if not self._path.endswith(self._filesystem.sep):
34            self._path += self._filesystem.sep
35
36    def _full_path(self, filename):
37        assert self._is_under(self._path, filename)
38        return self._filesystem.join(self._path, filename)
39
40    def _drop_directory_prefix(self, path):
41        return path[len(self._path):]
42
43    def _files_in_directory(self):
44        """Returns a list of all the files in the directory, including the path
45           to the directory"""
46        return self._filesystem.files_under(self._path)
47
48    def _is_under(self, dir, filename):
49        return bool(self._filesystem.relpath(self._filesystem.join(dir, filename), dir))
50
51    def open(self, filename):
52        return FileSetFileHandle(self, filename, self._filesystem)
53
54    def namelist(self):
55        return map(self._drop_directory_prefix, self._files_in_directory())
56
57    def read(self, filename):
58        return self._filesystem.read_text_file(self._full_path(filename))
59
60    def extract(self, filename, path):
61        """Extracts a file from this file set to the specified directory."""
62        src = self._full_path(filename)
63        dest = self._filesystem.join(path, filename)
64        # As filename may have slashes in it, we must ensure that the same
65        # directory hierarchy exists at the output path.
66        self._filesystem.maybe_make_directory(self._filesystem.dirname(dest))
67        self._filesystem.copyfile(src, dest)
68
69    def delete(self, filename):
70        filename = self._full_path(filename)
71        self._filesystem.remove(filename)
72