cfme.utils.ftp module

FTP manipulation library

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

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.

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

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

Parameters:
  • 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

Usage:

.. 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("foo.zip") # upload local file
  fs.file_names # return list of available files
  fs.files()  # return list of available file objects
  download_path = fs.download("foo.zip") # It will download foo.zip file

  f = fs.get_file("foo.zip")
  f.path    # It will return file storage path
  f.link    # gives remote file link; can be used to download with 'wget'
  f.download()  # download file
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
Returns:target path
file_names

List of remote FTP file names

files()[source]

List of FTP file objects

get_file(name)[source]
Arg:
name: name of remote file
Returns: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
Returns: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)

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

whole path for this directory

Type:Returns
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 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(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

time modified to match local computer’s time zone

Type:Returns
path

whole path for this file

Type:Returns
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:
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

download(target=None)[source]
Arg:
target: local path for download else it will consider current working path
Returns:target path

Remote FTP file url