qisys.sh – Common filesystem operations

Common filesystem operations

qisys.sh.get_config_path(*args)

Get a config path to read or write some configuration.

Parameters:args – a list of subfolders. Those will be created when needed
qisys.sh.get_cache_path(*args)

Get a config path to read or write some cached data

Parameters:args – a list of subfolders. Those will be created when needed
qisys.sh.get_share_path(*args)

Get a config path to read or write some persistent data

Parameters:args – a list of subfolders. Those will be created when needed
qisys.sh.get_path(*args)

Helper for get_*_path methods

qisys.sh.username()

Get the current user name

qisys.sh.mkdir(dest_dir, recursive=False)

Recursive mkdir (do not fail if file exists)

qisys.sh.ln(src, dst, symlink=True)

ln (do not fail if file exists)

qisys.sh.write_file_if_different(data, out_path, mode='w')

Write the data to out_path if the content is different

qisys.sh.configure_file(in_path, out_path, copy_only=False, *args, **kwargs)

Configure a file. :param in_path: input file :param out_path: output file

The out_path needs not to exist, missing leading directories will be created if necessary.

If copy_only is True, the contents will be copied “as is”.

If not, we will use the args and kwargs parameter as in:

in_content.format(*args, **kwargs)
qisys.sh.install(src, dest, filter_fun=None, quiet=False)

Install a directory or a file to a destination.

If filter_fun is not None, then the file will only be installed if filter_fun(relative/path/to/file) returns True.

If dest does not exist, it will be created first.

When installing files, if the destination already exists, it will be removed first, then overwritten by the new file.

This function will preserve relative symlinks between directories, used for instance in Mac frameworks:

|__ Versions
    |__ Current  -> 4.0
    |__ 4        -> 4.0
    |__ 4.0

Return the list of files installed (with relative paths)

qisys.sh.safe_copy(src, dest)

Copy a source file to a destination but do not overwrite dest if it is more recent than src

Create any missing directories when necessary

If dest is a directory, src will be copied inside dest.

qisys.sh.up_to_date(output_path, input_path)

” Return True if output_path exists and is more recent than input_path

qisys.sh.copy_git_src(src, dest)

Copy a source to a destination but only copy the files under version control. Assumes that src is inside a git worktree

qisys.sh.rm(name)

This one can take a file or a directory. Contrary to shutil.remove or os.remove, it:

  • won’t fail if the directory does not exist
  • won’t fail if the directory contains read-only files
  • won’t fail if the file does not exist

Please avoid using shutil.rmtree ...

qisys.sh.mv(src, dest)

Move a file into a directory, but do not crash if dest/src exists

qisys.sh.ls_r(directory)

Returns a sorted list of all the files present in a directory, relative to this directory.

For instance, with:

foo
|__ eggs
|    |__ c
|    |__ d
|__ empty
|__ spam
    |__a
    |__b

ls_r(foo) returns: [“eggs/c”, “eggs/d”, “empty/”, “spam/a”, “spam/b”]

qisys.sh.which(program)

find program in the environment PATH :return: path to program if found, None otherwise

qisys.sh.to_posix_path(path, fix_drive=False)

Returns a POSIX path from a DOS path :param fix_drive: if True, will replace c: by /c/ (ala mingw)

qisys.sh.to_dos_path(path)

Return a DOS path from a “windows with /” path. Useful because people sometimes use forward slash in environment variable, for instance

qisys.sh.to_native_path(path, normcase=True)

Return an absolute, native path from a path, :param normcase: make sure the path is all lower-case on case-insensitive filesystems

qisys.sh.is_path_inside(a, b)

Returns True if a is inside b

>>> is_path_inside("foo/bar", "foo")
True
>>> is_path_inside("gui/bar/libfoo", "lib")
False
qisys.sh.is_empty(path)

Check if a path is empty

class qisys.sh.TempDir(name='tmp')

This is a nice wrapper around tempfile module.

Usage:

with TempDir("foo-bar") as temp_dir:
    subdir = os.path.join(temp_dir, "subdir")
    do_foo(subdir)

This piece of code makes sure that:

  • a temporary directory named temp_dir has been created (guaranteed to exist, be empty, and writeable)
  • the directory will be removed when the scope of temp_dir has ended unless an exception has occurred and DEBUG environment variable is set.
qisys.sh.change_cwd(*args, **kwds)

Change the current working dir

qisys.sh.is_runtime(filename)

Filter function to only install runtime components of packages

Returns True if the file is a broken symlink

qisys.sh.is_binary(file_path)

Returns True if the file is binary

qisys.sh.is_executable_binary(file_path)

Returns true if the file: * is executable * is a binary (i.e not a script)

class qisys.sh.PreserveFileMetadata(path)

Preserve file metadata (permissions and times)