예제 #1
0
    def execute(self, args):

        base = os.path.realpath('.')

        tempdir = self.config.get('compilation', 'tempdir')
        tempdir = os.path.realpath(tempdir)

        master = args.master

        if args.define:
            defs = [a.split('=', 1) for a in args.define]
        else:
            defs = []

        name = os.path.basename(os.getcwd())
        if master != 'master':
            name = '{}-{}'.format(name, master)

        if args.destination:
            dest = args.destination
        else:
            dest = os.path.join(os.path.realpath('.'), name + '.pdf')

        ignore_regex_str = self.config.get('watch', 'ignore')
        if ignore_regex_str:
            ignore_regex = re.compile(ignore_regex_str)

        processors = loadprocessors(self.config, self.logger)

        def handler(event):
            relative = event.path[len(base) + 1:]

            self.logger.debug('Event received: {!r}'.format(event))

            if relative.startswith('.git/'):
                self.logger.debug('Ignoring GIT action')
                return

            if ignore_regex_str and ignore_regex.search(relative):
                self.logger.debug('Ignoring {!r} because of ignore regex '
                                  'from config'.format(relative))
                return

            if event.path.startswith(tempdir):
                self.logger.debug('Ignoring {!r}'.format(relative))
                return

            if event.path == dest:
                self.logger.debug('Ignoring {!r}'.format(relative))
                return

            if os.path.basename(event.path).startswith('.'):
                self.logger.debug('Ignoring {!r}'.format(relative))
                return

            if os.path.basename(event.path).endswith('~'):
                self.logger.debug('Ignoring {!r}'.format(relative))
                return

            if os.path.isdir(event.path):
                self.logger.debug('Ignoring directory {!r}'.format(relative))
                return

            if event.path.endswith('.log'):
                self.logger.debug('Ignoring {!r}'.format(relative))
                return

            for p in processors:
                if p.wants(event.path):
                    p.process_file(event.path)
                    return

            if isinstance(event, monitor.base.FileCreated):
                if event.path.endswith('.tex'):
                    subdir = os.path.dirname(relative)
                    builddir = os.path.join(tempdir, subdir)
                    if not os.path.exists(builddir):
                        # If a new .tex file was added and the containing
                        # directory does not exist in the build root, create it
                        os.mkdir(builddir)
                        self.logger.info('Added subdirectory {!r} to the '
                                         'build directory'.format(subdir))

            if event.path.endswith('.bib'):
                self.logger.info('Change detected at {!r}, recompiling with '
                                 'bibliography support...'.format(relative))
                self.compile(tempdir, master, defs=defs)
                self.compile_bib(tempdir, master)
                self.compile(tempdir, master, defs=defs)
                self.compile(tempdir, master, dest, defs=defs)
                self.logger.info('All done')
            elif event.path.endswith('.gls'):
                self.logger.info('Change detected at {!r}, recompiling with '
                                 'glossary support...'.format(relative))
                self.compile(tempdir, master, defs=defs)
                self.compile_glossary(tempdir, master, defs=defs)
                self.compile(tempdir, master, defs=defs)
                self.compile(tempdir, master, dest, defs=defs)
                self.logger.info('All done')
            else:
                self.logger.info('Change detected at {!r}, recompiling...'
                                 .format(relative))
                nomencl = self.get_nomencl_version(tempdir, master)
                success = self.compile(tempdir, master, defs=defs)
                if success and nomencl != self.get_nomencl_version(tempdir,
                                                                   master):
                    self.logger.info(
                        'Detected nomenclature change, recompiling...')
                    self.compile_nomencl(tempdir, master)
                    self.compile(tempdir, master, defs=defs)
                    success = self.compile(tempdir, master, defs=defs)
                    self.logger.info('All done')

                if success:
                    self.copy_pdf(tempdir, master, dest)

        self.mktempdir(tempdir)

        if args.initial:
            self.logger.info('Compiling initial version...'.format(base))
            self.compile(tempdir, master, dest, defs=defs)

        self.logger.info('Watching {} for changes...'.format(base))

        observer = monitor.Observer()
        observer.monitor(os.path.realpath('.'), handler)
        observer.start()

        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
예제 #2
0
    def execute(self, args):
        tempdir = self.config.get('compilation', 'tempdir')
        tempdir = os.path.realpath(tempdir)

        master = args.master

        if args.define:
            defs = [a.split('=', 1) for a in args.define]
        else:
            defs = []

        name = os.path.basename(os.getcwd())
        if master != 'master':
            name = '{}-{}'.format(name, master)

        if args.destination:
            dest = args.destination
        else:
            dest = os.path.join(os.path.realpath('.'), name + '.pdf')

        self.mktempdir(tempdir)

        # TODO: Make a plugin architecture to allow additional actions
        # to be run when compiling. This would allow to move the bibtex,
        # glossary, nomenclature and preprocessors out of this class.

        processors = loadprocessors(self.config, self.logger)

        if processors:
            for root, dirs, files in os.walk(os.path.realpath('.')):
                for file in files:
                    source = os.path.join(root, file)

                    for processor in processors:
                        if processor.wants(source):
                            processor.process_file(source)

        nomencl = self.get_nomencl_version(tempdir, master)

        success = self.compile(tempdir, master, defs=defs)

        nomencl = nomencl != self.get_nomencl_version(tempdir, master)

        recompile = args.bibtex or args.glossary or nomencl

        if success and recompile:
            support = []
            if args.bibtex:
                support.append('bibliography')
            if args.glossary:
                support.append('glossaries')
            if nomencl:
                support.append('nomenclature')

            support = ' and '.join([', '.join(support[:-1]), support[-1]])

            self.logger.info('Compiling document with {} support'.format(
                support))

            if args.bibtex:
                self.compile_bib(tempdir, master)

            if args.glossary:
                self.compile_glossary(tempdir, master)

            if nomencl:
                self.compile_nomencl(tempdir, master)

            self.compile(tempdir, master, defs=defs)
            success = self.compile(tempdir, master, defs=defs)

        if success:
            self.copy_pdf(tempdir, master, dest)