Esempio n. 1
0
    def write_batch_file(self):
        self.batch_accum.set_current_section('pre')
        self.batch_accum += self.platform_helper.get_install_instructions_prefix()
        self.batch_accum.set_current_section('post')
        var_stack.set_var("TOTAL_ITEMS_FOR_PROGRESS_REPORT").append(
            str(self.platform_helper.num_items_for_progress_report))
        self.batch_accum += self.platform_helper.get_install_instructions_postfix()
        lines = self.batch_accum.finalize_list_of_lines()
        lines_after_var_replacement = '\n'.join(
            [value_ref_re.sub(self.platform_helper.var_replacement_pattern, line) for line in lines])

        from utils import write_to_file_or_stdout

        out_file = var_stack.resolve("$(__MAIN_OUT_FILE__)", raise_on_fail=True)
        with write_to_file_or_stdout(out_file) as fd:
            fd.write(lines_after_var_replacement)
            fd.write('\n')

        if out_file != "stdout":
            self.out_file_realpath = os.path.realpath(out_file)
            # chmod to 0777 so that file created under sudo, can be re-written under regular user.
            # However regular user cannot chmod for file created under sudo, hence the try/except
            try:
                os.chmod(self.out_file_realpath, 0777)
            except:
                pass
        else:
            self.out_file_realpath = "stdout"
        msg = " ".join(
            (self.out_file_realpath, str(self.platform_helper.num_items_for_progress_report), "progress items"))
        print(msg)
        logging.info(msg)
Esempio n. 2
0
    def write_batch_file(self, in_batch_accum, file_name_post_fix=""):
        assert "__MAIN_OUT_FILE__" in config_vars

        config_vars[
            "TOTAL_ITEMS_FOR_PROGRESS_REPORT"] = in_batch_accum.total_progress_count(
            )

        in_batch_accum.initial_progress = self.internal_progress
        self.create_variables_assignment(in_batch_accum)
        self.init_python_batch(in_batch_accum)

        exit_on_errors = self.the_command != 'uninstall'  # in case of uninstall, go on with batch file even if some operations failed

        final_repr = repr(in_batch_accum)

        out_file: Path = config_vars.get("__MAIN_OUT_FILE__", None).Path()
        if out_file:
            out_file = out_file.parent.joinpath(out_file.name +
                                                file_name_post_fix)
            with MakeDir(out_file.parent, report_own_progress=False) as md:
                md()
            self.out_file_realpath = os.fspath(out_file)
        else:
            self.out_file_realpath = "stdout"

        with utils.write_to_file_or_stdout(out_file) as fd:
            fd.write(final_repr)
            fd.write('\n')

        msg = " ".join(
            (self.out_file_realpath,
             str(in_batch_accum.total_progress_count()), "progress items"))
        log.info(msg)
 def __call__(self, *args, **kwargs) -> None:
     PythonBatchCommandBase.__call__(self, *args, **kwargs)
     the_listing = utils.disk_item_listing(self.folder_to_list,
                                           ls_format=self.ls_format)
     with utils.write_to_file_or_stdout(
             self.out_file, append_to_file=self.out_file_append) as wfd:
         wfd.write(the_listing)
Esempio n. 4
0
    def command_output(self):
        out_file = var_stack.ResolveVarToStr("__MAIN_OUT_FILE__")

        output_format = var_stack.ResolveVarToStr("OUTPUT_FORMAT", default='text')

        if output_format == "json":
            output_text = json.dumps(self.output_data, indent=1)
        else:  # output_format == "text":  text is the default format
            lines = [", ".join(line_data) for line_data in self.output_data]
            output_text = "\n".join(lines)

        with utils.write_to_file_or_stdout(out_file) as wfd:
            wfd.write(output_text)
            wfd.write("\n")
Esempio n. 5
0
    def command_output(self):
        if "__MAIN_OUT_FILE__" in var_stack:
            out_file = var_stack.ResolveVarToStr("__MAIN_OUT_FILE__")
        else:
            out_file = "stdout"

        output_format = var_stack.ResolveVarToStr("__OUTPUT_FORMAT__")
        if output_format == "text":
            lines = [", ".join(line_data) for line_data in self.output_data]
            output_text = "\n".join(lines)
        elif output_format == "json":
            output_text = json.dumps(self.output_data)

        with utils.write_to_file_or_stdout(out_file) as wfd:
            wfd.write(output_text)
            wfd.write("\n")
Esempio n. 6
0
    def write_to_file(self, in_file, in_format="guess", comments=True, items_list=None, field_to_write=None):
        """ pass in_file="stdout" to output to stdout.
            in_format is either text, yaml, pickle
        """

        if items_list is None:
            items_list = self.get_items()
        if in_format == "guess":
            _, extension = os.path.splitext(in_file)
            in_format = map_info_extension_to_format[extension[1:]]
        if in_format in list(self.write_func_by_format.keys()):
            with utils.write_to_file_or_stdout(in_file) as wfd:
                self.write_func_by_format[in_format](wfd, items_list, comments, field_to_write=field_to_write)
                self.files_written_list.append(in_file)
        else:
            raise ValueError("Unknown write in_format " + in_format)
Esempio n. 7
0
    def do_ls(self):
        if "__MAIN_OUT_FILE__" in var_stack:
            out_file = var_stack.ResolveVarToStr("__MAIN_OUT_FILE__")
        else:
            out_file = "stdout"

        main_folder_to_list = var_stack.ResolveVarToStr("__MAIN_INPUT_FILE__")
        folders_to_list = []
        if var_stack.defined("__LIMIT_COMMAND_TO__"):
            limit_list = var_stack.ResolveVarToList("__LIMIT_COMMAND_TO__")
            for limit in limit_list:
                limit = utils.unquoteme(limit)
                folders_to_list.append(os.path.join(main_folder_to_list, limit))
        else:
            folders_to_list.append(main_folder_to_list)

        the_listing = utils.folder_listing(*folders_to_list)
        with utils.write_to_file_or_stdout(out_file) as wfd:
            wfd.write(the_listing)
Esempio n. 8
0
    def do_ls(self):
        main_folder_to_list = var_stack.ResolveVarToStr("__MAIN_INPUT_FILE__")
        folders_to_list = []
        if var_stack.defined("__LIMIT_COMMAND_TO__"):
            limit_list = var_stack.ResolveVarToList("__LIMIT_COMMAND_TO__")
            for limit in limit_list:
                limit = utils.unquoteme(limit)
                folders_to_list.append(os.path.join(main_folder_to_list, limit))
        else:
            folders_to_list.append(main_folder_to_list)

        ls_format = var_stack.ResolveVarToStr("LS_FORMAT", default='*')
        the_listing = utils.disk_item_listing(*folders_to_list, ls_format=ls_format)

        out_file = var_stack.ResolveVarToStr("__MAIN_OUT_FILE__")
        try:
            with utils.write_to_file_or_stdout(out_file) as wfd:
                wfd.write(the_listing)
        except NotADirectoryError:
            print("Cannot output to {}".format(out_file))
Esempio n. 9
0
    def write_batch_file(self):
        if "__MAIN_OUT_FILE__" not in var_stack and "__MAIN_INPUT_FILE__" in var_stack:
            var_stack.add_const_config_variable("__MAIN_OUT_FILE__", "from write_batch_file",
                                                "$(__MAIN_INPUT_FILE__)-$(__MAIN_COMMAND__).$(BATCH_EXT)")

        self.batch_accum.set_current_section('pre')
        self.batch_accum += self.platform_helper.get_install_instructions_prefix()
        self.batch_accum.set_current_section('post')
        var_stack.set_var("TOTAL_ITEMS_FOR_PROGRESS_REPORT").append(
            str(self.platform_helper.num_items_for_progress_report))
        self.batch_accum += self.platform_helper.get_install_instructions_postfix()
        lines = self.batch_accum.finalize_list_of_lines()
        for line in lines:
            if type(line) != str:
                raise TypeError("Not a string", type(line), line)

        # replace unresolved var references to native OS var references, e.g. $(HOME) would be %HOME% on Windows and ${HOME} one Mac
        lines_after_var_replacement = [value_ref_re.sub(self.platform_helper.var_replacement_pattern, line) for line in lines]
        output_text = "\n".join(lines_after_var_replacement)

        out_file = var_stack.ResolveVarToStr("__MAIN_OUT_FILE__")
        out_file = os.path.abspath(out_file)
        d_path, f_name = os.path.split(out_file)
        os.makedirs(d_path, exist_ok=True)
        with utils.write_to_file_or_stdout(out_file) as fd:
            fd.write(output_text)
            fd.write('\n')

        if out_file != "stdout":
            self.out_file_realpath = os.path.realpath(out_file)
            # chmod to 0777 so that file created under sudo, can be re-written under regular user.
            # However regular user cannot chmod for file created under sudo, hence the try/except
            try:
                os.chmod(self.out_file_realpath, 0o777)
            except Exception:
                pass
        else:
            self.out_file_realpath = "stdout"
        msg = " ".join(
            (self.out_file_realpath, str(self.platform_helper.num_items_for_progress_report), "progress items"))
        print(msg)
Esempio n. 10
0
    def command_output(self):
        if not bool(config_vars.get('__SILENT__', "false")):

            output_format = str(config_vars.get("OUTPUT_FORMAT", 'text'))

            if output_format == "json":
                output_text = json.dumps(self.output_data,
                                         indent=1,
                                         default=utils.extra_json_serializer)
            elif output_format == "yaml":
                io_str = io.StringIO()
                for yaml_data in self.output_data:
                    aYaml.writeAsYaml(yaml_data, io_str)
                output_text = io_str.getvalue()
            else:  # output_format == "text":  text is the default format
                lines = [
                    ", ".join(line_data) for line_data in self.output_data
                ]
                output_text = "\n".join(lines)

            out_file = config_vars.get("__MAIN_OUT_FILE__", None).Path()
            with utils.write_to_file_or_stdout(out_file) as wfd:
                wfd.write(output_text)
                wfd.write("\n")
Esempio n. 11
0
 def __command_output(self, _as_yaml_doc):
     out_file_path = config_vars.get("__MAIN_OUT_FILE__", None).Path()
     with utils.write_to_file_or_stdout(out_file_path) as wfd:
         aYaml.writeAsYaml(defines_yaml_doc, wfd)
         aYaml.writeAsYaml(index_yaml_doc, wfd)
Esempio n. 12
0
    def write_program_state(self):
        from utils import write_to_file_or_stdout

        state_file = var_stack.resolve("$(__MAIN_STATE_FILE__)", raise_on_fail=True)
        with write_to_file_or_stdout(state_file) as fd:
            augmentedYaml.writeAsYaml(self, fd)
Esempio n. 13
0
    def write_program_state(self):

        state_file = var_stack.ResolveVarToStr("__MAIN_STATE_FILE__")
        with utils.write_to_file_or_stdout(state_file) as fd:
            aYaml.writeAsYaml(self, fd)