예제 #1
0
    def do_coffee_compile(self, opts, timestamp=False, ignore_errors=False):
        from calibre.utils.serve_coffee import compile_coffeescript
        src_files = {}
        for src in self.COFFEE_DIRS:
            for f in glob.glob(self.j(self.SRC, __appname__, src,
                '*.coffee')):
                bn = os.path.basename(f).rpartition('.')[0]
                arcname = src.replace('/', '.') + '.' + bn + '.js'
                try:
                    with open(f, 'rb') as fs:
                        src_files[arcname] = (f, hashlib.sha1(fs.read()).hexdigest())
                except EnvironmentError:
                    time.sleep(0.1)
                    with open(f, 'rb') as fs:
                        src_files[arcname] = (f, hashlib.sha1(fs.read()).hexdigest())

        existing = {}
        dest = self.j(self.RESOURCES, 'compiled_coffeescript.zip')
        if os.path.exists(dest):
            with zipfile.ZipFile(dest, 'r') as zf:
                existing_hashes = {}
                raw = zf.comment
                if raw:
                    existing_hashes = json.loads(raw)
                for info in zf.infolist():
                    if info.filename in existing_hashes and src_files.get(info.filename, (None, None))[1] == existing_hashes[info.filename]:
                        existing[info.filename] = (zf.read(info), info, existing_hashes[info.filename])

        todo = set(src_files) - set(existing)
        updated = {}
        for arcname in todo:
            name = arcname.rpartition('.')[0]
            print ('\t%sCompiling %s'%(time.strftime('[%H:%M:%S] ') if
                        timestamp else '', name))
            src, sig = src_files[arcname]
            js, errors = compile_coffeescript(open(src, 'rb').read(), filename=src)
            if errors:
                print ('\n\tCompilation of %s failed'%name)
                for line in errors:
                    print >>sys.stderr, line
                if ignore_errors:
                    js = u'# Compilation from coffeescript failed'
                else:
                    raise SystemExit(1)
            else:
                if opts.show_js:
                    self.show_js(js)
                    print ('#'*80)
                    print ('#'*80)
            zi = zipfile.ZipInfo()
            zi.filename = arcname
            zi.date_time = time.localtime()[:6]
            updated[arcname] = (js.encode('utf-8'), zi, sig)
        if updated:
            hashes = {}
            with zipfile.ZipFile(dest, 'w', zipfile.ZIP_STORED) as zf:
                for raw, zi, sig in sorted(chain(updated.itervalues(), existing.itervalues()), key=lambda x: x[1].filename):
                    zf.writestr(zi, raw)
                    hashes[zi.filename] = sig
                zf.comment = json.dumps(hashes)
예제 #2
0
    def do_coffee_compile(self, opts, timestamp=False, ignore_errors=False):
        from calibre.utils.serve_coffee import compile_coffeescript
        src_files = {}
        for src in self.COFFEE_DIRS:
            for f in glob.glob(self.j(self.SRC, __appname__, src,
                '*.coffee')):
                bn = os.path.basename(f).rpartition('.')[0]
                arcname = src.replace('/', '.') + '.' + bn + '.js'
                try:
                    with open(f, 'rb') as fs:
                        src_files[arcname] = (f, hashlib.sha1(fs.read()).hexdigest())
                except EnvironmentError:
                    time.sleep(0.1)
                    with open(f, 'rb') as fs:
                        src_files[arcname] = (f, hashlib.sha1(fs.read()).hexdigest())

        existing = {}
        dest = self.j(self.RESOURCES, 'compiled_coffeescript.zip')
        if os.path.exists(dest):
            with zipfile.ZipFile(dest, 'r') as zf:
                existing_hashes = {}
                raw = zf.comment
                if raw:
                    existing_hashes = json.loads(raw)
                for info in zf.infolist():
                    if info.filename in existing_hashes and src_files.get(info.filename, (None, None))[1] == existing_hashes[info.filename]:
                        existing[info.filename] = (zf.read(info), info, existing_hashes[info.filename])

        todo = set(src_files) - set(existing)
        updated = {}
        for arcname in todo:
            name = arcname.rpartition('.')[0]
            print ('\t%sCompiling %s'%(time.strftime('[%H:%M:%S] ') if
                        timestamp else '', name))
            src, sig = src_files[arcname]
            js, errors = compile_coffeescript(open(src, 'rb').read(), filename=src)
            if errors:
                print ('\n\tCompilation of %s failed'%name)
                for line in errors:
                    print >>sys.stderr, line
                if ignore_errors:
                    js = u'# Compilation from coffeescript failed'
                else:
                    raise SystemExit(1)
            else:
                if opts.show_js:
                    self.show_js(js)
                    print ('#'*80)
                    print ('#'*80)
            zi = zipfile.ZipInfo()
            zi.filename = arcname
            zi.date_time = time.localtime()[:6]
            updated[arcname] = (js.encode('utf-8'), zi, sig)
        if updated:
            hashes = {}
            with zipfile.ZipFile(dest, 'w', zipfile.ZIP_STORED) as zf:
                for raw, zi, sig in sorted(chain(updated.itervalues(), existing.itervalues()), key=lambda x: x[1].filename):
                    zf.writestr(zi, raw)
                    hashes[zi.filename] = sig
                zf.comment = json.dumps(hashes)
예제 #3
0
def _compile_coffeescript(name):
    from calibre.utils.serve_coffee import compile_coffeescript
    src = js_name_to_path(name)
    with open(src, 'rb') as f:
        cs, errors = compile_coffeescript(f.read(), src)
        if errors:
            for line in errors:
                print(line)
            raise Exception('Failed to compile coffeescript' ': %s' % src)
        return cs
예제 #4
0
def _compile_coffeescript(name):
    from calibre.utils.serve_coffee import compile_coffeescript
    src = js_name_to_path(name)
    with open(src, 'rb') as f:
        cs, errors = compile_coffeescript(f.read(), src)
        if errors:
            for line in errors:
                print (line)
            raise Exception('Failed to compile coffeescript'
                    ': %s'%src)
        return cs
예제 #5
0
def _compile_coffeescript(name):
    from calibre.utils.serve_coffee import compile_coffeescript
    path = (u'/'.join(name.split('.'))) + '.coffee'
    d = os.path.dirname
    base = d(d(os.path.abspath(__file__)))
    src = os.path.join(base, path)
    with open(src, 'rb') as f:
        cs, errors = compile_coffeescript(f.read(), src)
        if errors:
            for line in errors:
                print (line)
            raise Exception('Failed to compile coffeescript'
                    ': %s'%src)
        return cs