Remove modules that aren't imported in project from file.
(file_, imports)
| 388 | |
| 389 | |
| 390 | def 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 | |
| 422 | def dynamic_versioning(scheme, imports): |
no test coverage detected