|
| 1 | +# Copyright 2024 The JAX Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +""" |
| 16 | +Converts MSYS Linux-like paths stored in env variables to Windows paths. |
| 17 | +
|
| 18 | +This is necessary on Windows, because some applications do not understand/handle |
| 19 | +Linux-like paths MSYS uses, for example, Bazel. |
| 20 | +""" |
| 21 | +import argparse |
| 22 | +import os |
| 23 | +import subprocess |
| 24 | + |
| 25 | +def msys_to_windows_path(msys_path): |
| 26 | + """Converts an MSYS path to a Windows path using cygpath. |
| 27 | +
|
| 28 | + Args: |
| 29 | + msys_path: The MSYS path to convert. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + The corresponding Windows path. |
| 33 | + """ |
| 34 | + try: |
| 35 | + # Use cygpath with the -w flag to convert to Windows format |
| 36 | + process = subprocess.run(['cygpath', '-w', msys_path], capture_output=True, text=True, check=True) |
| 37 | + windows_path = process.stdout.strip() |
| 38 | + return windows_path |
| 39 | + except FileNotFoundError: |
| 40 | + print("Error: cygpath not found. Make sure it's in your PATH.") |
| 41 | + return None |
| 42 | + except subprocess.CalledProcessError as e: |
| 43 | + print(f"Error converting path: {e}") |
| 44 | + return None |
| 45 | + |
| 46 | +def should_convert(var: str, |
| 47 | + convert: list[str] | None): |
| 48 | + """Check the variable name against convert list""" |
| 49 | + if var in convert: |
| 50 | + return True |
| 51 | + else: |
| 52 | + return False |
| 53 | + |
| 54 | +def main(parsed_args: argparse.Namespace): |
| 55 | + converted_paths = {} |
| 56 | + |
| 57 | + for var, value in os.environ.items(): |
| 58 | + if not value or not should_convert(var, |
| 59 | + parsed_args.convert): |
| 60 | + continue |
| 61 | + converted_path = msys_to_windows_path(value) |
| 62 | + converted_paths[var] = converted_path |
| 63 | + |
| 64 | + var_str = '\n'.join(f'export {k}="{v}"' |
| 65 | + for k, v in converted_paths.items()) |
| 66 | + # The string can then be piped into `source`, to re-set the |
| 67 | + # 'converted' variables. |
| 68 | + print(var_str) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == '__main__': |
| 72 | + parser = argparse.ArgumentParser(description=( |
| 73 | + 'Convert MSYS paths in environment variables to Windows paths.')) |
| 74 | + parser.add_argument('--convert', |
| 75 | + nargs='+', |
| 76 | + required=True, |
| 77 | + help='Space separated list of environment variables to convert. E.g: --convert env_var1 env_var2') |
| 78 | + args = parser.parse_args() |
| 79 | + |
| 80 | + main(args) |
0 commit comments