/usr/share/pyshared/bike/parsing/pathutils.py is in bicyclerepair 0.9-6.1.
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 153 154 155 156 157 158 159 160 161 162 163 | # A some of this code is take from Pythius -
# Copyright (GPL) 2001 Jurgen Hermann <jh@web.de>
from bike.globals import *
import os
def containsAny(str, set):
""" Check whether 'str' contains ANY of the chars in 'set'
"""
return 1 in [c in str for c in set]
def getPathOfModuleOrPackage(dotted_name, pathlist = None):
""" Get the filesystem path for a module or a package.
Return the file system path to a file for a module,
and to a directory for a package. Return None if
the name is not found, or is a builtin or extension module.
"""
from bike.parsing.newstuff import getPythonPath
if pathlist is None:
pathlist = getPythonPath()
import imp
# split off top-most name
parts = dotted_name.split('.', 1)
if len(parts) > 1:
# we have a dotted path, import top-level package
try:
file, pathname, description = imp.find_module(parts[0], pathlist)
if file: file.close()
except ImportError:
return None
# check if it's indeed a package
if description[2] == imp.PKG_DIRECTORY:
# recursively handle the remaining name parts
pathname = getPathOfModuleOrPackage(parts[1], [pathname])
else:
pathname = None
else:
# plain name
try:
file, pathname, description = imp.find_module(dotted_name, pathlist)
if file: file.close()
if description[2]not in[imp.PY_SOURCE, imp.PKG_DIRECTORY]:
pathname = None
except ImportError:
pathname = None
return pathname
def getFilesForName(name):
""" Get a list of module files for a filename, a module or package name,
or a directory.
"""
import imp
if not os.path.exists(name):
# check for glob chars
if containsAny(name, "*?[]"):
import glob
files = glob.glob(name)
list = []
for file in files:
list.extend(getFilesForName(file))
return list
# try to find module or package
name = getPathOfModuleOrPackage(name)
if not name:
return[]
if os.path.isdir(name):
# find all python files in directory
list = []
os.path.walk(name, _visit_pyfiles, list)
return list
elif os.path.exists(name) and not name.startswith("."):
# a single file
return [name]
return []
def _visit_pyfiles(list, dirname, names):
""" Helper for getFilesForName().
"""
# get extension for python source files
if not globals().has_key('_py_ext'):
import imp
global _py_ext
_py_ext = [triple[0]for triple in imp.get_suffixes()if triple[2] == imp.PY_SOURCE][0]
# don't recurse into CVS or Subversion directories
if 'CVS'in names:
names.remove('CVS')
if '.svn'in names:
names.remove('.svn')
names_copy = [] + names
for n in names_copy:
if os.path.isdir(os.path.join(dirname, n))and \
not os.path.exists(os.path.join(dirname, n, "__init__.py")):
names.remove(n)
# add all *.py files to list
list.extend(
[os.path.join(dirname, file)
for file in names
if os.path.splitext(file)[1] == _py_ext and not file.startswith(".")])
# returns the directory which holds the first package of the package
# hierarchy under which 'filename' belongs
def getRootDirectory(filename):
if os.path.isdir(filename):
dir = filename
else:
dir = os.path.dirname(filename)
while dir != "" and \
os.path.exists(os.path.join(dir, "__init__.py")):
dir = os.path.dirname(dir)
return dir
# Returns the root package directoryname of the package hierarchy
# under which 'filename' belongs
def getPackageBaseDirectory(filename):
if os.path.isdir(filename):
dir = filename
else:
dir = os.path.dirname(filename)
if not os.path.exists(os.path.join(dir, "__init__.py")):
# parent dir is not a package
return dir
while dir != "" and \
os.path.exists(os.path.join(os.path.dirname(dir), "__init__.py")):
dir = os.path.dirname(dir)
return dir
def filenameToModulePath(fname):
directoriesPreceedingRoot = getRootDirectory(fname)
import os
# strip off directories preceeding root package directory
if directoriesPreceedingRoot != "":
mpath = fname.replace(directoriesPreceedingRoot, "")
else:
mpath = fname
if(mpath[0] == os.path.normpath("/")):
mpath = mpath[1:]
mpath, ext = os.path.splitext(mpath)
mpath = mpath.replace(os.path.normpath("/"), ".")
return mpath
|