Back to index

python3.2  3.2.2
Functions | Variables
pysource Namespace Reference

Functions

def print_debug
def _open
def has_python_ext
def looks_like_python
def can_be_compiled
def walk_python_files

Variables

string __author__ = "Oleg Broytmann, Georg Brandl"
list __all__ = ["has_python_ext", "looks_like_python", "can_be_compiled", "walk_python_files"]
tuple binary_re = re.compile('[\x00-\x08\x0E-\x1F\x7F]')
 debug = False

Function Documentation

def pysource._open (   fullpath) [private]

Definition at line 33 of file pysource.py.

00033 
00034 def _open(fullpath):
00035     try:
00036         size = os.stat(fullpath).st_size
00037     except OSError as err: # Permission denied - ignore the file
00038         print_debug("%s: permission denied: %s" % (fullpath, err))
00039         return None
00040 
00041     if size > 1024*1024: # too big
00042         print_debug("%s: the file is too big: %d bytes" % (fullpath, size))
00043         return None
00044 
00045     try:
00046         return open(fullpath, 'rU')
00047     except IOError as err: # Access denied, or a special file - ignore it
00048         print_debug("%s: access denied: %s" % (fullpath, err))
00049         return None

Here is the call graph for this function:

Here is the caller graph for this function:

def pysource.can_be_compiled (   fullpath)

Definition at line 74 of file pysource.py.

00074 
00075 def can_be_compiled(fullpath):
00076     infile = _open(fullpath)
00077     if infile is None:
00078         return False
00079 
00080     code = infile.read()
00081     infile.close()
00082 
00083     try:
00084         compile(code, fullpath, "exec")
00085     except Exception as err:
00086         print_debug("%s: cannot compile: %s" % (fullpath, err))
00087         return False
00088 
00089     return True
00090 

Here is the call graph for this function:

def pysource.has_python_ext (   fullpath)

Definition at line 50 of file pysource.py.

00050 
00051 def has_python_ext(fullpath):
00052     return fullpath.endswith(".py") or fullpath.endswith(".pyw")

def pysource.looks_like_python (   fullpath)

Definition at line 53 of file pysource.py.

00053 
00054 def looks_like_python(fullpath):
00055     infile = _open(fullpath)
00056     if infile is None:
00057         return False
00058 
00059     line = infile.readline()
00060     infile.close()
00061 
00062     if binary_re.search(line):
00063         # file appears to be binary
00064         print_debug("%s: appears to be binary" % fullpath)
00065         return False
00066 
00067     if fullpath.endswith(".py") or fullpath.endswith(".pyw"):
00068         return True
00069     elif "python" in line:
00070         # disguised Python script (e.g. CGI)
00071         return True
00072 
00073     return False

Here is the call graph for this function:

def pysource.print_debug (   msg)

Definition at line 29 of file pysource.py.

00029 
00030 def print_debug(msg):
00031     if debug: print(msg)
00032 

Here is the caller graph for this function:

def pysource.walk_python_files (   paths,
  is_python = looks_like_python,
  exclude_dirs = None 
)
\
Recursively yield all Python source files below the given paths.

paths: a list of files and/or directories to be checked.
is_python: a function that takes a file name and checks whether it is a
           Python source file
exclude_dirs: a list of directory base names that should be excluded in
              the search

Definition at line 91 of file pysource.py.

00091 
00092 def walk_python_files(paths, is_python=looks_like_python, exclude_dirs=None):
00093     """\
00094     Recursively yield all Python source files below the given paths.
00095 
00096     paths: a list of files and/or directories to be checked.
00097     is_python: a function that takes a file name and checks whether it is a
00098                Python source file
00099     exclude_dirs: a list of directory base names that should be excluded in
00100                   the search
00101     """
00102     if exclude_dirs is None:
00103         exclude_dirs=[]
00104 
00105     for path in paths:
00106         print_debug("testing: %s" % path)
00107         if os.path.isfile(path):
00108             if is_python(path):
00109                 yield path
00110         elif os.path.isdir(path):
00111             print_debug("    it is a directory")
00112             for dirpath, dirnames, filenames in os.walk(path):
00113                 for exclude in exclude_dirs:
00114                     if exclude in dirnames:
00115                         dirnames.remove(exclude)
00116                 for filename in filenames:
00117                     fullpath = os.path.join(dirpath, filename)
00118                     print_debug("testing: %s" % fullpath)
00119                     if is_python(fullpath):
00120                         yield fullpath
00121         else:
00122             print_debug("    unknown type")
00123 

Here is the call graph for this function:


Variable Documentation

Definition at line 20 of file pysource.py.

string pysource.__author__ = "Oleg Broytmann, Georg Brandl"

Definition at line 18 of file pysource.py.

tuple pysource.binary_re = re.compile('[\x00-\x08\x0E-\x1F\x7F]')

Definition at line 25 of file pysource.py.

pysource.debug = False

Definition at line 27 of file pysource.py.