You are not logged in.

#1 2007-06-30 23:52:30

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

verify burned cd to iso image script [python {100% baby}]

Hi guys,
I needed this for work the other day and thought I'd share it with the rest of you. It's simple python script that verifies your burned cd to the iso image you created it from. Similar to the 'verify cd' option in k3b -except this can be run via the command line for those of us who need/prefer to run cdrecord. Just give it the complete path to the iso and the device node your burning device is on:

Usage: ./ISOVerify.py [[iso file] [device node]]

http://pastebin.archlinux.org/7057

#!/usr/bin/python

import os, sys, md5

# globals
CD_BLOCK_SIZE    = 2048
FILE_BLOCK_SIZE    = 512

def parse_cli():
    if len(sys.argv) != 3:
        print "Usage: "+sys.argv[0]+" [[iso file] [device node]]"
        sys.exit(1)
    iso      = sys.argv[1]
    dev_node = sys.argv[2]

    for f in iso, dev_node:
        if not os.path.exists(f):
            sys.stderr.write("Error: '"+f+"' does not exist!\n")
            sys.exit(1)
    return iso, dev_node

def get_bytes(file):
    return int(os.stat(file)[6])

def get_blocks(bytes, block_size):
    return bytes / block_size

def get_md5sum(file, block_size, blocks):
    m = md5.new()
    f = open(file, 'r')
    current_block = 1
    while current_block <= blocks:
        try:
            m.update(f.read(block_size))
            f.seek(block_size * current_block)
        except IOError:
            # If we get here, it means we dont have the expected number of bytes. 
            #Return the current digest as this should reflect the inconsistant data anyways.
            f.close()
            return m.hexdigest()
        current_block += 1
    f.close()
    return m.hexdigest()

def main():
    iso, dev_node = parse_cli()

    # iso md5sum
    print iso+": ",
    sys.__stdout__.flush()
    iso_bytes = get_bytes(iso)
    iso_blocks = get_blocks(iso_bytes, FILE_BLOCK_SIZE)
    iso_md5sum = get_md5sum(iso, FILE_BLOCK_SIZE, iso_blocks)
    print iso_md5sum

    # cd/dvd md5sum
    # use the total bytes from the iso image and divide by the cd's block
    # size to get the number of blocks for the device node.
    print dev_node+": ",
    sys.__stdout__.flush()
    dev_node_blocks = get_blocks(iso_bytes, CD_BLOCK_SIZE)
    dev_node_md5sum = get_md5sum(dev_node, CD_BLOCK_SIZE, dev_node_blocks)
    print dev_node_md5sum

    if iso_md5sum == dev_node_md5sum:
        print "md5sums match :)"
        sys.exit(0)
    else:
        print "md5sums DO NOT match!"
        sys.exit(0)

if __name__ == '__main__':
    main()

Offline

Board footer

Powered by FluxBB