Restore previous revision of Conda environment according to most recent
    action in :attr:`MICRODROP_CONDA_ACTIONS`.

    .. versionchanged:: 0.18
        Add support for action revision files compressed using ``bz2``.

    .. versionchanged:: 0.24
        Remove channels argument.  Use Conda channels as configured in Conda
        environment.

        Note that channels can still be explicitly set through :data:`*args`.

    Parameters
    ----------
    *args
        Extra arguments to pass to Conda ``install`` roll-back command.

    Returns
    -------
    int, dict
        Revision after roll back and Conda installation log object (from JSON
        Conda install output).

    See also
    --------

    `wheeler-microfluidics/microdrop#200 <https://github.com/wheeler-microfluidics/microdrop/issues/200>`

def rollback(*args, **kwargs):
    '''
    Restore previous revision of Conda environment according to most recent
    action in :attr:`MICRODROP_CONDA_ACTIONS`.

    .. versionchanged:: 0.18
        Add support for action revision files compressed using ``bz2``.

    .. versionchanged:: 0.24
        Remove channels argument.  Use Conda channels as configured in Conda
        environment.

        Note that channels can still be explicitly set through :data:`*args`.

    Parameters
    ----------
    *args
        Extra arguments to pass to Conda ``install`` roll-back command.

    Returns
    -------
    int, dict
        Revision after roll back and Conda installation log object (from JSON
        Conda install output).

    See also
    --------

    `wheeler-microfluidics/microdrop#200 <https://github.com/wheeler-microfluidics/microdrop/issues/200>`
    '''
    action_files = MICRODROP_CONDA_ACTIONS.files()
    if not action_files:
        # No action files, return current revision.
        logger.debug('No rollback actions have been recorded.')
        revisions_js = ch.conda_exec('list', '--revisions', '--json',
                                     verbose=False)
        revisions = json.loads(revisions_js)
        return revisions[-1]['rev']
    # Get file associated with most recent action.
    cre_rev = re.compile(r'rev(?P<rev>\d+)')
    action_file = sorted([(int(cre_rev.match(file_i.namebase).group('rev')),
                           file_i) for file_i in
                          action_files if cre_rev.match(file_i.namebase)],
                         reverse=True)[0]
    # Do rollback (i.e., install state of previous revision).
    if action_file.ext.lower() == '.bz2':
        # Assume file is compressed using bz2.
        with bz2.BZ2File(action_file, mode='r') as input_:
            action = json.load(input_)
    else:
        # Assume it is raw JSON.
        with action_file.open('r') as input_:
            action = json.load(input_)
    rollback_revision = action['revisions'][-2]
    conda_args = (['install', '--json'] + list(args) +
                  ['--revision', str(rollback_revision)])
    install_log_js = ch.conda_exec(*conda_args, verbose=False)
    install_log = json.loads(install_log_js.split('\x00')[-1])
    logger.debug('Rolled back to revision %s', rollback_revision)
    return rollback_revision, install_log