Beispiel #1
0
def diff_files(filen1, filen2, verbose=False):
    """Check that the MD5 sums of two files match

    This compares two files by computing the MD5 sums for each.

    Arguments:
      filen1: "source" file
      filen2: "target" file to be compared with filen1
      verbose: (optional) if True then report status for all
        files checked; otherwise only report summary

    Returns:
      Zero on success, 1 if errors were encountered
    """
    # Generate Md5sum for each file
    retval = 1
    try:
        chksum1 = Md5sum.md5sum(filen1)
        chksum2 = Md5sum.md5sum(filen2)
        if chksum1 == chksum2:
            report("OK: MD5 sums match", verbose)
            retval = 0
        else:
            report("FAILED: MD5 sums don't match", verbose)
    except IOError, ex:
        report("FAILED (%s)" % ex, verbose)
Beispiel #2
0
def diff_files(filen1,filen2,verbose=False):
    """Check that the MD5 sums of two files match

    This compares two files by computing the MD5 sums for each.

    Arguments:
      filen1: "source" file
      filen2: "target" file to be compared with filen1
      verbose: (optional) if True then report status for all
        files checked; otherwise only report summary

    Returns:
      Zero on success, 1 if errors were encountered
    """
    # Generate Md5sum for each file
    retval = 1
    try:
        chksum1 = Md5sum.md5sum(filen1)
        chksum2 = Md5sum.md5sum(filen2)
        if chksum1 == chksum2:
            report("OK: MD5 sums match",verbose)
            retval = 0
        else:
            report("FAILED: MD5 sums don't match",verbose)
    except IOError, ex:
        report("FAILED (%s)" % ex,verbose)
Beispiel #3
0
    def _fetch_md5s(self,filen):
        """Compute and return MD5 sums for each copy of a file

        Calculates the MD5 sums of each copy of the specified file
        in the source and target directories and returns a tuple
        (source_md5,target_md5).

        """
        chksum1 = Md5sum.md5sum(os.path.join(self._from_dir,filen))
        chksum2 = Md5sum.md5sum(os.path.join(self._to_dir,filen))
        return (chksum1,chksum2)
Beispiel #4
0
def compute_md5sum_for_file(filen, output_file=None):
    """Compute and write MD5 sum for specifed file

    Computes the MD5 sum for a file, and writes the sum and the file
    name either to stdout or to the specified file name.

    Note that the output format is compatible with the Linux
    'md5sum' program's '-c' option.

    Arguments:
      filen: file to compute the MD5 sum for
      output_file: (optional) name of file to write MD5 sum to

    Returns:
      Zero on success, 1 if errors were encountered
    """
    retval = 1
    if output_file:
        fp = open(output_file, 'w')
    else:
        fp = sys.stdout
    try:
        chksum = Md5sum.md5sum(filen)
        fp.write("%s  %s\n" % (chksum, filen))
    except IOError, ex:
        # Error accessing file, report and skip
        logging.error("%s: error while generating MD5 sum: '%s'" % (filen, ex))
        retval = 1
Beispiel #5
0
def compute_md5sum_for_file(filen,output_file=None):
    """Compute and write MD5 sum for specifed file

    Computes the MD5 sum for a file, and writes the sum and the file
    name either to stdout or to the specified file name.

    Note that the output format is compatible with the Linux
    'md5sum' program's '-c' option.

    Arguments:
      filen: file to compute the MD5 sum for
      output_file: (optional) name of file to write MD5 sum to

    Returns:
      Zero on success, 1 if errors were encountered

    """
    retval = 1
    if output_file:
        fp = open(output_file,'w')
    else:
        fp = sys.stdout
    try:
        chksum = Md5sum.md5sum(filen)
        fp.write("%s  %s\n" % (chksum,filen))
    except IOError, ex:
        # Error accessing file, report and skip
        logging.error("%s: error while generating MD5 sum: '%s'" % (filen,ex))
        retval = 1
Beispiel #6
0
def compute_md5sums(dirn,output_file=None):
    """Compute and write MD5 sums for all files in a directory

    Walks the directory tree under the specified directory and
    computes the MD5 for each file it finds. The sums are written
    along with the names of the files either to stdout or to the
    specified file name.

    Note that the output format is compatible with the Linux
    'md5sum' program's '-c' option.

    Arguments:
      dirn: directory to run the MD5 sum computation on
      output_file: (optional) name of file to write MD5 sums to

    Returns:
      Zero on success, 1 if errors were encountered
    """
    retval = 0
    if output_file:
        fp = open(output_file,'w')
    else:
        fp = sys.stdout
    for d in os.walk(dirn):
        # Calculate md5sum for each file
        for f in d[2]:
            try:
                filen = os.path.normpath(os.path.join(d[0],f))
                chksum = Md5sum.md5sum(filen)
                fp.write("%s  %s\n" % (chksum,filen))
            except IOError, ex:
                # Error accessing file, report and skip
                logging.error("%s: error while generating MD5 sum: '%s'" % (filen,ex))
                logging.error("%s: skipped" % filen)
                retval = 1
Beispiel #7
0
def verify_md5sums(chksum_file, verbose=False):
    """Check the MD5 sums for all entries specified in a file

    For all entries in the supplied file, check the MD5 sum is
    the same as that calculated by the function, and report
    whether they match or are different.

    The input file can either be output from this program or
    from the Linux 'md5sum' program.

    Arguments:
      chksum_file: name of the file containing the MD5 sums
      verbose: (optional) if True then report status for all
        files checked, plus a summary; otherwise only report
        failures

    Returns:
      Zero on success, 1 if errors were encountered
    """
    retval = 0
    nsuccess = 0
    failures = []
    missing = []
    badlines = []
    # Perform the verification
    for line in open(chksum_file, 'rU'):
        items = line.strip().split()
        if len(items) < 2:
            logging.error("Unable to read MD5 sum from line (skipped):")
            logging.error("%s" % line)
            badlines.append(line)
            retval = 1
            continue
        chksum = items[0]
        chkfile = line[len(chksum):].strip()
        try:
            new_chksum = Md5sum.md5sum(chkfile)
            if chksum == new_chksum:
                report("%s: OK" % chkfile, verbose)
                nsuccess += 1
            else:
                logging.error("%s: FAILED" % chkfile)
                failures.append(chkfile)
                retval = 1
        except IOError, ex:
            if not os.path.exists(chkfile):
                logging.error("%s: FAILED (file not found)" % chkfile)
                missing.append(chkfile)
            else:
                logging.error("%s: FAILED (%s)" % (chkfile, ex))
                failures.append(chkfile)
            retval = 1
Beispiel #8
0
def verify_md5sums(chksum_file,verbose=False):
    """Check the MD5 sums for all entries specified in a file

    For all entries in the supplied file, check the MD5 sum is
    the same as that calculated by the function, and report
    whether they match or are different.

    The input file can either be output from this program or
    from the Linux 'md5sum' program.

    Arguments:
      chksum_file: name of the file containing the MD5 sums
      verbose: (optional) if True then report status for all
        files checked, plus a summary; otherwise only report
        failures

    Returns:
      Zero on success, 1 if errors were encountered
    """
    retval = 0
    nsuccess = 0
    failures = []
    missing = []
    badlines = []
    # Perform the verification
    for line in open(chksum_file,'rU'):
        items = line.strip().split()
        if len(items) < 2:
            logging.error("Unable to read MD5 sum from line (skipped):")
            logging.error("%s" % line)
            badlines.append(line)
            retval = 1
            continue
        chksum = items[0]
        chkfile = line[len(chksum):].strip()
        try:
            new_chksum = Md5sum.md5sum(chkfile)
            if chksum == new_chksum:
                report("%s: OK" % chkfile,verbose)
                nsuccess += 1
            else:
                logging.error("%s: FAILED" % chkfile)
                failures.append(chkfile)
                retval = 1
        except IOError, ex:
            if not os.path.exists(chkfile):
                logging.error("%s: FAILED (file not found)" % chkfile)
                missing.append(chkfile)
            else:
                logging.error("%s: FAILED (%s)" % (chkfile,ex))
                failures.append(chkfile)
            retval = 1
Beispiel #9
0
def verify_md5sums(chksum_file,verbose=False):
    """Check the MD5 sums for all entries specified in a file

    For all entries in the supplied file, check the MD5 sum is
    the same as that calculated by the function, and report
    whether they match or are different.

    The input file can either be output from this program or
    from the Linux 'md5sum' program.

    Arguments:
      chksum_file: name of the file containing the MD5 sums
      verbose: (optional) if True then report status for all
        files checked, plus a summary; otherwise only report
        failures

    Returns:
      Zero on success, 1 if errors were encountered
    """
    retval = 0
    nsuccess = 0
    failures = []
    # Perform the verification
    for line in open(chksum_file,'rU'):
        items = line.strip().split()
        if len(items) != 2:
            sys.stderr.write("Badly formatted MD5 checksum line, skipped\n")
            continue
        chksum = items[0]
        chkfile = items[1]
        try:
            new_chksum = Md5sum.md5sum(chkfile)
            if chksum == new_chksum:
                report("%s: OK" % chkfile,verbose)
                nsuccess += 1
            else:
                sys.stderr.write("%s: FAILED\n" % chkfile)
                failures.append(chkfile)
                retval = 1
        except IOError, ex:
            if not os.path.exists(chkfile):
                sys.stderr.write("%s: FAILED (missing file)\n" % chkfile)
            else:
                sys.stderr.write("%s: FAILED (%s)\n" % (chkfile,ex))
            failures.append(chkfile)
            retval = 1
Beispiel #10
0
def verify_md5sums(chksum_file, verbose=False):
    """Check the MD5 sums for all entries specified in a file

    For all entries in the supplied file, check the MD5 sum is
    the same as that calculated by the function, and report
    whether they match or are different.

    The input file can either be output from this program or
    from the Linux 'md5sum' program.

    Arguments:
      chksum_file: name of the file containing the MD5 sums
      verbose: (optional) if True then report status for all
        files checked, plus a summary; otherwise only report
        failures

    Returns:
      Zero on success, 1 if errors were encountered
    """
    retval = 0
    nsuccess = 0
    failures = []
    # Perform the verification
    for line in open(chksum_file, 'rU'):
        items = line.strip().split()
        if len(items) != 2:
            sys.stderr.write("Badly formatted MD5 checksum line, skipped\n")
            continue
        chksum = items[0]
        chkfile = items[1]
        try:
            new_chksum = Md5sum.md5sum(chkfile)
            if chksum == new_chksum:
                report("%s: OK" % chkfile, verbose)
                nsuccess += 1
            else:
                sys.stderr.write("%s: FAILED\n" % chkfile)
                failures.append(chkfile)
                retval = 1
        except IOError, ex:
            if not os.path.exists(chkfile):
                sys.stderr.write("%s: FAILED (missing file)\n" % chkfile)
            else:
                sys.stderr.write("%s: FAILED (%s)\n" % (chkfile, ex))
            failures.append(chkfile)
            retval = 1
Beispiel #11
0
def compute_md5sums(dirn, output_file=None):
    """Compute and write MD5 sums for all files in a directory

    Walks the directory tree under the specified directory and
    computes the MD5 for each file it finds. The sums are written
    along with the names of the files either to stdout or to the
    specified file name.

    Note that the output format is compatible with the Linux
    'md5sum' program's '-c' option.

    Arguments:
      dirn: directory to run the MD5 sum computation on
      output_file: (optional) name of file to write MD5 sums to

    Returns:
      Zero on success, 1 if errors were encountered
    """
    retval = 0
    if output_file:
        fp = open(output_file, 'w')
    else:
        fp = sys.stdout
    for d in os.walk(dirn):
        # Calculate md5sum for each file
        for f in d[2]:
            try:
                filen = os.path.normpath(os.path.join(d[0], f))
                chksum = Md5sum.md5sum(filen)
                fp.write("%s  %s\n" % (chksum, filen))
            except IOError, ex:
                # Error accessing file, report and skip
                logging.error("%s: error while generating MD5 sum: '%s'" %
                              (filen, ex))
                logging.error("%s: skipped" % filen)
                retval = 1
Beispiel #12
0
def diff_directories(dirn1, dirn2, verbose=False):
    """Check one directory against another using MD5 sums

    This compares one directory against another by computing the
    MD5 sums for the contents of the first, and then checking these
    against the second.

    (Essentially this is automatically performing the compute/verify
    steps in a single operation.)

    Note that if there are different files in one directory compared
    with the other then this function will give different results
    depending on the order theh directories are specified. However
    for common files the actual MD5 sums will be the same regardless
    of order.

    Arguments:
      dirn1: "source" directory
      dirn2: "target" directory to be compared to dirn1
      verbose: (optional) if True then report status for all
        files checked; otherwise only report summary

    Returns:
      Zero on success, 1 if errors were encountered
    """
    retval = 0
    nsuccess = 0
    failures = []
    missing = []
    broken = []
    # Iterate over all files in the source directory
    for d in os.walk(dirn1):
        for f in d[2]:
            # Get full paths for source and target files
            filen1 = os.path.normpath(os.path.join(d[0], f))
            filen2 = filen1.replace(dirn1, dirn2, 1)
            # Check that source exists
            if not os.path.isfile(filen1):
                logging.error("%s: FAILED (broken file)" % filen1)
                broken.append(filen1)
                retval = 1
            # Check that target exists
            elif not os.path.isfile(filen2):
                logging.error("%s: FAILED (file not found)" % filen2)
                missing.append(filen2)
                retval = 1
            else:
                try:
                    # Calculate and compare MD5 sums
                    chksum1 = Md5sum.md5sum(filen1)
                    chksum2 = Md5sum.md5sum(filen2)
                    if chksum1 == chksum2:
                        report("%s: OK" % filen2, verbose)
                        nsuccess += 1
                    else:
                        logging.error("%s: FAILED" % filen2)
                        failures.append(filen2)
                        retval = 1
                except IOError, ex:
                    # Error accessing one or both files, report and skip
                    logging.error("%s: FAILED" % filen2)
                    logging.error("Error while generating MD5 sums: '%s'" % ex)
                    failures.append(filen2)
                    retval = 1
Beispiel #13
0
 def checksum_for_file(self,path):
     """
     """
     return Md5sum.md5sum(self.path(path))
Beispiel #14
0
def diff_directories(dirn1,dirn2,verbose=False):
    """Check one directory against another using MD5 sums

    This compares one directory against another by computing the
    MD5 sums for the contents of the first, and then checking these
    against the second.

    (Essentially this is automatically performing the compute/verify
    steps in a single operation.)

    Note that if there are different files in one directory compared
    with the other then this function will give different results
    depending on the order theh directories are specified. However
    for common files the actual MD5 sums will be the same regardless
    of order.

    Arguments:
      dirn1: "source" directory
      dirn2: "target" directory to be compared to dirn1
      verbose: (optional) if True then report status for all
        files checked; otherwise only report summary

    Returns:
      Zero on success, 1 if errors were encountered
    """
    retval = 0
    nsuccess = 0
    failures = []
    missing = []
    broken = []
    # Iterate over all files in the source directory
    for d in os.walk(dirn1):
        for f in d[2]:
            # Get full paths for source and target files
            filen1 = os.path.normpath(os.path.join(d[0],f))
            filen2 = filen1.replace(dirn1,dirn2,1)
            # Check that source exists
            if not os.path.isfile(filen1):
                logging.error("%s: FAILED (broken file)" % filen1)
                broken.append(filen1)
                retval = 1
            # Check that target exists
            elif not os.path.isfile(filen2):
                logging.error("%s: FAILED (file not found)" % filen2)
                missing.append(filen2)
                retval = 1
            else:
                try:
                    # Calculate and compare MD5 sums
                    chksum1 = Md5sum.md5sum(filen1)
                    chksum2 = Md5sum.md5sum(filen2)
                    if chksum1 == chksum2:
                        report("%s: OK" % filen2,verbose)
                        nsuccess += 1
                    else:
                        logging.error("%s: FAILED" % filen2)
                        failures.append(filen2)
                        retval = 1
                except IOError, ex:
                    # Error accessing one or both files, report and skip
                    logging.error("%s: FAILED" % filen2)
                    logging.error("Error while generating MD5 sums: '%s'" % ex)
                    failures.append(filen2)
                    retval = 1