MCPcopy
hub / github.com/bndr/pipreqs / clean

Function clean

pipreqs/pipreqs.py:390–419  ·  view source on GitHub ↗

Remove modules that aren't imported in project from file.

(file_, imports)

Source from the content-addressed store, hash-verified

388
389
390def clean(file_, imports):
391 """Remove modules that aren't imported in project from file."""
392 modules_not_imported = compare_modules(file_, imports)
393
394 if len(modules_not_imported) == 0:
395 logging.info("Nothing to clean in " + file_)
396 return
397
398 re_remove = re.compile("|".join(modules_not_imported))
399 to_write = []
400
401 try:
402 f = open(file_, "r+")
403 except OSError:
404 logging.error("Failed on file: {}".format(file_))
405 raise
406 else:
407 try:
408 for i in f.readlines():
409 if re_remove.match(i) is None:
410 to_write.append(i)
411 f.seek(0)
412 f.truncate()
413
414 for i in to_write:
415 f.write(i)
416 finally:
417 f.close()
418
419 logging.info("Successfully cleaned up requirements in " + file_)
420
421
422def dynamic_versioning(scheme, imports):

Callers 1

initFunction · 0.85

Calls 1

compare_modulesFunction · 0.85

Tested by

no test coverage detected