tea.shell Module

Module mimics some of the behaviors of the builtin shutil.

It adds logging to all operations and abstracting some other useful shell commands (functions).

tea.shell.split(s, posix=True)[source]

Split the string s using shell-like syntax.

Parameters:
  • s (str) – String to split
  • posix (bool) – Use posix split
Returns:

List of string parts

Return type:

list of str

tea.shell.search(path, matcher='*', dirs=False, files=True)[source]

Recursive search function.

Parameters:
  • path (str) – Path to search recursively
  • matcher (str or callable) – String pattern to search for or function that returns True/False for a file argument
  • dirs (bool) – if True returns directories that match the pattern
  • files (bool) – if True returns files that match the patter
Yields:

str – Found files and directories

tea.shell.chdir(directory)[source]

Change the current working directory.

Parameters:directory (str) – Directory to go to.
tea.shell.goto(*args, **kwds)[source]

Context object for changing directory.

Parameters:
  • directory (str) – Directory to go to.
  • create (bool) – Create directory if it doesn’t exists.

Usage:

>>> with goto(directory) as ok:
...     if not ok:
...         print 'Error'
...     else:
...         print 'All OK'
tea.shell.mkdir(path, mode=493, delete=False)[source]

Make a directory.

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.

Parameters:
  • path (str) – Directory to create
  • mode (int) – Directory mode
  • delete (bool) – Delete directory/file if exists
Returns:

True if succeeded else False

Return type:

bool

tea.shell.copy(source, destination)[source]

Copy file or directory.

Parameters:
  • source (str) – Source file or directory
  • destination (str) – Destination file or directory (where to copy).
Returns:

True if the operation is successful, False otherwise.

Return type:

bool

tea.shell.gcopy(pattern, destination)[source]

Copy all file found by glob.glob(pattern) to destination directory.

Parameters:
  • pattern (str) – Glob pattern
  • destination (str) – Path to the destination directory.
Returns:

True if the operation is successful, False otherwise.

Return type:

bool

tea.shell.move(source, destination)[source]

Move a file or directory (recursively) to another location.

If the destination is on our current file system, then simply use rename. Otherwise, copy source to the destination and then remove source.

Parameters:
  • source (str) – Source file or directory (file or directory to move).
  • destination (str) – Destination file or directory (where to move).
Returns:

True if the operation is successful, False otherwise.

Return type:

bool

tea.shell.gmove(pattern, destination)[source]

Move all file found by glob.glob(pattern) to destination directory.

Parameters:
  • pattern (str) – Glob pattern
  • destination (str) – Path to the destination directory.
Returns:

True if the operation is successful, False otherwise.

Return type:

bool

tea.shell.remove(path)[source]

Delete a file or directory.

Parameters:path (str) – Path to the file or directory that needs to be deleted.
Returns:True if the operation is successful, False otherwise.
Return type:bool
tea.shell.gremove(pattern)[source]

Remove all file found by glob.glob(pattern).

Parameters:pattern (str) – Pattern of files to remove
Returns:True if the operation is successful, False otherwise.
Return type:bool
tea.shell.read(path, encoding='utf-8')[source]

Read the content of the file.

Parameters:
  • path (str) – Path to the file
  • encoding (str) – File encoding. Default: utf-8
Returns:

File content or empty string if there was an error

Return type:

str

tea.shell.touch(path, content='', encoding='utf-8', overwrite=False)[source]

Create a file at the given path if it does not already exists.

Parameters:
  • path (str) – Path to the file.
  • content (str) – Optional content that will be written in the file.
  • encoding (str) – Encoding in which to write the content. Default: utf-8
  • overwrite (bool) – Overwrite the file if exists.
Returns:

True if the operation is successful, False otherwise.

Return type:

bool