cfme.utils.ftp module

FTP manipulation library

@author: Milan Falešník <>

class cfme.utils.ftp.FTPClient(host, login, password, upload_dir='/', time_diff=True)[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.


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

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"IMPORTANT_FILE", directories=False):
...    # To pickup its original name

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

>>> ftp.recursively_delete()

And it’s gone.


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


Finish work and close connection


Enter a directory


d – Directory name


Success of the action


Remove a file


f – File name


Success of the action

property filesystem

Returns the object structure of the filesystem


Root directory


Lists the content of a directory.


List of all items in current directory Return format is [(is_dir?, “name”, remote_time), …]


Create a directory


d – Directory name


Success of the action


Get current directory


Current directory


AssertionError – PWD command fails


Recursively deletes content of pwd

WARNING: Destructive!

  • d – Directory to enter (None for not entering - root directory)

  • d – str or None


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.

  • f – Requested file name

  • callback – Callable with one parameter accepting the data


Remove a directory


d – Directory name


Success of the action

storbinary(f, file_obj)[source]

Store file

You need to specify the file object.

  • f – Requested file name

  • file_obj – File object to be stored


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)

d – Directory to enter(None for no entering - root directory)


Directory structure in lists nad dicts.


AssertionError – When some of the FTP commands fail.


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 cfme.utils.ftp.FTPClientWrapper(entity_path=None, entrypoint=None, host=None, login=None, password=None)[source]

Bases: cfme.utils.ftp.FTPClient

This class is for miq remote file management with FTP. It is useful to make a collection of raw files related to customer BZ testing directly or indirectly. This will help to easily download testing related files in a runtime environment.

  • entity_path – entity which you want to access like Datastores, Dialogs.

  • entrypoint – FTP server entry point

  • host – FTP server host

  • login – FTP user

  • password – FTP password


.. code-block:: python

  fs = FileServer("miq")
  fs.directory_names    # list of current available entities
  fs.mkd("Dialogs")   # create new entity type
  fs.rmd("Dialogs")     # delete created entity type

  fs = FileServer("miq/Reports")
  fs.upload("") # upload local file
  fs.file_names # return list of available files
  fs.files()  # return list of available file objects
  download_path ="") # It will download file

  f = fs.get_file("")
  f.path    # It will return file storage path    # gives remote file link; can be used to download with 'wget'  # download file
property directory_names

List remote FTP directories

download(name, target=None)[source]

Download FTP file Arg:

name: remote file name target: local path for download else it will consider current working path


target path

property file_names

List of remote FTP file names


List of FTP file objects


name: name of remote file


FTP file object

upload(path, name=None)[source]

Upload FTP file Arg:

path: path of local file name: set name of file default it will consider original name of uploading file


Success of the action

class cfme.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)


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.


path – Path to change

property path

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).

  • by – Search string or regexp

  • files – Whether look for files

  • directories – Whether look for directories


List of all objects found in FS

exception cfme.utils.ftp.FTPException[source]

Bases: Exception

class cfme.utils.ftp.FTPFile(client, name, parent_dir, time=None)[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 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.


target – Target file name (None to preserver the original)

property local_time

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

property path

Returns: whole path for this file


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.


callback – Any callable that accepts one parameter as the data

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

Bases: cfme.utils.ftp.FTPFile

This is simple FTPFile class wrapper for FTPClientWrapper


target: local path for download else it will consider current working path


target path

Remote FTP file url