def rewrite_module(s, module_name):
    imports = parse_imports(s)
    groups = group_imports(imports, module_name)
    lines = s.splitlines()

    new_import_lines = None
    if groups:
        new_import_lines = groups[0]
        for g in groups[1:]:
            new_import_lines.append('')
            new_import_lines += g

    if new_import_lines:
        if imports:
            first_line = imports[0].line - 1
            last_line = imports[-1].line - 1
            lines[first_line:last_line + 1] = new_import_lines
        else:
            lines[0:1] = lines[0:1] + new_import_lines

    return '\n'.join(lines) + '\n'
dataset/ETHPy150Open bloomberg/phabricator-tools/meta/autofix/fiximports.py/rewrite_module/original