| 1 |
# -*- coding: utf-8 -*- |
| 2 |
########################################################################## |
| 3 |
# # |
| 4 |
# This file is part of Frama-C. # |
| 5 |
# # |
| 6 |
# Copyright (C) 2007-2022 # |
| 7 |
# CEA (Commissariat à l'énergie atomique et aux énergies # |
| 8 |
# alternatives) # |
| 9 |
# # |
| 10 |
# you can redistribute it and/or modify it under the terms of the GNU # |
| 11 |
# Lesser General Public License as published by the Free Software # |
| 12 |
# Foundation, version 2.1. # |
| 13 |
# # |
| 14 |
# It is distributed in the hope that it will be useful, # |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 17 |
# GNU Lesser General Public License for more details. # |
| 18 |
# # |
| 19 |
# See the GNU Lesser General Public License version 2.1 # |
| 20 |
# for more details (enclosed in the file licenses/LGPLv2.1). # |
| 21 |
# # |
| 22 |
########################################################################## |
| 23 |
|
| 24 |
"""This file provides some functions to open and filter source files |
| 25 |
before they are used by other scripts. These filters help improve |
| 26 |
the efficiency of regex-based heuristics.""" |
| 27 |
|
| 28 |
# These filters require external tools, either in the PATH, or in |
| 29 |
# environment variables (the latter has higher priority than the former). |
| 30 |
# - scc (a fork including option -k), to remove C comments (variable SCC); |
| 31 |
# - astyle, to re-indent lines (variable ASTYLE) |
| 32 |
# If a tool is absent, the filter is equivalent to a no-op. |
| 33 |
|
| 34 |
# These functions receive a file object (such as produced by open(), |
| 35 |
# subprocess.run, or a previous filter) and return a |
| 36 |
# file object containing the output. They abort execution in case |
| 37 |
# of errors when running the filters. Note that an absent tool |
| 38 |
# does _not_ lead to an error. |
| 39 |
|
| 40 |
import os |
| 41 |
from pathlib import Path |
| 42 |
import shutil |
| 43 |
import subprocess |
| 44 |
import sys |
| 45 |
|
| 46 |
# warnings about missing commands are disabled during testing |
| 47 |
emit_warns = os.getenv("PTESTS_TESTING") is None |
| 48 |
|
| 49 |
# Cache for get_command |
| 50 |
cached_commands = {} |
| 51 |
|
| 52 |
|
| 53 |
def resource_path(relative_path): |
| 54 |
"""Get absolute path to resource; only used by the pyinstaller standalone distribution""" |
| 55 |
base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))) |
| 56 |
return os.path.join(base_path, relative_path) |
| 57 |
|
| 58 |
|
| 59 |
def get_command(command, env_var_name): |
| 60 |
"""Returns a Path to the command; priority goes to the environment variable, |
| 61 |
then in the PATH, then in the resource directory (for a pyinstaller binary).""" |
| 62 |
if command in cached_commands: |
| 63 |
return cached_commands[command] |
| 64 |
p = os.getenv(env_var_name) |
| 65 |
if p: |
| 66 |
p = Path(p) |
| 67 |
else: |
| 68 |
p = shutil.which(command) |
| 69 |
if p: |
| 70 |
p = Path(p) |
| 71 |
else: |
| 72 |
p = Path(resource_path(command)) |
| 73 |
if not p.exists(): |
| 74 |
if emit_warns: |
| 75 |
print( |
| 76 |
f"info: optional external command '{command}' not found in PATH; " |
| 77 |
f"consider installing it or setting environment variable {env_var_name}" |
| 78 |
) |
| 79 |
p = None |
| 80 |
cached_commands[command] = p |
| 81 |
return p |
| 82 |
|
| 83 |
|
| 84 |
def run_and_check(command_and_args, input_data): |
| 85 |
try: |
| 86 |
return subprocess.check_output( |
| 87 |
command_and_args, |
| 88 |
input=input_data, |
| 89 |
stderr=None, |
| 90 |
encoding="ascii", |
| 91 |
errors="ignore", |
| 92 |
) |
| 93 |
except subprocess.CalledProcessError as e: |
| 94 |
sys.exit(f"error running command: {command_and_args}\n{e}") |
| 95 |
|
| 96 |
|
| 97 |
def filter_with_scc(input_data): |
| 98 |
scc = get_command("scc", "SCC") |
| 99 |
if scc: |
| 100 |
return run_and_check([scc, "-k"], input_data) |
| 101 |
else: |
| 102 |
return input_data |
| 103 |
|
| 104 |
|
| 105 |
def filter_with_astyle(input_data): |
| 106 |
astyle = get_command("astyle", "ASTYLE") |
| 107 |
if astyle: |
| 108 |
return run_and_check( |
| 109 |
[astyle, "--keep-one-line-blocks", "--keep-one-line-statements"], input_data |
| 110 |
) |
| 111 |
else: |
| 112 |
return input_data |
| 113 |
|
| 114 |
|
| 115 |
def open_and_filter(filename, apply_filters): |
| 116 |
# we ignore encoding errors and use ASCII to avoid issues when |
| 117 |
# opening files with different encodings (UTF-8, ISO-8859, etc) |
| 118 |
with open(filename, "r", encoding="ascii", errors="ignore") as f: |
| 119 |
data = f.read() |
| 120 |
if apply_filters: |
| 121 |
data = filter_with_astyle(filter_with_scc(data)) |
| 122 |
return data |