Take a command (either a single command or list of arguments) and return the environment created after running that command. Note that if the command must be a batch file or .cmd file, or the changes to the environment will not be captured. If initial is supplied, it is used as
(env_cmd, initial=None)
| 38 | |
| 39 | |
| 40 | def get_environment_from_batch_command(env_cmd, initial=None): |
| 41 | """ |
| 42 | Take a command (either a single command or list of arguments) |
| 43 | and return the environment created after running that command. |
| 44 | Note that if the command must be a batch file or .cmd file, or the |
| 45 | changes to the environment will not be captured. |
| 46 | |
| 47 | If initial is supplied, it is used as the initial environment passed |
| 48 | to the child process. |
| 49 | """ |
| 50 | if not isinstance(env_cmd, (list, tuple)): |
| 51 | env_cmd = [env_cmd] |
| 52 | if not os.path.exists(env_cmd[0]): |
| 53 | raise RuntimeError("Error: %s does not exist" % (env_cmd[0],)) |
| 54 | |
| 55 | # construct the command that will alter the environment |
| 56 | env_cmd = subprocess.list2cmdline(env_cmd) |
| 57 | # create a tag so we can tell in the output when the proc is done |
| 58 | tag = "Done running command" |
| 59 | # construct a cmd.exe command to do accomplish this |
| 60 | cmd = 'cmd.exe /s /c "{env_cmd} && echo "{tag}" && set"'.format(**vars()) |
| 61 | # launch the process |
| 62 | proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=initial) |
| 63 | # parse the output sent to stdout |
| 64 | lines = proc.stdout |
| 65 | # consume whatever output occurs until the tag is reached |
| 66 | for line in lines: |
| 67 | line = line.decode("utf-8") |
| 68 | if "The specified configuration type is missing." in line: |
| 69 | raise AssertionError( |
| 70 | "Error executing %s. View http://blog.ionelmc.ro/2014/12/21/compiling-python-extensions-on-windows/ for details." |
| 71 | % (env_cmd) |
| 72 | ) |
| 73 | if tag in line: |
| 74 | break |
| 75 | if sys.version_info[0] > 2: |
| 76 | # define a way to handle each KEY=VALUE line |
| 77 | handle_line = lambda l: l.decode("utf-8").rstrip().split("=", 1) |
| 78 | else: |
| 79 | # define a way to handle each KEY=VALUE line |
| 80 | handle_line = lambda l: l.rstrip().split("=", 1) |
| 81 | # parse key/values into pairs |
| 82 | pairs = map(handle_line, lines) |
| 83 | # make sure the pairs are valid |
| 84 | valid_pairs = filter(validate_pair, pairs) |
| 85 | # construct a dictionary of the pairs |
| 86 | result = dict(valid_pairs) |
| 87 | # let the process finish |
| 88 | proc.communicate() |
| 89 | return result |
| 90 | |
| 91 | |
| 92 | def remove_binaries(suffixes): |