# Path: src/rosrepo/ui.py
# def ask_personal_access_token(domain):
#     if not isatty(sys.stdin):
#         fatal("Need TTY to query credentials\n")
#     if isatty(sys.stderr):
#         fd = sys.stderr
#     elif isatty(sys.stdout):
#         fd = sys.stdout
#     else:
#         fatal("Need TTY to query credentials\n")
#     msg("\n@!Authentication required for @{cf}%s\n" % domain, fd=fd)
#     return readline("Personal Access Token: ", fd=fd)
# 
# def ask_username_and_password(domain):
#     if not isatty(sys.stdin):
#         fatal("Need TTY to query credentials\n")
#     if isatty(sys.stderr):
#         fd = sys.stderr
#     elif isatty(sys.stdout):
#         fd = sys.stdout
#     else:
#         fatal("Need TTY to query credentials\n")
#     msg("\n@!Authentication required for @{cf}%s\n" % domain, fd=fd)
#     while True:
#         login = readline("Username: ", fd=fd)
#         if login == "":
#             continue
#         passwd = getpass("Password: ")
#         if passwd == "":
#             msg("Starting over\n\n", fd=fd)
#             continue
#         return login, passwd
# 
# def msg(text, max_width=None, use_color=None, wrap=True, fd=None, **wrap_args):
#     from .terminal_color import ansi
#     if fd is None:
#         fd = sys.stderr
#     if use_color is None:
#         use_color = isatty(fd)
#     ansi_text = color_fmt(text, use_color=use_color)
#     if wrap:
#         try:
#             if max_width is None:
#                 max_width, _ = get_terminal_size()
#         except OSError:
#             pass
#     fd.write(wrap_ansi_text(ansi_text, max_width, **wrap_args) + (ansi('reset') if use_color else ""))
# 
# def warning(text, use_color=None):
#     prog = "rosrepo"
#     msg("@!@{yf}%s: warning: %s" % (prog, text), use_color=use_color, indent_next=len(prog) + 11)
# 
# def error(text, use_color=None):
#     prog = "rosrepo"
#     msg("@!@{rf}%s: error: %s" % (prog, text), indent_next=len(prog) + 9)
# 
# def fatal(text):
#     raise UserError(text)
# 
# Path: src/rosrepo/util.py
# def yaml_load(stream, Loader=SafeLoader):
# def yaml_dump(data, stream=None, Dumper=SafeDumper, **kwargs):
#     def __init__(self, *args, **kwargs):
#     def __str__(self):
#     def __getitem__(self, item):
#     def __setitem__(self, item, value):
#     def __len__(self):
# def is_deprecated_package(manifest):
# def deprecated_package_info(manifest):
# def path_has_prefix(path, prefix):
# def has_package_path(obj, paths):
# def env_path_list_contains(path_list, path):
# def makedirs(path):
# def write_atomic(filepath, data, mode=0o644, ignore_fail=False):
# def isatty(fd):
# def get_terminal_size():
# def find_program(program):
#     def is_exe(fpath):
# def getmtime(path):
# def call_process(args, bufsize=0, stdin=None, stdout=None, stderr=None, cwd=None, env=None, input_data=None):
# def create_multiprocess_manager():
# def _worker_init(worker_init, worker_init_args):
# def run_multiprocess_workers(worker, workload, worker_init=None, worker_init_args=(), jobs=None, timeout=None):
# class NamedTuple(object):
# class UserError(RuntimeError):


import requests
import sys
import os
import concurrent.futures
from pygit2 import Repository
    from scandir import walk as os_walk
    from os import walk as os_walk
from dateutil.parser import parse as date_parse
    from urllib import quote as urlquote
    from urllib.parse import quote as urlquote
from catkin_pkg.package import parse_package_string, InvalidPackage, PACKAGE_MANIFEST_FILENAME
    from urlparse import urljoin, urlsplit
    from urllib.parse import urljoin, urlsplit
from .ui import ask_personal_access_token, ask_username_and_password, msg, warning, error, fatal
from .util import iteritems, NamedTuple, yaml_dump
            page_count = int(r.headers.get("X-Total-Pages", 0))
        else:
            return []
        page_no += 1
    files = [e["name"] for e in entries if e["type"] == "blob"]
    dirs = [e["name"] for e in entries if e["type"] == "tree" and not e["name"].startswith(".")]
    if "CATKIN_IGNORE" in files:
        return []
    for e in entries:
        if e["type"] == "blob" and e["name"] == PACKAGE_MANIFEST_FILENAME:
            return [(path, e["id"])]
    if depth == 0:
        return []
    result = []
    for d in dirs:
        result += crawl_project_for_packages(session, url, project_id, os.path.join(path, d), depth - 1, timeout)
    return result


_cached_tokens = {}


def acquire_gitlab_private_token(label, url, credentials_callback=ask_username_and_password):
    global _cached_tokens
    if url in _cached_tokens:
        return _cached_tokens[url]
    retries = 3
    while retries > 0:
        retries -= 1
        login, passwd = credentials_callback("%s [%s]" % (label, url))

# Path: src/rosrepo/ui.py
# def ask_personal_access_token(domain):
#     if not isatty(sys.stdin):
#         fatal("Need TTY to query credentials\n")
#     if isatty(sys.stderr):
#         fd = sys.stderr
#     elif isatty(sys.stdout):
#         fd = sys.stdout
#     else:
#         fatal("Need TTY to query credentials\n")
#     msg("\n@!Authentication required for @{cf}%s\n" % domain, fd=fd)
#     return readline("Personal Access Token: ", fd=fd)
# 
# def ask_username_and_password(domain):
#     if not isatty(sys.stdin):
#         fatal("Need TTY to query credentials\n")
#     if isatty(sys.stderr):
#         fd = sys.stderr
#     elif isatty(sys.stdout):
#         fd = sys.stdout
#     else:
#         fatal("Need TTY to query credentials\n")
#     msg("\n@!Authentication required for @{cf}%s\n" % domain, fd=fd)
#     while True:
#         login = readline("Username: ", fd=fd)
#         if login == "":
#             continue
#         passwd = getpass("Password: ")
#         if passwd == "":
#             msg("Starting over\n\n", fd=fd)
#             continue
#         return login, passwd
# 
# def msg(text, max_width=None, use_color=None, wrap=True, fd=None, **wrap_args):
#     from .terminal_color import ansi
#     if fd is None:
#         fd = sys.stderr
#     if use_color is None:
#         use_color = isatty(fd)
#     ansi_text = color_fmt(text, use_color=use_color)
#     if wrap:
#         try:
#             if max_width is None:
#                 max_width, _ = get_terminal_size()
#         except OSError:
#             pass
#     fd.write(wrap_ansi_text(ansi_text, max_width, **wrap_args) + (ansi('reset') if use_color else ""))
# 
# def warning(text, use_color=None):
#     prog = "rosrepo"
#     msg("@!@{yf}%s: warning: %s" % (prog, text), use_color=use_color, indent_next=len(prog) + 11)
# 
# def error(text, use_color=None):
#     prog = "rosrepo"
#     msg("@!@{rf}%s: error: %s" % (prog, text), indent_next=len(prog) + 9)
# 
# def fatal(text):
#     raise UserError(text)
# 
# Path: src/rosrepo/util.py
# def yaml_load(stream, Loader=SafeLoader):
# def yaml_dump(data, stream=None, Dumper=SafeDumper, **kwargs):
#     def __init__(self, *args, **kwargs):
#     def __str__(self):
#     def __getitem__(self, item):
#     def __setitem__(self, item, value):
#     def __len__(self):
# def is_deprecated_package(manifest):
# def deprecated_package_info(manifest):
# def path_has_prefix(path, prefix):
# def has_package_path(obj, paths):
# def env_path_list_contains(path_list, path):
# def makedirs(path):
# def write_atomic(filepath, data, mode=0o644, ignore_fail=False):
# def isatty(fd):
# def get_terminal_size():
# def find_program(program):
#     def is_exe(fpath):
# def getmtime(path):
# def call_process(args, bufsize=0, stdin=None, stdout=None, stderr=None, cwd=None, env=None, input_data=None):
# def create_multiprocess_manager():
# def _worker_init(worker_init, worker_init_args):
# def run_multiprocess_workers(worker, workload, worker_init=None, worker_init_args=(), jobs=None, timeout=None):
# class NamedTuple(object):
# class UserError(RuntimeError):


# Path: src/rosrepo/gitlab.py
import requests
import sys
import os
import concurrent.futures
from pygit2 import Repository
    from scandir import walk as os_walk
    from os import walk as os_walk
from dateutil.parser import parse as date_parse
    from urllib import quote as urlquote
    from urllib.parse import quote as urlquote
from catkin_pkg.package import parse_package_string, InvalidPackage, PACKAGE_MANIFEST_FILENAME
    from urlparse import urljoin, urlsplit
    from urllib.parse import urljoin, urlsplit
from .ui import ask_personal_access_token, ask_username_and_password, msg, warning, error, fatal
from .util import iteritems, NamedTuple, yaml_dump

            page_count = int(r.headers.get("X-Total-Pages", 0))
        else:
            return []
        page_no += 1
    files = [e["name"] for e in entries if e["type"] == "blob"]
    dirs = [e["name"] for e in entries if e["type"] == "tree" and not e["name"].startswith(".")]
    if "CATKIN_IGNORE" in files:
        return []
    for e in entries:
        if e["type"] == "blob" and e["name"] == PACKAGE_MANIFEST_FILENAME:
            return [(path, e["id"])]
    if depth == 0:
        return []
    result = []
    for d in dirs:
        result += crawl_project_for_packages(session, url, project_id, os.path.join(path, d), depth - 1, timeout)
    return result


_cached_tokens = {}


def acquire_gitlab_private_token(label, url, credentials_callback=ask_username_and_password):
    global _cached_tokens
    if url in _cached_tokens:
        return _cached_tokens[url]
    retries = 3
    while retries > 0:
        retries -= 1
        login, passwd = credentials_callback("%s [%s]" % (label, url))

        r = requests.post(urljoin(url, "api/v4/session"), data={"login": login, "password": passwd})