def convert(self, target, prefixes=[""], suffixes=[""], check=None, context=None): """ Use conversion rules to make a dependency tree for a given target file, and return the final node, or None if the file does not exist and cannot be built. The optional arguments 'prefixes' and 'suffixes' are lists of strings that can be added at the beginning and the end of the name when searching for the file. Prefixes are tried in order, and for each prefix, suffixes are tried in order; the first file from this order that exists or can be made is kept. The optional arguments 'check' and 'context' have the same meaning as in 'Converter.best_rule'. """ # Try all suffixes and prefixes until something is found. last = None for t in [p + target + s for s in suffixes for p in prefixes]: # Define a check function, according to preferences. if self.conv_prefs.has_key(t): prefs = self.conv_prefs[t] def do_check(vars, prefs=prefs): if prefs is not None: for key, val in prefs.items(): if not (vars.has_key(key) and vars[key] == val): return 0 return 1 else: prefs = None do_check = check # Find the best applicable rule. ans = self.converter.best_rule(t, check=do_check, context=context) if ans is not None: if last is None or ans["cost"] < last["cost"]: last = ans # Check if the target exists. if prefs is None and os.path.exists(t): if last is not None and last["cost"] <= 0: break msg.log(_("`%s' is `%s', no rule applied") % (target, t)) return rubber.depend.Leaf(self.depends, t) if last is None: return None msg.log( _("`%s' is `%s', made from `%s' by rule `%s'") % (target, last["target"], last["source"], last["name"])) return self.converter.apply(last)
def clean(self): """ Remove the files produced by this rule and recursively clean all dependencies. """ for file in self.products: if os.path.exists(file): msg.log(_("removing %s") % file) os.unlink(file) for source in self.source_nodes(): source.clean() self.date = None
def clean (self): """ Remove the files produced by this rule and recursively clean all dependencies. """ for file in self.products: if os.path.exists(file): msg.log(_("removing %s") % file) os.unlink(file) for source in self.source_nodes(): source.clean() self.date = None
def convert (self, target, prefixes=[""], suffixes=[""], check=None, context=None): """ Use conversion rules to make a dependency tree for a given target file, and return the final node, or None if the file does not exist and cannot be built. The optional arguments 'prefixes' and 'suffixes' are lists of strings that can be added at the beginning and the end of the name when searching for the file. Prefixes are tried in order, and for each prefix, suffixes are tried in order; the first file from this order that exists or can be made is kept. The optional arguments 'check' and 'context' have the same meaning as in 'Converter.best_rule'. """ # Try all suffixes and prefixes until something is found. last = None for t in [p + target + s for s in suffixes for p in prefixes]: # Define a check function, according to preferences. if self.conv_prefs.has_key(t): prefs = self.conv_prefs[t] def do_check (vars, prefs=prefs): if prefs is not None: for key, val in prefs.items(): if not (vars.has_key(key) and vars[key] == val): return 0 return 1 else: prefs = None do_check = check # Find the best applicable rule. ans = self.converter.best_rule(t, check=do_check, context=context) if ans is not None: if last is None or ans["cost"] < last["cost"]: last = ans # Check if the target exists. if prefs is None and os.path.exists(t): if last is not None and last["cost"] <= 0: break msg.log(_("`%s' is `%s', no rule applied") % (target, t)) return rubber.depend.Leaf(self.depends, t) if last is None: return None msg.log(_("`%s' is `%s', made from `%s' by rule `%s'") % (target, last["target"], last["source"], last["name"])) return self.converter.apply(last)
def main (self, cmdline): self.env = Environment() self.prologue = [] self.epilogue = [] self.act = None args = self.parse_opts(cmdline) if not self.act: self.act = "check" msg.log(_( "This is Rubber's information extractor version %s.") % version) if len(args) != 1: sys.stderr.write(_("You must specify one source file.\n")) sys.exit(1) src = args[0] if self.env.set_source(src): sys.stderr.write(_("I cannot find %s.\n") % src) sys.exit(1) if self.act == "deps": self.prepare(src) deps = {} for dep in self.env.main.source_nodes(): for file in dep.leaves(): deps[file] = None print(' '.join(deps.keys())) elif self.act == "rules": self.prepare(src) seen = {} next_ = [self.env.final] while len(next_) > 0: node = next_[0] next_ = next_[1:] if seen.has_key(node): continue seen[node] = None if len(node.sources) == 0: continue print("\n%s:" % ' '.join(node.products), end=',') print(' '.join(node.sources)) next_.extend(node.source_nodes()) else: self.prepare(src, parse=0) return self.info_log(self.act) return 0
def main(self, cmdline): self.env = Environment() self.prologue = [] self.epilogue = [] self.act = None args = self.parse_opts(cmdline) if not self.act: self.act = "check" msg.log( _("This is Rubber's information extractor version %s.") % version) if len(args) != 1: sys.stderr.write(_("You must specify one source file.\n")) sys.exit(1) src = args[0] if self.env.set_source(src): sys.stderr.write(_("I cannot find %s.\n") % src) sys.exit(1) if self.act == "deps": self.prepare(src) deps = {} for dep in self.env.main.source_nodes(): for file in dep.leaves(): deps[file] = None print(' '.join(deps.keys())) elif self.act == "rules": self.prepare(src) seen = {} next_ = [self.env.final] while len(next_) > 0: node = next_[0] next_ = next_[1:] if seen.has_key(node): continue seen[node] = None if len(node.sources) == 0: continue print("\n%s:" % ' '.join(node.products), end=',') print(' '.join(node.sources)) next_.extend(node.source_nodes()) else: self.prepare(src, parse=0) return self.info_log(self.act) return 0
def run(self): source = self.sources[0] if not os.path.exists(source): msg.info(_("{} not yet generated").format(msg.simplify(source)), pkg="asymptote") return True os.rename(self.aux, self.bak) msg.log(_("saving {} to {}").format(msg.simplify(self.aux), msg.simplify(self.bak)), pkg="asymptote") ret = super(Shell_Restoring_Aux, self).run() msg.log(_("restoring {} to {}").format(msg.simplify(self.aux), msg.simplify(self.bak)), pkg="asymptote") os.rename(self.bak, self.aux) return ret
def run (self): source = self.sources [0] if not os.path.exists (source): msg.info(_("{} not yet generated").format (msg.simplify (source)), pkg="asymptote") return True os.rename (self.aux, self.bak) msg.log (_ ("saving {} to {}").format (msg.simplify (self.aux), msg.simplify (self.bak)), pkg="asymptote") ret = super (Shell_Restoring_Aux, self).run () msg.log (_ ("restoring {} to {}").format (msg.simplify (self.aux), msg.simplify (self.bak)), pkg="asymptote") os.rename (self.bak, self.aux) return ret
def add_bib_resource(self, doc, opt, name): msg.log(_("bibliography resource discovered: %s" % name), pkg="biblio") options = rubber.util.parse_keyval(opt) # If the file name looks like it contains a control sequence # or a macro argument, forget about this resource. if name.find("\\") > 0 or name.find("#") > 0: return # skip Biber remote resources if "location" in options and options["location"] == "remote": return filename = self.find_bib(name) if filename is None: msg.error(_("cannot find bibliography resource %s") % name, pkg="biblatex") else: self.add_source(filename)
def add_bib_resource(self, doc, opt, name): msg.log(_("bibliography resource discovered: %s" % name), pkg="biblio") options = rubber.util.parse_keyval(opt) # If the file name looks like it contains a control sequence # or a macro argument, forget about this resource. if name.find('\\') > 0 or name.find('#') > 0: return # skip Biber remote resources if "location" in options and options["location"] == "remote": return filename = self.find_bib(name) if filename is None: msg.error(_("cannot find bibliography resource %s") % name, pkg="biblatex") else: self.add_source(filename)
def main (self, cmdline): """ Run Rubber for the specified command line. This processes each specified source in order (for making or cleaning). If an error happens while making one of the documents, the whole process stops. The method returns the program's exit code. """ args = self.parse_opts (cmdline) initial_dir = os.getcwd() msg.cwd = os.path.join(initial_dir, "") if self.place == ".": self.place = initial_dir if self.place is not None: msg.path = self.place self.place = os.path.abspath(self.place) msg.log (_("This is Rubber version %s.") % rubber_version) for srcname in args: src = os.path.join(initial_dir, srcname) # Go to the appropriate directory try: if self.place != ".": if self.place is None: msg.path = os.path.dirname(src) os.chdir(os.path.dirname(src)) else: os.chdir(self.place) except OSError as e: msg.error(_("Error changing to working directory: %s") % e.strerror) rubber.util.abort_generic_error () # prepare the source file. this may require a pre-processing # step, or dumping stdin. thus, the input filename may change. # in case of build mode, preprocessors will be run as part of # prepare_source. env = self.env = Environment () src = self.prepare_source (src) # safe mode is off during the prologue self.env.is_in_unsafe_mode_ = True if self.include_only is not None: env.main.includeonly (self.include_only) # at this point, the LaTeX source file must exist; if it is # the result of pre-processing, this has happened already. # the main LaTeX file is not found via find_file (unlike # most other resources) by design: paths etc may be set up # from within via rubber directives, so that wouldn't make a # whole lot of sense. if not os.path.exists (src): msg.error (_("LaTeX source file not found: '%s'") % src) rubber.util.abort_generic_error () saved_vars = env.main.vars env.main.vars = rubber.util.Variables (saved_vars, { "cwd": initial_dir }) for dir in self.path: env.main.do_path(dir) for cmd in self.prologue: cmd = rubber.util.parse_line (cmd, env.main.vars) env.main.command(cmd[0], cmd[1:], {'file': 'command line'}) env.main.vars = saved_vars # safe mode is enforced for anything that comes from the .tex file self.env.is_in_unsafe_mode_ = self.unsafe env.main.parse() saved_vars = env.main.vars env.main.vars = rubber.util.Variables (saved_vars, { "cwd": initial_dir }) for cmd in self.epilogue: cmd = rubber.util.parse_line (cmd, env.main.vars) env.main.command(cmd[0], cmd[1:], {'file': 'command line'}) env.main.vars = saved_vars if self.compress is not None: last_node = env.final filename = last_node.products[0] if self.compress == 'gzip': import gzip env.final = rubber.converters.compressor.Node ( env.depends, gzip.GzipFile, '.gz', filename) else: # self.compress == 'bzip2' import bz2 env.final = rubber.converters.compressor.Node ( env.depends, bz2.BZ2File, '.bz2', filename) self.process_source (env) exit (0)
def execute(self, prog, env={}, pwd=None, out=None, kpse=0): """ Silently execute an external program. The `prog' argument is the list of arguments for the program, `prog[0]' is the program name. The `env' argument is a dictionary with definitions that should be added to the environment when running the program. The standard output is passed line by line to the `out' function (or discarded by default). If the optional argument `kpse' is true, the error output is parsed and messages from Kpathsea are processed (to indicate e.g. font compilation), otherwise the error output is kept untouched. """ msg.info(_("executing: %s") % " ".join(prog)) if pwd: msg.log(_(" in directory %s") % pwd) if env != {}: msg.log(_(" with environment: %r") % env) progname = prog_available(prog[0]) if not progname: msg.error(_("%s not found") % prog[0]) return 1 penv = os.environ.copy() for (key, val) in env.items(): penv[key] = val if kpse: stderr = subprocess.PIPE else: stderr = None process = Popen(prog, executable=progname, env=penv, cwd=pwd, stdin=devnull(), stdout=subprocess.PIPE, stderr=stderr) if kpse: def parse_kpse(): for line in process.stderr.readlines(): line = line.rstrip() match = re_kpse.match(line) if not match: continue cmd = match.group("cmd") if self.kpse_msg.has_key(cmd): msg.progress(match.expand(self.kpse_msg[cmd])) else: msg.progress(_("kpathsea running %s") % cmd) t = threading.Thread(target=parse_kpse) t.start() #thread.start_new_thread(parse_kpse, ()) if out is not None: for line in process.stdout.readlines(): out(line) else: process.stdout.readlines() ret = process.wait() msg.log(_("process %d (%s) returned %d") % (process.pid, prog[0], ret)) return ret
def execute (self, prog, env={}, pwd=None, out=None, kpse=0): """ Silently execute an external program. The `prog' argument is the list of arguments for the program, `prog[0]' is the program name. The `env' argument is a dictionary with definitions that should be added to the environment when running the program. The standard output is passed line by line to the `out' function (or discarded by default). If the optional argument `kpse' is true, the error output is parsed and messages from Kpathsea are processed (to indicate e.g. font compilation), otherwise the error output is kept untouched. """ msg.info(_("executing: %s") % " ".join(prog)) if pwd: msg.log(_(" in directory %s") % pwd) if env != {}: msg.log(_(" with environment: %r") % env) progname = prog_available(prog[0]) if not progname: msg.error(_("%s not found") % prog[0]) return 1 penv = os.environ.copy() for (key,val) in env.items(): penv[key] = val if kpse: stderr = subprocess.PIPE else: stderr = None process = Popen(prog, executable = progname, env = penv, cwd = pwd, stdin = devnull(), stdout = subprocess.PIPE, stderr = stderr) if kpse: def parse_kpse (): for line in process.stderr.readlines(): line = line.rstrip() match = re_kpse.match(line) if not match: continue cmd = match.group("cmd") if self.kpse_msg.has_key(cmd): msg.progress(match.expand(self.kpse_msg[cmd])) else: msg.progress(_("kpathsea running %s") % cmd) t = threading.Thread(target=parse_kpse) t.start() #thread.start_new_thread(parse_kpse, ()) if out is not None: for line in process.stdout.readlines(): out(line) else: process.stdout.readlines() ret = process.wait() msg.log(_("process %d (%s) returned %d") % (process.pid, prog[0],ret)) return ret
def hook_newindex (self, loc, index, idx, ind): self.register(index, idx, ind, 'ilg') msg.log(_("index %s registered") % index, pkg='index')
def main (self, cmdline): """ Run Rubber for the specified command line. This processes each specified source in order (for making or cleaning). If an error happens while making one of the documents, the whole process stops. The method returns the program's exit code. """ self.jobname = None self.prologue = [] self.epilogue = [] self.clean = 0 self.force = 0 self.unsafe = False self.warn = 0 self.warn_boxes = 0 self.warn_misc = 0 self.warn_refs = 0 self.place = "." args = self.parse_opts(cmdline) initial_dir = os.getcwd() msg.cwd = os.path.join(initial_dir, "") if self.place != "." and self.place is not None: msg.path = self.place self.place = os.path.abspath(self.place) msg.log(_("This is Rubber version %s.") % version) for srcname in args: src = os.path.abspath(os.path.join(initial_dir, srcname)) # Go to the appropriate directory try: if self.place != ".": if self.place is None: msg.path = os.path.dirname(src) os.chdir(os.path.dirname(src)) src = os.path.basename(src) else: os.chdir(self.place) except OSError as e: msg.error(_("Error changing to working directory: %s") % e.strerror) return 1 # Check the source and prepare it for processing env = Environment() if env.set_source(src, jobname=self.jobname): return 1 self.jobname = None env.is_in_unsafe_mode_ = self.unsafe if self.include_only is not None: env.main.includeonly(self.include_only) if self.clean: if env.main.products == []: msg.warn(_("there is no LaTeX source for %s") % srcname) continue else: env.make_source() saved_vars = env.main.vars env.main.vars = Variables(saved_vars, { "cwd": initial_dir }) for dir in self.path: env.main.do_path(dir) for cmd in self.prologue: cmd = parse_line(cmd, env.main.vars) env.main.command(cmd[0], cmd[1:], {'file': 'command line'}) env.main.vars = saved_vars env.main.parse() saved_vars = env.main.vars env.main.vars = Variables(saved_vars, { "cwd": initial_dir }) for cmd in self.epilogue: cmd = parse_line(cmd, env.main.vars) env.main.command(cmd[0], cmd[1:], {'file': 'command line'}) env.main.vars = saved_vars if self.compress is not None: last_node = env.final filename = last_node.products[0] if self.compress == 'gzip': from rubber.converters.gz import GzipDep env.final = GzipDep(env.depends, filename + '.gz', filename) elif self.compress == 'bzip2': from rubber.converters.bzip2 import Bzip2Dep env.final = Bzip2Dep(env.depends, filename + '.bz2', filename) # Compile the document if self.clean: env.final.clean() continue if self.force: ret = env.main.make(True) if ret != ERROR and env.final is not env.main: ret = env.final.make() else: # This is a hack for the call to get_errors() below # to work when compiling failed when using -f. env.final.failed_dep = env.main.failed_dep else: ret = env.final.make(self.force) if ret == ERROR: msg.info(_("There were errors compiling %s.") % srcname) number = self.max_errors for err in env.final.failed().get_errors(): if number == 0: msg.info(_("More errors.")) break msg.display(**err) number -= 1 return 1 if ret == UNCHANGED: msg(1, _("nothing to be done for %s") % srcname) if self.warn: log = env.main.log if log.read(env.main.target + ".log"): msg.error(_("cannot read the log file")) return 1 msg.display_all(log.parse(boxes=self.warn_boxes, refs=self.warn_refs, warnings=self.warn_misc)) return 0
def main(self, cmdline): """ Run Rubber for the specified command line. This processes each specified source in order (for making or cleaning). If an error happens while making one of the documents, the whole process stops. The method returns the program's exit code. """ args = self.parse_opts(cmdline) initial_dir = os.getcwd() msg.cwd = os.path.join(initial_dir, "") if self.place == ".": self.place = initial_dir if self.place is not None: msg.path = self.place self.place = os.path.abspath(self.place) global rubber msg.log(_("This is Rubber version %s.") % rubber_version) for srcname in args: src = os.path.join(initial_dir, srcname) # Go to the appropriate directory try: if self.place != ".": if self.place is None: msg.path = os.path.dirname(src) os.chdir(os.path.dirname(src)) else: os.chdir(self.place) except OSError as e: msg.error( _("Error changing to working directory: %s") % e.strerror) rubber.util.abort_generic_error() # prepare the source file. this may require a pre-processing # step, or dumping stdin. thus, the input filename may change. # in case of build mode, preprocessors will be run as part of # prepare_source. env = self.env = Environment() self.env.is_in_unsafe_mode_ = self.unsafe src = self.prepare_source(src) if self.include_only is not None: env.main.includeonly(self.include_only) # at this point, the LaTeX source file must exist; if it is # the result of pre-processing, this has happened already. # the main LaTeX file is not found via find_file (unlike # most other resources) by design: paths etc may be set up # from within via rubber directives, so that wouldn't make a # whole lot of sense. if not os.path.exists(src): msg.error(_("LaTeX source file not found: '%s'") % src) rubber.util.abort_generic_error() saved_vars = env.main.vars env.main.vars = rubber.util.Variables(saved_vars, {"cwd": initial_dir}) for dir in self.path: env.main.do_path(dir) for cmd in self.prologue: cmd = rubber.util.parse_line(cmd, env.main.vars) env.main.command(cmd[0], cmd[1:], {'file': 'command line'}) env.main.vars = saved_vars env.main.parse() saved_vars = env.main.vars env.main.vars = rubber.util.Variables(saved_vars, {"cwd": initial_dir}) for cmd in self.epilogue: cmd = rubber.util.parse_line(cmd, env.main.vars) env.main.command(cmd[0], cmd[1:], {'file': 'command line'}) env.main.vars = saved_vars if self.compress is not None: last_node = env.final filename = last_node.products[0] if self.compress == 'gzip': import gzip env.final = rubber.converters.compressor.Node( env.depends, gzip.GzipFile, '.gz', filename) else: # self.compress == 'bzip2' import bz2 env.final = rubber.converters.compressor.Node( env.depends, bz2.BZ2File, '.bz2', filename) self.process_source(env) exit(0)
def hook_newindex(self, loc, index, idx, ind): self.register(index, idx, ind, 'ilg') msg.log(_("index %s registered") % index, pkg='index')