aiida.transport documentation

This chapter describes the generic implementation of a transport plugin. The currently implemented are the local and the ssh plugin. The local plugin makes use only of some standard python modules like os and shutil. The ssh plugin is a wrapper to the library paramiko, that you installed with AiiDA.

A generic set of tests is contained in plugin_test.py, while plugin-specific tests are written separately.

Generic transport class

class aiida.transport.__init__.FileAttribute(init=None)[source]

A class, resembling a dictionary, to describe the attributes of a file, that is returned by get_attribute(). Possible keys: st_size, st_uid, st_gid, st_mode, st_atime, st_mtime

class aiida.transport.__init__.Transport(*args, **kwargs)[source]

Abstract class for a generic transport (ssh, local, ...) Contains the set of minimal methods

__enter__()[source]

For transports that require opening a connection, opens all required channels (used in ‘with’ statements)

__exit__(type, value, traceback)[source]

Closes connections, if needed (used in ‘with’ statements).

chdir(path)[source]

Change directory to ‘path’

Parameters:path (str) – path to change working directory into.
Raises:IOError, if the requested path does not exist
Return type:string
chmod(path, mode)[source]

Change permissions of a path.

Parameters:
  • path (str) – path to file
  • mode (int) – new permissions
chown(path, uid, gid)[source]

Change the owner (uid) and group (gid) of a file. As with python’s os.chown function, you must pass both arguments, so if you only want to change one, use stat first to retrieve the current owner and group.

Parameters:
  • path (str) – path to the file to change the owner and group of
  • uid (int) – new owner’s uid
  • gid (int) – new group id
close()[source]

Closes the local transport channel

copy(remotesource, remotedestination, *args, **kwargs)[source]

Copy a file or a directory from remote source to remote destination (On the same remote machine)

Parameters:
  • remotesource (str) – path of the remote source directory / file
  • remotedestination (str) – path of the remote destination directory / file
Raises:

IOError, if one of src or dst does not exist

copy_from_remote_to_remote(transportdestination, remotesource, remotedestination, **kwargs)[source]

Copy files or folders from a remote computer to another remote computer.

Parameters:
  • transportdestination – transport to be used for the destination computer
  • remotesource (str) – path to the remote source directory / file
  • remotedestination (str) – path to the remote destination directory / file
  • kwargs – keyword parameters passed to the call to transportdestination.put, except for ‘dereference’ that is passed to self.get

Note

the keyword ‘dereference’ SHOULD be set to False for the final put (onto the destination), while it can be set to the value given in kwargs for the get from the source. In that way, a symbolic link would never be followed in the final copy to the remote destination. That way we could avoid getting unknown (potentially malicious) files into the destination computer. HOWEVER, since dereference=False is currently NOT supported by all plugins, we still force it to True for the final put.

Note

the supported keys in kwargs are callback, dereference, overwrite and ignore_nonexisting.

copyfile(remotesource, remotedestination, *args, **kwargs)[source]

Copy a file from remote source to remote destination (On the same remote machine)

Parameters:
  • remotesource (str) – path of the remote source directory / file
  • remotedestination (str) – path of the remote destination directory / file
Raises:

IOError – if one of src or dst does not exist

copytree(remotesource, remotedestination, *args, **kwargs)[source]

Copy a folder from remote source to remote destination (On the same remote machine)

Parameters:
  • remotesource (str) – path of the remote source directory / file
  • remotedestination (str) – path of the remote destination directory / file
Raises:

IOError – if one of src or dst does not exist

exec_command_wait(command, **kwargs)[source]

Execute the command on the shell, waits for it to finish, and return the retcode, the stdout and the stderr.

Enforce the execution to be run from the pwd (as given by self.getcwd), if this is not None.

Parameters:command (str) – execute the command given as a string
Returns:a list: the retcode (int), stdout (str) and stderr (str).
get(remotepath, localpath, *args, **kwargs)[source]

Retrieve a file or folder from remote source to local destination dst must be an absolute path (src not necessarily)

Parameters:
  • remotepath – (str) remote_folder_path
  • localpath – (str) local_folder_path
get_attribute(path)[source]

Return an object FixedFieldsAttributeDict for file in a given path, as defined in aiida.common.extendeddicts Each attribute object consists in a dictionary with the following keys:

  • st_size: size of files, in bytes
  • st_uid: user id of owner
  • st_gid: group id of owner
  • st_mode: protection bits
  • st_atime: time of most recent access
  • st_mtime: time of most recent modification
Parameters:path (str) – path to file
Returns:object FixedFieldsAttributeDict
get_mode(path)[source]

Return the portion of the file’s mode that can be set by chmod().

Parameters:path (str) – path to file
Returns:the portion of the file’s mode that can be set by chmod()
classmethod get_short_doc()[source]

Return the first non-empty line of the class docstring, if available

classmethod get_valid_auth_params()[source]

Return the internal list of valid auth_params

classmethod get_valid_transports()[source]
Returns:a list of existing plugin names
getcwd()[source]

Get working directory

Returns:a string identifying the current working directory
getfile(remotepath, localpath, *args, **kwargs)[source]

Retrieve a file from remote source to local destination dst must be an absolute path (src not necessarily)

Parameters:
  • remotepath (str) – remote_folder_path
  • localpath (str) – local_folder_path
gettree(remotepath, localpath, *args, **kwargs)[source]

Retrieve a folder recursively from remote source to local destination dst must be an absolute path (src not necessarily)

Parameters:
  • remotepath (str) – remote_folder_path
  • localpath (str) – local_folder_path
glob(pathname)[source]

Return a list of paths matching a pathname pattern.

The pattern may contain simple shell-style wildcards a la fnmatch.

gotocomputer_command(remotedir)[source]

Return a string to be run using os.system in order to connect via the transport to the remote directory.

Expected behaviors:

  • A new bash session is opened
  • A reasonable error message is produced if the folder does not exist
Parameters:remotedir (str) – the full path of the remote directory
iglob(pathname)[source]

Return an iterator which yields the paths matching a pathname pattern.

The pattern may contain simple shell-style wildcards a la fnmatch.

isdir(path)[source]

True if path is an existing directory.

Parameters:path (str) – path to directory
Returns:boolean
isfile(path)[source]

Return True if path is an existing file.

Parameters:path (str) – path to file
Returns:boolean
listdir(path='.', pattern=None)[source]

Return a list of the names of the entries in the given path. The list is in arbitrary order. It does not include the special entries ‘.’ and ‘..’ even if they are present in the directory.

Parameters:
  • path (str) – path to list (default to ‘.’)
  • pattern (str) – if used, listdir returns a list of files matching filters in Unix style. Unix only.
Returns:

a list of strings

logger

Return the internal logger. If you have set extra parameters using _set_logger_extra(), a suitable LoggerAdapter instance is created, bringing with itself also the extras.

makedirs(path, ignore_existing=False)[source]

Super-mkdir; 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.

Parameters:
  • path (str) – directory to create
  • ignore_existing (bool) – if set to true, it doesn’t give any error if the leaf directory does already exist
Raises:

OSError, if directory at path already exists

mkdir(path, ignore_existing=False)[source]

Create a folder (directory) named path.

Parameters:
  • path (str) – name of the folder to create
  • ignore_existing (bool) – if True, does not give any error if the directory already exists
Raises:

OSError, if directory at path already exists

normalize(path='.')[source]

Return the normalized path (on the server) of a given path. This can be used to quickly resolve symbolic links or determine what the server is considering to be the “current folder”.

Parameters:path (str) – path to be normalized
Raises:IOError – if the path can’t be resolved on the server
open()[source]

Opens a local transport channel

path_exists(path)[source]

Returns True if path exists, False otherwise.

put(localpath, remotepath, *args, **kwargs)[source]

Put a file or a directory from local src to remote dst. src must be an absolute path (dst not necessarily)) Redirects to putfile and puttree.

Parameters:
  • localpath (str) – absolute path to local source
  • remotepath (str) – path to remote destination
putfile(localpath, remotepath, *args, **kwargs)[source]

Put a file from local src to remote dst. src must be an absolute path (dst not necessarily))

Parameters:
  • localpath (str) – absolute path to local file
  • remotepath (str) – path to remote file
puttree(localpath, remotepath, *args, **kwargs)[source]

Put a folder recursively from local src to remote dst. src must be an absolute path (dst not necessarily))

Parameters:
  • localpath (str) – absolute path to local folder
  • remotepath (str) – path to remote folder
remove(path)[source]

Remove the file at the given path. This only works on files; for removing folders (directories), use rmdir.

Parameters:path (str) – path to file to remove
Raises:IOError – if the path is a directory
rename(oldpath, newpath)[source]

Rename a file or folder from oldpath to newpath.

Parameters:
  • oldpath (str) – existing name of the file or folder
  • newpath (str) – new name for the file or folder
Raises:
  • IOError – if oldpath/newpath is not found
  • ValueError – if oldpath/newpath is not a valid string
rmdir(path)[source]

Remove the folder named path. This works only for empty folders. For recursive remove, use rmtree.

Parameters:path (str) – absolute path to the folder to remove
rmtree(path)[source]

Remove recursively the content at path

Parameters:path (str) – absolute path to remove

Create a symbolic link between the remote source and the remote destination.

Parameters:
  • remotesource – remote source
  • remotedestination – remote destination
whoami()[source]

Get the remote username

Returns:list of username (str), retval (int), stderr (str)
aiida.transport.__init__.TransportFactory(module)[source]

Used to return a suitable Transport subclass.

Parameters:module (str) – name of the module containing the Transport subclass
Returns:the transport subclass located in module ‘module’
exception aiida.transport.__init__.TransportInternalError[source]

Raised if there is a transport error that is raised to an internal error (e.g. a transport method called without opening the channel first).

aiida.transport.__init__.copy_from_remote_to_remote(transportsource, transportdestination, remotesource, remotedestination, **kwargs)[source]

Copy files or folders from a remote computer to another remote computer.

Parameters:
  • transportsource – transport to be used for the source computer
  • transportdestination – transport to be used for the destination computer
  • remotesource (str) – path to the remote source directory / file
  • remotedestination (str) – path to the remote destination directory / file
  • kwargs – keyword parameters passed to the final put, except for ‘dereference’ that is passed to the initial get

Note

it uses the method transportsource.copy_from_remote_to_remote

Developing a plugin

The transport class is actually almost never used in first person by the user. It is mostly utilized by the ExecutionManager, that use the transport plugin to connect to the remote computer to manage the calculation. The ExecutionManager has to be able to use always the same function, or the same interface, regardless of which kind of connection is actually really using.

The generic transport class contains a set of minimal methods that an implementation must support, in order to be fully compatible with the other plugins. If not, a NotImplementedError will be raised, interrupting the managing of the calculation or whatever is using the transport plugin.

Since it is important that all plugins have the same interface, or the same response behavior, a set of generic tests has been written (alongside with set of tests that are implementation specific). After every modification, or when implementing a new plugin, it is crucial to run the tests and verify that everything is passed. The modification of tests possibly means breaking back-compatibility and/or modifications to every piece of code using a transport plugin.

If an unexpected behavior is observed during the usage, the way of fixing it is:

  1. Write a new test that shows the problem (one test for one problem when possible)
  2. Fix the bug
  3. Verify that the test is passed correctly

The importance of point 1) is often neglected, but unittesting is a useful tool that helps you avoiding the repetition of errors. Despite the appearence, it’s a time-saver! Not only, the tests help you seeing how the plugin is used.

As for the general functioning of the plugin, the __init__ method is used only to initialize the class instance, without actually opening the transport channel. The connection must be opened only by the __enter__ method, (and closed by __exit__. The __enter__ method let you use the transport class using the with statement (see Python docs), in a way similar to the following:

t = TransportPlugin()
with open(t):
    t.do_something_remotely

To ensure this, for example, the local plugin uses a hidden boolean variable _is_open that is set when the __enter__ and __exit__ methods are called. The Ssh logic is instead given by the property sftp.

The other functions that require some care are the copying functions, called using the following terminology:

  1. put: from local source to remote destination
  2. get: from remote source to local destination
  3. copy: copying files from remote source to remote destination

Note that these functions must copy files or folders regardless, internally, they will fallback to functions like putfile or puttree.

The last function requiring care is exec_command_wait, which is an analogue to the subprocess Python module. The function gives the freedom to execute a string as a remote command, thus it could produce nasty effects if not written with care. Be sure to escape any string for bash!

Currently, the implemented plugins are the Local and the Ssh transports. The Local one is simply a wrapper to some standard Python modules, like shutil or os, those functions are simply interfaced in a different way with AiiDA. The SSh instead is an interface to the Paramiko library.

Below, you can find a template to fill for a new transport plugin, with a minimal docstring that also work for the sphinx documentation.

class NewTransport(aiida.transport.Transport):

  def __init__(self, machine, **kwargs):
      """
      Initialize the Transport class.

      :param machine: the machine to connect to
      """

  def __enter__(self):
      """
      Open the connection
      """

  def __exit__(self, type, value, traceback):
      """
      Close the connection
      """

  def chdir(self,path):
      """
      Change directory to 'path'

      :param str path: path to change working directory into.
      :raises: IOError, if the requested path does not exist
      :rtype: string
      """

  def chmod(self,path,mode):
      """
      Change permissions of a path.

      :param str path: path to file
      :param int mode: new permissions
      """

  def copy(self,remotesource,remotedestination,*args,**kwargs):
      """
      Copy a file or a directory from remote source to remote destination
      (On the same remote machine)

      :param str remotesource: path of the remote source directory / file
      :param str remotedestination: path of the remote destination directory / file

      :raises: IOError, if source or destination does not exist
      """
      raise NotImplementedError

  def copyfile(self,remotesource,remotedestination,*args,**kwargs):
      """
      Copy a file from remote source to remote destination
      (On the same remote machine)

      :param str remotesource: path of the remote source directory / file
      :param str remotedestination: path of the remote destination directory / file

      :raises IOError: if one of src or dst does not exist
      """

  def copytree(self,remotesource,remotedestination,*args,**kwargs):
      """
      Copy a folder from remote source to remote destination
      (On the same remote machine)

      :param str remotesource: path of the remote source directory / file
      :param str remotedestination: path of the remote destination directory / file

      :raise IOError: if one of src or dst does not exist
      """

  def exec_command_wait(self,command, **kwargs):
      """
      Execute the command on the shell, waits for it to finish,
      and return the retcode, the stdout and the stderr.

      Enforce the execution to be run from the pwd (as given by
      self.getcwd), if this is not None.

      :param str command: execute the command given as a string
      :return: a tuple: the retcode (int), stdout (str) and stderr (str).
      """

  def get_attribute(self,path):
      """
      Return an object FixedFieldsAttributeDict for file in a given path,
      as defined in aiida.common.extendeddicts
      Each attribute object consists in a dictionary with the following keys:

      * st_size: size of files, in bytes

      * st_uid: user id of owner

      * st_gid: group id of owner

      * st_mode: protection bits

      * st_atime: time of most recent access

      * st_mtime: time of most recent modification

      :param str path: path to file
      :return: object FixedFieldsAttributeDict
      """

  def getcwd(self):
      """
      Get working directory

      :return: a string identifying the current working directory
      """

  def get(self, remotepath, localpath, *args, **kwargs):
      """
      Retrieve a file or folder from remote source to local destination
      dst must be an absolute path (src not necessarily)

      :param remotepath: (str) remote_folder_path
      :param localpath: (str) local_folder_path
      """

  def getfile(self, remotepath, localpath, *args, **kwargs):
      """
      Retrieve a file from remote source to local destination
      dst must be an absolute path (src not necessarily)

      :param str remotepath: remote_folder_path
      :param str localpath: local_folder_path
      """

  def gettree(self, remotepath, localpath, *args, **kwargs):
      """
      Retrieve a folder recursively from remote source to local destination
      dst must be an absolute path (src not necessarily)

      :param str remotepath: remote_folder_path
      :param str localpath: local_folder_path
      """

  def gotocomputer_command(self, remotedir):
      """
      Return a string to be run using os.system in order to connect
      via the transport to the remote directory.

      Expected behaviors:

      * A new bash session is opened

      * A reasonable error message is produced if the folder does not exist

      :param str remotedir: the full path of the remote directory
      """

  def isdir(self,path):
      """
      True if path is an existing directory.

      :param str path: path to directory
      :return: boolean
      """

  def isfile(self,path):
      """
      Return True if path is an existing file.

      :param str path: path to file
      :return: boolean
      """

  def listdir(self, path='.',pattern=None):
      """
      Return a list of the names of the entries in the given path.
      The list is in arbitrary order. It does not include the special
      entries '.' and '..' even if they are present in the directory.

      :param str path: path to list (default to '.')
      :param str pattern: if used, listdir returns a list of files matching
                          filters in Unix style. Unix only.
      :return: a list of strings
      """

  def makedirs(self,path,ignore_existing=False):
      """
      Super-mkdir; 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.

      :param str path: directory to create
      :param bool ignore_existing: if set to true, it doesn't give any error
                                   if the leaf directory does already exist

      :raises: OSError, if directory at path already exists
      """

  def mkdir(self,path,ignore_existing=False):
      """
      Create a folder (directory) named path.

      :param str path: name of the folder to create
      :param bool ignore_existing: if True, does not give any error if the
                                   directory already exists

      :raises: OSError, if directory at path already exists
      """

  def normalize(self,path='.'):
      """
      Return the normalized path (on the server) of a given path.
      This can be used to quickly resolve symbolic links or determine
      what the server is considering to be the "current folder".

      :param str path: path to be normalized

      :raise IOError: if the path can't be resolved on the server
      """

  def put(self, localpath, remotepath, *args, ** kwargs):
      """
      Put a file or a directory from local src to remote dst.
      src must be an absolute path (dst not necessarily))
      Redirects to putfile and puttree.

      :param str localpath: path to remote destination
      :param str remotepath: absolute path to local source
      """

  def putfile(self, localpath, remotepath, *args, ** kwargs):
      """
      Put a file from local src to remote dst.
      src must be an absolute path (dst not necessarily))

      :param str localpath: path to remote file
      :param str remotepath: absolute path to local file
      """

  def puttree(self, localpath, remotepath, *args, ** kwargs):
      """
      Put a folder recursively from local src to remote dst.
      src must be an absolute path (dst not necessarily))

      :param str localpath: path to remote folder
      :param str remotepath: absolute path to local folder
      """

 def rename(src,dst):
      """
      Rename a file or folder from src to dst.

      :param str oldpath: existing name of the file or folder
      :param str newpath: new name for the file or folder

      :raises IOError: if src/dst is not found
      :raises ValueError: if src/dst is not a valid string
      """

  def remove(self,path):
      """
      Remove the file at the given path. This only works on files;
      for removing folders (directories), use rmdir.

      :param str path: path to file to remove

      :raise IOError: if the path is a directory
      """

  def rmdir(self,path):
      """
      Remove the folder named path.
      This works only for empty folders. For recursive remove, use rmtree.

      :param str path: absolute path to the folder to remove
      """
      raise NotImplementedError

  def rmtree(self,path):
      """
      Remove recursively the content at path

      :param str path: absolute path to remove
      """