Exemple #1
0
 def makedirs(path):
     if not dry_run:
         os.makedirs(path)
     valid(_('Directory %(f)s created.'), f=path)
Exemple #2
0
    def write_files(self, pypackage_name, dirname, context, destination_root, dry_run=False):
        """
        write a full directory of template or plain files. Filenames can be templated too.
        pypackage_name is the Python package containing templates
        dirname is the directory name inside the Python package
        context is the Jinja2 context
        destination_root is the destination directory
        """

        def get_path(src_root_, remaining_path, dst_root_, context_, prefix_len_):
            """return relative source path (to template dir) and destination path
            destination path is the absolute destination path
            """
            src_path_ = (src_root_ + '/' + remaining_path)[prefix_len_:]
            dst_path_ = src_path_
            if os.sep != '/':
                dst_path_ = dst_path_.replace('/', os.sep)
            dst_path_ = Template(dst_path_).render(**context_)
            if dst_path_.endswith('_tpl'):
                dst_path_ = dst_path_[:-4]
            return src_path_, os.path.join(dst_root_, dst_path_)

        def makedirs(path):
            if not dry_run:
                os.makedirs(path)
            valid(_('Directory %(f)s created.'), f=path)

        loader = PackageLoader(pypackage_name, dirname)
        env = Environment(loader=loader)
        env.filters['lower_first_letter'] = lower_first_letter
        destination_root = Template(destination_root).render(**context)
        if destination_root.endswith('_tpl'):
            destination_root = destination_root[:-4]
        if not os.path.isdir(destination_root):
            makedirs(destination_root)
        prefix_len = len(dirname) + 1
        for root, dirnames, filenames in walk(pypackage_name, dirname):
            new_dirnames = [dirname for dirname in dirnames]
            for dirname in new_dirnames:
                if dirname in ('.svn', '.git', '.hg', 'CVS'):
                    dirnames.remove(dirname)
                    continue
                src_path, dst_path = get_path(root, dirname, destination_root, context, prefix_len)
                if not os.path.isdir(dst_path):
                    makedirs(dst_path)
            for filename in filenames:
                if filename[-4:] == '_inc':
                    continue
                src_path, dst_path = get_path(root, filename, destination_root, context, prefix_len)
                if self.dry_run:
                    f_out = six.BytesIO()
                else:
                    if not os.path.isdir(os.path.dirname(dst_path)):
                        continue
                    if os.path.isdir(dst_path):
                        shutil.rmtree(dst_path)
                    elif os.path.exists(dst_path):
                        os.remove(dst_path)
                    f_out = open(dst_path, 'wb')
                if filename[-4:] == '_tpl':
                    template = env.get_template(src_path)
                    f_out.write(template.render(**context).encode('utf-8'))
                else:
                    f_in = pkg_resources.resource_stream(pypackage_name, root + '/' + filename)
                    data = f_in.read(10240)
                    while data:
                        f_out.write(data)
                        data = f_in.read(10240)
                    f_in.close()
                f_out.close()
                valid(_('File %(f)s written.'), f=dst_path)
Exemple #3
0
def info(text, stdout=None, stderr=None, **kwargs):
    sprint(yellow(_('INFO: '), bold=True) + yellow(text % kwargs))
    if stdout is not None:
        sprint(stdout)
    if stderr is not None:
        sprint(red(stderr))
Exemple #4
0
def warning(text, stdout=None, stderr=None, **kwargs):
    sprint(yellow(_('WARN: '), bold=True) + white(text % kwargs))
    if stdout is not None:
        sprint(stdout)
    if stderr is not None:
        sprint(red(stderr))
Exemple #5
0
def invalid(text, stdout=None, stderr=None, **kwargs):
    sprint(red(_('KO: '), bold=True) + red(text % kwargs))
    if stdout is not None:
        sprint(stdout)
    if stderr is not None:
        sprint(red(stderr))
Exemple #6
0
def valid(text, stdout=None, stderr=None, **kwargs):
    sprint(green(_('OK: '), bold=True) + green(text % kwargs))
    if stdout is not None:
        sprint(stdout)
    if stderr is not None:
        sprint(red(stderr))