Пример #1
0
    def run_task_file(cls, config_file, task, **kwargs):
        config_path = get_config(config_file)

        # Check if file exists
        if config_path is None:
            if config_file:
                logger.critical("File %r not found", config_file)

            else:
                logger.critical("No suitable config file could be found")

            return False

        # Load configuration file
        try:
            logger.info('Loading config file %r', config_path)
            config = ConfigSource(BuildRunner.__environment_replacements).load(config_path)

        except ParseError, parse_error:
            logger.error('Parse error: %s', parse_error)
            logger.exception()
            if not kwargs.get('guarded', True):
                raise

            return False
Пример #2
0
    def get_source(self, source):
        if isinstance(source, basestring):
            source = self.__config.sources[source]

        logger.debug('Trying to parse source %r' % source.name)
        if source not in self.__source_results:
            parser = source.parser.load(self.__locator)
            if isinstance(parser, type) and issubclass(parser, SourceBase):
                parser = parser()

            if not isinstance(parser, SourceBase):
                raise BuilderError("Parser reference %s could not be loaded" % parser)

            res = parser.load(source.resource, self.__locator)
            if isinstance(res, etree._ElementTree):
                res = res.getroot()

            for transform in source.transform:
                modtrans = transform.load(self.__locator)
                res = modtrans(res)

            self.__source_results[source] = res
            logger.info('Source %r successfuly parsed' % source.name)

        else:
            logger.debug('Source already parsed')

        return self.__source_results[source]
Пример #3
0
    def get_source(self, source):
        if isinstance(source, basestring):
            source = self.__config.sources[source]

        logger.debug('Trying to parse source %r' % source.name)
        if source not in self.__source_results:
            parser = source.parser.load(self.__locator)
            if isinstance(parser, type) and issubclass(parser, SourceBase):
                parser = parser()

            if not isinstance(parser, SourceBase):
                raise BuilderError("Parser reference %s could not be loaded" %
                                   parser)

            res = parser.load(source.resource, self.__locator)
            if isinstance(res, etree._ElementTree):
                res = res.getroot()

            for transform in source.transform:
                modtrans = transform.load(self.__locator)
                res = modtrans(res)

            self.__source_results[source] = res
            logger.info('Source %r successfuly parsed' % source.name)

        else:
            logger.debug('Source already parsed')

        return self.__source_results[source]
Пример #4
0
        def exclude_files(filename):
            exclude = (filename[-4:] == '.pyc' or os.path.basename(filename)[0] == '.')
            if exclude:
                logger.debug('File %r not included', filename)

            else:
                logger.info('Adding file %r', filename)

            return exclude
Пример #5
0
    def execute(self):
        out = open(self._arg, 'w')
        self.create_compressed_script(out)
        out.close()

        os.chmod(self._arg, 0755)

        logger.info('Output %r created', self._arg)
        return True
Пример #6
0
    def run(self, argv):
        try:
            logger.info('Running command %s with arguments (%s)', self.name, ', '.join(map(repr, argv)))
            if not self.prepare(argv):
                return False

            return self.execute()

        except Exception, e:
            self.handle_exception(str(e), traceback.format_exc())
            return False
Пример #7
0
    def _collect(self):
        '''Collect subgenerators from instance'''

        for factory, generator in collect_marked_bound(self, 'generator'):
            priority = get_mark_default(generator, 'priority', PRI_BASE)
            matchers = get_mark_default(generator, 'matchers', [])

            logger.info('Registering sub-generator %r into generator %r; priority=%d, matchers=%r', generator, self, priority, matchers)

            if factory is not None:
                logger.debug('Factory found for sub-generator %r: %r', generator, factory)
                generator = factory(generator)

            self.register(generator, priority=priority, matcher=ObjectGenerator.__get_internal_matcher(matchers))
Пример #8
0
    def remove_dest_file(self, relpath):
        logger.debug('Trying to remove %r' % relpath)
        try:
            destination = self.get_target_path(relpath)

        except BuilderError:
            logger.debug('Path containing %r not found' % relpath)
            return

        if os.path.isfile(destination):
            logger.info('Removing file %s' % destination)
            os.unlink(destination)

        else:
            logger.debug('File %r not found' % relpath)
Пример #9
0
    def remove_dest_file(self, relpath):
        logger.debug('Trying to remove %r' % relpath)
        try:
            destination = self.get_target_path(relpath)

        except BuilderError:
            logger.debug('Path containing %r not found' % relpath)
            return

        if os.path.isfile(destination):
            logger.info('Removing file %s' % destination)
            os.unlink(destination)

        else:
            logger.debug('File %r not found' % relpath)
Пример #10
0
    def run_task(self, task, *args, **kwargs):
        if not self.__builders:
            logger.error("No builders found")
            return False

        for builder in self.__builders:
            try:
                if not builder.run_task(task, *args, **kwargs):
                    logger.info('Builder has no task %s' % task)

                else:
                    logger.info('Completed task %s on %s' % (task, builder))

            except Exception, error:
                logger.critical('Could not complete %s: %s', task, error)
                logger.exception()
                return False
Пример #11
0
    def run_task(self, task, *args, **kwargs):
        if not self.__builders:
            logger.error("No builders found")
            return False

        for builder in self.__builders:
            try:
                if not builder.run_task(task, *args, **kwargs):
                    logger.info('Builder has no task %s' % task)

                else:
                    logger.info('Completed task %s on %s' % (task, builder))

            except Exception, error:
                logger.critical('Could not complete %s: %s', task, error)
                logger.exception()
                return False
Пример #12
0
    def run_task_file(cls, config_file, task, **kwargs):
        config_path = get_config(config_file)

        # Check if file exists
        if config_path is None:
            if config_file:
                logger.critical("File %r not found", config_file)

            else:
                logger.critical("No suitable config file could be found")

            return False

        # Load configuration file
        try:
            logger.info('Loading config file %r', config_path)
            config = ConfigSource(
                BuildRunner.__environment_replacements).load(config_path)

        except ParseError, parse_error:
            logger.error('Parse error: %s', parse_error)
            logger.exception()
            return False
Пример #13
0
    def create_compressed_script(self, target):
        '''Create a script containing the compressed codega module and the main script

        Arguments:
        target -- Target file object
        '''

        def exclude_files(filename):
            exclude = (filename[-4:] == '.pyc' or os.path.basename(filename)[0] == '.')
            if exclude:
                logger.debug('File %r not included', filename)

            else:
                logger.info('Adding file %r', filename)

            return exclude

        def compress(target, name, path):
            tar.add(path, arcname=name, exclude=exclude_files)

        def checksum(data):
            return sha256(data).hexdigest()

        def split_data(data, line_width=80):
            enc = b64encode(data)
            res = []
            while enc:
                line = '#>>' + enc[:line_width] + '<<'
                enc = enc[line_width:]
                res.append(line)

            return '\n'.join(res)

        def load_main_code():
            '''Load cgmake code'''

            cgmake = []
            start = False

            for i in open(sys.argv[0]):
                i = i.rstrip()

                if i[:6] == '# MARK':
                    start = True
                    continue

                if not start:
                    continue

                cgmake.append(i)

            return '\n'.join(cgmake)

        logger.info('Creating self-contained script')
        logger.info('Compressing codega path %r', os.path.dirname(codega.__file__))

        output = StringIO()
        tar = TarFile.open(fileobj=output, mode='w:bz2')
        compress(tar, 'codega', os.path.dirname(codega.__file__))

        tar.close()
        data = output.getvalue()

        checksum = checksum(data)
        logger.info('Data compressed, checksum is %s' % checksum)
        split = split_data(data)
        main_code = load_main_code()

        print >> target, '''#!/usr/bin/env python

import os
import os.path
import tarfile
import sys
import base64


outdir = '.codega-%(checksum)s'
outdir = os.path.join(os.path.dirname(__file__), outdir)


def check_decompress():
    global outdir

    try:
        from cStringIO import StringIO

    except ImportError:
        from StringIO import StringIO

    if os.path.isdir(outdir) and os.path.isdir(os.path.join(outdir, 'codega')):
        return

    if not os.path.isdir(outdir):
        if os.path.exists(outdir):
            raise Exception("%%s exists but is not a directory" %% outdir)

        data = ''.join(map(lambda l: l[3:-3], filter(lambda l: l[:3] == '#>>' and l[-3:] == '<<\\n', open(__file__))))
        data = base64.b64decode(data)

        os.mkdir(outdir)
        inobj = StringIO(data)
        tarfile.open(fileobj = inobj, mode = 'r:*').extractall(path = outdir)

sys.path.insert(0, outdir)
check_decompress()

%(main_code)s

%(split)s
''' % locals()
        logger.info('Self-contained script created')