예제 #1
0
    def set(self, args):
        """ 
        Sets the environment to correspond to this level.
        """
        
        # Get full syntax
        syntax, depth = self.syntax_and_depth(args)

        # Check to see if level exists
        level_list = Level.find_by_syntax(syntax)

        if not level_list:
            pipeline.utils.report("Error - Syntax does not match any levels")
            return

        if len(level_list) > 1:
            pipeline.utils.report("Error - Syntax matches multiple levels")
            return

        # Get the level we've found
        level = level_list[0]

        shell = Shell()

        # Grab settings info for reference
        hierarchy = settings.hierarchy()
        abbr = settings.abbreviations()

        # Clean out the any pipeline paths from PATH so we can reset it
        pattern = shell.getenv("@JOBS_ROOT") + r'/[\w/]*/share/bin'
        shell.clean('PATH', pattern)

        # Clean prod from PATH as well
        pattern = shell.getenv("@JOBS_ROOT") + r'/prod/[\w/]*'
        shell.clean('PATH', pattern)

        aliases = ['', 'bin']

        # Remove all environment variables and aliases
        # associated with levels below this one
        depth = level.depth + 1
        if level.depth < len(hierarchy) - 1:
            for level_name in hierarchy[level.depth + 1:]:
                shell.unset("@" + level_name.upper())
                for alias in aliases:
                    shell.unalias(abbr[depth] + alias)

                depth += 1
                

        bin_path = ""
        # Set environment from level 
        for l in level.branch():
            shell.set("@" + hierarchy[l.depth], l.name)
            bin_path += "%s/share/bin:" % l.file_path()

        # Set aliases
        cd_path = "cd $@JOBS_ROOT"
        for index, level_name in enumerate(hierarchy[:level.depth + 1]):
            # alias current level
            cd_path += os.sep + "$@" + level_name.upper()
            shell.alias(abbr[index], cd_path)

        prod = shell.getenv("@PROD")
        bin_path += "%s/bin" % prod
        shell.insert("PATH", bin_path)

        self.write_last(level)

        shell.commit()