Update the portage tree using the first available method from the following
    list:

    - emaint sync
    - eix-sync
    - emerge-webrsync
    - emerge --sync

    To prevent the portage tree from being synced within one day of the
    previous sync, add the following pillar data for this minion:

    .. code-block:: yaml

        portage:
          sync_wait_one_day: True

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.refresh_db

def refresh_db(**kwargs):
    '''
    Update the portage tree using the first available method from the following
    list:

    - emaint sync
    - eix-sync
    - emerge-webrsync
    - emerge --sync

    To prevent the portage tree from being synced within one day of the
    previous sync, add the following pillar data for this minion:

    .. code-block:: yaml

        portage:
          sync_wait_one_day: True

    CLI Example:

    .. code-block:: bash

        salt '*' pkg.refresh_db
    '''
    has_emaint = os.path.isdir('/etc/portage/repos.conf')
    has_eix = True if 'eix.sync' in __salt__ else False
    has_webrsync = True if __salt__['makeconf.features_contains']('webrsync-gpg') else False

    # Remove rtag file to keep multiple refreshes from happening in pkg states
    salt.utils.pkg.clear_rtag(__opts__)
    # Option to prevent syncing package tree if done in the last 24 hours
    if __salt__['pillar.get']('portage:sync_wait_one_day', False):
        main_repo_root = __salt__['cmd.run']('portageq get_repo_path / gentoo')
        day = datetime.timedelta(days=1)
        now = datetime.datetime.now()
        timestamp = datetime.datetime.fromtimestamp(os.path.getmtime(main_repo_root))
        if now - timestamp < day:
            log.info('Did not sync package tree since last sync was done at'
                     ' %s, less than 1 day ago', timestamp)
            return False

    if has_emaint:
        return __salt__['cmd.retcode']('emaint sync -a') == 0
    elif has_eix:
        return __salt__['eix.sync']()
    elif has_webrsync:
        # GPG sign verify is supported only for 'webrsync'
        cmd = 'emerge-webrsync -q'
        # Prefer 'delta-webrsync' to 'webrsync'
        if salt.utils.path.which('emerge-delta-webrsync'):
            cmd = 'emerge-delta-webrsync -q'
        return __salt__['cmd.retcode'](cmd) == 0
    else:
        # Default to deprecated `emerge --sync` form
        return __salt__['cmd.retcode']('emerge --ask n --quiet --sync') == 0