Esempio n. 1
0
def test_cache_new(tmp_path):
    responses.add(responses.GET, TEST_URL, b'foobar')

    now = int(time.time())
    with cached_get(TEST_URL, tmp_path / 'test.txt') as f:
        assert f.read() == b'foobar'
    assert os.path.exists(tmp_path / 'test.txt')
    assert int(os.stat(tmp_path / 'test.txt').st_mtime) >= now
Esempio n. 2
0
def test_cache_update_change(tmp_path):
    responses.add(responses.GET, TEST_URL, b'foobar')
    with open(tmp_path / 'test.txt', 'wb') as f:
        f.write(b'bar')
        f.flush()
        os.utime(f.fileno(), (1623002816, 1623002816))

    now = int(time.time())
    with cached_get(TEST_URL, tmp_path / 'test.txt') as f2:
        assert f2.read() == b'foobar'
    assert os.path.exists(tmp_path / 'test.txt')
    assert int(os.stat(tmp_path / 'test.txt').st_mtime) >= now
Esempio n. 3
0
def test_cache_no_update(tmp_path):
    with open(tmp_path / 'test.txt', 'wb') as f:
        f.write(b'bar')
    with cached_get(TEST_URL, tmp_path / 'test.txt') as f2:
        assert f2.read() == b'bar'
Esempio n. 4
0
def test_404(tmp_path):
    responses.add(responses.GET, TEST_URL, status=404)
    with pytest.raises(requests.HTTPError):
        cached_get(TEST_URL, tmp_path / 'test.txt')
Esempio n. 5
0
def main() -> int:
    """CLI interface for kuroneko scraper."""
    colorama.init()
    argp = argparse.ArgumentParser()
    db_source = argp.add_mutually_exclusive_group()
    db_source.add_argument('-d',
                           '--database',
                           type=argparse.FileType('r'),
                           help='Use bug database from specified json file '
                           '(if not specified, database will be fetched '
                           'from --database-url)')
    db_source.add_argument('--database-url',
                           default=DEFAULT_DB_URL,
                           help=f'Fetch bug database from specified URL '
                           f'(default: {DEFAULT_DB_URL})')
    argp.add_argument('--cache-file',
                      help=f'File used to store a cached copy of bug database '
                      f'(default: {DEFAULT_CACHE_PATH})')
    argp.add_argument('-q',
                      '--quiet',
                      action='store_true',
                      help='Disable progress messages')
    args = argp.parse_args()

    # load the database
    db = Database()
    if args.database is not None:
        if not args.quiet:
            print(f'Using local database {args.database.name}',
                  file=sys.stderr)
    else:
        if not args.quiet:
            print(f'Using remote database {args.database_url}',
                  file=sys.stderr)
        if args.cache_file is None:
            os.makedirs(XDG_CACHE_HOME, exist_ok=True)
            args.cache_file = DEFAULT_CACHE_PATH
        args.database = cached_get(args.database_url, args.cache_file)
        if not args.quiet:
            if isinstance(args.database, io.BytesIO):
                print('Database update fetched', file=sys.stderr)
            else:
                print('Local cache is up-to-date', file=sys.stderr)
    db.load(args.database)
    args.database.close()

    # initialize pkgcore
    config = load_config()
    domain = config.get_default('domain')
    vdb = domain.repos_raw['vdb']

    # do a quick search for vulnerable packages
    restrict = packages_to_restriction(db)
    if not args.quiet:
        print('Searching for vulnerable packages', file=sys.stderr)
    vulnerable = vdb.match(restrict)

    # match vulnerable packages to bugs
    if not args.quiet:
        print(file=sys.stderr)
    first_one = True
    for pkg in vulnerable:
        for bug_pkg, bug in find_applicable_bugs(pkg, db):
            if first_one:
                first_one = False
            else:
                print()
            print_bug(bug, bug_pkg, pkg.cpvstr)

    return 0