Module scriptutil
source code
Copyright (c) 2008, Muharem Hrnjadovic
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
-
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
-
Neither the name of Muharem Hrnjadovic nor the names of other
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
Module providing functions commonly used in shell scripting:
-
ffind() : finds files in a directory tree
-
ffindgrep(): finds files in a directory tree and matches their
content to regular expressions
-
freplace() : in-place search/replace of files in a directory tree
with regular expressions
-
printr() : prints the results of the ffind()/ffindgrep()
functions
Please see the documentation strings of the particular functions for
detailed information.
sequence
|
ffind(path,
shellglobs=None,
namefs=None,
relative=True)
Find files in the directory tree starting at 'path' (filtered by
Unix shell-style wildcards ('shellglobs') and/or the functions in the
'namefs' sequence). |
source code
|
|
dict
|
ffindgrep(path,
regexl,
shellglobs=None,
namefs=None,
relative=True,
linenums=False)
Find files in the directory tree starting at 'path' (filtered by
Unix shell-style wildcards ('shellglobs') and/or the functions in the
'namefs' sequence) and search inside these. |
source code
|
|
number
|
freplace(path,
regexl,
shellglobs=None,
namefs=None,
bext=' .bak ' )
Find files in the directory tree starting at 'path' (filtered by
Unix shell-style wildcards ('shellglobs') and/or the functions in the
'namefs' sequence) and perform an in-place search/replace operation
on these. |
source code
|
|
|
|
ffind(path,
shellglobs=None,
namefs=None,
relative=True)
| source code
|
Find files in the directory tree starting at 'path' (filtered by Unix
shell-style wildcards ('shellglobs') and/or the functions in the 'namefs'
sequence).
Please not that the shell wildcards work in a cumulative fashion i.e.
each of them is applied to the full set of file *names* found.
Conversely, all the functions in 'namefs'
-
only get to see the output of their respective predecessor
function in the sequence (with the obvious exception of the first
function)
-
are applied to the full file *path* (whereas the shell-style
wildcards are only applied to the file *names*)
- Parameters:
path (string) - starting path of the directory tree to be searched
shellglobs (sequence) - an optional sequence of Unix shell-style wildcards that are to
be applied to the file *names* found
namefs (sequence) - an optional sequence of functions to be applied to the file
*paths* found
relative (bool) - a boolean flag that determines whether absolute or relative
paths should be returned
- Returns: sequence
- paths for files found
|
ffindgrep(path,
regexl,
shellglobs=None,
namefs=None,
relative=True,
linenums=False)
| source code
|
Find files in the directory tree starting at 'path' (filtered by Unix
shell-style wildcards ('shellglobs') and/or the functions in the 'namefs'
sequence) and search inside these.
Additionaly, the file content will be filtered by the regular
expressions in the 'regexl' sequence. Each entry in the latter is a
-
either a string (with the regex definition)
-
or a tuple with arguments accepted by re.compile() (the re.M and
re.S flags will have no effect though)
For all the files that pass the file name/content tests the function
returns a dictionary where the
-
key is the file name and the
-
value is a string with lines filtered by 'regexl'
- Parameters:
path (string) - starting path of the directory tree to be searched
shellglobs (sequence) - an optional sequence of Unix shell-style wildcards that are to
be applied to the file *names* found
namefs (sequence) - an optional sequence of functions to be applied to the file
*paths* found
relative (bool) - a boolean flag that determines whether absolute or relative
paths should be returned
linenums (bool) - turns on line numbers for found files (like grep -n)
- Returns: dict
- file name (key) and lines filtered by 'regexl' (value)
|
freplace(path,
regexl,
shellglobs=None,
namefs=None,
bext=' .bak ' )
| source code
|
Find files in the directory tree starting at 'path' (filtered by Unix
shell-style wildcards ('shellglobs') and/or the functions in the 'namefs'
sequence) and perform an in-place search/replace operation on these.
Additionally, an in-place search/replace operation is performed on the
content of all the files (whose names passed the tests) using the regular
expressions in 'regexl'.
Please note: 'regexl' is a sequence of 3-tuples, each having the
following elements:
-
search string (Python regex syntax)
-
replace string (Python regex syntax)
-
regex flags or 'None' (re.compile syntax)
Copies of the modified files are saved in backup files using the
extension specified in 'bext'.
- Parameters:
path (string) - starting path of the directory tree to be searched
shellglobs (sequence) - an optional sequence of Unix shell-style wildcards that are to
be applied to the file *names* found
namefs (sequence) - an optional sequence of functions to be applied to the file
*paths* found
- Returns: number
- total number of files modified
|
Print the results of the ffind()/ffindgrep() functions.
The output format is similar to the one used by the UNIX find
utility.
|