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='')
| 713 | |
| 714 | |
| 715 | def _syscmd_file(target, default=''): |
| 716 | |
| 717 | """ Interface to the system's file command. |
| 718 | |
| 719 | The function uses the -b option of the file command to have it |
| 720 | omit the filename in its output. Follow the symlinks. It returns |
| 721 | default in case the command should fail. |
| 722 | |
| 723 | """ |
| 724 | if sys.platform in {'dos', 'win32', 'win16', 'ios', 'tvos', 'watchos'}: |
| 725 | # XXX Others too ? |
| 726 | return default |
| 727 | |
| 728 | try: |
| 729 | import subprocess |
| 730 | except ImportError: |
| 731 | return default |
| 732 | target = _follow_symlinks(target) |
| 733 | # "file" output is locale dependent: force the usage of the C locale |
| 734 | # to get deterministic behavior. |
| 735 | env = dict(os.environ, LC_ALL='C') |
| 736 | try: |
| 737 | # -b: do not prepend filenames to output lines (brief mode) |
| 738 | output = subprocess.check_output(['file', '-b', target], |
| 739 | stderr=subprocess.DEVNULL, |
| 740 | env=env) |
| 741 | except (OSError, subprocess.CalledProcessError): |
| 742 | return default |
| 743 | if not output: |
| 744 | return default |
| 745 | # With the C locale, the output should be mostly ASCII-compatible. |
| 746 | # Decode from Latin-1 to prevent Unicode decode error. |
| 747 | return output.decode('latin-1') |
| 748 | |
| 749 | ### Information about the used architecture |
| 750 |
no test coverage detected