Exemplo n.º 1
0
def compile(base_dir, output_dir, dest_dir):
    config = g['config']
    _remove_old_files(output_dir)

    rel_output = '/' + output_dir[len(dest_dir):].lstrip('/')

    static_dirs = set()

    for key in g:
        if not isinstance(key, tuple):
            continue
        unique_key = _gen_key()
        filemap = g[key]
        ext = extensions[key[0]]
        files = OrderedDict((filename, 1) for template in filemap for filename in filemap[template]).keys()
        file_comp = collections.defaultdict(list)
        for filename in files:
            aggregate = _decorate_key(config['map'].get(filename, 'maincompiled.' + ext), unique_key)
            file_comp[aggregate].append(filename)
        for target, filelist in file_comp.items():
            abstarget = os.path.join(output_dir, target)
            absfilelist = [os.path.join(base_dir, filename) for filename in filelist]
            logger.info('Compiling {0}'.format(abstarget))
            combined_file_obj = None
            if len(absfilelist) == 1:
                combined_file = absfilelist[0]
            else:
                combined_file_obj = _combine_files(absfilelist, ext)
                combined_file = combined_file_obj.name

            compiler_fmt = compilers[key[0]]
            if '%(input)s' in compiler_fmt:
                data = None
                cmd = compiler_fmt % {'input': combined_file}
            else:
                data = read_file_data([combined_file])
                cmd = compiler_fmt
            output = run_command(cmd, data=data)

            with open(abstarget, 'wb+') as f:
                f.write(output.std_out)
            target = os.path.join(rel_output, target)
            g['compiled'].update(dict((filename, target) for filename in filelist))

            if key[0] == 'text/css':
                static_dirs.update(os.path.dirname(filename) for filename in absfilelist)

    for d in static_dirs:
        for dirpath, dirnames, filenames in os.walk(d, followlinks=True):
            reldir = dirpath[len(d):].lstrip('/')
            target_dir = os.path.join(output_dir, reldir)
            for filepath in filenames:
                target_path = os.path.join(target_dir, filepath)
                filepath = os.path.join(dirpath, filepath)
                if is_updated(filepath, target_path):
                    new_dir = os.path.dirname(target_path)
                    if not os.path.exists(new_dir):
                        os.makedirs(new_dir)
                    shutil.copyfile(filepath, target_path)
Exemplo n.º 2
0
def walk_for_changed(source, dest, dependencies):
    changed = set()
    for dirpath, dirnames, filenames in os.walk(source, followlinks=True):
        reldir = dirpath[len(source):].lstrip('/')
        for filename in filenames:
            if not filename.lower().endswith('.html'):
                continue
            name = os.path.join(reldir, filename)
            if is_updated(os.path.join(source, reldir, filename),
                          os.path.join(dest, reldir, filename)):
                changed.add(name)
                changed.update(dependencies.get_affected_files(name))
    return changed
Exemplo n.º 3
0
def handle_precompile_file(source, dest, incremental=False):
    if '.' not in os.path.basename(source):
        return False
    ext = source.rsplit('.', 1)[1]
    if not (ext in ext_mime and ext_mime[ext] in pre_compilers):
        return False
    compiler, type_, new_ext, incremental = pre_compilers[ext_mime[ext]][:4]
    if not incremental:
        return
    dest = rename_ext(dest, new_ext)
    if incremental and not is_updated(source, dest):
        return False
    _run_precompile(source, dest, compiler)
    return True
Exemplo n.º 4
0
def pre_compile(src, type_, head, ctxname):
    compiler, type_, ext = pre_compilers[type_][:3]
    key = (type_, head)
    script_list = g[key].setdefault(ctxname, [])
    old_file = os.path.join(g['base_dir'], src.lstrip('/'))
    new_name = os.path.join(os.path.dirname(src).strip('/'), 'compiled-' + hashlib.md5(src).hexdigest()) + "." + ext

    if new_name in script_list:
        return

    script_list.append(new_name)

    new_name = os.path.join(g['base_dir'], new_name.lstrip('/'))

    if not is_updated(old_file, new_name):
        return

    _run_precompile(old_file, new_name, compiler)
Exemplo n.º 5
0
def compile_file(env, source_name, source_file, dest_file, incremental):
    if incremental and not is_updated(source_file, dest_file):
        return

    if dest_file:
        logger.info("Compiling {0} -> {1}".format(source_file, dest_file))
    ctx = {
        'datetime': datetime,
        'env': EnvWrapper(),
        'file': source_name,
        }
    try:
        template = env.get_template(source_name)
        run_plugins(global_config[0]['plugins'], template)
        result = template.render(ctx).encode('utf8')
    except Exception as e:
        logger.error("Error compiling {0}".format(source_name), exc_info=True)
        return
    if not dest_file:
        return
    with with_dir(open, dest_file, 'w+') as f:
        f.write(result)
Exemplo n.º 6
0
def copy_file(source, dest, incremental):
    if not incremental or is_updated(source, dest):
        if not os.path.exists(os.path.dirname(dest)):
            os.makedirs(os.path.dirname(dest))
        logger.debug("Copying file {0} to output directory".format(source))
        shutil.copyfile(source, dest)