<commit_before>#!/usr/bin/env python

import argparse
import getpass
import logging
import netrc
import os
import platform
import subprocess
import sys
import time
import traceback
from distutils.spawn import find_executable
from mailer import Mailer
from mailer import Message
from pysphere import VIServer


class BackupDiskMntState(Exception):
    pass

class CheckBDSError(Exception):
    pass

class FindExeError(Exception):
    pass

class PowerState(Exception):
    pass

class ExistingExport(Exception):
    pass


def sync_buffers():
    """Force changed blocks to disk."""
    subprocess.check_call(sync)
    logging.info('Sync buffers successful.')
    return


def wakeup_removeable(backupdisk):
    """Try to wake up removeable disk if it has gone to sleep."""
    dev = None
    for line in open('/proc/mounts'):
        if line.split()[1] == backupdisk:
            dev = line.split()[0]
            subprocess.check_call([sg_start, '--start', dev])
            logging.info('sg_start told %s to wake up.', dev)
            break
    return


def mnt_removeable(backupdisk):
    """Mount removeable disk."""
    if os.path.ismount(backupdisk):
        raise BackupDiskMntState('%s already mounted, is this expected?' %(backupdisk))
    else:
        subprocess.check_call([mount, backupdisk])
        logging.info('Mounted %s successfully.', backupdisk)        
        return False


def check_bds(bdspath):
    """Test BDS dir exists and is rw on removeable disk."""
    if not os.path.isdir(bdspath):
        raise CheckBDSError('%s not found.' %(bdspath))
    if not os.access(bdspath, os.W_OK|os.R_OK):
        raise CheckBDSError('%s is not writeable.' %(bdspath))
    logging.info('Backup disk validation passed.')
    return


def unmnt_removeable(backupdisk):
    """
    Unmount removeable disk, if it fails to unmount then retry 5 times
    60 seconds apart, then raise exception.
    """
    unmount_success = False
    retry = 5
    if not os.path.ismount(backupdisk):
        raise BackupDiskMntState('%s not mounted, is this expected?' %(backupdisk))
    else:
        while not unmount_success:
            try:
                # may need to put this in stop function alone?  Or
                # before unexport_bds?
                subprocess.check_call([umount, backupdisk])
                logging.info('Unmounted %s successfully.', backupdisk)
                return
            except subprocess.CalledProcessError as e:
                logging.warning(e)
                retry = retry - 1
                if retry == 0:
                    raise
                time.sleep(60)
                pass


def export_bds(avbaserver, bdspath, nfsopts):
    """Export BDS path to Archive VBA."""
    #https://bugzilla.redhat.com/show_bug.cgi?id=966237
    n = subprocess.check_output([showmount, '-e', '--no-headers']).splitlines()
    nfsmounts = [ x.split() for x in n ]
    for item in nfsmounts:
        if item[0] == bdspath:
            try:
                raise ExistingExport('%s is already exported according to showmount -e!' %(bdspath))
            finally:
                subprocess.check_call([exportfs, '-r'])
                logging.warning('Reexport all directories completed.')
    else:
        subprocess.check_call([exportfs, '-o', nfsopts, avbaserver + ':' + bdspath])
        logging.info('Exported %s successfully to %s', bdspath, avbaserver)
        return


def unexport_bds(avbaserver, bdspath):
    """Unexport BDS path to Archive VBA."""
    n = subprocess.check_output([showmount, '-e', '--no-headers']).splitlines()
    nfsmounts = [ x.split() for x in n ]
    for item in nfsmounts:
        if item[0] == bdspath:
            """Give the VBA some time to shutdown before unexporting bds."""
            time.sleep(30)
            subprocess.check_call([exportfs, '-u', avbaserver + ':' + bdspath])
            logging.info('Unexported %s successfully from %s', bdspath, avbaserver)
            return
    else:
        raise ExistingExport('%s not exported according to showmount -e!' %(bdspath))


def connect_viserver(viserver, username, password):
    """Connect to vCenter/ESX(i) and return an authtoken."""
    server = VIServer()
    server.connect(viserver, username, password)
    logging.info('Connected to %s successfully', viserver)
    return server

<commit_msg>Add logging for sleep in unexport_bds<commit_after>#!/usr/bin/env python

import argparse
import getpass
import logging
import netrc
import os
import platform
import subprocess
import sys
import time
import traceback
from distutils.spawn import find_executable
from mailer import Mailer
from mailer import Message
from pysphere import VIServer


class BackupDiskMntState(Exception):
    pass

class CheckBDSError(Exception):
    pass

class FindExeError(Exception):
    pass

class PowerState(Exception):
    pass

class ExistingExport(Exception):
    pass


def sync_buffers():
    """Force changed blocks to disk."""
    subprocess.check_call(sync)
    logging.info('Sync buffers successful.')
    return


def wakeup_removeable(backupdisk):
    """Try to wake up removeable disk if it has gone to sleep."""
    dev = None
    for line in open('/proc/mounts'):
        if line.split()[1] == backupdisk:
            dev = line.split()[0]
            subprocess.check_call([sg_start, '--start', dev])
            logging.info('sg_start told %s to wake up.', dev)
            break
    return


def mnt_removeable(backupdisk):
    """Mount removeable disk."""
    if os.path.ismount(backupdisk):
        raise BackupDiskMntState('%s already mounted, is this expected?' %(backupdisk))
    else:
        subprocess.check_call([mount, backupdisk])
        logging.info('Mounted %s successfully.', backupdisk)        
        return False


def check_bds(bdspath):
    """Test BDS dir exists and is rw on removeable disk."""
    if not os.path.isdir(bdspath):
        raise CheckBDSError('%s not found.' %(bdspath))
    if not os.access(bdspath, os.W_OK|os.R_OK):
        raise CheckBDSError('%s is not writeable.' %(bdspath))
    logging.info('Backup disk validation passed.')
    return


def unmnt_removeable(backupdisk):
    """
    Unmount removeable disk, if it fails to unmount then retry 5 times
    60 seconds apart, then raise exception.
    """
    unmount_success = False
    retry = 5
    if not os.path.ismount(backupdisk):
        raise BackupDiskMntState('%s not mounted, is this expected?' %(backupdisk))
    else:
        while not unmount_success:
            try:
                # may need to put this in stop function alone?  Or
                # before unexport_bds?
                subprocess.check_call([umount, backupdisk])
                logging.info('Unmounted %s successfully.', backupdisk)
                return
            except subprocess.CalledProcessError as e:
                logging.warning(e)
                retry = retry - 1
                if retry == 0:
                    raise
                time.sleep(60)
                pass


def export_bds(avbaserver, bdspath, nfsopts):
    """Export BDS path to Archive VBA."""
    #https://bugzilla.redhat.com/show_bug.cgi?id=966237
    n = subprocess.check_output([showmount, '-e', '--no-headers']).splitlines()
    nfsmounts = [ x.split() for x in n ]
    for item in nfsmounts:
        if item[0] == bdspath:
            try:
                raise ExistingExport('%s is already exported according to showmount -e!' %(bdspath))
            finally:
                subprocess.check_call([exportfs, '-r'])
                logging.warning('Reexport all directories completed.')
    else:
        subprocess.check_call([exportfs, '-o', nfsopts, avbaserver + ':' + bdspath])
        logging.info('Exported %s successfully to %s', bdspath, avbaserver)
        return


def unexport_bds(avbaserver, bdspath):
    """Unexport BDS path to Archive VBA."""
    secs = 30
    n = subprocess.check_output([showmount, '-e', '--no-headers']).splitlines()
    nfsmounts = [ x.split() for x in n ]
    for item in nfsmounts:
        if item[0] == bdspath:
            """Give the VBA some time to shutdown before unexporting bds."""
            logging.info('Sleeping %s seconds before unexporting BDS', secs)
            time.sleep(secs)
            subprocess.check_call([exportfs, '-u', avbaserver + ':' + bdspath])
            logging.info('Unexported %s successfully from %s', bdspath, avbaserver)
            return
    else:
        raise ExistingExport('%s not exported according to showmount -e!' %(bdspath))


def connect_viserver(viserver, username, password):
    """Connect to vCenter/ESX(i) and return an authtoken."""
    server = VIServer()
    server.connect(viserver, username, password)
    logging.info('Connected to %s successfully', viserver)
    return server


{'fraction_non_alphanumeric': 0.10713067212555442, 'fraction_numerical': 0.004549073126350506, 'mean_word_length': 2.596728016359918, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1400039', 'n_tokens_mistral': 2718, 'n_tokens_neox': 2602, 'n_words': 766}