Exemplo n.º 1
0
    def __replaceTag( self ):
        
        cfg          = self.__config["release"]
        ig_file_exts = cfg["ignore_file_exts"]
        data         = { "project_name" : self.__config["name"] }
        data.update( cfg["variables"] )
        tobj         = StringTemple( "", data )
        DATE_FORMAT  = LOC["DATE_FORMAT"]
        TIME_FOMAT   = "%H:%M:%S"
        DTIME_FORMAT = "%s %s" % ( DATE_FORMAT, TIME_FOMAT )
        replace_scope= CFG.get("replace_scope")

        #DEBUG
        MESSAGE(
            "   IG_FILE_EXTS : %s" % ( ig_file_exts ),
            c      = DEBUG,
            prefix = ""
        )

        for path in self.__files:
            ext = os.path.splitext( path )[1].lower()
            rpath = path.replace( os.path.dirname( self.__releasepath ), "" )
            if ext in ig_file_exts:
                MESSAGE( "   [IGNORE]" + rpath, c = DEBUG, prefix = "" )
                continue

            MESSAGE( "   [REPLACE]" + rpath, c = DEBUG, prefix = "" )
                
            localtime = time.localtime()
            tobj.expanData( {
                "FILE" : os.path.basename( path ),
                "PATH" : os.path.dirname( path ),
                "MDATE": time.strftime( DTIME_FORMAT, time.localtime( os.path.getctime( path ) ) ),
                "TIME" : time.strftime( TIME_FOMAT  , localtime ),
                "DATE" : time.strftime( DATE_FORMAT , localtime )
            } )

            temp    = ""
            with open( path, "r" )as fobj: temp += fobj.read()
            size    = len( temp )
            #temp    = temp.decode("utf8")
            content = ""
            for item in replace_scope:
                if ext not in item["file_ext"]: continue
                
                scopes  = []
                start   = 0
                pattern = re.compile( item["pattern"] )
                content = ""
                tobj.setTemple( "" )
                while start < size:
                    region = re.search( pattern, temp[start:] )
                    if region:
                        offset_start = start + region.start()
                        offset_end   = start + region.end()
                       # print offset_start, offset_end
                        part         = temp[ offset_start:offset_end ]
                        space        = temp[ start:offset_start ]
                        scopes.append( part )
                        if re.search( project_release.WRITESPACE_RE_PATTERN, space ):
                            tobj.expanTemple( space + part )
                        else:
                            content += tobj.parse() + temp[ start:offset_start ]
                            tobj.setTemple( part )
                            
                        start = offset_end
                    
                    else:
                        break
                content += tobj.parse() + temp[ start: ]
                #content = content.encode("utf8")
                #DEBUG
                if DEBUG:
                    MESSAGE( 
                        "            %s\n"
                        "            %s\n"
                        "            ---REPLACE SCOPE---\n"
                        "            %s\n" 
                        "            -------" % ( 
                            item["pattern"], 
                            ",".join( item["file_ext"] ),
                            "\n            ".join( [
                                re.sub( "\n", "\n            ", scope.decode("utf8") ) \
                                for scope in scopes
                            ] )
                        ),
                        c = DEBUG, 
                        prefix = "" 
                    )
            if content:
                try:
                    fobj = open( path, "w" ) 
                    fobj.write( content )
                except IOError:
                    raise IOError
                finally:
                    fobj.close()