Mercurial > vim-crosoft
annotate scripts/list_sln_files.py @ 9:4ba6df1b2f97
Add built-in properties to the solution environment before resolving items.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 24 Sep 2020 22:57:50 -0700 |
parents | d5ddd9ffaf11 |
children | cfcac4ed7d21 |
rev | line source |
---|---|
0 | 1 import argparse |
2 import logging | |
3 import os.path | |
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 | 6 |
7 | |
8 logger = logging.getLogger(__name__) | |
9 | |
10 | |
11 def main(args=None): | |
12 parser = argparse.ArgumentParser() | |
13 parser.add_argument('solution', | |
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.") |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
17 parser.add_argument('--list-cache', |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
18 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
|
19 "pre-saved file list. Otherwise, compute the " |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
20 "file list and save it to the given path.")) |
0 | 21 parser.add_argument('-p', '--project', |
22 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
|
23 parser.add_argument('-t', '--type', |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
24 action='append', |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
25 help="The type(s) of items to list.") |
0 | 26 parser.add_argument('-v', '--verbose', |
27 action='store_true', | |
28 help="Show verbose information.") | |
29 args = parser.parse_args(args) | |
30 setup_logging(args.verbose) | |
31 | |
5
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
32 cache, loaded = SolutionCache.load_or_rebuild(args.solution, args.cache) |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 try: |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
43 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
|
44 print(fp.read()) |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
45 return |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
46 except OSError: |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
47 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
|
48 else: |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
49 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
|
50 "recomputing it.") |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
51 |
0 | 52 slnobj = cache.slnobj |
53 | |
54 projs = slnobj.projects | |
55 if args.project: | |
56 projs = [slnobj.find_project_by_name(args.project)] | |
57 projs = list(filter(lambda p: not p.is_folder, projs)) | |
58 | |
5
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
59 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
|
60 items = [] |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
61 |
0 | 62 for p in projs: |
63 ig = p.defaultitemgroup() | |
5
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
64 for i in ig.get_items_of_types(itemtypes): |
0 | 65 file_path = os.path.abspath(os.path.join(p.absdirpath, i.include)) |
66 print(file_path) | |
5
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
67 items.append(file_path + '\n') |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
68 |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
69 if args.list_cache: |
bac97082e229
Add more caching to listing solution files.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
70 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
|
71 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
|
72 fp.writelines(items) |
0 | 73 |
74 | |
75 if __name__ == '__main__': | |
76 main() |