Example #1
0
File: auth.py Project: s70160/SH
def login():
  port = 9090
  redirect_uri, oauth_uri = auth_redirect_link(port)

  web_server = ClientRedirectServer(('localhost', port), ClientRedirectHandler)
  print(f'To sign in, use your browser and navigate to {oauth_uri}')
  webbrowser.open(oauth_uri, new=2)

  while True:
    web_server.handle_request()
    if 'code' in web_server.query_params:
      code = web_server.query_params['code']
      break
    elif 'error' in web_server.query_params:
      print('Authentication Error: "%s". Description: "%s" ' % (
        web_server.query_params['error'],
        web_server.query_params.get('error_description')), file=sys.stderr)
      break

  try:
    auth_resp = CommaApi().post('v2/auth/', data={'code': code, 'redirect_uri': redirect_uri})
    set_token(auth_resp['access_token'])
    print('Authenticated')
  except APIError as e:
    print(f'Authentication Error: {e}', file=sys.stderr)
Example #2
0
def login(method):
    oauth_uri = auth_redirect_link(method)

    web_server = ClientRedirectServer(('localhost', PORT),
                                      ClientRedirectHandler)
    print(f'To sign in, use your browser and navigate to {oauth_uri}')
    webbrowser.open(oauth_uri, new=2)

    while True:
        web_server.handle_request()
        if 'code' in web_server.query_params:
            break
        elif 'error' in web_server.query_params:
            print('Authentication Error: "{}". Description: "{}" '.format(
                web_server.query_params['error'],
                web_server.query_params.get('error_description')),
                  file=sys.stderr)
            break

    try:
        auth_resp = CommaApi().post('v2/auth/',
                                    data={
                                        'code':
                                        web_server.query_params['code'],
                                        'provider':
                                        web_server.query_params['provider']
                                    })
        set_token(auth_resp['access_token'])
    except APIError as e:
        print(f'Authentication Error: {e}', file=sys.stderr)
Example #3
0
  def _get_segments_remote(self):
    api = CommaApi(get_token())
    route_files = api.get('v1/route/' + self.name.canonical_name + '/files')
    self.files = list(chain.from_iterable(route_files.values()))

    segments = {}
    for url in self.files:
      _, dongle_id, time_str, segment_num, fn = urlparse(url).path.rsplit('/', maxsplit=4)
      segment_name = f'{dongle_id}|{time_str}--{segment_num}'
      if segments.get(segment_name):
        segments[segment_name] = Segment(
          segment_name,
          url if fn in LOG_FILENAMES else segments[segment_name].log_path,
          url if fn in QLOG_FILENAMES else segments[segment_name].qlog_path,
          url if fn in CAMERA_FILENAMES else segments[segment_name].camera_path,
          url if fn in DCAMERA_FILENAMES else segments[segment_name].dcamera_path,
          url if fn in ECAMERA_FILENAMES else segments[segment_name].ecamera_path,
          url if fn in QCAMERA_FILENAMES else segments[segment_name].qcamera_path,
        )
      else:
        segments[segment_name] = Segment(
          segment_name,
          url if fn in LOG_FILENAMES else None,
          url if fn in QLOG_FILENAMES else None,
          url if fn in CAMERA_FILENAMES else None,
          url if fn in DCAMERA_FILENAMES else None,
          url if fn in ECAMERA_FILENAMES else None,
          url if fn in QCAMERA_FILENAMES else None,
        )

    return sorted(segments.values(), key=lambda seg: seg.name.segment_num)
Example #4
0
def juggle_route(route_or_segment_name, segment_count, qlog, can, layout):
    segment_start = 0
    if 'cabana' in route_or_segment_name:
        query = parse_qs(urlparse(route_or_segment_name).query)
        api = CommaApi(get_token())
        logs = api.get(
            f'v1/route/{query["route"][0]}/log_urls?sig={query["sig"][0]}&exp={query["exp"][0]}'
        )
    elif route_or_segment_name.startswith(
            "http://") or route_or_segment_name.startswith(
                "https://") or os.path.isfile(route_or_segment_name):
        logs = [route_or_segment_name]
    else:
        route_or_segment_name = SegmentName(route_or_segment_name,
                                            allow_route_name=True)
        segment_start = max(route_or_segment_name.segment_num, 0)

        if route_or_segment_name.segment_num != -1 and segment_count is None:
            segment_count = 1

        r = Route(route_or_segment_name.route_name.canonical_name)
        logs = r.qlog_paths() if qlog else r.log_paths()

    segment_end = segment_start + segment_count if segment_count else None
    logs = logs[segment_start:segment_end]

    if None in logs:
        ans = input(
            f"{logs.count(None)}/{len(logs)} of the rlogs in this segment are missing, would you like to fall back to the qlogs? (y/n) "
        )
        if ans == 'y':
            logs = r.qlog_paths()[segment_start:segment_end]
        else:
            print("Please try a different route or segment")
            return

    all_data = []
    with multiprocessing.Pool(24) as pool:
        for d in pool.map(load_segment, logs):
            all_data += d

    if not can:
        all_data = [d for d in all_data if d.which() not in ['can', 'sendcan']]

    # Infer DBC name from logs
    dbc = None
    for cp in [m for m in all_data if m.which() == 'carParams']:
        try:
            DBC = __import__(f"selfdrive.car.{cp.carParams.carName}.values",
                             fromlist=['DBC']).DBC
            dbc = DBC[cp.carParams.carFingerprint]['pt']
        except Exception:
            pass
        break

    with tempfile.NamedTemporaryFile(suffix='.rlog', dir=juggle_dir) as tmp:
        save_log(tmp.name, all_data, compress=False)
        del all_data
        start_juggler(tmp.name, dbc, layout)
Example #5
0
def juggle_route(route_name, segment_number, segment_count, qlog, can, layout):
    if 'cabana' in route_name:
        query = parse_qs(urlparse(route_name).query)
        api = CommaApi(get_token())
        logs = api.get(
            f'v1/route/{query["route"][0]}/log_urls?sig={query["sig"][0]}&exp={query["exp"][0]}'
        )
    elif route_name.startswith("http://") or route_name.startswith(
            "https://") or os.path.isfile(route_name):
        logs = [route_name]
    else:
        r = Route(route_name)
        logs = r.qlog_paths() if qlog else r.log_paths()

    if segment_number is not None:
        logs = logs[segment_number:segment_number + segment_count]

    if None in logs:
        fallback_answer = input(
            "At least one of the rlogs in this segment does not exist, would you like to use the qlogs? (y/n) : "
        )
        if fallback_answer == 'y':
            logs = r.qlog_paths()
            if segment_number is not None:
                logs = logs[segment_number:segment_number + segment_count]
        else:
            print(
                f"Please try a different {'segment' if segment_number is not None else 'route'}"
            )
            return

    all_data = []
    with multiprocessing.Pool(24) as pool:
        for d in pool.map(load_segment, logs):
            all_data += d

    if not can:
        all_data = [d for d in all_data if d.which() not in ['can', 'sendcan']]

    # Infer DBC name from logs
    dbc = None
    for cp in [m for m in all_data if m.which() == 'carParams']:
        try:
            DBC = __import__(f"selfdrive.car.{cp.carParams.carName}.values",
                             fromlist=['DBC']).DBC
            dbc = DBC[cp.carParams.carFingerprint]['pt']
        except (ImportError, KeyError, AttributeError):
            pass
        break

    tempfile = NamedTemporaryFile(suffix='.rlog', dir=juggle_dir)
    save_log(tempfile.name, all_data, compress=False)
    del all_data

    start_juggler(tempfile.name, dbc, layout)
Example #6
0
  def _get_segments_remote(self):
    api = CommaApi(get_token())
    route_files = api.get('v1/route/' + self.route_name + '/files')

    segments = {}
    for url in chain.from_iterable(route_files.values()):
      _, _, dongle_id, time_str, segment_num, fn = urlparse(url).path.split('/')
      segment_name = f'{dongle_id}|{time_str}--{segment_num}'
      if segments.get(segment_name):
        segments[segment_name] = RouteSegment(
          segment_name,
          url if fn in LOG_FILENAMES else segments[segment_name].log_path,
          url if fn in CAMERA_FILENAMES else segments[segment_name].camera_path
        )
      else:
        segments[segment_name] = RouteSegment(
          segment_name,
          url if fn in LOG_FILENAMES else None,
          url if fn in CAMERA_FILENAMES else None
        )

    return sorted(segments.values(), key=lambda seg: seg.canonical_name.segment_num)
Example #7
0
def get_bootlogs(dongle_id: str):
    api = CommaApi(get_token())
    r = api.get(f'v1/devices/{dongle_id}/bootlogs')
    return [Bootlog(b) for b in r]
Example #8
0
    except APIError as e:
        print(f'Authentication Error: {e}', file=sys.stderr)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Login to your comma account')
    parser.add_argument('method',
                        default='google',
                        const='google',
                        nargs='?',
                        choices=['google', 'apple', 'github', 'jwt'])
    parser.add_argument('jwt', nargs='?')

    args = parser.parse_args()
    if args.method == 'jwt':
        if args.jwt is None:
            print("method JWT selected, but no JWT was provided")
            exit(1)

        set_token(args.jwt)
    else:
        login(args.method)

    try:
        me = CommaApi(token=get_token()).get('/v1/me')
        print("Authenticated!")
        pprint.pprint(me)
    except UnauthorizedError:
        print("Got invalid JWT")
        exit(1)