annotate scripts/list_sln_files.py @ 15:cfcac4ed7d21 default tip

Improve loading of solution files - New argument to force a rebuild of the cache - Gracefully handle missing projects in a solution - Handle more different xml namespaces - Support more edge cases
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 29 Aug 2023 12:59:54 -0700
parents d5ddd9ffaf11
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import argparse
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import logging
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import os.path
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 from logutil import setup_logging
5
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
5 from vsutil import SolutionCache, ITEM_TYPE_SOURCE_FILES
0
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 logger = logging.getLogger(__name__)
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 def main(args=None):
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 parser = argparse.ArgumentParser()
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 parser.add_argument('solution',
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 help="The path to the Visual Studio solution file.")
5
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
15 parser.add_argument('-c', '--cache',
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
16 help="The solution cache file to load.")
15
cfcac4ed7d21 Improve loading of solution files
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
17 parser.add_argument('--rebuild-cache',
cfcac4ed7d21 Improve loading of solution files
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
18 action='store_true',
cfcac4ed7d21 Improve loading of solution files
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
19 help="Force rebuild the cache even if it is valid")
5
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
20 parser.add_argument('--list-cache',
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
21 help=("If the solution cache is valid, use this "
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
22 "pre-saved file list. Otherwise, compute the "
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
23 "file list and save it to the given path."))
0
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 parser.add_argument('-p', '--project',
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 help="Only list files in the named project.")
5
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
26 parser.add_argument('-t', '--type',
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
27 action='append',
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
28 help="The type(s) of items to list.")
0
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 parser.add_argument('-v', '--verbose',
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 action='store_true',
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 help="Show verbose information.")
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 args = parser.parse_args(args)
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 setup_logging(args.verbose)
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34
15
cfcac4ed7d21 Improve loading of solution files
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
35 cache, loaded = SolutionCache.load_or_rebuild(args.solution, args.cache,
cfcac4ed7d21 Improve loading of solution files
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
36 args.rebuild_cache)
5
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
37 if loaded and args.list_cache:
8
d5ddd9ffaf11 Don't crash trying to list a solution file if no cache exists yet.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
38 caches_exist = True
d5ddd9ffaf11 Don't crash trying to list a solution file if no cache exists yet.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
39 try:
d5ddd9ffaf11 Don't crash trying to list a solution file if no cache exists yet.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
40 cache_dt = os.path.getmtime(args.cache)
d5ddd9ffaf11 Don't crash trying to list a solution file if no cache exists yet.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
41 list_cache_dt = os.path.getmtime(args.list_cache)
d5ddd9ffaf11 Don't crash trying to list a solution file if no cache exists yet.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
42 except OSError:
d5ddd9ffaf11 Don't crash trying to list a solution file if no cache exists yet.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
43 caches_exist = False
d5ddd9ffaf11 Don't crash trying to list a solution file if no cache exists yet.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
44 if caches_exist and list_cache_dt > cache_dt:
5
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
45 logger.debug("Solution cache was valid, re-using the file list cache.")
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
46 try:
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
47 with open(args.list_cache, 'r') as fp:
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
48 print(fp.read())
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
49 return
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
50 except OSError:
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
51 logger.debug("File list cache unreachable, recomputing it.")
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
52 else:
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
53 logger.debug("Solution cache was valid but file list cache was older, "
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
54 "recomputing it.")
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
55
0
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 slnobj = cache.slnobj
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 projs = slnobj.projects
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 if args.project:
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 projs = [slnobj.find_project_by_name(args.project)]
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 projs = list(filter(lambda p: not p.is_folder, projs))
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62
5
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
63 itemtypes = args.type or ITEM_TYPE_SOURCE_FILES
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
64 items = []
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
65
0
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 for p in projs:
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 ig = p.defaultitemgroup()
15
cfcac4ed7d21 Improve loading of solution files
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
68 if ig is None:
cfcac4ed7d21 Improve loading of solution files
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
69 continue
5
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
70 for i in ig.get_items_of_types(itemtypes):
15
cfcac4ed7d21 Improve loading of solution files
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
71 if i.include:
cfcac4ed7d21 Improve loading of solution files
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
72 file_path = os.path.abspath(os.path.join(p.absdirpath, i.include))
cfcac4ed7d21 Improve loading of solution files
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
73 print(file_path)
cfcac4ed7d21 Improve loading of solution files
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
74 items.append(file_path + '\n')
5
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
75
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
76 if args.list_cache:
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
77 logger.debug("Writing file list cache: %s" % args.list_cache)
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
78 with open(args.list_cache, 'w') as fp:
bac97082e229 Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
79 fp.writelines(items)
0
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 if __name__ == '__main__':
5d2c0db51914 Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 main()