1- # Copyright 2019-2020 Siemens AG
1+ # Copyright 2019-2021 Siemens AG
22# SPDX-License-Identifier: MIT
33
44import re
55import logging
66import requests
77from datetime import date , timedelta
88
9- from typing import List
10- from fossology .obj import Agents , User , File , TokenScope , SearchTypes , get_options
9+ from typing import Dict , List
10+ from fossology .obj import (
11+ Agents ,
12+ Upload ,
13+ User ,
14+ File ,
15+ TokenScope ,
16+ SearchTypes ,
17+ get_options ,
18+ )
1119from fossology .folders import Folders
1220from fossology .uploads import Uploads
1321from fossology .jobs import Jobs
2230logger .setLevel (logging .DEBUG )
2331
2432
33+ def search_headers (
34+ searchType : SearchTypes = SearchTypes .ALLFILES ,
35+ upload : Upload = None ,
36+ filename : str = None ,
37+ tag : str = None ,
38+ filesizemin : int = None ,
39+ filesizemax : int = None ,
40+ license : str = None ,
41+ copyright : str = None ,
42+ group : str = None ,
43+ ) -> Dict :
44+ headers = {"searchType" : searchType .value }
45+ if upload :
46+ headers ["uploadId" ] = str (upload .id )
47+ if filename :
48+ headers ["filename" ] = filename
49+ if tag :
50+ headers ["tag" ] = tag
51+ if filesizemin :
52+ headers ["filesizemin" ] = filesizemin
53+ if filesizemax :
54+ headers ["filesizemax" ] = filesizemax
55+ if license :
56+ headers ["license" ] = license
57+ if copyright :
58+ headers ["copyright" ] = copyright
59+ if group :
60+ headers ["groupName" ] = group
61+ return headers
62+
63+
2564def fossology_token (
2665 url , username , password , token_name , token_scope = TokenScope .READ , token_expire = None
2766):
@@ -50,6 +89,8 @@ def fossology_token(
5089 :type expire: string, e.g. 2019-12-25
5190 :return: the new token
5291 :rtype: string
92+ :raises AuthenticationError: if the username or password is incorrect
93+ :raises FossologyApiError: if another error occurs
5394 """
5495 data = {
5596 "username" : username ,
@@ -67,6 +108,9 @@ def fossology_token(
67108 if response .status_code == 201 :
68109 token = response .json ()["Authorization" ]
69110 return re .sub ("Bearer " , "" , token )
111+ elif response .status_code == 404 :
112+ description = "Authentication error"
113+ raise AuthenticationError (description , response )
70114 else :
71115 description = "Error while generating new token"
72116 raise FossologyApiError (description , response )
@@ -131,8 +175,8 @@ def _auth(self):
131175 if user .name == self .name :
132176 self .user = user
133177 return self .user
134- logger . error ( f"User { self .name } was not found" )
135- raise AuthenticationError (self . host )
178+ description = f"User { self .name } was not found on { self . host } "
179+ raise AuthenticationError (description )
136180
137181 def close (self ):
138182 self .session .close ()
@@ -222,28 +266,31 @@ def delete_user(self, user):
222266
223267 def search (
224268 self ,
225- searchType = SearchTypes .ALLFILES ,
226- filename = None ,
227- tag = None ,
228- filesizemin = None ,
229- filesizemax = None ,
230- license = None ,
231- copyright = None ,
232- group = None ,
269+ searchType : SearchTypes = SearchTypes .ALLFILES ,
270+ upload : Upload = None ,
271+ filename : str = None ,
272+ tag : str = None ,
273+ filesizemin : int = None ,
274+ filesizemax : int = None ,
275+ license : str = None ,
276+ copyright : str = None ,
277+ group : str = None ,
233278 ):
234279 """Search for a specific file
235280
236281 API Endpoint: GET /search
237282
238283 :param searchType: Limit search to: directory, allfiles (default), containers
284+ :param upload: Limit search to a specific upload
239285 :param filename: Filename to find, can contain % as wild-card
240286 :param tag: tag to find
241287 :param filesizemin: Min filesize in bytes
242288 :param filesizemax: Max filesize in bytes
243289 :param license: License search filter
244290 :param copyright: Copyright search filter
245291 :param group: the group name to choose while performing search (default: None)
246- :type searchType: SearchType Enum
292+ :type searchType: one of SearchTypes Enum
293+ :type upload: Upload
247294 :type filename: string
248295 :type tag: string
249296 :type filesizemin: int
@@ -256,22 +303,17 @@ def search(
256303 :raises FossologyApiError: if the REST call failed
257304 :raises AuthorizationError: if the user can't access the group
258305 """
259- headers = {"searchType" : searchType .value }
260- if filename :
261- headers ["filename" ] = filename
262- if tag :
263- headers ["tag" ] = tag
264- if filesizemin :
265- headers ["filesizemin" ] = filesizemin
266- if filesizemax :
267- headers ["filesizemax" ] = filesizemax
268- if license :
269- headers ["license" ] = license
270- if copyright :
271- headers ["copyright" ] = copyright
272- if group :
273- headers ["groupName" ] = group
274-
306+ headers = search_headers (
307+ searchType ,
308+ upload ,
309+ filename ,
310+ tag ,
311+ filesizemin ,
312+ filesizemax ,
313+ license ,
314+ copyright ,
315+ group ,
316+ )
275317 response = self .session .get (f"{ self .api } /search" , headers = headers )
276318
277319 if response .status_code == 200 :
@@ -294,7 +336,7 @@ def filesearch(
294336
295337 The response does not generate Python objects yet, the plain JSON data is simply returned.
296338
297- :param filelist: the list of files (or containers) to search for (default: [])
339+ :param filelist: the list of files (or containers) hashes to search for (default: [])
298340 :param group: the group name to choose while performing search (default: None)
299341 :type filelist: list
300342 :return: list of items corresponding to the search criteria
0 commit comments