|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import struct |
16 | | -import fcntl |
17 | 16 | import os |
18 | 17 | import sys |
19 | 18 |
|
| 19 | +if os.name == 'nt': |
| 20 | + import win32con |
| 21 | + import win32file |
| 22 | + import pywintypes |
| 23 | + __overlapped = pywintypes.OVERLAPPED() |
| 24 | +elif os.name == 'posix': |
| 25 | + import fcntl |
| 26 | +else: |
| 27 | + raise RuntimeError('PortaLocker only defined for nt and posix platforms') |
| 28 | + |
20 | 29 | from rosidl_cmake import convert_camel_case_to_lower_case_underscore |
21 | 30 | from rosidl_cmake import expand_template |
22 | 31 | from rosidl_cmake import extract_message_types |
@@ -129,34 +138,91 @@ def generate_micro_ros_agent_xml_support(args): |
129 | 138 |
|
130 | 139 | # Write file content |
131 | 140 | fd1 = open(src_file, "w") |
132 | | - fcntl.lockf(fd1, fcntl.LOCK_EX) |
133 | | - fd1.write(file_content) |
| 141 | + if os.name == 'nt': |
| 142 | + # Lock |
| 143 | + hfile1 = win32file._get_osfhandle(fd1.fileno()) |
| 144 | + win32file.LockFileEx(hfile1, win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, __overlapped) |
| 145 | + |
| 146 | + # Write |
| 147 | + fd1.write(file_content) |
| 148 | + |
| 149 | + # Unlock |
| 150 | + win32file.UnlockFileEx(hfile1, 0, -0x10000, __overlapped) |
| 151 | + elif os.name == 'posix': |
| 152 | + # Lock |
| 153 | + fcntl.flock(fd1.fileno(), fcntl.LOCK_EX) |
| 154 | + |
| 155 | + # Write |
| 156 | + fd1.write(file_content) |
| 157 | + |
| 158 | + # Unlock |
| 159 | + fcntl.flock(fd1.fileno(), fcntl.LOCK_UN) |
134 | 160 | fd1.close() |
135 | 161 |
|
136 | 162 |
|
137 | 163 | # Open collect file |
138 | 164 | collec_file = os.path.join(args['output_dir'], "DEFAULT_FASTRTPS_PROFILES.xml") |
139 | 165 | fd2 = open(collec_file, "w") |
140 | | - fcntl.lockf(fd2, fcntl.LOCK_EX) |
141 | | - |
142 | | - |
143 | | - # Generate head |
144 | | - fd2.write("<profiles>\n") |
145 | | - |
146 | | - |
147 | | - # Append all files contents |
148 | | - for filename in os.listdir(srcs_dir): |
149 | | - if filename.endswith(".xml"): |
150 | | - fd3 = open(os.path.join(srcs_dir, filename), "r+") |
151 | | - fcntl.lockf(fd3, fcntl.LOCK_EX) |
152 | | - fd2.write(fd3.read()) |
153 | | - fd3.close() |
154 | | - |
155 | | - |
156 | | - # Generate tail |
157 | | - fd2.write("</profiles>\n") |
158 | | - |
159 | | - |
| 166 | + if os.name == 'nt': |
| 167 | + # Lock |
| 168 | + hfile2 = win32file._get_osfhandle(fd2.fileno()) |
| 169 | + win32file.LockFileEx(hfile2, win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, __overlapped) |
| 170 | + |
| 171 | + # Generate head |
| 172 | + fd2.write("<profiles>\n") |
| 173 | + |
| 174 | + # Append all files contents |
| 175 | + for filename in os.listdir(srcs_dir): |
| 176 | + if filename.endswith(".xml"): |
| 177 | + # Open |
| 178 | + fd3 = open(os.path.join(srcs_dir, filename), "r+") |
| 179 | + |
| 180 | + # Lock |
| 181 | + hfile3 = win32file._get_osfhandle(fd3.fileno()) |
| 182 | + win32file.LockFileEx(hfile3, win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, __overlapped) |
| 183 | + |
| 184 | + # Write |
| 185 | + fd2.write(fd3.read()) |
| 186 | + |
| 187 | + # Unlock |
| 188 | + win32file.UnlockFileEx(hfile3, 0, -0x10000, __overlapped) |
| 189 | + fd3.close() |
| 190 | + |
| 191 | + # Generate tail |
| 192 | + fd2.write("</profiles>\n") |
| 193 | + |
| 194 | + # UnLock |
| 195 | + win32file.UnlockFileEx(hfile2, 0, -0x10000, __overlapped) |
| 196 | + elif os.name == 'posix': |
| 197 | + # Lock |
| 198 | + fcntl.flock(fd2.fileno(), fcntl.LOCK_EX) |
| 199 | + |
| 200 | + # Generate head |
| 201 | + fd2.write("<profiles>\n") |
| 202 | + |
| 203 | + # Append all files contents |
| 204 | + for filename in os.listdir(srcs_dir): |
| 205 | + if filename.endswith(".xml"): |
| 206 | + # Open |
| 207 | + fd3 = open(os.path.join(srcs_dir, filename), "r+") |
| 208 | + |
| 209 | + # Lock |
| 210 | + fcntl.flock(fd3, fcntl.LOCK_EX) |
| 211 | + |
| 212 | + # Write |
| 213 | + fd2.write(fd3.read()) |
| 214 | + |
| 215 | + # Unlock |
| 216 | + fcntl.flock(fd3.fileno(), fcntl.LOCK_UN) |
| 217 | + |
| 218 | + # Close |
| 219 | + fd3.close() |
| 220 | + |
| 221 | + # Generate tail |
| 222 | + fd2.write("</profiles>\n") |
| 223 | + |
| 224 | + # UnLock |
| 225 | + fcntl.flock(fd2.fileno(), fcntl.LOCK_UN) |
160 | 226 | # Close file |
161 | 227 | fd2.close() |
162 | 228 |
|
|
0 commit comments