~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Martin Pool
  • Date: 2005-07-23 00:18:14 UTC
  • Revision ID: mbp@sourcefrog.net-20050723001814-03baa015c59253f6
- Add less-sucky is_within_any

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
 
40
40
_QUOTE_RE = re.compile(r'([^a-zA-Z0-9.,:/_~-])')
 
41
 
 
42
_SLASH_RE = re.compile(r'[\\/]+')
 
43
 
41
44
def quotefn(f):
42
45
    """Return a quoted filename filename
43
46
 
131
134
 
132
135
def is_inside(dir, fname):
133
136
    """True if fname is inside dir.
 
137
    
 
138
    The parameters should typically be passed to os.path.normpath first, so
 
139
    that . and .. and repeated slashes are eliminated, and the separators
 
140
    are canonical for the platform.
 
141
    
 
142
    >>> is_inside('src', 'src/foo.c')
 
143
    True
 
144
    >>> is_inside('src', 'srccontrol')
 
145
    False
 
146
    >>> is_inside('src', 'src/a/a/a/foo.c')
 
147
    True
 
148
    >>> is_inside('foo.c', 'foo.c')
 
149
    True
134
150
    """
135
 
    return os.path.commonprefix([dir, fname]) == dir
136
 
 
 
151
    # XXX: Most callers of this can actually do something smarter by 
 
152
    # looking at the inventory
 
153
    
 
154
    dir = dir.split(os.sep)
 
155
    pl = len(dir)
 
156
    fname = fname.split(os.sep)
 
157
   
 
158
    return (len(fname) >= pl) and (dir == fname[:pl])
 
159
    
137
160
 
138
161
def is_inside_any(dir_list, fname):
139
162
    """True if fname is inside any of given dirs."""