/usr/share/pyshared/Codeville/cdv_glob.py is in codeville 0.8.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | # Written by Ross Cohen
# see LICENSE.txt for license information
from client_helpers import children_count
from client_helpers import filename_to_handle
import fnmatch
import os, sys
from os import path
from path import subpath
import re
class Glob:
def __init__(self, co, files):
self.co = co
self.local = co.local
if sys.platform == 'win32':
self._glob_cache = {}
unglobbedfiles = files
files = []
for file in unglobbedfiles:
files += self._glob(file)
del self._glob_cache
# normalize the filenames
self.files = []
for file in files:
try:
self.files.append(subpath(self.local, file)[1])
except ValueError:
print 'warning - %s is not under the repository' % (file,)
return
def fs_walk(self, expand=1):
for file in self.files:
parent, name = path.split(file)
if name == '...':
if expand:
for lfile in self._fs_expand(parent):
yield (lfile, 1)
continue
if not path.exists(path.join(self.local, file)):
print 'warning - %s does not exist' % (file,)
continue
yield (file, 0)
return
def db_walk(self, deletes=0, txn=None):
for file in self.files:
parent, name = path.split(file)
if name == '...':
for lhandle in self._db_expand(parent, deletes, txn):
yield (lhandle, 1)
continue
handle = filename_to_handle(self.co, file, txn, deletes=deletes)
if handle is None:
print 'warning - %s is not in the repository' % (file,)
continue
yield (handle, 0)
return
def _fs_expand(self, local):
root_cmp = path.join(self.local, '.cdv')
filter = path.join(self.local, '.cdv', '')
root_len = len(self.local + path.sep)
filter_len = len(filter)
for root, dirs, files, in os.walk(path.join(self.local, local)):
if root == root_cmp:
dirs[:] = []
continue
if root[:filter_len] == filter:
dirs[:] = []
continue
nroot = root[root_len:]
if nroot != local:
yield nroot
for file in files:
yield path.join(nroot, file)
return
def _db_expand(self, local, deletes, txn):
handle = filename_to_handle(self.co, local, txn)
dirs = [handle]
while len(dirs):
lhandle = dirs.pop()
children = children_count(self.co, lhandle, txn, deletes=deletes)
dirs.extend(children)
for handle in children:
yield handle
return
def _glob(self, pathname):
if not self._has_magic(pathname):
return [pathname]
dirname, basename = os.path.split(pathname)
if not dirname:
return self._glob1(os.curdir, basename)
elif self._has_magic(dirname):
list = self._glob(dirname)
else:
list = [dirname]
if not self._has_magic(basename):
result = []
for dirname in list:
if basename or os.path.isdir(dirname):
name = os.path.join(dirname, basename)
if os.path.lexists(name):
result.append(name)
else:
result = []
for dirname in list:
sublist = self._glob1(dirname, basename)
for name in sublist:
result.append(os.path.join(dirname, name))
if len(result) == 0:
result.append(pathname)
return result
def _glob1(self, dirname, pattern):
if not dirname: dirname = os.curdir
if dirname in self._glob_cache:
names = self._glob_cache[dirname]
else:
try:
names = os.listdir(dirname)
except os.error:
return []
self._glob_cache[dirname] = names
if pattern[0]!='.':
names=filter(lambda x: x[0]!='.',names)
return fnmatch.filter(names,pattern)
_magic_check = re.compile('[*?[]')
def _has_magic(self, s):
return self._magic_check.search(s) is not None
|