dkfileutils package¶
Submodules¶
dkfileutils.changed module¶
Check if contents of directory has changed.
-
class
dkfileutils.changed.
Directory
[source]¶ Bases:
dkfileutils.path.Path
A path that is a directory.
-
dkfileutils.changed.
changed
(dirname, filename='.md5', args=None, glob=None)[source]¶ Has glob changed in dirname
- Args:
- dirname: directory to measure filename: filename to store checksum
dkfileutils.listfiles module¶
List interesting files.
-
dkfileutils.listfiles.
list_files
(dirname='.', digest=True)[source]¶ Yield (digest, fname) tuples for all interesting files in dirname.
-
dkfileutils.listfiles.
read_skipfile
(dirname='.', defaults=None)[source]¶ The .skipfile should contain one entry per line, listing files/directories that should be skipped by
list_files()
.
dkfileutils.path module¶
Poor man’s pathlib.
(Path instances are subclasses of str, so interoperability with existing os.path code is greater than with Python 3’s pathlib.)
-
class
dkfileutils.path.
Path
[source]¶ Bases:
str
Poor man’s pathlib.
-
absolute
()¶ Return an absolute path.
-
access
(path, mode) → True if granted, False otherwise[source]¶ Use the real uid/gid to test for access to a path. Note that most operations will use the effective uid/gid, therefore this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to the path. The mode argument can be F_OK to test existence, or the inclusive-OR of R_OK, W_OK, and X_OK.
-
commonprefix
(*args)[source]¶ Given a list of pathnames, returns the longest common leading component
-
expandvars
()[source]¶ Expand shell variables of form $var and ${var}. Unknown variables are left unchanged.
-
ext
¶
-
glob
(pat)[source]¶ pat can be an extended glob pattern, e.g. ‘**/*.less’ This code handles negations similarly to node.js’ minimatch, i.e. a leading ! will negate the entire pattern.
-
join
(*args)[source]¶ Join two or more pathname components, inserting ‘/’ as needed. If any component is an absolute path, all previous path components will be discarded. An empty last part will result in a path that ends with a separator.
-
list
(filterfn=<function <lambda>>)[source]¶ Return all direct descendands of directory self for which filterfn returns True.
-
listdir
(path) → list_of_strings[source]¶ Return a list containing the names of the entries in the directory.
path: path of directory to listThe list is in arbitrary order. It does not include the special entries ‘.’ and ‘..’ even if they are present in the directory.
-
makedirs
(path[, mode=0777])[source]¶ Super-mkdir; create a leaf directory and all intermediate ones. Works like mkdir, except that any intermediate path segment (not just the rightmost) will be created if it does not exist. This is recursive.
-
parent
¶
-
parents
¶
-
realpath
()[source]¶ Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path.
-
removedirs
(path)[source]¶ Super-rmdir; remove a leaf directory and all empty intermediate ones. Works like rmdir except that, if the leaf directory is successfully removed, directories corresponding to rightmost path segments will be pruned away until either the whole path is consumed or an error occurs. Errors during this latter phase are ignored – they generally mean that a directory was not empty.
-
renames
(old, new)[source]¶ Super-rename; create directories as necessary and delete any left empty. Works like rename, except creation of any intermediate directories needed to make the new pathname good is attempted first. After the rename, directories corresponding to rightmost path segments of the old name will be pruned until either the whole path is consumed or a nonempty directory is found.
Note: this function can fail with the new directory structure made if you lack permissions needed to unlink the leaf directory or file.
-
rmtree
(subdir=None)[source]¶ Recursively delete a directory tree.
If ignore_errors is set, errors are ignored; otherwise, if onerror is set, it is called to handle the error with arguments (func, path, exc_info) where func is os.listdir, os.remove, or os.rmdir; path is the argument to that function that caused it to fail; and exc_info is a tuple returned by sys.exc_info(). If ignore_errors is false and onerror is None, an exception is raised.
-
split
(sep=None, maxsplit=-1)[source]¶ Split a pathname. Returns tuple “(head, tail)” where “tail” is everything after the final slash. Either part may be empty.
-
splitext
()[source]¶ Split the extension from a pathname.
Extension is everything from the last dot to the end, ignoring leading dots. Returns “(root, ext)”; ext may be empty.
-
touch
(mode=438, exist_ok=True)[source]¶ Create this file with the given access mode, if it doesn’t exist. Based on:
-
dkfileutils.pfind module¶
CLI usage: pfind path filename
will find the closest ancestor directory
conataining filename (used for finding syncspec.txt and config files).
-
dkfileutils.pfind.
pfind
(path, *fnames)[source]¶ Find the first fname in the closest ancestor directory. For the purposes of this function, we are our own closest ancestor, i.e. given the structure:
/srv |-- myapp | |-- __init__.py | `-- myapp.py `-- setup.py
then both
pfind('/srv', 'setup.py')
andpfind('/srv/myapp', 'setup.py')
will return/srv/setup.py
-
dkfileutils.pfind.
pfindall
(path, *fnames)[source]¶ Find all fnames in the closest ancestor directory. For the purposes of this function, we are our own closest ancestor. I.e. given the structure:
. `-- a |-- b | |-- c | | `-- x.txt | `-- x.txt `-- y.txt
the call:
dict(pfindall('a/b/c', 'x.txt', 'y.txt'))
will return:
{ 'x.txt': 'a/b/c/x.txt', 'y.txt': 'a/y.txt' }
a/b/x.txt
is not returned, sincea/b/c/x.txt
is the “closest”x.txt
when starting froma/b/c
(note: pfindall only looks “upwards”, ie. towards the root).
dkfileutils.which module¶
Print where on the path an executable is located.
Module contents¶
dkfileutils - simple, common file utilities.