Example #1
0
    def __needsToBeBuilt(self, rule):
       
        """
        Checks to see if a rule needs to be built.
        This is based on when the rule was last modified compared to it's dependencies.
        It is assumed that the rule exists, because that check should have been already made.

        Arguments:
        rule - String representing the name of the rule
        """

        thisRule = self.__json_object['Rules'][rule]
        time_last_modified = os.path.getmtime(rule)
        
        if 'depends' in thisRule:
            for dependency in thisRule['depends'].split():
                    
                #In all of the following cases, it is decided that the rule must be built.
                #If none of them are true for all dependencies of a rule, then False is returned.
                if not functions.isFileInDir(dependency):
                    #This means that the dependency does not exist in the directory.
                    return True

                if time_last_modified < os.path.getmtime(dependency):
                    #This means that a dependency has been modified.
                    return True
            
                if self.__needsToBeBuilt(dependency):
                    #Now a recursive check is made for every dependency of this rule
                    return True

        #None of the other cases returned True
        return False
Example #2
0
    def build(self, rule):
        
        """
        Attempts to recursively build from a rule
        
        Arguments:
        rule: String representing the name of the rule
        """
    
        thisRule = ""

        if functions.isFileInDir(rule):
            if rule not in self.__json_object['Rules']:
                #There is no rule for this. Nothing needs to be done.
                return 
            else:
                if not self.__needsToBeBuilt(rule):
                    return

        try:
            thisRule = self.__json_object['Rules'][rule]
        except KeyError:
            print >> sys.stderr, "Rule not found: " + rule
            raise KeyError

        if 'depends' in thisRule:
            for dependency in thisRule['depends'].split():
                try:
                    self.build(dependency)
                except subprocess.CalledProcessError as e:
                    raise e
        
        if 'commands' in thisRule:
            commands = thisRule['commands']

            #Go through every command and run each one separately
            for command in commands:
                command_to_execute = []
                
                #Replace all variables in the commands string
                for word in command.split():
                    if word[0] == '$':
                        #send in the word without the preceding '$'
                        for replacement in self.__replaceVar(word[1:]).split():
                            command_to_execute.append(replacement)
                    else:
                        command_to_execute.append(word)

                #Actually execute the commands  
                try:
                    print "Executing: " + command
                    self.__execute_command(command_to_execute)
                except subprocess.CalledProcessError as e:
                    raise e
Example #3
0
    def __needsToBeBuilt(self, rule):
        """
        Checks to see if a rule needs to be built.
        This is based on when the rule was last modified compared to it's dependencies.
        It is assumed that the rule exists, because that check should have been already made.

        Arguments:
        rule - String representing the name of the rule
        """

        if rule not in self.__json_object['Rules']:
            #If this is true, then this base case must be the result of a recursive call
            #This is a dependency for some other rule that does exist.
            if functions.isFileInDir(rule):
                return
            else:
                print >> stderr, "File not found: " + rule
                raise KeyError

        thisRule = self.__json_object['Rules'][rule]
        time_last_modified = os.path.getmtime(rule)

        if 'depends' in thisRule:
            for dependency in thisRule['depends']:

                #In all of the following cases, it is decided that the rule must be built.
                #If none of them are true for all dependencies of a rule, then False is returned.
                if not functions.isFileInDir(dependency):
                    #This means that the dependency does not exist in the directory.
                    return True

                if time_last_modified < os.path.getmtime(dependency):
                    #This means that a dependency has been modified.
                    return True

                if self.__needsToBeBuilt(dependency):
                    #Now a recursive check is made for every dependency of this rule
                    return True

        #None of the other cases returned True
        return False
Example #4
0
    def build(self, rule):
        """
        Attempts to recursively build from a rule

        Arguments:
        rule: String representing the name of the rule
        """

        thisRule = ""

        if functions.isFileInDir(rule):
            if rule not in self.__json_object['Rules']:
                #There is no rule for this. Nothing needs to be done.
                return
            else:
                if not self.__needsToBeBuilt(rule):
                    return

        try:
            thisRule = self.__json_object['Rules'][rule]
        except KeyError:
            print >> sys.stderr, "Rule not found: " + rule
            raise KeyError

        if 'depends' in thisRule:
            for dependency in thisRule['depends']:
                try:
                    self.build(dependency)
                except subprocess.CalledProcessError as e:
                    raise e

        if 'commands' in thisRule:
            commands = thisRule['commands']

            #Go through every command and run each one separately
            for command in commands:
                command_to_execute = []

                #Replace all variables in the commands string
                for word in command.split():
                    if word[0] == '$':
                        #send in the word without the preceding '$'
                        for replacement in self.__replaceVar(word[1:]).split():
                            command_to_execute.append(replacement)
                    else:
                        command_to_execute.append(word)

                #Actually execute the commands
                try:
                    print "Executing: " + command_to_execute
                    self.__execute_command(command_to_execute)
                except subprocess.CalledProcessError as e:
                    raise e