Interface to the system's file command. The function uses the -b option of the file command to have it omit the filename in its output. Follow the symlinks. It returns default in case the command should fail.
(target, default='')
| 668 | |
| 669 | |
| 670 | def _syscmd_file(target, default=''): |
| 671 | |
| 672 | """ Interface to the system's file command. |
| 673 | |
| 674 | The function uses the -b option of the file command to have it |
| 675 | omit the filename in its output. Follow the symlinks. It returns |
| 676 | default in case the command should fail. |
| 677 | |
| 678 | """ |
| 679 | if sys.platform in {'dos', 'win32', 'win16', 'ios', 'tvos', 'watchos'}: |
| 680 | # XXX Others too ? |
| 681 | return default |
| 682 | |
| 683 | try: |
| 684 | import subprocess |
| 685 | except ImportError: |
| 686 | return default |
| 687 | target = _follow_symlinks(target) |
| 688 | # "file" output is locale dependent: force the usage of the C locale |
| 689 | # to get deterministic behavior. |
| 690 | env = dict(os.environ, LC_ALL='C') |
| 691 | try: |
| 692 | # -b: do not prepend filenames to output lines (brief mode) |
| 693 | output = subprocess.check_output(['file', '-b', target], |
| 694 | stderr=subprocess.DEVNULL, |
| 695 | env=env) |
| 696 | except (OSError, subprocess.CalledProcessError): |
| 697 | return default |
| 698 | if not output: |
| 699 | return default |
| 700 | # With the C locale, the output should be mostly ASCII-compatible. |
| 701 | # Decode from Latin-1 to prevent Unicode decode error. |
| 702 | return output.decode('latin-1') |
| 703 | |
| 704 | ### Information about the used architecture |
| 705 |
no test coverage detected
searching dependent graphs…