File: filesystem.py

package info (click to toggle)
gst-qa-system 0.0%2Bgit20110920.4750a8e8-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 980 kB
  • sloc: python: 8,498; xml: 855; makefile: 42
file content (118 lines) | stat: -rw-r--r-- 3,892 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# GStreamer QA system
#
#       generators/filesystem.py
#
# Copyright (c) 2007, Edward Hervey <bilboed@bilboed.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

"""
File system related generators
"""

import os.path
from fnmatch import fnmatch

from insanity.generator import Generator
from insanity.log import debug, info

class FileSystemGenerator(Generator):
    """
    Arguments:
    * list of paths/files
    * recursive option (default : True)
    * matching option (default : [])
    * reject option (default : [])

    Returns:
    * file system path
    """

    __args__ = {
        "paths":"List of paths or files",
        "recursive":"If True, go down in subdirectories (default:True)",
        "matching":"List of masks for files to be taken into account",
        "reject":"List of masks for files to NOT be taken into account"
        }

    __produces__ = "paths"

    def __init__(self, paths=[], recursive=True,
                 matching=[], reject=[], *args,
                 **kwargs):
        """
        paths : list of paths and/or files
        recursive : go down in subdirectories
        matching : will only return files matching the given masks
        reject : will not return files matching the given masks
        """
        Generator.__init__(self, paths=paths, recursive=recursive,
                           matching=matching, reject=reject,
                           *args, **kwargs)
        self.paths = paths
        self.recursive = recursive
        self.matching = matching
        self.reject = reject
        info("paths:%r, recursive:%r, matching:%r, reject:%r" % (paths, recursive, matching, reject))

    def _is_valid_file(self, filename):
        """ returns True if the given filename is valid """
        if self.matching:
            # try against the positive matches
            for match in self.matching:
                if fnmatch(filename, match):
                    return True
            return False

        if self.reject:
            # try against the negative matches
            for match in self.reject:
                if fnmatch(filename, match):
                    return False
        # if there's no matching exceptions, it's valid
        return True

    def _get_files(self, directory):
        res = []
        for dirpath, dirnames, filenames in os.walk(directory):
            res.extend([os.path.join(dirpath, fn) for fn in filenames if self._is_valid_file(fn)])
            if not self.recursive:
                break
        res.sort()
        return res

    def _generate(self):
        res = []
        for path in self.paths:
            fullpath = os.path.abspath(path)
            if os.path.isfile(fullpath) and self._is_valid_file(fullpath):
                res.append(fullpath)
            else:
                res.extend(self._get_files(fullpath))
        info("Returning %d files" % len(res))
        return res

class URIFileSystemGenerator(FileSystemGenerator):
    """
    Same as FileSystemGenerator, excepts that it returns URIs instead
    of file system paths.
    """

    __produces__ = "URI"

    def _generate(self):
        return ["file://%s" % x for x in FileSystemGenerator._generate(self)]