diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -12,6 +12,7 @@ import subprocess from setuptools import find_packages, setup +from setuptools.command.develop import develop from setuptools.command.sdist import sdist here = os.path.abspath(os.path.dirname(__file__)) @@ -76,7 +77,7 @@ ql_dir = os.path.join(self.build_lib, "swh/search/query_language") if not os.path.exists(os.path.join(ql_dir, "src/parser.c")): - generate_parser(ql_dir) + generate_parser(ql_dir, copy_tree=True) static_dir = os.path.join(self.build_lib, "swh/search/static") os.makedirs(static_dir, exist_ok=True) @@ -89,14 +90,6 @@ print("swh_ql.so file generated") -class TSBuildWasmCommand(TSCommand): - description = "Builds swh_ql.wasm" - - def run(self): - subprocess.run([yarn, "build-wasm"], check=True) - print("swh_ql.wasm file generated") - - class TSBuildCommand(TSCommand): description = "Builds swh_ql.so and swh_ql.wasm" @@ -121,15 +114,24 @@ if not self.dry_run: self.run_command("ts_install") - generate_parser(dist_ql_path) + generate_parser(dist_ql_path, copy_tree=True) + + +class custom_develop(develop): + def run(self): + super().run() + + if not self.dry_run: + generate_parser("swh/search/query_language", copy_tree=False) -def generate_parser(dest_path): - # FIXME: setuptools should copy this itself... - print("Copying parser files") - if os.path.exists(dest_path): - shutil.rmtree(dest_path) - shutil.copytree("swh/search/query_language", dest_path) +def generate_parser(dest_path, copy_tree): + if copy_tree: + # FIXME: setuptools should copy this itself... + print("Copying parser files") + if os.path.exists(dest_path): + shutil.rmtree(dest_path) + shutil.copytree("swh/search/query_language", dest_path) print("Getting path") path = subprocess.check_output([yarn, "bin"]).decode().strip() @@ -174,6 +176,7 @@ cmdclass={ "build": custom_build, "sdist": custom_sdist, + "develop": custom_develop, "ts_install": TSInstallCommand, "ts_build_so": TSBuildSoCommand, "ts_build": TSBuildCommand, diff --git a/swh/search/translator.py b/swh/search/translator.py --- a/swh/search/translator.py +++ b/swh/search/translator.py @@ -1,10 +1,19 @@ +# Copyright (C) 2021 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import logging import os +import tempfile from pkg_resources import resource_filename from tree_sitter import Language, Parser from swh.search.utils import get_expansion, unescape +logger = logging.getLogger(__name__) + class Translator: @@ -16,16 +25,13 @@ } def __init__(self): - ql_rel_paths = [ - "static/swh_ql.so", # installed - "../../query_language/swh_ql.so", # development - ] - for ql_rel_path in ql_rel_paths: - ql_path = resource_filename("swh.search", ql_rel_path) - if os.path.exists(ql_path): - break - else: - assert False, "swh_ql.so was not found in any of the expected paths" + ql_path = resource_filename("swh.search", "static/swh_ql.so") + if not os.path.exists(ql_path): + logging.info("%s does not exist, building in temporary directory", ql_path) + self._build_dir = tempfile.TemporaryDirectory(prefix="swh.search-build") + source_path = resource_filename("swh.search", "query_language") + ql_path = os.path.join(self._build_dir.name, "swh_ql.so") + Language.build_library(ql_path, [source_path]) search_ql = Language(ql_path, "swh_search_ql")