예제 #1
0
def create_parser(items: [str], start_str: str, list_start: str,
                  list_closure: str, list_sep: str) -> LineParser:
    return LineParser(
        lambda l: l.startswith(start_str),
        lambda l: internal_list_mutation(l, items, start_str, list_start,
                                         list_closure, list_sep),
        with_newline=False)
예제 #2
0
def update_default_grub(root_uuid: str, root_key_file: str):
    grub_default = "/etc/default/grub"
    success = append_lines_to_end(grub_default, ["GRUB_ENABLE_CRYPTODISK=y"])
    if success == ManipulationResult.NO_MATCH:
        print("Failed to append cryptodisk to update %s, needs manual fixing" %
              grub_default,
              flush=True)
        return

    cmd_line = "cryptdevice=UUID=%s:croot" \
               " root=%s cryptkey=rootfs:%s" \
               " resume=%s" % (root_uuid, CRYPT_ROOT, root_key_file, CRYPT_SWAP)
    success = insert_unique_to_list(file_name=grub_default,
                                    items=cmd_line.split(" "),
                                    start_str="GRUB_CMDLINE_LINUX=",
                                    list_start='"',
                                    list_closure='"',
                                    list_sep=" ")
    if success == ManipulationResult.NO_MATCH:
        linux_line = 'GRUB_CMDLINE_LINUX="' + cmd_line + '"'
        success = insert_after_match(
            grub_default,
            LineParser(match=lambda l: l.startswith("GRUB_DISTRIBUTOR"),
                       replacer=lambda _: linux_line))

    if success == ManipulationResult.NO_MATCH:
        print("Failed to add cmdline linux update to %s, needs manual fixing" %
              grub_default,
              flush=True)
예제 #3
0
def insert_on_match(file_name: str, match: str, replacement: str,
                    index_delta: int) -> ManipulationResult:
    lp = LineParser(lambda l: match in l, lambda l: replacement)
    with FileModifier(file_name=file_name, parsers=lp) as fm:
        f = fm.read_lines_and_trim_parsers()
        if len(f.parsers) == 0:
            return ManipulationResult.NO_CHANGE
        if modify(f, match, replacement, index_delta):
            return ManipulationResult.CHANGED
        else:
            return ManipulationResult.NO_CHANGE
예제 #4
0

def modify(f: OpenFileModification) -> bool:
    modified = True
    for line in f.input_lines:
        any_match = False
        for line_replacer in f.parsers:
            result = line_replacer.generate_replacement(line)
            if result is not None:
                f.output_lines.append(result)
                any_match = True
                modified = True
        if not any_match:
            f.output_lines.append(line)
    return modified


if __name__ == "__main__":
    l1 = "touched"
    l2 = "untouched"
    l3 = "also touched"
    replacers = [LineParser(lambda l: l.startswith("touch"), lambda _: l1),
                 LineParser(lambda l: l.startswith("also touch"), lambda _: l3)]
    fmod = FileModifier("line_replace_tst.txt", replacers)
    modify(fmod.read_lines_and_trim_parsers())
    after = fmod.modified
    assert len(after) == 3
    assert after[0] == l1 + "\n"
    assert after[1] == l2 + "\n"
    assert after[2] == l3 + "\n"
예제 #5
0

def insert_after_match(file_name: str, next_line_appenders: [LineParser]) -> ManipulationResult:
    return FileModifier.modify(file_name, next_line_appenders, modify)


def modify(f: OpenFileModification) -> bool:
    success = False
    for line in f.input_lines:
        f.output_lines.append(line)
        for line_replacer in f.parsers:
            result = line_replacer.generate_replacement(line)
            if result is not None:
                success = True
                f.output_lines.append(result)
    return success


if __name__ == "__main__":
    l1 = "appendafter"
    l2 = "created"
    l3 = "donotappendafter"
    appenders = [LineParser(lambda l: l.startswith("appendafter"), lambda _: l2)]
    fmod = FileModifier("next_line_append.txt", appenders)
    modify(fmod.read_lines_and_trim_parsers())
    after = fmod.modified
    assert len(after) == 3
    assert after[0] == l1 + "\n"
    assert after[1] == l2 + "\n"
    assert after[2] == l3 + "\n"
예제 #6
0
def set_pulse_autospawn():
    replace_on_match("/etc/pulse/client.conf", [
        LineParser(lambda l: l.startswith("autospawn = no"),
                   lambda _: "autospawn = yes")
    ])
예제 #7
0
def append_policy() -> LineParser:
    return LineParser(lambda a: a.startswith("[POLICY]"),
                      lambda _: "AutoEnable=true")
예제 #8
0
def append_general() -> LineParser:
    return LineParser(lambda a: a.startswith("[GENERAL]"),
                      lambda _: "ENABLE=SOURCE,SINK,MEDIA,SOCKET")