comparison scripts/list_sln_files.py @ 5:bac97082e229

Add more caching to listing solution files.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 24 Oct 2019 11:16:11 -0700
parents 5d2c0db51914
children d5ddd9ffaf11
comparison
equal deleted inserted replaced
4:ae0fb567f459 5:bac97082e229
1 import argparse 1 import argparse
2 import logging 2 import logging
3 import os.path 3 import os.path
4 from logutil import setup_logging 4 from logutil import setup_logging
5 from vsutil import SolutionCache 5 from vsutil import SolutionCache, ITEM_TYPE_SOURCE_FILES
6 6
7 7
8 logger = logging.getLogger(__name__) 8 logger = logging.getLogger(__name__)
9 9
10 10
11 def main(args=None): 11 def main(args=None):
12 parser = argparse.ArgumentParser() 12 parser = argparse.ArgumentParser()
13 parser.add_argument('solution', 13 parser.add_argument('solution',
14 help="The path to the Visual Studio solution file.") 14 help="The path to the Visual Studio solution file.")
15 parser.add_argument('-c', '--cache',
16 help="The solution cache file to load.")
17 parser.add_argument('--list-cache',
18 help=("If the solution cache is valid, use this "
19 "pre-saved file list. Otherwise, compute the "
20 "file list and save it to the given path."))
15 parser.add_argument('-p', '--project', 21 parser.add_argument('-p', '--project',
16 help="Only list files in the named project.") 22 help="Only list files in the named project.")
17 parser.add_argument('-c', '--cache', 23 parser.add_argument('-t', '--type',
18 help="Use a cache file to store the file list.") 24 action='append',
25 help="The type(s) of items to list.")
19 parser.add_argument('-v', '--verbose', 26 parser.add_argument('-v', '--verbose',
20 action='store_true', 27 action='store_true',
21 help="Show verbose information.") 28 help="Show verbose information.")
22 args = parser.parse_args(args) 29 args = parser.parse_args(args)
23 setup_logging(args.verbose) 30 setup_logging(args.verbose)
24 31
25 cache, _ = SolutionCache.load_or_rebuild(args.solution, args.cache) 32 cache, loaded = SolutionCache.load_or_rebuild(args.solution, args.cache)
33 if loaded and args.list_cache:
34 cache_dt = os.path.getmtime(args.cache)
35 list_cache_dt = os.path.getmtime(args.list_cache)
36 if list_cache_dt > cache_dt:
37 logger.debug("Solution cache was valid, re-using the file list cache.")
38 try:
39 with open(args.list_cache, 'r') as fp:
40 print(fp.read())
41 return
42 except OSError:
43 logger.debug("File list cache unreachable, recomputing it.")
44 else:
45 logger.debug("Solution cache was valid but file list cache was older, "
46 "recomputing it.")
47
26 slnobj = cache.slnobj 48 slnobj = cache.slnobj
27 49
28 projs = slnobj.projects 50 projs = slnobj.projects
29 if args.project: 51 if args.project:
30 projs = [slnobj.find_project_by_name(args.project)] 52 projs = [slnobj.find_project_by_name(args.project)]
31 projs = list(filter(lambda p: not p.is_folder, projs)) 53 projs = list(filter(lambda p: not p.is_folder, projs))
32 54
55 itemtypes = args.type or ITEM_TYPE_SOURCE_FILES
56 items = []
57
33 for p in projs: 58 for p in projs:
34 ig = p.defaultitemgroup() 59 ig = p.defaultitemgroup()
35 for i in ig.get_source_items(): 60 for i in ig.get_items_of_types(itemtypes):
36 file_path = os.path.abspath(os.path.join(p.absdirpath, i.include)) 61 file_path = os.path.abspath(os.path.join(p.absdirpath, i.include))
37 print(file_path) 62 print(file_path)
63 items.append(file_path + '\n')
64
65 if args.list_cache:
66 logger.debug("Writing file list cache: %s" % args.list_cache)
67 with open(args.list_cache, 'w') as fp:
68 fp.writelines(items)
38 69
39 70
40 if __name__ == '__main__': 71 if __name__ == '__main__':
41 main() 72 main()