github.com/Schaudge/hts@v0.0.0-20240223063651-737b4d69d68c/patch_imports.py (about)

     1  #!/usr/bin/env python3
     2  
     3  """Script for rewriting imports from github.com/biogo -> github.com/Schaudge/hts.
     4  
     5  Usage:
     6  
     7    cd $GRAIL/go/srcgitthub.com/Schaudge/hts
     8    ./patch_imports.py
     9  
    10  """
    11  
    12  
    13  import os
    14  import logging
    15  import re
    16  from typing import List
    17  
    18  
    19  def patch(path: str) -> None:
    20      """Patch the import lines of the given python. It updates the file in place."""
    21      changed = False
    22      lines: List[str] = []
    23      with open(path) as fd:
    24          for line in fd.readlines():
    25              new_line = re.sub(
    26                  r'"github.com/biogo/hts', '"github.com/Schaudge/hts', line
    27              )
    28              if new_line != line:
    29                  line = new_line
    30                  changed = True
    31              lines.append(line)
    32      if not changed:
    33          return
    34      logging.info("Patching %s", path)
    35      with open(path, "w") as fd:
    36          for line in lines:
    37              fd.write(line)
    38  
    39  
    40  def main() -> None:
    41      """Entry point"""
    42      logging.basicConfig(level=logging.DEBUG)
    43      for root, _, files in os.walk("."):
    44          for path in files:
    45              if path.endswith(".go"):
    46                  patch(os.path.join(root, path))
    47  
    48  
    49  main()