Skip to content

Commit

Permalink
Simple util for extracting file paths from a cache dir.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvl committed Jul 18, 2024
1 parent 1b0c0ff commit 5718c6b
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Tools/preferred_paths_from_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/python3
#
# Copyright 2024 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Helper to act on a directory of cached discovery data."""

import argparse
import json
import os
import sys

def Main(args):
"""Main method."""
parser = argparse.ArgumentParser()
parser.add_argument('--index-name', default="index.json")
parser.add_argument('--skip', default=[], action='append')
parser.add_argument('cache_dir')
opts = parser.parse_args(args)

index_path = os.path.join(opts.cache_dir, opts.index_name)
with open(index_path, 'r') as fp:
services = json.load(fp).get('items')

file_names = []
for x in services:
if not x.get("preferred"):
continue
name = x.get('name')
if name in opts.skip:
continue
version = x.get('version')
file_names.append(f'{name}.{version}.json')

perferred_paths = [
os.path.join(opts.cache_dir, x)
for x in file_names
]
print(" ".join(perferred_paths))

if __name__ == '__main__':
sys.exit(Main(sys.argv[1:]))

0 comments on commit 5718c6b

Please sign in to comment.