| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2019 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import os |
| 7 |
import subprocess |
| 8 |
from pathlib import Path |
| 9 |
import typing as T |
| 10 |
|
| 11 |
def ls_as_bytestream() -> bytes: |
| 12 |
if os.path.exists('.git'): |
| 13 |
return subprocess.run(['git', 'ls-tree', '-r', '--name-only', 'HEAD'], |
| 14 |
stdout=subprocess.PIPE).stdout |
| 15 |
|
| 16 |
files = [str(p) for p in Path('.').glob('**/*') |
| 17 |
if not p.is_dir() and |
| 18 |
not next((x for x in p.parts if x.startswith('.')), None)] |
| 19 |
return '\n'.join(files).encode() |
| 20 |
|
| 21 |
|
| 22 |
def cscope() -> int: |
| 23 |
ls = b'\n'.join([b'"%s"' % f for f in ls_as_bytestream().split()]) |
| 24 |
return subprocess.run(['cscope', '-v', '-b', '-i-'], input=ls).returncode |
| 25 |
|
| 26 |
|
| 27 |
def ctags() -> int: |
| 28 |
ls = ls_as_bytestream() |
| 29 |
return subprocess.run(['ctags', '-L-'], input=ls).returncode |
| 30 |
|
| 31 |
|
| 32 |
def etags() -> int: |
| 33 |
ls = ls_as_bytestream() |
| 34 |
return subprocess.run(['etags', '-'], input=ls).returncode |
| 35 |
|
| 36 |
|
| 37 |
def run(args: T.List[str]) -> int: |
| 38 |
tool_name = args[0] |
| 39 |
srcdir_name = args[1] |
| 40 |
os.chdir(srcdir_name) |
| 41 |
assert tool_name in {'cscope', 'ctags', 'etags'} |
| 42 |
res = globals()[tool_name]() |
| 43 |
assert isinstance(res, int) |
| 44 |
return res |