From: W. Trevor King Date: Wed, 13 Nov 2013 16:12:58 +0000 (-0800) Subject: swc-windows-installer.py: Use regular expressions to POSIX-ify paths X-Git-Tag: v0.1~21 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=70c97fca300d0f2cb9e7c565faa222ba1de9f3bb;p=swc-setup-windows-installer.git swc-windows-installer.py: Use regular expressions to POSIX-ify paths On a Windows 7 SP1 box with Python 2.7.3: >>> import os.path >>> os.path.expanduser('~') 'c:/Users/JohnDoe' That wasn't matching the previous POSIX-ification patterns, which assumed the drive prefix would be uppercase (C:). Now we use a regular expression to match both cases. --- diff --git a/swc-windows-installer.py b/swc-windows-installer.py index 3d2a790..041642a 100755 --- a/swc-windows-installer.py +++ b/swc-windows-installer.py @@ -26,6 +26,7 @@ try: # Python 3 except ImportError: # Python 2 from StringIO import StringIO as _BytesIO import os +import re try: # Python 3 from urllib.request import urlopen as _urlopen except ImportError: # Python 2 @@ -93,9 +94,15 @@ def update_bash_profile(extra_paths=()): with open(config_path, 'a') as f: f.write('\n'.join(lines)) + def make_posix_path(windows_path): """Convert a Windows path to a posix path""" - return windows_path.replace('\\', '/').replace('C:', '/c') + for regex, sub in [ + (re.compile(r'\\'), '/'), + (re.compile('^[Cc]:'), '/c'), + ]: + windows_path = regex.sub(sub, windows_path) + return windows_path def main():