diff --git a/aore/miscutils/sphinx.py b/aore/miscutils/sphinx.py index 44b21ca..c4e2c07 100644 --- a/aore/miscutils/sphinx.py +++ b/aore/miscutils/sphinx.py @@ -6,43 +6,49 @@ import os from bottle import template from aore.aoutils.aoxmltableentry import AoXmlTableEntry +from aore.config import db as dbconfig, sphinx_index_addjobj, sphinx_var_dir, trashfolder, sphinx_index_sugg from aore.dbutils.dbhandler import DbHandler from trigram import trigram -from aore.config import db as dbconfig, sphinx_index_addjobj, sphinx_var_dir, trashfolder, sphinx_index_sugg - class SphinxHelper: def __init__(self, ): self.index_binary = None self.files = dict() - def configure_indexer(self, indexer_binary): + def configure_indexer(self, indexer_binary, config_filename): logging.info("Start configuring Sphinx...") self.index_binary = indexer_binary # Create ADDROBJ config self.files['addrobj.conf'] = self.__create_ao_index_config() - # Indexing ADDROBJ config - run_index_cmd = "{} -c {} --all".format(self.index_binary, self.files['addrobj.conf']) - logging.info("Indexing main ({})...".format(sphinx_index_addjobj)) - #os.system(run_index_cmd) - logging.info("{} index was created.".format(sphinx_index_addjobj)) - # Produce dict file self.files['dict.txt'] = self.__create_suggestion_dict() # Put dict into db - #self.files['dict.csv'] = self.__dbexport_sugg_dict() + self.files['dict.csv'] = self.__dbexport_sugg_dict() # Create SUGGEST config self.files['suggest.conf'] = self.__create_sugg_index_config() - run_index_cmd = "{} -c {} --all".format(self.index_binary, self.files['suggest.conf']) - logging.info("Indexing main ({})...".format(sphinx_index_sugg)) - os.system(run_index_cmd) - logging.info("{} index was created.".format(sphinx_index_sugg)) + # Create main config (sphinx.conf) + out_fname = self.__create_main_config(config_filename) + + # Indexing both configs + run_index_cmd = "{} -c {} --all".format(self.index_binary, out_fname) + logging.info("Indexing main ({})...".format(out_fname)) + os.system(run_index_cmd) + logging.info("All indexes were created.".format(out_fname)) + + # remove temp files + for fname, fpath in self.files.iteritems(): + try: + os.remove(fpath) + except: + logging.warning("Cannot delete {}. Not accessible.".format(fpath)) + logging.info("Temporary files removed.") + logging.info("Successfully configured. Please restart searchd.") def __create_sugg_index_config(self): fname = os.path.abspath(trashfolder + "suggest.conf") @@ -115,11 +121,27 @@ class SphinxHelper: run_builddict_cmd = "{} {} -c {} --buildstops {} 200000 --buildfreqs".format(self.index_binary, sphinx_index_addjobj, self.files['addrobj.conf'], fname) - #os.system(run_builddict_cmd) + os.system(run_builddict_cmd) logging.info("Done.") return fname + def __create_main_config(self, config_fname): + out_filename = os.path.abspath(config_fname) + logging.info("Creating main config {}...".format(out_filename)) -# TRASH -# conf_data = template('aore/templates/sphinx/sphinx.conf', sphinx_var_path=sphinx_var_dir) \ No newline at end of file + conf_data = template('aore/templates/sphinx/sphinx.conf', sphinx_var_path=sphinx_var_dir) + + f = open(out_filename, "w") + for fname, fpath in self.files.iteritems(): + if ".conf" in fname: + with open(fpath, "r") as conff: + for line in conff: + f.write(line) + f.write('\n') + f.write(conf_data) + f.close() + + logging.info("Done.") + + return out_filename diff --git a/manage.py b/manage.py index 2636e22..50227b9 100644 --- a/manage.py +++ b/manage.py @@ -30,6 +30,8 @@ def main(): help="Configure sphinx. Creates sphinx.conf in working direcory") p.add_option('--indexer-path', '-i', help="Path to sphinx indexer binary. Must be specified for '--sphinx-configure'") + p.add_option('--output-conf', '-o', + help="Output config filename. Must be specified for '--sphinx-configure'") options, arguments = p.parse_args() @@ -41,11 +43,11 @@ def main(): if options.database == "update": update_base(options.source, int(options.update_count)) - if options.sphinx and options.indexer_path: + if options.sphinx and options.indexer_path and options.output_conf: sphinxh = SphinxHelper() - sphinxh.configure_indexer(options.indexer_path) + sphinxh.configure_indexer(options.indexer_path, options.output_conf) if __name__ == '__main__': - sph = SphinxSearch() - sph.get_suggest('апасьево') - #main() + #sph = SphinxSearch() + #sph.get_suggest('апасьево') + main()