|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 4 | +# See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 5 | +# All rights not expressly granted are reserved. |
| 6 | +# |
| 7 | +# This software is distributed under the terms of the GNU General Public |
| 8 | +# License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 9 | +# |
| 10 | +# In applying this license CERN does not waive the privileges and immunities |
| 11 | +# granted to it by virtue of its status as an Intergovernmental Organization |
| 12 | +# or submit itself to any jurisdiction. |
| 13 | + |
| 14 | +"""! |
| 15 | +@brief Fix include style |
| 16 | +@author Vít Kučera <vit.kucera@cern.ch>, Inha University |
| 17 | +@date 2026-10-08 |
| 18 | +
|
| 19 | +NB: Run before sorting. |
| 20 | +
|
| 21 | +Usage: format_includes.py FILE [FILE ...] |
| 22 | +""" |
| 23 | + |
| 24 | +import re |
| 25 | +import sys |
| 26 | + |
| 27 | +LOCAL = ('"', '"') |
| 28 | +EXTERNAL = ("<", ">") |
| 29 | + |
| 30 | +INCLUDE = re.compile(r"(\s*#include\s+)(\S+)") |
| 31 | + |
| 32 | + |
| 33 | +def fix_line(line: str) -> str: |
| 34 | + m = INCLUDE.match(line) |
| 35 | + if not m: |
| 36 | + return line |
| 37 | + pre, tok = m.groups() |
| 38 | + rest = line[m.end() :] |
| 39 | + h = tok[1:-1] |
| 40 | + |
| 41 | + if re.match(r"(PWG[A-Z]{2}|Common|ALICE3|DPG|EventFiltering|PID|Tools|Tutorials)/.*\.h", h): |
| 42 | + d = LOCAL # O2Physics |
| 43 | + elif re.match( |
| 44 | + r"(Algorithm|CCDB|Common[A-Z]|DataFormats|DCAFitter|Detectors|EMCAL|FDD|Field|Framework|FT0|FV0|GlobalTracking|GPU|ITS|MathUtils|MCH|MFT|MID|PHOS|ReconstructionDataFormats|SimulationDataFormat|TOF|TPC|ZDC).*/.*\.h", |
| 45 | + h, |
| 46 | + ): |
| 47 | + d = EXTERNAL # O2 |
| 48 | + elif re.match(r"(T[A-Z]|Math/|Roo[A-Z])[A-Za-z0-9/]+\.h", h): |
| 49 | + d = EXTERNAL # ROOT |
| 50 | + elif re.match(r"KF[A-Z][A-Za-z0-9]+\.h", h): |
| 51 | + d = EXTERNAL # KFParticle |
| 52 | + elif re.match(r"(fastjet/|onnxruntime)", h): |
| 53 | + d = EXTERNAL # FastJet, ONNX |
| 54 | + elif re.match(r".*DataModel/", h): |
| 55 | + d = LOCAL # incomplete path to DataModel |
| 56 | + elif re.match(r"([A-Za-z0-9_]+/)+[A-Za-z0-9_]+\.h", h): |
| 57 | + d = EXTERNAL # other third-party |
| 58 | + elif re.match(r'".*\.', tok): |
| 59 | + return line # other local-looking file |
| 60 | + elif re.match(r"[a-z_]+\.h", h): |
| 61 | + d = EXTERNAL # C system |
| 62 | + elif re.match(r"[a-z_/]+$", h): |
| 63 | + d = EXTERNAL # C++ system (whole string) |
| 64 | + else: |
| 65 | + return line |
| 66 | + |
| 67 | + return pre + d[0] + h + d[1] + rest |
| 68 | + |
| 69 | + |
| 70 | +def process(path: str): |
| 71 | + # newline='' keeps the original line endings untouched. |
| 72 | + with open(path, newline="") as f: |
| 73 | + lines = f.readlines() |
| 74 | + new_lines = [fix_line(line) for line in lines] |
| 75 | + if new_lines != lines: |
| 76 | + with open(path, "w", newline="") as f: |
| 77 | + f.writelines(new_lines) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + if len(sys.argv) < 2: |
| 82 | + sys.exit(__doc__) |
| 83 | + for p in sys.argv[1:]: |
| 84 | + process(p) |
0 commit comments