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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#
# Copyright 2013 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio 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 3, or (at your option)
# any later version.
#
# GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
""" Remove blocks module """
import os
import re
import sys
import glob
from optparse import OptionGroup
from util_functions import remove_pattern_from_file
from modtool_base import ModTool
from cmakefile_editor import CMakeFileEditor
class ModToolRemove(ModTool):
""" Remove block (delete files and remove Makefile entries) """
name = 'remove'
aliases = ('rm', 'del')
def __init__(self):
ModTool.__init__(self)
def setup_parser(self):
" Initialise the option parser for 'gr_modtool.py rm' "
parser = ModTool.setup_parser(self)
parser.usage = '%prog rm [options]. \n Call %prog without any options to run it interactively.'
ogroup = OptionGroup(parser, "Remove module options")
ogroup.add_option("-p", "--pattern", type="string", default=None,
help="Filter possible choices for blocks to be deleted.")
ogroup.add_option("-y", "--yes", action="store_true", default=False,
help="Answer all questions with 'yes'.")
parser.add_option_group(ogroup)
return parser
def setup(self):
ModTool.setup(self)
options = self.options
if options.pattern is not None:
self._info['pattern'] = options.pattern
elif options.block_name is not None:
self._info['pattern'] = options.block_name
elif len(self.args) >= 2:
self._info['pattern'] = self.args[1]
else:
self._info['pattern'] = raw_input('Which blocks do you want to delete? (Regex): ')
if len(self._info['pattern']) == 0:
self._info['pattern'] = '.'
self._info['yes'] = options.yes
def run(self):
""" Go, go, go! """
def _remove_cc_test_case(filename=None, ed=None):
""" Special function that removes the occurrences of a qa*.cc file
from the CMakeLists.txt. """
if filename[:2] != 'qa':
return
if self._info['version'] == '37':
(base, ext) = os.path.splitext(filename)
if ext == '.h':
remove_pattern_from_file(self._file['qalib'],
'^#include "%s"\s*$' % filename)
remove_pattern_from_file(self._file['qalib'],
'^\s*s->addTest\(gr::%s::%s::suite\(\)\);\s*$' % (
self._info['modname'], base)
)
elif ext == '.cc':
ed.remove_value('list',
'\$\{CMAKE_CURRENT_SOURCE_DIR\}/%s' % filename,
'APPEND test_%s_sources' % self._info['modname'])
else:
filebase = os.path.splitext(filename)[0]
ed.delete_entry('add_executable', filebase)
ed.delete_entry('target_link_libraries', filebase)
ed.delete_entry('GR_ADD_TEST', filebase)
ed.remove_double_newlines()
def _remove_py_test_case(filename=None, ed=None):
""" Special function that removes the occurrences of a qa*.{cc,h} file
from the CMakeLists.txt and the qa_$modname.cc. """
if filename[:2] != 'qa':
return
filebase = os.path.splitext(filename)[0]
ed.delete_entry('GR_ADD_TEST', filebase)
ed.remove_double_newlines()
def _make_swig_regex(filename):
filebase = os.path.splitext(filename)[0]
pyblockname = filebase.replace(self._info['modname'] + '_', '')
regexp = r'(^\s*GR_SWIG_BLOCK_MAGIC2?\(%s,\s*%s\);|^\s*.include\s*"(%s/)?%s"\s*)' % \
(self._info['modname'], pyblockname, self._info['modname'], filename)
return regexp
# Go, go, go!
if not self._skip_subdirs['lib']:
self._run_subdir('lib', ('*.cc', '*.h'), ('add_library',),
cmakeedit_func=_remove_cc_test_case)
if not self._skip_subdirs['include']:
incl_files_deleted = self._run_subdir(self._info['includedir'], ('*.h',), ('install',))
if not self._skip_subdirs['swig']:
swig_files_deleted = self._run_subdir('swig', ('*.i',), ('install',))
for f in incl_files_deleted + swig_files_deleted:
# TODO do this on all *.i files
remove_pattern_from_file(self._file['swig'], _make_swig_regex(f))
if not self._skip_subdirs['python']:
py_files_deleted = self._run_subdir('python', ('*.py',), ('GR_PYTHON_INSTALL',),
cmakeedit_func=_remove_py_test_case)
for f in py_files_deleted:
remove_pattern_from_file(self._file['pyinit'], '.*import\s+%s.*' % f[:-3])
remove_pattern_from_file(self._file['pyinit'], '.*from\s+%s\s+import.*\n' % f[:-3])
if not self._skip_subdirs['grc']:
self._run_subdir('grc', ('*.xml',), ('install',))
def _run_subdir(self, path, globs, makefile_vars, cmakeedit_func=None):
""" Delete all files that match a certain pattern in path.
path - The directory in which this will take place
globs - A tuple of standard UNIX globs of files to delete (e.g. *.xml)
makefile_vars - A tuple with a list of CMakeLists.txt-variables which
may contain references to the globbed files
cmakeedit_func - If the CMakeLists.txt needs special editing, use this
"""
# 1. Create a filtered list
files = []
for g in globs:
files = files + glob.glob("%s/%s"% (path, g))
files_filt = []
print "Searching for matching files in %s/:" % path
for f in files:
if re.search(self._info['pattern'], os.path.basename(f)) is not None:
files_filt.append(f)
if len(files_filt) == 0:
print "None found."
return []
# 2. Delete files, Makefile entries and other occurences
files_deleted = []
ed = CMakeFileEditor('%s/CMakeLists.txt' % path)
yes = self._info['yes']
for f in files_filt:
b = os.path.basename(f)
if not yes:
ans = raw_input("Really delete %s? [Y/n/a/q]: " % f).lower().strip()
if ans == 'a':
yes = True
if ans == 'q':
sys.exit(0)
if ans == 'n':
continue
files_deleted.append(b)
print "Deleting %s." % f
os.unlink(f)
print "Deleting occurrences of %s from %s/CMakeLists.txt..." % (b, path)
for var in makefile_vars:
ed.remove_value(var, b)
if cmakeedit_func is not None:
cmakeedit_func(b, ed)
ed.write()
return files_deleted
|