| OLD | NEW |
| 1 # Copyright (C) 2010 Google Inc. All rights reserved. | 1 # Copyright (C) 2010 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| 11 # in the documentation and/or other materials provided with the | 11 # in the documentation and/or other materials provided with the |
| 12 # distribution. | 12 # distribution. |
| 13 # * Neither the name of Google Inc. nor the names of its | 13 # * Neither the name of Google Inc. nor the names of its |
| 14 # contributors may be used to endorse or promote products derived from | 14 # contributors may be used to endorse or promote products derived from |
| 15 # this software without specific prior written permission. | 15 # this software without specific prior written permission. |
| 16 # | 16 # |
| 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | 28 |
| 29 """generic routines to convert platform-specific paths to URIs.""" | 29 """Generic routines to convert platform-specific paths to URIs.""" |
| 30 | 30 |
| 31 import atexit | |
| 32 import subprocess | |
| 33 import threading | |
| 34 import urllib | 31 import urllib |
| 35 | 32 |
| 36 | 33 |
| 37 def abspath_to_uri(platform, path): | 34 def abspath_to_uri(platform, path): |
| 38 """Converts a platform-specific absolute path to a file: URL.""" | 35 """Converts a platform-specific absolute path to a file: URL.""" |
| 39 return 'file:' + _escape(_convert_path(platform, path)) | 36 return 'file:' + _escape(_convert_path(platform, path)) |
| 40 | 37 |
| 41 | 38 |
| 42 def cygpath(path): | |
| 43 """Converts an absolute cygwin path to an absolute Windows path.""" | |
| 44 return _CygPath.convert_using_singleton(path) | |
| 45 | |
| 46 | |
| 47 # Note that this object is not threadsafe and must only be called | |
| 48 # from multiple threads under protection of a lock (as is done in cygpath()) | |
| 49 class _CygPath(object): | |
| 50 """Manages a long-running 'cygpath' process for file conversion.""" | |
| 51 _lock = None | |
| 52 _singleton = None | |
| 53 | |
| 54 @staticmethod | |
| 55 def stop_cygpath_subprocess(): | |
| 56 if not _CygPath._lock: | |
| 57 return | |
| 58 | |
| 59 with _CygPath._lock: | |
| 60 if _CygPath._singleton: | |
| 61 _CygPath._singleton.stop() | |
| 62 | |
| 63 @staticmethod | |
| 64 def convert_using_singleton(path): | |
| 65 if not _CygPath._lock: | |
| 66 _CygPath._lock = threading.Lock() | |
| 67 | |
| 68 with _CygPath._lock: | |
| 69 if not _CygPath._singleton: | |
| 70 _CygPath._singleton = _CygPath() | |
| 71 # Make sure the cygpath subprocess always gets shutdown cleanly. | |
| 72 atexit.register(_CygPath.stop_cygpath_subprocess) | |
| 73 | |
| 74 return _CygPath._singleton.convert(path) | |
| 75 | |
| 76 def __init__(self): | |
| 77 self._child_process = None | |
| 78 | |
| 79 def start(self): | |
| 80 assert self._child_process is None | |
| 81 args = ['cygpath', '-f', '-', '-wa'] | |
| 82 self._child_process = subprocess.Popen(args, | |
| 83 stdin=subprocess.PIPE, | |
| 84 stdout=subprocess.PIPE) | |
| 85 | |
| 86 def is_running(self): | |
| 87 if not self._child_process: | |
| 88 return False | |
| 89 return self._child_process.returncode is None | |
| 90 | |
| 91 def stop(self): | |
| 92 if self._child_process: | |
| 93 self._child_process.stdin.close() | |
| 94 self._child_process.wait() | |
| 95 self._child_process = None | |
| 96 | |
| 97 def convert(self, path): | |
| 98 if not self.is_running(): | |
| 99 self.start() | |
| 100 self._child_process.stdin.write('%s\r\n' % path) | |
| 101 self._child_process.stdin.flush() | |
| 102 windows_path = self._child_process.stdout.readline().rstrip() | |
| 103 # Some versions of cygpath use lowercase drive letters while others | |
| 104 # use uppercase. We always convert to uppercase for consistency. | |
| 105 windows_path = '%s%s' % (windows_path[0].upper(), windows_path[1:]) | |
| 106 return windows_path | |
| 107 | |
| 108 | |
| 109 def _escape(path): | 39 def _escape(path): |
| 110 """Handle any characters in the path that should be escaped.""" | 40 """Handle any characters in the path that should be escaped.""" |
| 111 # FIXME: web browsers don't appear to blindly quote every character | 41 # FIXME: web browsers don't appear to blindly quote every character |
| 112 # when converting filenames to files. Instead of using urllib's default | 42 # when converting filenames to files. Instead of using urllib's default |
| 113 # rules, we allow a small list of other characters through un-escaped. | 43 # rules, we allow a small list of other characters through un-escaped. |
| 114 # It's unclear if this is the best possible solution. | 44 # It's unclear if this is the best possible solution. |
| 115 return urllib.quote(path, safe='/+:') | 45 return urllib.quote(path, safe='/+:') |
| 116 | 46 |
| 117 | 47 |
| 118 def _convert_path(platform, path): | 48 def _convert_path(platform, path): |
| 119 """Handles any os-specific path separators, mappings, etc.""" | 49 """Handles any os-specific path separators, mappings, etc.""" |
| 120 if platform.is_cygwin(): | |
| 121 return _winpath_to_uri(cygpath(path)) | |
| 122 if platform.is_win(): | 50 if platform.is_win(): |
| 123 return _winpath_to_uri(path) | 51 return _winpath_to_uri(path) |
| 124 return _unixypath_to_uri(path) | 52 return _unixypath_to_uri(path) |
| 125 | 53 |
| 126 | 54 |
| 127 def _winpath_to_uri(path): | 55 def _winpath_to_uri(path): |
| 128 """Converts a window absolute path to a file: URL.""" | 56 """Converts a window absolute path to a file: URL.""" |
| 129 return '///' + path.replace('\\', '/') | 57 return '///' + path.replace('\\', '/') |
| 130 | 58 |
| 131 | 59 |
| 132 def _unixypath_to_uri(path): | 60 def _unixypath_to_uri(path): |
| 133 """Converts a unix-style path to a file: URL.""" | 61 """Converts a unix-style path to a file: URL.""" |
| 134 return '//' + path | 62 return '//' + path |
| OLD | NEW |