Ejemplo n.º 1
0
    def __init__(self, pid) :
        '''Intitate the whole class and create the object.'''

        self.pid                            = pid
        self.log                            = ProjLog(self.pid)
        self.tools                          = Tools()
        self.user                           = UserConfig()
        self.config                         = Config(pid)
        self.local                          = ProjLocal(self.pid)
        self.userConfig                     = self.user.userConfig
        self.config.getProjectConfig()
        self.projectConfig                  = self.config.projectConfig
        if self.userConfig['System']['textDifferentialViewerCommand'] == '' :
            self.diffViewCmd                = None
        else :
            self.diffViewCmd                = self.userConfig['System']['textDifferentialViewerCommand']
            # Make sure the diff view command is a list
            if type(self.diffViewCmd) != list :
                self.diffViewCmd            = [self.userConfig['System']['textDifferentialViewerCommand']]

        # Log messages for this module
        self.errorCodes     = {
            '0210' : ['WRN', 'File does not exist: [<<1>>] compare cannot be done.'],
            '0280' : ['ERR', 'Failed to compare files with error: [<<1>>]'],
            '0285' : ['ERR', 'Cannot compare component [<<1>>] because a coresponding subcomponent could not be found.'],
            '0290' : ['ERR', 'Compare test type: [<<1>>] is not valid.'],
            '0295' : ['MSG', 'Comparing: [<<1>>] with parent: [<<2>>] Close the viewer to return to the terminal prompt.'],
            '0220' : ['MSG', 'Comparison not needed, files seem to be the same.'],
            '0300' : ['WRN', 'Files are different but visual compare is not enabled.']
        }
Ejemplo n.º 2
0
class ProjCompare (object) :

    def __init__(self, pid) :
        '''Intitate the whole class and create the object.'''

        self.pid                            = pid
        self.log                            = ProjLog(self.pid)
        self.tools                          = Tools()
        self.user                           = UserConfig()
        self.config                         = Config(pid)
        self.local                          = ProjLocal(self.pid)
        self.userConfig                     = self.user.userConfig
        self.config.getProjectConfig()
        self.projectConfig                  = self.config.projectConfig
        if self.userConfig['System']['textDifferentialViewerCommand'] == '' :
            self.diffViewCmd                = None
        else :
            self.diffViewCmd                = self.userConfig['System']['textDifferentialViewerCommand']
            # Make sure the diff view command is a list
            if type(self.diffViewCmd) != list :
                self.diffViewCmd            = [self.userConfig['System']['textDifferentialViewerCommand']]

        # Log messages for this module
        self.errorCodes     = {
            '0210' : ['WRN', 'File does not exist: [<<1>>] compare cannot be done.'],
            '0280' : ['ERR', 'Failed to compare files with error: [<<1>>]'],
            '0285' : ['ERR', 'Cannot compare component [<<1>>] because a coresponding subcomponent could not be found.'],
            '0290' : ['ERR', 'Compare test type: [<<1>>] is not valid.'],
            '0295' : ['MSG', 'Comparing: [<<1>>] with parent: [<<2>>] Close the viewer to return to the terminal prompt.'],
            '0220' : ['MSG', 'Comparison not needed, files seem to be the same.'],
            '0300' : ['WRN', 'Files are different but visual compare is not enabled.']
        }


###############################################################################
############################## Compare Functions ##############################
###############################################################################
######################## Error Code Block Series = 0200 ########################
###############################################################################

    def compareComponent (self, gid, cid, parent='backup') :
        '''Compare a component with its source which was copied into the project
        when the component was created. This will pull up the user's differential
        viewer and compare the two files.'''

        cType       = self.projectConfig['Groups'][gid]['cType']
        pFile       = self.local.getComponentFiles(gid, cid, cType)[parent.lower()]
        wFile       = self.local.getComponentFiles(gid, cid, cType)['working']
        # Do a quick check between the current and the source
        # Run the compare app if a difference is found
        if self.isDifferent(wFile, pFile) :
            self.compare(wFile, pFile)
        else :
            self.log.writeToLog(self.errorCodes['0220'])


    def isDifferent (self, new, old) :
        '''Return True if the contents of the files are different.'''

        # If one file is missing, return True
        if not os.path.exists(new) or not os.path.exists(old) :
            return True

        # Inside of diffl() open both files with universial line endings then
        # check each line for differences.
        diff = difflib.ndiff(open(new, 'rU').readlines(), open(old, 'rU').readlines())
        for d in diff :
            if d[:1] == '+' or d[:1] == '-' :
                return True
        # FIXME: Is returning False better than None?
        return False


    def compare (self, old, new) :
        '''Run a compare on two files.'''

#        import pdb; pdb.set_trace()

        # If there are any differences, open the diff viewer
        if self.isDifferent(old, new) :
            # If no diffViewCmd is found this may be running headless
            # in that case just report that the file is different and
            # and leave the function
            if not self.diffViewCmd :
                self.log.writeToLog(self.errorCodes['0300'])
            else :
                # To prevent file names being pushed back to the list ref
                # we need to use extend() rather than append()
                cmd = []
                cmd.extend(self.diffViewCmd)
                cmd.extend([old, new])
                try :
                    self.log.writeToLog(self.errorCodes['0295'], [self.tools.fName(old),self.tools.fName(new)])
                    subprocess.call(cmd)
                except Exception as e :
                    # If we don't succeed, we should probably quite here
                    self.log.writeToLog(self.errorCodes['0280'], [str(e)])
        else :
            self.log.writeToLog(self.errorCodes['0220'])