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='')
| 600 | |
| 601 | |
| 602 | def _syscmd_file(target, default=''): |
| 603 | |
| 604 | """ Interface to the system's file command. |
| 605 | |
| 606 | The function uses the -b option of the file command to have it |
| 607 | omit the filename in its output. Follow the symlinks. It returns |
| 608 | default in case the command should fail. |
| 609 | |
| 610 | """ |
| 611 | if sys.platform in ('dos', 'win32', 'win16'): |
| 612 | # XXX Others too ? |
| 613 | return default |
| 614 | |
| 615 | try: |
| 616 | import subprocess |
| 617 | except ImportError: |
| 618 | return default |
| 619 | target = _follow_symlinks(target) |
| 620 | # "file" output is locale dependent: force the usage of the C locale |
| 621 | # to get deterministic behavior. |
| 622 | env = dict(os.environ, LC_ALL='C') |
| 623 | try: |
| 624 | # -b: do not prepend filenames to output lines (brief mode) |
| 625 | output = subprocess.check_output(['file', '-b', target], |
| 626 | stderr=subprocess.DEVNULL, |
| 627 | env=env) |
| 628 | except (OSError, subprocess.CalledProcessError): |
| 629 | return default |
| 630 | if not output: |
| 631 | return default |
| 632 | # With the C locale, the output should be mostly ASCII-compatible. |
| 633 | # Decode from Latin-1 to prevent Unicode decode error. |
| 634 | return output.decode('latin-1') |
| 635 | |
| 636 | ### Information about the used architecture |
| 637 |
no test coverage detected