Skip to content

Commit 57edd6c

Browse files
committed
Simple util for extracting file paths from a cache dir.
1 parent 5ae9d1c commit 57edd6c

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Tools/preferred_paths_from_cache.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/python3
2+
#
3+
# Copyright 2024 Google Inc. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
"""Helper to act on a directory of cached discovery data."""
19+
20+
import argparse
21+
import json
22+
import os
23+
import sys
24+
25+
def Main(args):
26+
"""Main method."""
27+
parser = argparse.ArgumentParser()
28+
parser.add_argument('--index-name', default="index.json")
29+
parser.add_argument('--skip', default=[], action='append')
30+
parser.add_argument('cache_dir')
31+
opts = parser.parse_args(args)
32+
33+
index_path = os.path.join(opts.cache_dir, opts.index_name)
34+
with open(index_path, 'r') as fp:
35+
services = json.load(fp).get('items')
36+
37+
file_names = []
38+
for x in services:
39+
if not x.get("preferred"):
40+
continue
41+
name = x.get('name')
42+
if name in opts.skip:
43+
continue
44+
version = x.get('version')
45+
file_names.append(f'{name}.{version}.json')
46+
47+
perferred_paths = [
48+
os.path.join(opts.cache_dir, x)
49+
for x in file_names
50+
]
51+
print(" ".join(perferred_paths))
52+
53+
if __name__ == '__main__':
54+
sys.exit(Main(sys.argv[1:]))

0 commit comments

Comments
 (0)