예제 #1
0
 def write_command(cls, command, path: Text = None):
     path = path if path else '/tmp'
     timestamp = TimeUtils.utc_timestamp()
     cls.sequence += 1
     tmp_file = f'appyratus-sys-exec-{timestamp}-{cls.sequence}.sh'
     file_path = PathUtils.join(path, tmp_file)
     File.write(file_path, f"#!/usr/bin/env bash\n{command}")
     PathUtils.make_executable(file_path, user=True)
     return file_path
예제 #2
0
 def from_filepath(cls, filepath: Text):
     source = File.read(filepath)
     ast = AstParser().parse_module(filepath)
     module = cls(
         name=ast['module'],
         path=ast['file'],
         classes=ast['classes'],
         functions=ast['functions'],
         imports=ast['imports'],
         ast=ast,
     )
     return module
예제 #3
0
파일: embryo.py 프로젝트: gigaquads/embryo
    def _load_templates(self):
        """
        Read all template file. Each template string is stored in a dict, keyed
        by the relative path at which it exists, relative to the templates root
        directory. The file paths themselves are templatized and are therefore
        rendered as well in this procedure.
        """
        assert self.dumped_context is not None

        say('loading templates...')

        context = self.dumped_context
        templates_path = build_embryo_filepath(self.path, 'templates')
        templates = {}

        if not os.path.isdir(templates_path):
            return templates

        for root, dirs, files in os.walk(templates_path):
            for fname in files:
                if fname.endswith('.swp'):
                    continue

                # the file path may itself be templatized. here, we render the
                # filepath template using the context dict and read in the
                # template files.

                # fpath here is the templatized file path to the template
                fpath = os.path.join(root, fname)

                # rel_fpath is the path relative to the root templates dir
                rel_fpath = fpath.replace(templates_path, '').lstrip('/')

                # fname_template is the jinja2 Template for the rel_fpath str
                try:
                    fname_template = self.jinja_env.from_string(rel_fpath)
                except TemplateSyntaxError:
                    shout(
                        'could not render template '
                        'for file path string: {p}', p=fpath
                    )
                    raise

                # finally rendered_rel_fpath is the rendered relative path
                rendered_rel_fpath = fname_template.render(context)

                # now actually read the file into the resulting dict.
                try:
                    templates[rendered_rel_fpath] = File.read(fpath)
                except Exception:
                    raise TemplateLoadFailed(fpath)

        return templates
예제 #4
0
파일: embryo.py 프로젝트: gigaquads/embryo
    def _render_tree(self) -> Dict:
        """
        Read and deserialized the file system tree yaml file as well as render
        it, as it is a templatized file.
        """
        assert self.dumped_context is not None

        say('rendering tree.yml...')

        context = self.dumped_context.copy()
        context.update(self.related)
        fpath = build_embryo_filepath(self.path, 'tree')

        tree_yml_tpl = File.read(fpath)
        if tree_yml_tpl is None:
            shout(f'no tree.yml file in {fpath}')
            return
        tree_yml = self.jinja_env.from_string(tree_yml_tpl).render(context)
        tree = Yaml.load(tree_yml)
        return tree
예제 #5
0
 def write(self, abs_path: Text, data) -> None:
     File.write(abs_path, data)
예제 #6
0
 def read(self, abs_path: Text) -> object:
     return File.read(abs_path)
예제 #7
0
 def perform(self, value: Text) -> Text:
     return File.read(value)