Exemple #1
0
def load_dir(registry, dirname, directory):
    storage = registry.settings['amd.debug.data']

    cache = storage['cache'].get(dirname)
    if cache is None:
        cache = storage['cache'][dirname] = {'mtime': 0, 'files': []}

    all_mods = storage['mods']

    # check dir mtime
    mtime = os.path.getmtime(directory)
    if os.path.getmtime(directory) > cache['mtime']:
        # unload info
        for mod, info in list(all_mods.items()):
            if 'path' in info and info['path'].startswith(directory):
                del all_mods[mod]

        cache.update(mtime=mtime, files={})

    mods = []
    for filename in os.listdir(directory):
        filepath = os.path.join(directory, filename)

        # check mtime (caching)
        try:
            mtime = os.path.getmtime(filepath)
        except OSError:
            continue

        if filename in cache['files'] and mtime <= cache['files'][filename]:
            continue

        cache['files'][filename] = mtime

        p = os.path.join(dirname, filename)

        if filename.endswith('.js'):
            with open(os.path.join(directory, filename), 'rb') as f:
                for name, deps in amd.extract_mod(filename[:-3],
                                                  text_(f.read()), p):
                    mods.append((name, p, amd.JS_MOD, filepath))
        if filename.endswith('.css'):
            mods.append((filename, p, amd.CSS_MOD, filepath))

    for name, fname, tp, fpath in mods:
        if name in all_mods and fpath != all_mods[name]['fpath']:
            log.error(
                "amd module '%s' already exists in '%s' file, skipping '%s'",
                name, all_mods[name]['fpath'], fpath)
        else:
            log.info("Update module information: %s path:%s", name, fname)

            md5 = hashlib.md5()
            md5.update(open(fpath, 'rb').read())
            all_mods[name] = {
                'fname': fname,
                'tp': tp,
                'fpath': fpath,
                'md5': md5.hexdigest()
            }
Exemple #2
0
def load_dir(registry, dirname, directory):
    storage = registry.settings['amd.debug.data']

    cache = storage['cache'].get(dirname)
    if cache is None:
        cache = storage['cache'][dirname] = {'mtime': 0, 'files': []}

    all_mods = storage['mods']

    # check dir mtime
    mtime = os.path.getmtime(directory)
    if os.path.getmtime(directory) > cache['mtime']:
        # unload info
        for mod, info in list(all_mods.items()):
            if 'path' in info and info['path'].startswith(directory):
                del all_mods[mod]

        cache.update(mtime=mtime, files={})

    mods = []
    for filename in os.listdir(directory):
        filepath = os.path.join(directory, filename)

        # check mtime (caching)
        try:
            mtime = os.path.getmtime(filepath)
        except OSError:
            continue

        if filename in cache['files'] and mtime <= cache['files'][filename]:
            continue

        cache['files'][filename] = mtime

        p = os.path.join(dirname, filename)

        if filename.endswith('.js'):
            with open(os.path.join(directory, filename), 'rb') as f:
                for name, deps in amd.extract_mod(
                        filename[:-3], text_(f.read()), p):
                    mods.append((name, p, amd.JS_MOD, filepath))
        if filename.endswith('.css'):
            mods.append((filename, p, amd.CSS_MOD, filepath))

    for name, fname, tp, fpath in mods:
        if name in all_mods and fpath != all_mods[name]['fpath']:
            log.error(
                "amd module '%s' already exists in '%s' file, skipping '%s'",
                name, all_mods[name]['fpath'], fpath)
        else:
            log.info("Update module information: %s path:%s", name, fname)

            md5 = hashlib.md5()
            md5.update(open(fpath, 'rb').read())
            all_mods[name] = {
                'fname': fname, 'tp': tp, 'fpath': fpath,
                'md5': md5.hexdigest()}
Exemple #3
0
    def test_extract_mod(self):
        from pyramid_amdjs.amd import extract_mod

        res = extract_mod(
            'test', "define('test2', ['test3', 'test4'], function(){})", None)
        self.assertEqual(list(res), [('test2', ['test3', 'test4'])])