Call the given cmd in a subprocess using os.system on Windows or subprocess.call using the system shell on other platforms. Parameters ---------- cmd : str Command to execute.
(self, cmd)
| 2677 | raise CalledProcessError(exit_code, cmd) |
| 2678 | |
| 2679 | def system_raw(self, cmd): |
| 2680 | """Call the given cmd in a subprocess using os.system on Windows or |
| 2681 | subprocess.call using the system shell on other platforms. |
| 2682 | |
| 2683 | Parameters |
| 2684 | ---------- |
| 2685 | cmd : str |
| 2686 | Command to execute. |
| 2687 | """ |
| 2688 | cmd = self.var_expand(cmd, depth=1) |
| 2689 | # warn if there is an IPython magic alternative. |
| 2690 | if cmd == "": |
| 2691 | main_cmd = "" |
| 2692 | else: |
| 2693 | main_cmd = cmd.split()[0] |
| 2694 | has_magic_alternatives = ("pip", "conda", "cd") |
| 2695 | |
| 2696 | if main_cmd in has_magic_alternatives: |
| 2697 | warnings.warn( |
| 2698 | ( |
| 2699 | "You executed the system command !{0} which may not work " |
| 2700 | "as expected. Try the IPython magic %{0} instead." |
| 2701 | ).format(main_cmd) |
| 2702 | ) |
| 2703 | |
| 2704 | # protect os.system from UNC paths on Windows, which it can't handle: |
| 2705 | if sys.platform == 'win32': |
| 2706 | from IPython.utils._process_win32 import AvoidUNCPath |
| 2707 | with AvoidUNCPath() as path: |
| 2708 | if path is not None: |
| 2709 | cmd = '"pushd %s &&"%s' % (path, cmd) |
| 2710 | try: |
| 2711 | ec = os.system(cmd) |
| 2712 | except KeyboardInterrupt: |
| 2713 | print('\n' + self.get_exception_only(), file=sys.stderr) |
| 2714 | ec = -2 |
| 2715 | else: |
| 2716 | # For posix the result of the subprocess.call() below is an exit |
| 2717 | # code, which by convention is zero for success, positive for |
| 2718 | # program failure. Exit codes above 128 are reserved for signals, |
| 2719 | # and the formula for converting a signal to an exit code is usually |
| 2720 | # signal_number+128. To more easily differentiate between exit |
| 2721 | # codes and signals, ipython uses negative numbers. For instance |
| 2722 | # since control-c is signal 2 but exit code 130, ipython's |
| 2723 | # _exit_code variable will read -2. Note that some shells like |
| 2724 | # csh and fish don't follow sh/bash conventions for exit codes. |
| 2725 | executable = os.environ.get('SHELL', None) |
| 2726 | try: |
| 2727 | # Use env shell instead of default /bin/sh |
| 2728 | ec = subprocess.call(cmd, shell=True, executable=executable) |
| 2729 | except KeyboardInterrupt: |
| 2730 | # intercept control-C; a long traceback is not useful here |
| 2731 | print('\n' + self.get_exception_only(), file=sys.stderr) |
| 2732 | ec = 130 |
| 2733 | if ec > 128: |
| 2734 | ec = -(ec - 128) |
| 2735 | |
| 2736 | # We explicitly do NOT return the subprocess status code, because |