#!/usr/bin/env python
#
# Copyright C 2002-2003 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.
#
# $Id: isi_md5 165513 2010-11-05 17:50:34Z cjohnson $

import sys, os, os.path
from popen2 import Popen4
import re, md5, string

# This is the list of files for which we will not track md5s.  This
# is because these files are prone to change when nodes are added, 
# and so can cause images created while nodes are being added to
# be unusable.  See bug 61750.
no_md5_list = ["etc/ntp.conf\n",
               "usr/local/share/snmp/snmpd.conf\n", 
               "etc/hosts\n",
               "etc/syslog.conf\n",
               "usr/local/SupportIQ/Agent/dashboard.xml\n"]

def hexstr(s):
    h = string.hexdigits
    r = ''
    for c in s:
        i = ord(c)
        r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
    return r
    
def gen_md5(fname):
    file = open(fname, 'r')
    md = md5.new(file.read())
    md5_in_hex = hexstr(md.digest())
    file.close()
    return md5_in_hex
    
def write_file(type, list_file, md5_file = None):
    p = Popen4('find . -type ' + type + ' -print')
    files = p.fromchild.readlines()
    var_re = re.compile(r"var/")
    var_log_re = re.compile(r"var/log/")
    
    for fname in files:
        if (len(fname) > 2):
            fname = fname[2:]
            if (fname.startswith('.') or var_log_re.match(fname)):
                continue
            if (md5_file != None and var_re.match(fname) == None and
                    fname not in no_md5_list):
                f_name = fname[:len(fname) - 1]
                md5_in_hex = gen_md5(f_name)
                md5_file.write(f_name + '\t' + md5_in_hex + '\n')
        
            list_file.write(fname)
            
def create_files(md5_fname = 'boot/.md5', list_fname = 'boot/.files'):
    tmp_md5 = '/tmp/.md5'
    tmp_list = '/tmp/.files'
    md5_file = open(tmp_md5, 'w')
    list_file = open(tmp_list, 'w')
    write_file('d', list_file)
    list_file.write(md5_fname + '\n')
    list_file.write(list_fname + '\n')
    write_file('f', list_file, md5_file)
    write_file('l', list_file)
    
    md5_file.close()
    list_file.close()

    Popen4('mv -f ' + tmp_md5 + ' ' + md5_fname).wait()
    Popen4('mv -f ' + tmp_list + ' ' + list_fname).wait()
    
def main():
    if len(sys.argv) != 2:
        print "Usage: isi_md5 directory"
    else:
        os.chdir(sys.argv[1])
        create_files()
    
if __name__ == "__main__":
    main()
