dbrtools/dbr/dbrutils.py
changeset 178 eab8a264a833
child 188 0d02b2df4cbb
equal deleted inserted replaced
176:6d3c3db11e72 178:eab8a264a833
       
     1 # Copyright (c) 2009 Symbian Foundation Ltd
       
     2 # This component and the accompanying materials are made available
       
     3 # under the terms of the License "Eclipse Public License v1.0"
       
     4 # which accompanies this distribution, and is available
       
     5 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     6 #
       
     7 # Initial Contributors:
       
     8 # Symbian Foundation Ltd - initial contribution.
       
     9 #
       
    10 # Contributors:
       
    11 # mattd <mattd@symbian.org>
       
    12 #
       
    13 # Description:
       
    14 # DBRutils - Module for handling little bits of stuff to do with generating hashes and scaning directories
       
    15 
       
    16 import re
       
    17 import os
       
    18 import sys
       
    19 import string
       
    20 from os.path import join, isfile, stat
       
    21 from stat import *
       
    22 
       
    23 import glob # temporary (I hope) used for grabbing stuf from zip files...
       
    24 
       
    25 
       
    26 
       
    27 def defaultdb():
       
    28   return os.path.join(patchpath(),'baseline.db')
       
    29 
       
    30 def patchpath():
       
    31   return os.path.join(epocroot(),'%s/' % patch_path_internal())
       
    32 
       
    33 def patch_path_internal():
       
    34   return 'epoc32/relinfo'
       
    35 
       
    36 def exclude_dirs():
       
    37     fixpath = re.compile('\\\\')
       
    38     leadingslash = re.compile('^%s' % fixpath.sub('/',epocroot()))
       
    39     return [string.lower(leadingslash.sub('',fixpath.sub('/',os.path.join(epocroot(),'epoc32/build')))),string.lower(leadingslash.sub('',fixpath.sub('/',patch_path_internal())))]
       
    40 
       
    41 def exclude_files():
       
    42 #    return ['\.sym$','\.dll$'] # just testing...
       
    43     return ['\.sym$']
       
    44     
       
    45 def epocroot():
       
    46     return os.environ.get('EPOCROOT')
       
    47 
       
    48 def scanenv():
       
    49     print 'Scanning local environment'
       
    50     directory = os.path.join(epocroot(),'epoc32')
       
    51     env = scandir(directory, exclude_dirs(), exclude_files())
       
    52     return env
       
    53 
       
    54 def createzip(files, name):
       
    55     tmpfilename = os.tmpnam( )
       
    56     print tmpfilename    
       
    57     f = open(tmpfilename,'w')
       
    58     for file in sorted(files):
       
    59         str = '%s%s' % (file,'\n')
       
    60         f.write(str)    
       
    61     f.close()
       
    62     os.chdir(epocroot())
       
    63     exestr = '7z a -Tzip -i@%s %s' %(tmpfilename,name)
       
    64     print 'executing: >%s<\n' %exestr
       
    65     os.system(exestr)
       
    66     os.unlink(tmpfilename)
       
    67 
       
    68 def extractfiles(files, path):
       
    69     zips = glob.glob(os.path.join(path, '*.zip'))
       
    70     for name in zips:
       
    71       extractfromzip(files, name)    
       
    72         
       
    73     
       
    74 def extractfromzip(files, name):
       
    75     tmpfilename = os.tmpnam( )
       
    76     print tmpfilename
       
    77     os.chdir(epocroot())
       
    78     f = open(tmpfilename,'w')
       
    79     for file in sorted(files):
       
    80         str = '%s%s' % (file,'\n')
       
    81         f.write(str)    
       
    82     f.close()
       
    83     exestr = '7z x -y -i@%s %s' %(tmpfilename,name)
       
    84     print 'executing: >%s<\n' %exestr
       
    85     os.system(exestr)
       
    86     os.unlink(tmpfilename)
       
    87 
       
    88 def deletefiles(files):
       
    89     os.chdir(epocroot())
       
    90     for file in files:
       
    91       print 'deleting %s' %file
       
    92       os.unlink(file)
       
    93           
       
    94 
       
    95 def generateMD5s(testset):
       
    96     db = dict()
       
    97     if(len(testset)):
       
    98 #      print testset
       
    99       os.chdir(epocroot())
       
   100       tmpfilename = os.tmpnam( )
       
   101       print tmpfilename, '\n'
       
   102       f = open(tmpfilename,'w')
       
   103       for file in testset:
       
   104           entry = dict()
       
   105           entry['md5'] = 'xxx'
       
   106           db[file] = entry
       
   107           str = '%s%s' % (file,'\n')
       
   108           f.write(str)
       
   109       f.close()
       
   110       outputfile = os.tmpnam() 
       
   111       exestr = 'evalid -f %s %s %s' % (tmpfilename, epocroot(), outputfile)
       
   112 #      print exestr
       
   113       exeresult = os.system(exestr) 
       
   114       if(exeresult):
       
   115         sys.exit('Fatal error executing: %s\nReported error: %s' % (exestr,os.strerror(exeresult)))
       
   116       else:  
       
   117         db = gethashes(db,outputfile)
       
   118         os.unlink(outputfile)
       
   119         os.unlink(tmpfilename)
       
   120     return db
       
   121 
       
   122 # Brittle and nasty!!!
       
   123 def gethashes(db,md5filename):
       
   124     os.chdir(epocroot())
       
   125 #    print 'trying to open %s' % md5filename
       
   126     file = open(md5filename,'r')
       
   127     root = ''
       
   128     fixpath = re.compile('\\\\')
       
   129     leadingslash = re.compile('^%s' % fixpath.sub('/',epocroot()))
       
   130 
       
   131     evalidparse = re.compile('(.+)\sTYPE=(.+)\sMD5=(.+)')
       
   132     dirparse = re.compile('Directory:(\S+)')
       
   133     for line in file:
       
   134         res = evalidparse.match(line)
       
   135         if(res):
       
   136             filename = "%s%s" % (root,res.group(1))
       
   137             filename = string.lower(fixpath.sub('/',leadingslash.sub('',filename)))            
       
   138 #            print "found %s" % filename   
       
   139             if(filename in db):
       
   140                 db[filename]['md5'] = res.group(3)
       
   141 
       
   142         else:
       
   143             res = dirparse.match(line)
       
   144             if(res):
       
   145                 if(res.group(1) == '.'):
       
   146                     root = ''
       
   147                 else:
       
   148                     root = '%s/' % res.group(1)
       
   149             
       
   150     file.close()
       
   151     return db
       
   152 
       
   153 
       
   154 def scandir(top, exclude_dirs, exclude_files):
       
   155 # exclude_dirs must be in lower case...
       
   156 #    print "Remember to expand the logged dir from", top, "!!!"
       
   157     countdown = 0
       
   158     env = dict()
       
   159     fixpath = re.compile('\\\\')
       
   160     leadingslash = re.compile('^%s' % fixpath.sub('/',epocroot()))
       
   161     
       
   162     ignorestr=''
       
   163     for exclude in exclude_files:
       
   164       if(len(ignorestr)):
       
   165         ignorestr = '%s|%s' % (ignorestr, exclude)
       
   166       else:
       
   167         ignorestr = exclude    
       
   168     ignore = re.compile(ignorestr) 
       
   169 
       
   170     for root, dirs, files in os.walk(top, topdown=True):
       
   171         for dirname in dirs:
       
   172 #            print string.lower(leadingslash.sub('',fixpath.sub('/',os.path.join(root,dirname))))
       
   173             if(string.lower(leadingslash.sub('',fixpath.sub('/',os.path.join(root,dirname)))) in exclude_dirs):
       
   174 #              print 'removing: %s' % os.path.join(root,dirname)
       
   175               dirs.remove(dirname)
       
   176         for name in files:
       
   177             filename = os.path.join(root, name)
       
   178             statinfo = os.stat(filename)
       
   179             fn = string.lower(leadingslash.sub('',fixpath.sub('/',filename)))
       
   180 #            print '%s\t%s' % (filename, fn);
       
   181             if(countdown == 0):
       
   182                 print '.',
       
   183                 countdown = 1000
       
   184             countdown = countdown-1
       
   185             if not ignore.search(fn,1):
       
   186               entry = dict()
       
   187               entry['time'] = '%d' % statinfo[ST_MTIME]
       
   188               entry['size'] = '%d' % statinfo[ST_SIZE]
       
   189               entry['md5'] = 'xxx'
       
   190               env[fn] = entry
       
   191   #            data = [statinfo[ST_MTIME],statinfo[ST_SIZE],'xxx']
       
   192   #            env[fn] = data
       
   193     print '\n'
       
   194     return env