MCPcopy
hub / github.com/ShishirPatil/gorilla / parse_requirements

Function parse_requirements

goex/exec_engine/container_utils/code_parser.py:250–300  ·  view source on GitHub ↗

Parse a requirements formatted file. Traverse a string until a delimiter is detected, then split at said delimiter, get module name by element index, create a dict consisting of module:version, and add dict to list of parsed modules. Args: file_: File to parse. Raises:

(file_)

Source from the content-addressed store, hash-verified

248
249
250def parse_requirements(file_):
251 """Parse a requirements formatted file.
252
253 Traverse a string until a delimiter is detected, then split at said
254 delimiter, get module name by element index, create a dict consisting of
255 module:version, and add dict to list of parsed modules.
256
257 Args:
258 file_: File to parse.
259
260 Raises:
261 OSerror: If there's any issues accessing the file.
262
263 Returns:
264 tuple: The contents of the file, excluding comments.
265 """
266 modules = []
267 # For the dependency identifier specification, see
268 # https://www.python.org/dev/peps/pep-0508/#complete-grammar
269 delim = ["<", ">", "=", "!", "~"]
270
271 try:
272 f = open(file_, "r")
273 except OSError:
274 logging.error("Failed on file: {}".format(file_))
275 raise
276 else:
277 try:
278 data = [x.strip() for x in f.readlines() if x != "\n"]
279 finally:
280 f.close()
281
282 data = [x for x in data if x[0].isalpha()]
283
284 for x in data:
285 # Check for modules w/o a specifier.
286 if not any([y in x for y in delim]):
287 modules.append({"name": x, "version": None})
288 for y in x:
289 if y in delim:
290 module = x.split(y)
291 module_name = module[0]
292 module_version = module[-1].replace("=", "")
293 module = {"name": module_name, "version": module_version}
294
295 if module not in modules:
296 modules.append(module)
297
298 break
299
300 return modules
301
302
303def compare_modules(file_, imports):

Callers 1

compare_modulesFunction · 0.85

Calls 3

errorMethod · 0.80
formatMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected