Exemple #1
0
    def remove_script_from_profile(self, profile_id: str, script_id: str) \
            -> ActionResult:
        """
        Remove script from profile

        Args:
            profile_id (str): profile name
            script_id (str): script path

        Returns:
            ActionResult: return error when profile
                or script not found
        """

        result = ActionResult()

        temp_result, profile = self._check_profile_exists(profile_id)
        if not temp_result.success() or not profile:
            return temp_result

        if script_id not in profile.script_id_list:
            result.add_warning(
                ErrorMessages.script_not_in_profile.format(
                    profile=profile.identifier(), script=script_id))
            return result

        # try to stop script if no one else if running this script
        # get all profiles and libraries currently running contains script
        profiles = self.find_running_profiles_contains_script(script_id)
        library = library_service.find_library_contains_script(script_id)

        # check to see anything is running other than this script
        if len(profiles) <= 1 and not (library and library.is_running()):
            temp_result, _ = library_service.stop_script(script_id)
            result.merge(temp_result)

        # script will be removed even has error
        profile.remove(script_id)
        result.ignore_error()
        return result
Exemple #2
0
    def add_script(self, profile_id: str, script_id: str) \
            -> Tuple[ActionResult, Profile]:
        """
        Add script into profile

        Args:
            profile_name (str): profile name
            script_id (str): script path

        Returns:
            Tuple[ActionResult, Profile]:
                ActionResult: return error when profile
                    or script not found
        """

        result = ActionResult()

        # check whether profile exists
        temp_result, profile = self._check_profile_exists(profile_id)
        if not temp_result.success() or not profile:
            return result, None

        # find profile
        profile = self.find(profile_id)
        if profile.has_script(script_id):
            result.add_warning(
                ErrorMessages.profile_already_contains_script.format(
                    script_id))
            return result, profile

        # find script
        script = library_service.find_script(script_id)
        if not script:
            result.add_error(
                ErrorMessages.could_not_find_script.format(script_id))
            return result, None

        return self.profile_manager.add_script(profile, script)
Exemple #3
0
    def init_library(self, path: str) -> Tuple[ActionResult, Library]:
        """
        Initialize directory and all its sub-directories

        Args:
            path (str): directory path

        Returns:
            Tuple[ActionResult, Library]: return error
                when path is not directory
        """
        result = ActionResult()
        path = self.utility.format_path(path)

        # check whether path is a valid directory
        if not self.utility.is_dir(path):
            result.add_error(
                ErrorMessages.path_is_not_directory_path.format(path))
            return result, None

        files = self.utility.get_files_in_directory(path)
        if not files:
            # ignore the directory do not have any file
            result.add_warning(ErrorMessages.directory_is_empty.format(path))
            return result, None

        library = Library(path)
        for file in files:
            # initialize script file
            temp_result, script = self.script_manager.init_script(file)
            result.merge(temp_result)

            # only add script to the list when no error returned.
            if temp_result.success() and script:
                library.add(script)

        return result, library