forked from robocup-junior/rcj-soccersim
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeclare_externproto.py
146 lines (121 loc) · 4.97 KB
/
declare_externproto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# Copyright 1996-2023 Cyberbotics Ltd.
#
# 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
#
# https://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.
"""Makes local PROTO references extern"""
import os
import re
import sys
import argparse
from pathlib import Path
import xml.etree.ElementTree as ET
parser = argparse.ArgumentParser(description='Declares EXTERNPROTO references')
parser.add_argument('webots_path', nargs=1, default=None)
args = parser.parse_args()
# ensure it's a valid webots path
if sys.platform.startswith('darwin'):
protolist = os.path.join(args.webots_path[0], 'contents', 'resources', 'proto-list.xml')
else:
protolist = os.path.join(args.webots_path[0], 'resources', 'proto-list.xml')
if not os.path.exists(protolist):
raise RuntimeError(f'Path {protolist} is not a valid webots path, proto-list.xml not found')
# parse proto-list.xml
tree = ET.parse(protolist)
root = tree.getroot()
official_protos = {}
for proto in root:
official_protos[proto.find('name').text] = proto.find('url').text
files = []
files.extend(Path('./protos').rglob('*.proto'))
files.extend(Path('./worlds').rglob('*.wbt'))
local_files = {}
for file in files:
local_files[file.stem] = file.resolve()
for file, path in local_files.items():
# read asset
with open(path, "r") as f:
contents = f.read()
# find any match by bruteforce, starting with local ones
local_matches = []
for key, value in local_files.items():
identifier = key.replace('+', r'\+').replace('-', r'\-').replace('_', r'\_')
regexp = re.compile(rf'{identifier}\s*' + re.escape('{'))
if regexp.search(contents):
if key not in local_matches:
local_matches.append(key)
# now among the official ones
official_matches = []
for key, value in official_protos.items():
identifier = key.replace('+', r'\+').replace('-', r'\-').replace('_', r'\_')
regexp = re.compile(rf'{identifier}\s*' + re.escape('{'))
if regexp.search(contents):
if key not in official_matches:
official_matches.append(key)
contents = contents.splitlines(keepends=True)
header = []
index = None
for n, line in enumerate(contents):
if line.replace(' ', '').replace('\t', '') == '\n':
continue
clean_line = line.strip()
if clean_line.startswith('#'):
header.append(clean_line + '\n')
else:
index = n
break
if not header:
raise RuntimeError(f'File {path.name} is invalid because it has no header')
version = re.search(r'^#\s*VRML_SIM\s+([a-zA-Z0-9\-]+)\s+utf8', header[0])
if not version:
raise RuntimeError(f'The header of {path.name} is not recognized')
elif (version.group(1) >= 'R2022b'):
print(f'Skipping "{path.name}" because header is already R2022b or higher')
continue
if index:
# consume all the empty lines following the index or previous attempts at declaring EXTERNPROTO
while contents[index] == '\n' or contents[index].startswith('EXTERNPROTO'):
del contents[index]
else:
raise RuntimeError(f'File {proto} has an invalid structure')
if len(local_matches) > 0 or len(official_matches) > 0:
print(f'File "{path.name}" depends on:')
for match in local_matches:
print(f' [LOCAL] {match}.proto')
for match in official_matches:
if match not in local_matches:
print(f' [OFFICIAL] {match}.proto')
else:
print(f'File "{path.name}" does not reference any PROTO')
declarations = []
for match in local_matches:
relative_path = os.path.relpath(local_files[match], path.parent)
relative_path = relative_path.replace("\\", "/")
declarations.append(f'EXTERNPROTO "{relative_path}"\n')
for match in official_matches:
if match in local_matches: # favor existing local PROTO over official one if the name is the same
continue
declarations.append(f'EXTERNPROTO "{official_protos[match]}"\n')
# insert declaration in the PROTO file
if len(declarations) > 0:
contents.insert(index, '\n')
for declaration in declarations:
contents.insert(index, declaration)
contents.insert(index, '\n')
# update proto header
contents = contents[n:] # remove old header
header[0] = '#VRML_SIM R2023a utf8\n'
contents = header + contents
# write to file
with open(path, "w") as f:
contents = "".join(contents)
f.write(contents)