-
Notifications
You must be signed in to change notification settings - Fork 4
/
codegrep.py
executable file
·56 lines (49 loc) · 1.78 KB
/
codegrep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# --- BEGIN_HEADER ---
#
# codegrep - a simple helper to locate strings in the project code.
# Copyright (C) 2003-2020 The MiG Project lead by Brian Vinter
#
# This file is part of MiG.
#
# MiG is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# MiG is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# --- END_HEADER ---
#
"""Grep for a regular expression in all code files"""
from __future__ import print_function
from __future__ import absolute_import
import glob
import os
import sys
from mig.shared.projcode import code_root, code_files
from mig.shared.safeeval import subprocess_call
if '__main__' == __name__:
if len(sys.argv) < 2:
print('Usage: %s PATTERN' % sys.argv[0])
print('Grep for PATTERN in all code files')
sys.exit(1)
mig_code_base = os.path.dirname(sys.argv[0])
pattern = sys.argv[1]
expanded_paths = []
for code_path in code_files:
path_pattern = os.path.join(mig_code_base, code_root, code_path)
expanded_paths += glob.glob(os.path.normpath(path_pattern))
command_list = ["grep", "-E", "%s" % pattern] + expanded_paths
# NOTE: we use command list to avoid shell requirement
subprocess_call(command_list)