Skip to content

Commit

Permalink
fixing server error messages (#14588)
Browse files Browse the repository at this point in the history
* fixing server error messages

* fix tests
  • Loading branch information
memsharded authored Aug 28, 2023
1 parent 9d916b2 commit 23d5f30
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 42 deletions.
4 changes: 2 additions & 2 deletions conans/server/rest/controller/v2/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def search(auth_user):
@app.route(r.common_search_packages, method=["GET"])
@app.route(r.common_search_packages_revision, method=["GET"])
def search_packages(name, version, username, channel, auth_user, revision=None):
query = request.params.get("q", None)
# query is no longer server side
search_service = SearchService(app.authorizer, app.server_store, auth_user)
ref = RecipeReference(name, version, username, channel, revision)
info = search_service.search_packages(ref, query)
info = search_service.search_packages(ref)
return info
63 changes: 25 additions & 38 deletions conans/server/service/v2/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import re
from fnmatch import translate

from conan.api.output import ConanOutput
from conans.errors import NotFoundException, ForbiddenException, RecipeNotFoundException
from conans.errors import ForbiddenException, RecipeNotFoundException
from conans.model.package_ref import PkgReference
from conans.model.recipe_ref import RecipeReference
from conans.paths import CONANINFO
Expand All @@ -13,59 +12,47 @@
from conans.util.files import load


def _get_local_infos_min(server_store, ref, look_in_all_rrevs):

def _get_local_infos_min(server_store, ref):
result = {}
rrevs = server_store.get_recipe_revisions_references(ref) if look_in_all_rrevs else [None]

for rrev in rrevs:
new_ref = copy.copy(ref)
if rrev:
new_ref.revision = rrev.revision
subdirs = list_folder_subdirs(server_store.packages(new_ref), level=1)
for package_id in subdirs:
if package_id in result:
continue
# Read conaninfo
try:
pref = PkgReference(new_ref, package_id)
revision_entry = server_store.get_last_package_revision(pref)
if not revision_entry:
raise NotFoundException("")
pref.revision = revision_entry.revision
info_path = os.path.join(server_store.package(pref), CONANINFO)
if not os.path.exists(info_path):
raise NotFoundException("")
content = load(info_path)
# From Conan 1.48 the conaninfo.txt is sent raw.
result[package_id] = {"content": content}
except Exception as exc: # FIXME: Too wide
ConanOutput().error("Package %s has no ConanInfo file" % str(pref))
if str(exc):
ConanOutput().error(str(exc))
new_ref = ref
subdirs = list_folder_subdirs(server_store.packages(new_ref), level=1)
for package_id in subdirs:
if package_id in result:
continue
# Read conaninfo
pref = PkgReference(new_ref, package_id)
revision_entry = server_store.get_last_package_revision(pref)
if not revision_entry:
continue # server can store empty package-revision list files
pref.revision = revision_entry.revision
info_path = os.path.join(server_store.package(pref), CONANINFO)
if not os.path.exists(info_path):
raise Exception(f"No conaninfo.txt file for listed {pref}")
content = load(info_path)
# From Conan 1.48 the conaninfo.txt is sent raw.
result[package_id] = {"content": content}

return result


def search_packages(server_store, ref, look_in_all_rrevs):
def search_packages(server_store, ref):
"""
Used both for v1 and v2. V1 will iterate rrevs.
Return a dict like this:
{package_ID: {name: "OpenCV",
version: "2.14",
settings: {os: Windows}}}
param ref: RecipeReference object
"""
if not look_in_all_rrevs and ref.revision is None:
if ref.revision is None: # TODO: Verify this sometimes happen
latest_rev = server_store.get_last_revision(ref).revision
ref.revision = latest_rev

ref_norev = copy.copy(ref)
ref_norev.revision = None
if not os.path.exists(server_store.conan_revisions_root(ref_norev)):
raise RecipeNotFoundException(ref)
infos = _get_local_infos_min(server_store, ref, look_in_all_rrevs)
infos = _get_local_infos_min(server_store, ref)
return infos


Expand All @@ -76,10 +63,10 @@ def __init__(self, authorizer, server_store, auth_user):
self._server_store = server_store
self._auth_user = auth_user

def search_packages(self, reference, look_in_all_rrevs=False):
def search_packages(self, reference):
"""Shared between v1 and v2, v1 will iterate rrevs"""
self._authorizer.check_read_conan(self._auth_user, reference)
info = search_packages(self._server_store, reference, look_in_all_rrevs)
info = search_packages(self._server_store, reference)
return info

def _search_recipes(self, pattern=None, ignorecase=True):
Expand Down
4 changes: 2 additions & 2 deletions conans/test/unittests/server/service/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ def test_search(self):
ref3_norev.revision = None
self.assertEqual(info, [ref3_norev])

info = self.search_service.search_packages(ref2, None)
info = self.search_service.search_packages(ref2)
self.assertEqual(info, {'12345587754': {'content': '\n[options]\n use_Qt=False\n',
}})

info = self.search_service.search_packages(ref3, None)
info = self.search_service.search_packages(ref3)
self.assertEqual(info, {'77777777777': {'content': '\n[options]\n use_Qt=True\n'}
})

Expand Down

0 comments on commit 23d5f30

Please sign in to comment.