def get_tex_root(view): view_file = view.file_name() root = view_file directives = parse_tex_directives(view, only_for=['root']) try: root = directives['root'] except KeyError: pass else: if not is_tex_file(root): root = view_file if not os.path.isabs(root) and view_file is not None: file_path, _ = os.path.split(view_file) root = os.path.normpath(os.path.join(file_path, root)) if root == view_file: root = get_tex_root_from_settings(view) if root is not None: return root return view_file return root
def run(self, cmd="", file_regex="", path=""): # Try to handle killing with self.proc_lock: if self.proc: # if we are running, try to kill running process self.output("\n\n### Got request to terminate compilation ###") if sublime.platform() == 'windows': startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW subprocess.call( 'taskkill /t /f /pid {pid}'.format(pid=self.proc.pid), startupinfo=startupinfo, shell=True ) else: os.killpg(self.proc.pid, signal.SIGTERM) self.proc = None return else: # either it's the first time we run, or else we have no running processes self.proc = None view = self.view = self.window.active_view() if view.is_dirty(): print ("saving...") view.run_command('save') # call this on view, not self.window if view.file_name() is None: sublime.error_message('Please save your file before attempting to build.') return self.file_name = getTeXRoot.get_tex_root(view) if not os.path.isfile(self.file_name): sublime.error_message(self.file_name + ": file not found.") return self.tex_base, self.tex_ext = os.path.splitext(self.file_name) tex_dir = os.path.dirname(self.file_name) if not is_tex_file(self.file_name): sublime.error_message("%s is not a TeX source file: cannot compile." % (os.path.basename(view.file_name()),)) return # Output panel: from exec.py if not hasattr(self, 'output_view'): self.output_view = self.window.get_output_panel("exec") # Dumb, but required for the moment for the output panel to be picked # up as the result buffer self.window.get_output_panel("exec") self.output_view.settings().set("result_file_regex", "^([^:\n\r]*):([0-9]+):?([0-9]+)?:? (.*)$") # self.output_view.settings().set("result_line_regex", line_regex) self.output_view.settings().set("result_base_dir", tex_dir) self.window.run_command("show_panel", {"panel": "output.exec"}) self.output_view.settings().set("result_file_regex", file_regex) self.plat = sublime.platform() if self.plat == "osx": self.encoding = "UTF-8" elif self.plat == "windows": self.encoding = getOEMCP() elif self.plat == "linux": self.encoding = "UTF-8" else: sublime.error_message("Platform as yet unsupported. Sorry!") return # Get platform settings, builder, and builder settings platform_settings = get_setting(self.plat, {}) builder_name = get_setting("builder", "traditional") self.hide_panel_level = get_setting("hide_build_panel", "never") self.display_bad_boxes = get_setting("display_bad_boxes", False) # This *must* exist, so if it doesn't, the user didn't migrate if builder_name is None: sublime.error_message("LaTeXTools: you need to migrate your preferences. See the README file for instructions.") return # Default to 'traditional' builder if builder_name in ['', 'default']: builder_name = 'traditional' # relative to ST packages dir! builder_path = get_setting("builder_path", "") builder_file_name = builder_name + 'Builder.py' builder_class_name = builder_name.capitalize() + 'Builder' builder_settings = get_setting("builder_settings", {}) # parse root for any %!TEX directives tex_directives = parse_tex_directives( self.file_name, multi_values=['options'], key_maps={'ts-program': 'program'} ) # determine the engine engine = tex_directives.get('program', builder_settings.get("program", "pdflatex")) engine = engine.lower() # Sanity check: if "strange" engine, default to pdflatex (silently...) if engine not in [ 'pdflatex', "pdftex", 'xelatex', 'xetex', 'lualatex', 'luatex' ]: engine = 'pdflatex' options = builder_settings.get("options", []) if isinstance(options, strbase): options = [options] if 'options' in tex_directives: options.extend(tex_directives['options']) # Read the env option (platform specific) builder_platform_settings = builder_settings.get(self.plat) if builder_platform_settings: self.env = builder_platform_settings.get("env", None) else: self.env = None # Safety check: if we are using a built-in builder, disregard # builder_path, even if it was specified in the pref file if builder_name in ['simple', 'traditional', 'script', 'default','']: builder_path = None # Now actually get the builder ltt_path = os.path.join(sublime.packages_path(),'LaTeXTools','builders') if builder_path: bld_path = os.path.join(sublime.packages_path(), builder_path) else: bld_path = ltt_path bld_file = os.path.join(bld_path, builder_file_name) if not os.path.isfile(bld_file): sublime.error_message("Cannot find builder " + builder_name + ".\n" \ "Check your LaTeXTools Preferences") return # We save the system path and TEMPORARILY add the builders path to it, # so we can simply "import pdfBuilder" in the builder module # For custom builders, we need to add both the LaTeXTools builders # path, as well as the custom path specified above. # The mechanics are from http://effbot.org/zone/import-string.htm syspath_save = list(sys.path) sys.path.insert(0, ltt_path) if builder_path: sys.path.insert(0, bld_path) builder_module = __import__(builder_name + 'Builder') sys.path[:] = syspath_save print(repr(builder_module)) builder_class = getattr(builder_module, builder_class_name) print(repr(builder_class)) # We should now be able to construct the builder object self.builder = builder_class( self.file_name, self.output, engine, options, tex_directives, builder_settings, platform_settings ) # Restore Python system path sys.path[:] = syspath_save # Now get the tex binary path from prefs, change directory to # that of the tex root file, and run! self.path = platform_settings['texpath'] os.chdir(tex_dir) CmdThread(self).start() print (threading.active_count())
def run(self, cmd="", file_regex="", path=""): # Try to handle killing with self.proc_lock: if self.proc: # if we are running, try to kill running process self.output("\n\n### Got request to terminate compilation ###") if sublime.platform() == 'windows': startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW subprocess.call( 'taskkill /t /f /pid {pid}'.format(pid=self.proc.pid), startupinfo=startupinfo, shell=True ) else: os.killpg(self.proc.pid, signal.SIGTERM) self.proc = None return else: # either it's the first time we run, or else we have no running processes self.proc = None view = self.view = self.window.active_view() if view.is_dirty(): print ("saving...") view.run_command('save') # call this on view, not self.window if view.file_name() is None: sublime.error_message('Please save your file before attempting to build.') return self.file_name = getTeXRoot.get_tex_root(view) if not os.path.isfile(self.file_name): sublime.error_message(self.file_name + ": file not found.") return self.tex_base, self.tex_ext = os.path.splitext(self.file_name) tex_dir = os.path.dirname(self.file_name) if not is_tex_file(self.file_name): sublime.error_message("%s is not a TeX source file: cannot compile." % (os.path.basename(view.file_name()),)) return # Output panel: from exec.py if not hasattr(self, 'output_view'): self.output_view = self.window.get_output_panel("exec") # Dumb, but required for the moment for the output panel to be picked # up as the result buffer self.window.get_output_panel("exec") self.output_view.settings().set("result_file_regex", "^([^:\n\r]*):([0-9]+):?([0-9]+)?:? (.*)$") # self.output_view.settings().set("result_line_regex", line_regex) self.output_view.settings().set("result_base_dir", tex_dir) self.window.run_command("show_panel", {"panel": "output.exec"}) self.output_view.settings().set("result_file_regex", file_regex) self.plat = sublime.platform() if self.plat == "osx": self.encoding = "UTF-8" elif self.plat == "windows": self.encoding = getOEMCP() elif self.plat == "linux": self.encoding = "UTF-8" else: sublime.error_message("Platform as yet unsupported. Sorry!") return # Get platform settings, builder, and builder settings platform_settings = get_setting(self.plat, {}) builder_name = get_setting("builder", "traditional") self.hide_panel_level = get_setting("hide_build_panel", "never") self.display_bad_boxes = get_setting("display_bad_boxes", False) # This *must* exist, so if it doesn't, the user didn't migrate if builder_name is None: sublime.error_message("LaTeXTools: you need to migrate your preferences. See the README file for instructions.") self.window.run_command('hide_panel', {"panel": "output.exec"}) return # Default to 'traditional' builder if builder_name in ['', 'default']: builder_name = 'traditional' # this is to convert old-style names (e.g. AReallyLongName) # to new style plugin names (a_really_long_name) builder_name = _classname_to_internal_name(builder_name) builder_settings = get_setting("builder_settings", {}) # parse root for any %!TEX directives tex_directives = parse_tex_directives( self.file_name, multi_values=['options'], key_maps={'ts-program': 'program'} ) # determine the engine engine = tex_directives.get('program', builder_settings.get("program", "pdflatex")) engine = engine.lower() # Sanity check: if "strange" engine, default to pdflatex (silently...) if engine not in [ 'pdflatex', "pdftex", 'xelatex', 'xetex', 'lualatex', 'luatex' ]: engine = 'pdflatex' options = builder_settings.get("options", []) if isinstance(options, strbase): options = [options] if 'options' in tex_directives: options.extend(tex_directives['options']) # Read the env option (platform specific) builder_platform_settings = builder_settings.get(self.plat) if builder_platform_settings: self.env = builder_platform_settings.get("env", None) else: self.env = None # Now actually get the builder builder_path = get_setting("builder_path", "") # relative to ST packages dir! # Safety check: if we are using a built-in builder, disregard # builder_path, even if it was specified in the pref file if builder_name in ['simple', 'traditional', 'script']: builder_path = None if builder_path: bld_path = os.path.join(sublime.packages_path(), builder_path) add_plugin_path(bld_path) try: builder = get_plugin('{0}_builder'.format(builder_name)) except NoSuchPluginException: sublime.error_message("Cannot find builder " + builder_name + ".\n" \ "Check your LaTeXTools Preferences") self.window.run_command('hide_panel', {"panel": "output.exec"}) return print(repr(builder)) self.builder = builder( self.file_name, self.output, engine, options, tex_directives, builder_settings, platform_settings ) # Now get the tex binary path from prefs, change directory to # that of the tex root file, and run! self.path = platform_settings['texpath'] os.chdir(tex_dir) CmdThread(self).start() print(threading.active_count())