utils.ftp module

FTP manipulation library

@author: Milan Falešník <mfalesni@redhat.com>

class utils.ftp.FTPClient(host, login, password)[source]

Bases: object

FTP Client encapsulation

This class provides basic encapsulation around ftplib’s FTP class. It wraps some methods and allows to easily delete whole directory or walk through the directory tree.

Usage:

>>> from utils.ftp import FTPClient
>>> ftp = FTPClient("host", "user", "password")
>>> only_files_with_EVM_in_name = ftp.filesystem.search("EVM", directories=False)
>>> only_files_by_regexp = ftp.filesystem.search(re.compile("regexp"), directories=False)
>>> some_directory = ftp.filesystem.cd("a/b/c") # cd's to this directory
>>> root = some_directory.cd("/")

Always going through filesystem property is a bit slow as it parses the structure on each use. If you are sure that the structure will remain intact between uses, you can do as follows to save the time:

>>> fs = ftp.filesystem

Let’s download some files:

>>> for f in ftp.filesystem.search("IMPORTANT_FILE", directories=False):
...     f.download()    # To pickup its original name
...     f.download("custom_name")

We finished the testing, so we don’t need the content of the directory:

>>> ftp.recursively_delete()

And it’s gone.

cdup()[source]

Goes one level up in directory hierarchy (cd ..)

close()[source]

Finish work and close connection

connect()[source]
cwd(d)[source]

Enter a directory

Parameters:d – Directory name
Returns:Success of the action
dele(f)[source]

Remove a file

Parameters:f – File name
Returns:Success of the action
filesystem[source]

Returns the object structure of the filesystem

Returns:Root directory
ls()[source]

Lists the content of a directory.

Returns:List of all items in current directory Return format is [(is_dir?, “name”, remote_time), ...]
mkd(d)[source]

Create a directory

Parameters:d – Directory name
Returns:Success of the action
pwd()[source]

Get current directory

Returns:Current directory
Raises:AssertionError – PWD command fails
recursively_delete(d=None)[source]

Recursively deletes content of pwd

WARNING: Destructive!

Parameters:
  • d – Directory to enter (None for not entering - root directory)
  • d – str or None
Raises:

AssertionError – When some of the FTP commands fail.

retrbinary(f, callback)[source]

Download file

You need to specify the callback function, which accepts one parameter (data), to be processed.

Parameters:
  • f – Requested file name
  • callback – Callable with one parameter accepting the data
rmd(d)[source]

Remove a directory

Parameters:d – Directory name
Returns:Success of the action
storbinary(f, file_obj)[source]

Store file

You need to specify the file object.

Parameters:
  • f – Requested file name
  • file_obj – File object to be stored
tree(d=None)[source]

Walks the tree recursively and creates a tree

Base structure is a list. List contains directory content and the type decides whether it’s a directory or a file: - tuple: it’s a file, therefore it represents file’s name and time - dict: it’s a directory. Then the dict structure is as follows:

dir: directory name
content: list of directory content (recurse)
Parameters:d – Directory to enter(None for no entering - root directory)
Returns:Directory structure in lists nad dicts.
Raises:AssertionError – When some of the FTP commands fail.
update_time_difference()[source]

Determine the time difference between the FTP server and this computer.

This is done by uploading a fake file, reading its time and deleting it. Then the self.dt variable captures the time you need to ADD to the remote time or SUBTRACT from local time.

The FTPFile object carries this automatically as it has .local_time property which adds the client’s .dt to its time.

class utils.ftp.FTPDirectory(client, name, items, parent_dir=None, time=None)[source]

Bases: object

FTP FS Directory encapsulation

This class represents one directory. Contains pointers to all child directories (self.directories) and also all files in current directory (self.files)

cd(path)[source]

Change to a directory

Changes directory to a path specified by parameter path. There are three special cases: / - climbs by self.parent_dir up in the hierarchy until it reaches root element. . - does nothing .. - climbs one level up in hierarchy, if present, otherwise does the same as preceeding.

Parameters:path – Path to change
path[source]

Returns – whole path for this directory

search(by, files=True, directories=True)[source]

Recursive search by string or regexp.

Searches throughout all the filesystem structure from top till the bottom until it finds required files or dirctories. You can specify either plain string or regexp. String search does classic in, regexp matching is done by exact matching (by.match).

Parameters:
  • by – Search string or regexp
  • files – Whether look for files
  • directories – Whether look for directories
Returns:

List of all objects found in FS

exception utils.ftp.FTPException[source]

Bases: exceptions.Exception

class utils.ftp.FTPFile(client, name, parent_dir, time)[source]

Bases: object

FTP FS File encapsulation

This class represents one file in the FS hierarchy. It encapsulates mainly its position in FS and adds the possibility of downloading the file.

download(target=None)[source]

Download file into this machine

Wrapper around self.retr function. It downloads the file from remote filesystem into local filesystem. Name is either preserved original, or can be changed.

Parameters:target – Target file name (None to preserver the original)
local_time[source]

Returns – time modified to match local computer’s time zone

path[source]

Returns – whole path for this file

retr(callback)[source]

Retrieve file

Wrapper around ftplib.FTP.retrbinary(). This function cd’s to the directory where this file is present, then calls the FTP’s retrbinary() function with provided callable and then cd’s back where it started to keep it consistent.

Parameters:

callback – Any callable that accepts one parameter as the data

Raises:
  • AssertionError – When any of the CWD or CDUP commands fail.
  • ftplib.error_perm – When retrbinary call of ftplib fails