Пример #1
0
def store_data_to_temp_file(data):
    temp_file = tempfile.NamedTemporaryFile(delete=False)
    logger.debug("Writing data: ")  # + repr(data))
    logger.debug("To temporary file: " + temp_file.name)
    temp_file.write(data)
    temp_file.close()
    return temp_file.name
Пример #2
0
    def __init__(self, modulePath):
        """
        Initializes internal variables.

        Parameters:
        1. modulePath (str): path to the module to be loaded.

        Attributes:
        1. path (str): path to the module loaded.
        2. name (str): name of the module loaded.
        3. fd (fd): File Descriptor of the module loaded
        4. handle (obj): Handle to the object loaded
        """
        if not c_path.validate_file(modulePath):
            raise AttributeError('Module does not exist')

        moduleName, extention = os.path.splitext(os.path.basename(modulePath))
        moduleFd = open(modulePath, 'r')
        try:
            moduleHandle = imp.load_source(moduleName, modulePath, moduleFd)
        except Exception:
            logger.debug(traceback.format_exc())
            moduleFd.close()
            raise

        self.path = modulePath
        self.name = moduleName
        self.fd = moduleFd
        self.handle = moduleHandle
Пример #3
0
    def __init__(self, modulePath):
        """
        Initializes internal variables.

        Parameters:
        1. modulePath (str): path to the module to be loaded.

        Attributes:
        1. path (str): path to the module loaded.
        2. name (str): name of the module loaded.
        3. fd (fd): File Descriptor of the module loaded
        4. handle (obj): Handle to the object loaded
        """
        if not c_path.validate_file(modulePath):
            raise AttributeError('Module does not exist')

        moduleName, extention = os.path.splitext(os.path.basename(modulePath))
        moduleFd = open(modulePath, 'r')
        try:
            moduleHandle = imp.load_source(moduleName, modulePath, moduleFd)
        except Exception:
            logger.debug(traceback.format_exc())
            moduleFd.close()
            raise

        self.path = modulePath
        self.name = moduleName
        self.fd = moduleFd
        self.handle = moduleHandle
Пример #4
0
def zipDir(directory, zipfilename):
    """create zip package from files in directory"""
    if c_path.validate_dir_write(directory):
        zip_handler = zipfile.ZipFile(zipfilename, 'w')
        try:
            for root, dirs, files in os.walk(directory):
                for entry in files:
                    if not entry.endswith('.zip'):
                        logger.debug('add to zip: ' + entry)
                        zip_handler.write(c_path.join(root, entry), entry)
        finally:
            zip_handler.close()
    else:
        raise RuntimeError('cannot write to directory: ' + str(directory))
Пример #5
0
def backup_file(filePath, maxBackups=10):
    """
    Create backup for the file.
        File.txt -> File_1.txt

    Parameters:
    1. filePath: File to be backed up
    2. maxBackups: Maximum number of backups to create in the location

    Return:
    1. returnValue: True if file back up is successful
    """
    returnValue = False
    filename, extention = os.path.splitext(filePath)

    if c_path.validate_file(filePath) and c_path.validate_file_write(filePath):
        for index in reversed(range(0, maxBackups)):
            backup_file_path = filename + '_{0}'.format(index + 1) + extention
            origFile = filename + '_{0}'.format(index) + extention
            if c_path.validate_file(backup_file_path):
                try:
                    os.remove(backup_file_path)
                except Exception:
                    logger.debug(traceback.format_exc())
                    raise CoreError(
                        CoreErrorCode.GENERIC_FAILURE,
                        'Removing file: {0}'.format(sys.exc_info()[1]))
            if c_path.validate_file(origFile):
                try:
                    os.rename(origFile, backup_file_path)
                except Exception:
                    logger.debug(traceback.format_exc())
                    raise CoreError(
                        CoreErrorCode.GENERIC_FAILURE,
                        'Renaming file: {0}'.format(sys.exc_info()[1]))

        backup_file_path = filename + '_{0}'.format(1) + extention
        f_retValue, f_retErr = c_path.copyFile(filePath,
                                               backup_file_path,
                                               force=True)
        if not f_retValue:
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                            'Backing up: {0}'.format(f_retErr))
        else:
            returnValue = True
    return returnValue
Пример #6
0
def run_command(cmd, expected_retcode=0, large_output=False, log=True):
    if log:
        logger.debug('Running command: ' + str(cmd))
    if large_output:
        try:
            retcode = 0
            stderr_data = ""
            output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            output = e.output
            retcode = e.returncode
            stderr_data = output
    else:
        retcode, output, stderr_data = CoreSubprocess.simpleExecuteCommand(cmd)
    if retcode != expected_retcode:
        raise RuntimeError(cmd[0] + ' retcode: ' + str(retcode) +
                           '\nOutput: ' + stderr_data)
    return output
Пример #7
0
def clean_dir(path, timer=30, recreate=True, suppress_warning=False):
    """
    Removes all the files from the given directory.
    IMPORTANT: Use this method with caution.

    Parameters:
    1. Path (str): The directory that needs to be cleared of files.

    Returns:
    1. returnValue (bool): True
    2. returnError (str): Error if the cleaning of the directory failed
    """

    path = normalize(path)
    if not path:
        return False, 'Invalid path given'

    # Notify user if directory contains files
    if validate_dir(path):
        if not len(os.listdir(path)):
            return True, ''

        if not (timer == 0 or suppress_warning):
            if not logger.warningTimerContinue(
                    'Cleaning directory with existing files: "{0}"'.format(
                        path),
                    timer=timer):
                return False, 'User canceled cleanup'

    # Try three times to clean the directory
    returnValue = False
    returnError = ''
    for i in range(0, 3):
        if i:
            time.sleep(i)

        # Try deleting the directory
        try:
            shutil.rmtree(path, ignore_errors=False, onerror=_handler_readonly)
        except OSError as (err, strerror):
            pass
        except Exception:
            logger.debug(traceback.format_exc())
            returnError += '{0}\n'.format(sys.exc_info()[1])
Пример #8
0
def backup_file(filePath, maxBackups=10):
    """
    Create backup for the file.
        File.txt -> File_1.txt

    Parameters:
    1. filePath: File to be backed up
    2. maxBackups: Maximum number of backups to create in the location

    Return:
    1. returnValue: True if file back up is successful
    """
    returnValue = False
    filename, extention = os.path.splitext(filePath)

    if c_path.validate_file(filePath) and c_path.validate_file_write(filePath):
        for index in reversed(range(0, maxBackups)):
            backup_file_path = filename + '_{0}'.format(index + 1) + extention
            origFile = filename + '_{0}'.format(index) + extention
            if c_path.validate_file(backup_file_path):
                try:
                    os.remove(backup_file_path)
                except Exception:
                    logger.debug(traceback.format_exc())
                    raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                            'Removing file: {0}'.format(sys.exc_info()[1]))
            if c_path.validate_file(origFile):
                try:
                    os.rename(origFile, backup_file_path)
                except Exception:
                    logger.debug(traceback.format_exc())
                    raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                            'Renaming file: {0}'.format(sys.exc_info()[1]))

        backup_file_path = filename + '_{0}'.format(1) + extention
        f_retValue, f_retErr = c_path.copyFile(filePath, backup_file_path,
                                                     force=True)
        if not f_retValue:
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                            'Backing up: {0}'.format(f_retErr))
        else:
            returnValue = True
    return returnValue
Пример #9
0
def clean_dir(path, timer=30):
    """
    Removes all the files from the given directory.
    IMPORTANT: Use this method with caution.

    Parameters:
    1. Path (str): The directory that needs to be cleared of files.

    Returns:
    1. returnValue (bool): True
    2. returnError (str): Error if the cleaning of the directory failed
    """

    path = normalize(path)
    if not path:
        return False, 'Invalid path given'

    # Notify user if directory contains files
    if validate_dir(path):
        if not len(os.listdir(path)):
            return True, ''

        if not logger.warningTimerContinue('Cleaning directory with existing files: "{0}"'.format(path), timer=timer):
            return False, 'User canceled cleanup'

    # Try three times to clean the directory
    returnValue = False
    returnError = ''
    for i in range (0, 3):
        if i:
            time.sleep(i)

        # Try deleting the directory
        try:
            shutil.rmtree(path, ignore_errors=False, onerror=_handler_readonly)
        except OSError as (err, strerror):
            pass
        except Exception:
            logger.debug(traceback.format_exc())
            returnError += '{0}\n'.format(sys.exc_info()[1])
Пример #10
0
def compareFileContents(file1=None, file2=None, file1_contents=None,
                        file2_contents=None):
    """
    Compare the contents of two files.
    IMP: Only one of file1 and file1_contents should be provided.
    IMP: Only one of file2 and file2_contents should be provided.

    Parameters:
    1. file1: File 1 to be compared
    2. file2: File 2 to be compared
    3. file1_contents: Contents of file 1 to be used for comparison.
    4. file2_contents: Contents of file 2 to be used for comparison.

    Return:
    1. returnValue: True if files match
    """
    returnValue = False

    if not ((file1 is None) ^ (file1_contents is None)):
        raise AttributeError('One of file1 or file1_contents must be given')
    if not ((file2 is None) ^ (file2_contents is None)):
        raise AttributeError('One of file2 or file2_contents must be given')

    if file1_contents is None:
        try:
            fd = open(file1, 'r')
        except Exception:
            logger.debug(traceback.format_exc())
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                                'Opening file1: {0}'.format(sys.exc_info()[1]))
        else:
            file1_contents = fd.read()
            fd.close()

    if file2_contents is None:
        try:
            fd = open(file2, 'r')
        except Exception:
            logger.debug(traceback.format_exc())
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                                'Opening file2: {0}'.format(sys.exc_info()[1]))
        else:
            file2_contents = fd.read()
            fd.close()

    if file1_contents is not None and file2_contents is not None:
        try:
            hashtotal_file1 = hashlib.md5()
            hashtotal_file2 = hashlib.md5()
            hashtotal_file1.update(file1_contents)
            hashtotal_file2.update(file2_contents)
            returnValue = hashtotal_file1.hexdigest() == hashtotal_file2.hexdigest()
        except Exception:
            logger.debug(traceback.format_exc())
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                                'Getting hash: {0}'.format(sys.exc_info()[1]))

    return returnValue
def compareFileContents(file1=None, file2=None, file1_contents=None,
                        file2_contents=None):
    """
    Compare the contents of two files.
    IMP: Only one of file1 and file1_contents should be provided.
    IMP: Only one of file2 and file2_contents should be provided.

    Parameters:
    1. file1: File 1 to be compared
    2. file2: File 2 to be compared
    3. file1_contents: Contents of file 1 to be used for comparison.
    4. file2_contents: Contents of file 2 to be used for comparison.

    Return:
    1. returnValue: True if files match
    """
    returnValue = False

    if not ((file1 is None) ^ (file1_contents is None)):
        raise AttributeError('One of file1 or file1_contents must be given')
    if not ((file2 is None) ^ (file2_contents is None)):
        raise AttributeError('One of file2 or file2_contents must be given')

    if file1_contents is None:
        try:
            fd = open(file1, 'r')
        except Exception:
            logger.debug(traceback.format_exc())
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                                'Opening file1: {0}'.format(sys.exc_info()[1]))
        else:
            file1_contents = fd.read()
            fd.close()

    if file2_contents is None:
        try:
            fd = open(file2, 'r')
        except Exception:
            logger.debug(traceback.format_exc())
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                                'Opening file2: {0}'.format(sys.exc_info()[1]))
        else:
            file2_contents = fd.read()
            fd.close()

    if file1_contents is not None and file2_contents is not None:
        try:
            hashtotal_file1 = hashlib.md5()
            hashtotal_file2 = hashlib.md5()
            hashtotal_file1.update(file1_contents)
            hashtotal_file2.update(file2_contents)
            returnValue = hashtotal_file1.hexdigest() == hashtotal_file2.hexdigest()
        except Exception:
            logger.debug(traceback.format_exc())
            raise CoreError(CoreErrorCode.GENERIC_FAILURE,
                                'Getting hash: {0}'.format(sys.exc_info()[1]))

    return returnValue
            returnError += '{0}\n'.format(sys.exc_info()[1])

        # Verify directory deletion
        if validate_dir(path):
            returnError += 'Directory deletion failed\n'
            continue

        # Create directory
        try:
            os.makedirs(path)
        except OSError as (err, strerror):
            returnError += '{0}\n'.format(strerror)
            returnError += 'Directory creation failed\n'
            continue
        except Exception:
            logger.debug(traceback.format_exc())
            returnError += '{0}\n'.format(sys.exc_info()[1])

        # Verify directory creation
        if not validate_dir_write(path):
            returnError += 'Directory creation failed\n'
            continue

        returnValue = True
        break

    return returnValue, returnError.strip()


def validate_file(path):
    """ Returns True if path is an existing file and read access is available """
Пример #13
0
    def sendMail(cls,
                 sender,
                 recipients,
                 subject,
                 body,
                 filesList=[],
                 recipients_cc=[],
                 recipients_bcc=[]):
        """
        Parameters:
        1. sender (str): Email address or name to be specified as the sender.
        2. recipients (str): Comma separated email addresses for "to".
        3. subject (str): Subject of the email.
        4. body (str): Body of the email.
        5. filesList (str): List of file paths to attach in the email.
        6. recipients_cc (str): Comma separated email addresses for "cc".
        7. recipients_bcc (str): Comma separated email addresses for "bcc".

        Return:
        1. returnValue (bool): success/failure
        2. returnError (str): error if sending email failed.
        """

        returnValue = True
        returnError = ''

        # Append the sender's machine name to the end of the email.
        body = ''.join([
            body, '\n',
            'This is an automated message from "{0}"'.format(platform.node())
        ])

        # Create the multi-part email message
        msg = MIMEMultipart()
        msg['Subject'] = subject
        msg['From'] = sender
        msg['To'] = COMMASPACE.join(recipients)
        msg['Cc'] = COMMASPACE.join(recipients_cc)
        msg['Bcc'] = COMMASPACE.join(recipients_bcc)
        msg.attach(MIMEText(body))

        for eachFile in filesList:
            try:
                part = MIMEBase('application', "octet-stream")
                part.set_payload(open(eachFile, "rb").read())
                Encoders.encode_base64(part)
                part.add_header(
                    'Content-Disposition',
                    'attachment; filename="%s"' % os.path.basename(eachFile))
                msg.attach(part)
            except Exception:
                logger.debug(traceback.format_exc())
                logger.warning(
                    'Could not attach file to email: {0}'.format(eachFile))

        # Create connection to the SMTP server and send the email
        try:
            server = smtplib.SMTP('qcmail1.qualcomm.com')
        except Exception:
            logger.debug(traceback.format_exc())
            returnValue = False
            returnError = sys.exc_info()[1]
        else:
            server.starttls()
            try:
                server.sendmail(sender,
                                recipients + recipients_cc + recipients_bcc,
                                msg.as_string())
            except Exception:
                logger.debug(traceback.format_exc())
                returnValue = False
                returnError = sys.exc_info()[1]
            server.quit()

        return returnValue, returnError
Пример #14
0
            returnError += '{0}\n'.format(sys.exc_info()[1])

        # Verify directory deletion
        if validate_dir(path):
            returnError += 'Directory deletion failed\n'
            continue

        # Create directory
        try:
            os.makedirs(path)
        except OSError as (err, strerror):
            returnError += '{0}\n'.format(strerror)
            returnError += 'Directory creation failed\n'
            continue
        except Exception:
            logger.debug(traceback.format_exc())
            returnError += '{0}\n'.format(sys.exc_info()[1])

        # Verify directory creation
        if not validate_dir_write(path):
            returnError += 'Directory creation failed\n'
            continue

        returnValue = True
        break

    return returnValue, returnError.strip()

def validate_file(path):
    """ Returns True if path is an existing file and read access is available """
    return (os.access(path, os.F_OK or os.R_OK) and os.path.isfile(path))
Пример #15
0
    def sendMail(cls, sender, recipients, subject, body, filesList=[], recipients_cc=[], recipients_bcc=[]):
        """
        Parameters:
        1. sender (str): Email address or name to be specified as the sender.
        2. recipients (str): Comma separated email addresses for "to".
        3. subject (str): Subject of the email.
        4. body (str): Body of the email.
        5. filesList (str): List of file paths to attach in the email.
        6. recipients_cc (str): Comma separated email addresses for "cc".
        7. recipients_bcc (str): Comma separated email addresses for "bcc".

        Return:
        1. returnValue (bool): success/failure
        2. returnError (str): error if sending email failed.
        """

        returnValue = True
        returnError = ''

        # Append the sender's machine name to the end of the email.
        body = ''.join([
                        body,
                        '\n',
                        'This is an automated message from "{0}"'.format(platform.node())
                        ])

        # Create the multi-part email message
        msg = MIMEMultipart()
        msg['Subject'] = subject
        msg['From'] = sender
        msg['To'] = COMMASPACE.join(recipients)
        msg['Cc'] = COMMASPACE.join(recipients_cc)
        msg['Bcc'] = COMMASPACE.join(recipients_bcc)
        msg.attach(MIMEText(body))

        for eachFile in filesList:
            try:
                part = MIMEBase('application', "octet-stream")
                part.set_payload(open(eachFile, "rb").read())
                Encoders.encode_base64(part)
                part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(eachFile))
                msg.attach(part)
            except Exception:
                logger.debug(traceback.format_exc())
                logger.warning('Could not attach file to email: {0}'.format(eachFile))

        # Create connection to the SMTP server and send the email
        try:
            server = smtplib.SMTP('qcmail1.qualcomm.com')
        except Exception:
            logger.debug(traceback.format_exc())
            returnValue = False
            returnError = sys.exc_info()[1]
        else:
            server.starttls()
            try:
                server.sendmail(sender, recipients + recipients_cc + recipients_bcc, msg.as_string())
            except Exception:
                logger.debug(traceback.format_exc())
                returnValue = False
                returnError = sys.exc_info()[1]
            server.quit()

        return returnValue, returnError