OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 import errno | 3 import errno |
4 import filecmp | 4 import filecmp |
5 import os.path | 5 import os.path |
6 import re | 6 import re |
7 import tempfile | 7 import tempfile |
| 8 import sys |
8 | 9 |
9 | 10 |
10 def BuildFileAndTarget(build_file, target): | 11 def BuildFileAndTarget(build_file, target): |
11 # NOTE: If you just want to split up target into a build_file and target, | 12 # NOTE: If you just want to split up target into a build_file and target, |
12 # and you know that target already has a build_file that's been produced by | 13 # and you know that target already has a build_file that's been produced by |
13 # this function, pass '' for build_file. | 14 # this function, pass '' for build_file. |
14 | 15 |
15 # NOTE: rsplit is used to disambiguate the Windows drive letter separator. | 16 # NOTE: rsplit is used to disambiguate the Windows drive letter separator. |
16 target_split = target.rsplit(':', 1) | 17 target_split = target.rsplit(':', 1) |
17 if len(target_split) == 2: | 18 if len(target_split) == 2: |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 # tempfile.mkstemp uses an overly restrictive mode, resulting in a | 253 # tempfile.mkstemp uses an overly restrictive mode, resulting in a |
253 # file that can only be read by the owner, regardless of the umask. | 254 # file that can only be read by the owner, regardless of the umask. |
254 # There's no reason to not respect the umask here, which means that | 255 # There's no reason to not respect the umask here, which means that |
255 # an extra hoop is required to fetch it and reset the new file's mode. | 256 # an extra hoop is required to fetch it and reset the new file's mode. |
256 # | 257 # |
257 # No way to get the umask without setting a new one? Set a safe one | 258 # No way to get the umask without setting a new one? Set a safe one |
258 # and then set it back to the old value. | 259 # and then set it back to the old value. |
259 umask = os.umask(077) | 260 umask = os.umask(077) |
260 os.umask(umask) | 261 os.umask(umask) |
261 os.chmod(self.tmp_path, 0666 & ~umask) | 262 os.chmod(self.tmp_path, 0666 & ~umask) |
| 263 if sys.platform == 'win32': |
| 264 # NOTE: on windows (but not cygwin) rename will not replace an |
| 265 # existing file, so it must be preceded with a remove. Sadly there |
| 266 # is no way to make the switch atomic. |
| 267 os.remove(filename) |
262 os.rename(self.tmp_path, filename) | 268 os.rename(self.tmp_path, filename) |
263 except Exception: | 269 except Exception: |
264 # Don't leave turds behind. | 270 # Don't leave turds behind. |
265 os.unlink(self.tmp_path) | 271 os.unlink(self.tmp_path) |
266 raise | 272 raise |
267 | 273 |
268 return Writer() | 274 return Writer() |
OLD | NEW |