Exemplo n.º 1
0
def sync(config_file: str, max_auto_requests: int = 0):
    """
    Synchronize between OEC and NASA
    """
    local_requests = []

    def sync_callback(request: UpdateRequest):
        """Collects and display an update request"""
        local_requests.append(request)

    sync_object = Synchronizer(config_file)

    # automatic mode
    if max_auto_requests > 0:
        print("Starting auto-sync...")
        sync_object.sync(sync_callback, get_progress_callback())
        req_to_send = local_requests[:max_auto_requests]
        for req_idx, req in enumerate(req_to_send):
            try:
                print("\nSubmitting request %d...\t\t" % req_idx, end="")
                sync_object.submit(req)
                print("PR #%d (%s)" % (req.pullreq_num, req.pullreq_url))
            except Exception as ex:
                logging.exception(ex)
        return

    # interactive mode
    while True:
        # start the menu
        options = [
            ("Discard changes and exit the program", None, CliAction.exit),
            (
                "Synchronize",
                lambda:
                (local_requests.clear(),
                 sync_object.sync(sync_callback, get_progress_callback())),
                CliAction.back  # need to regenerate menu text
            ),
        ]
        if len(local_requests) > 0:
            options += [
                ("List local update requests", lambda: frozenset(
                    map(lambda tup: StyledPrint.update_request(tup[1], tup[0]),
                        enumerate(local_requests))), CliAction.stay),
                ("Select a local update request",
                 lambda: select_request(sync_object, local_requests),
                 CliAction.back),
            ]

        title = "\n" \
                "+======================================================+\n" \
                "|                OEC-SYNC - MAIN MENU                  |\n" \
                "+======================================================+\n"
        title = Style.apply(title, Style.HEADER, Style.BOLD) + \
            "OEC repository:  %s\n" \
            "Current user:    %s\n" \
            "Remote requests: %d\n" \
            "Local requests:  %d\n" \
            % (sync_object.db.repo.html_url,
                sync_object.db.user.login,
                len(sync_object.db.requests),
                len(local_requests))

        try:
            if not Cli.menu(title, options):
                break
        except EOFError:
            return
Exemplo n.º 2
0
def main(source, target, filter):
    synchronizer = Synchronizer(source, target)
    synchronizer.filters = [TagFilter(f) for f in filter]
    synchronizer.sync()