def input(self, **kwargs): if self.filename is None: raise FilterError('VueCompiler can only compile files, not inline code.') if not os.path.exists(self.rollup_bin): raise FilterError("Rollup not installed, please run 'make npminstall' in source root.") self.options += (('export_name', re.sub( r'^([a-z])|[^a-z0-9A-Z]+([a-zA-Z0-9])?', lambda s: s.group(0)[-1].upper(), os.path.basename(self.filename).split('.')[0] )),) return super().input(**kwargs)
def output(self, **kwargs): options = dict(self.options) options['outfile'] = kwargs['outfile'] infiles = [] for infile in kwargs['content_meta']: # type, full_filename, relative_filename # In debug mode we use the full path so that in development we see changes without having to call # collectstatic. This breaks the sourcemaps. In production, we want sourcemaps to work so we # use relative path which will take files from `staticfiles` automatically. if settings.DEBUG: infiles.append(infile[1]) else: infiles.append(infile[2]) options['uglifyjs'] = os.path.join(settings.BASE_DIR, 'node_modules', 'uglify-js', 'bin', 'uglifyjs') options['infiles'] = ' '.join(f for f in infiles) options['mapfile'] = kwargs['outfile'].replace('.js', '.map.js') options['mapurl'] = '{}{}'.format(settings.STATIC_URL, options['mapfile']) options['maproot'] = settings.STATIC_URL self.cwd = kwargs['root_location'] try: command = fstr(self.command).format(**options) proc = subprocess.Popen(command, shell=True, cwd=self.cwd, stdout=self.stdout, stdin=self.stdin, stderr=self.stderr) err = proc.communicate() except (IOError, OSError) as e: raise FilterError('Unable to apply %s (%r): %s' % (self.__class__.__name__, self.command, e)) else: # If the process doesn't return a 0 success code, throw an error if proc.wait() != 0: if not err: err = ('Unable to apply %s (%s)' % (self.__class__.__name__, self.command)) raise FilterError(err) if self.verbose: self.logger.debug(err)
def input(self, **kwargs): options = dict(self.options) if self.infile is None: if "{infile}" in self.command: if self.filename is None: self.infile = tempfile.NamedTemporaryFile(mode="w") self.infile.write(self.content) self.infile.flush() os.fsync(self.infile) options["infile"] = self.infile.name else: self.infile = open(self.filename) options["infile"] = self.filename if "{outfile}" in self.command and not "outfile" in options: ext = ".%s" % self.type and self.type or "" self.outfile = tempfile.NamedTemporaryFile(mode='r+', suffix=ext) options["outfile"] = self.outfile.name try: command = fstr(self.command).format(**options) proc = subprocess.Popen(command, shell=True, cwd=self.cwd, stdout=self.stdout, stdin=self.stdin, stderr=self.stderr) if self.infile is None: filtered, err = proc.communicate(self.content) else: filtered, err = proc.communicate() except (IOError, OSError), e: raise FilterError('Unable to apply %s (%r): %s' % (self.__class__.__name__, self.command, e))
def output(self, **kwargs): options = dict(self.options) options['outfile'] = kwargs['outfile'] infiles = [] for infile in kwargs['content_meta']: # type, full_filename, relative_filename infiles.append(infile[2]) options['infiles'] = ' '.join(f for f in infiles) options['mapfile'] = kwargs['outfile'].replace('.js', '.map.js') options['mapurl'] = '{}{}'.format( settings.STATIC_URL, options['mapfile']) options['maproot'] = settings.STATIC_URL self.cwd = kwargs['root_location'] try: command = fstr(self.command).format(**options) proc = subprocess.Popen( command, shell=True, cwd=self.cwd, stdout=self.stdout, stdin=self.stdin, stderr=self.stderr) err = proc.communicate() except (IOError, OSError), e: raise FilterError('Unable to apply %s (%r): %s' % (self.__class__.__name__, self.command, e))
def output(self, **kwargs): infile = None outfile = None try: if "{infile}" in self.command: infile = tempfile.NamedTemporaryFile(mode='w') infile.write(self.content) infile.flush() self.options["infile"] = self.filename or infile.name if "{outfile}" in self.command: ext = ".%s" % self.type and self.type or "" outfile = tempfile.NamedTemporaryFile(mode='rw', suffix=ext) self.options["outfile"] = outfile.name command = stringformat.FormattableString(self.command) proc = subprocess.Popen(cmd_split(command.format(**self.options)), stdout=self.stdout, stdin=self.stdin, stderr=self.stderr) if infile is not None: filtered, err = proc.communicate() else: filtered, err = proc.communicate(self.content) except (IOError, OSError), e: raise FilterError('Unable to apply %s (%r): %s' % (self.__class__.__name__, self.command, e))
def input(self, **kwargs): if self.filename is None: raise FilterError('VueCompiler can only compile files, not inline code.') self.options += (('export_name', re.sub( r'^([a-z])|[^a-z0-9A-Z]+([a-zA-Z0-9])?', lambda s: s.group(0)[-1].upper(), os.path.basename(self.filename).split('.')[0] )),) return super().input(**kwargs)
def __init__(self, content, filter_type=None, verbose=0, command=None, **kwargs): super(CompilerFilter, self).__init__(content, filter_type, verbose) if command: self.command = command self.options.update(kwargs) if self.command is None: raise FilterError("Required command attribute not set") self.stdout = subprocess.PIPE self.stdin = subprocess.PIPE self.stderr = subprocess.PIPE
def __init__(self, content, command=None, filename=None, *args, **kwargs): super(CompilerFilter, self).__init__(content, *args, **kwargs) if command: self.command = command if self.command is None: raise FilterError("Required attribute 'command' not given") self.filename = filename self.stdout = subprocess.PIPE self.stdin = subprocess.PIPE self.stderr = subprocess.PIPE
def __init__(self, content, attrs, **kwargs): config_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'static', 'npm_dir') node_path = os.path.join(settings.STATIC_ROOT, 'node_prefix', 'node_modules') self.rollup_bin = os.path.join(node_path, 'rollup', 'dist', 'bin', 'rollup') rollup_config = os.path.join(config_dir, 'rollup.config.js') if not os.path.exists(self.rollup_bin) and not settings.DEBUG: raise FilterError("Rollup not installed or pretix not built properly, please run 'make npminstall' in source root.") command = ( ' '.join(( 'NODE_PATH=' + shlex.quote(node_path), shlex.quote(self.rollup_bin), '-c', shlex.quote(rollup_config)) ) + ' --input {infile} -n {export_name} --file {outfile}' ) super().__init__(content, command=command, **kwargs)
def __init__(self, content, command=None, *args, **kwargs): super(CompilerFilter, self).__init__(content, *args, **kwargs) self.cwd = None if command: self.command = command if self.command is None: raise FilterError("Required attribute 'command' not given") if isinstance(self.options, dict): new_options = () for item in kwargs.iteritems(): new_options += (item, ) self.options = new_options for item in kwargs.iteritems(): self.options += (item, ) self.stdout = subprocess.PIPE self.stdin = subprocess.PIPE self.stderr = subprocess.PIPE self.infile, self.outfile = None, None
def input(self, **kwargs): encoding = self.default_encoding options = dict(self.options) if self.infile is None and "{infile}" in self.command: # create temporary input file if needed if self.filename is None: self.infile = NamedTemporaryFile(mode='wb') self.infile.write(self.content.encode(encoding)) self.infile.flush() options["infile"] = self.infile.name else: # we use source file directly, which may be encoded using # something different than utf8. If that's the case file will # be included with charset="something" html attribute and # charset will be available as filter's charset attribute encoding = self.charset # or self.default_encoding self.infile = open(self.filename) options["infile"] = self.filename if "{outfile}" in self.command and "outfile" not in options: # create temporary output file if needed ext = self.type and ".%s" % self.type or "" self.outfile = NamedTemporaryFile(mode='r+', suffix=ext) options["outfile"] = self.outfile.name # Quote infile and outfile for spaces etc. if "infile" in options: options["infile"] = shell_quote(options["infile"]) if "outfile" in options: options["outfile"] = shell_quote(options["outfile"]) try: command = self.command.format(**options) proc = subprocess.Popen(command, shell=True, cwd=self.cwd, stdout=self.stdout, stdin=self.stdin, stderr=self.stderr) if self.infile is None: # if infile is None then send content to process' stdin filtered, err = proc.communicate(self.content.encode(encoding)) else: filtered, err = proc.communicate() filtered, err = filtered.decode(encoding), err.decode(encoding) except (IOError, OSError) as e: raise FilterError('Unable to apply %s (%r): %s' % (self.__class__.__name__, self.command, e)) else: if proc.wait() != 0: # command failed, raise FilterError exception if not err: err = ('Unable to apply %s (%s)' % (self.__class__.__name__, self.command)) if filtered: err += '\n%s' % filtered raise FilterError(err) if self.verbose: self.logger.debug(err) outfile_path = options.get('outfile') if outfile_path: with io.open(outfile_path, 'r', encoding=encoding) as file: filtered = file.read() finally: if self.infile is not None: self.infile.close() if self.outfile is not None: self.outfile.close() return smart_text(filtered)
ext = ".%s" % self.type and self.type or "" outfile = tempfile.NamedTemporaryFile(mode='w', suffix=ext) self.options["outfile"] = outfile.name cmd = stringformat.FormattableString(self.command).format(**self.options) proc = subprocess.Popen(cmd_split(cmd), stdout=self.stdout, stdin=self.stdin, stderr=self.stderr) if infile is not None: filtered, err = proc.communicate() else: filtered, err = proc.communicate(self.content) except (IOError, OSError), e: raise FilterError('Unable to apply %s (%r): %s' % ( self.__class__.__name__, self.command, e)) finally: if infile: infile.close() if proc.wait() != 0: if not err: err = 'Unable to apply %s (%s)' % ( self.__class__.__name__, self.command) raise FilterError(err) if self.verbose: self.logger.debug(err) if outfile is not None: try: outfile_obj = open(outfile.name) filtered = outfile_obj.read() finally: outfile_obj.close() return filtered