예제 #1
0
def select_request(syncr: Synchronizer,
                   all_requests: List[UpdateRequest]) -> None:
    """
    The subroutine to select an update request
    :param syncr: The synchronizer object.
    :param all_requests: List of all update requests.
    """
    def content_editor(content: str) -> str:
        tf_name = ""
        with tempfile.NamedTemporaryFile(suffix=".xml", delete=False) as tf:
            tf_name = tf.name
            tf.write(content.encode('utf-8'))

        if Cli.confirm("Edit the file before submission? "
                       "This will launch the default editor '%s'" % EDITOR):
            while True:
                if os.name == 'posix':
                    call([EDITOR, tf_name])
                else:
                    call(['cmd.exe', '/c', tf_name])

                if Cli.confirm("Finished editing and submit?"):
                    break

        with open(tf_name, 'r', encoding='utf-8') as f:
            new_content = f.read()
            return new_content

    def edit_request(r: UpdateRequest):
        r.title = Cli.input_str("Title", r.title)
        r.message = Cli.input_str("Message", r.message)
        r.reference = Cli.input_str("Reference", r.reference)

    while True:
        req_index = Cli.input_int("Select an update request", 0,
                                  len(all_requests) - 1)
        req = all_requests[req_index]

        # preview the changes in the request
        StyledPrint.update_request(req, req_index)

        options = [
            ("Edit & submit", lambda:
             (edit_request(req), syncr.submit(req, editor=content_editor),
              print("Created Github Pull Request #%d (%s)" %
                    (req.pullreq_num, req.pullreq_url)),
              all_requests.pop(req_index)), CliAction.back),
            ("Reject", lambda:
             (syncr.reject(req),
              print("Created and closed Github Pull Request #%d "
                    "(%s)" % (req.pullreq_num, req.pullreq_url)),
              all_requests.pop(req_index)), CliAction.back),
            ("Re-select", None, CliAction.back), ("Back", None, CliAction.exit)
        ]

        if not Cli.menu("", options):
            return
예제 #2
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