diff options
author | japm48 <japm48@users.noreply.github.com> | 2020-03-26 16:01:00 +0100 |
---|---|---|
committer | Michael Dickens <michael.dickens@ettus.com> | 2020-04-10 14:57:01 -0400 |
commit | 4c4a85f2ebc43c9b7b3d5fdb8ad8387eae735f5f (patch) | |
tree | 10b5aa2671aafb9e393af7538177d2e48f60780d /gr-utils/modtool/core/rm.py | |
parent | 78955a24c6c24f92f7b22ab9223e8e0b60b1dc4b (diff) |
gr-utils: restructure {mod,block}tool folders
Move modtool and blocktool outside of the python folder,
as per issue #2462.
Diffstat (limited to 'gr-utils/modtool/core/rm.py')
-rw-r--r-- | gr-utils/modtool/core/rm.py | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/gr-utils/modtool/core/rm.py b/gr-utils/modtool/core/rm.py new file mode 100644 index 0000000000..72c9da4666 --- /dev/null +++ b/gr-utils/modtool/core/rm.py @@ -0,0 +1,167 @@ +# +# Copyright 2013, 2018, 2019 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +# +""" Remove blocks module """ + +from __future__ import print_function +from __future__ import absolute_import +from __future__ import unicode_literals + +import os +import re +import sys +import glob +import logging + +from ..tools import remove_pattern_from_file, CMakeFileEditor +from .base import ModTool, ModToolException + +logger = logging.getLogger(__name__) + + +class ModToolRemove(ModTool): + """ Remove block (delete files and remove Makefile entries) """ + name = 'remove' + description = 'Remove a block from a module.' + + def __init__(self, blockname=None, **kwargs): + ModTool.__init__(self, blockname, **kwargs) + self.info['pattern'] = blockname + + def validate(self): + """ Validates the arguments """ + ModTool._validate(self) + if not self.info['pattern'] or self.info['pattern'].isspace(): + raise ModToolException("Incorrect blockname (Regex)!") + + def run(self): + """ Go, go, go! """ + # This portion will be covered by the CLI + if not self.cli: + self.validate() + else: + from ..cli import cli_input + 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'], + r'^#include "{}"\s*$'.format(filename)) + remove_pattern_from_file(self._file['qalib'], + r'^\s*s->addTest\(gr::{}::{}::suite\(\)\);\s*$'.format( + self.info['modname'], base) + ) + self.scm.mark_file_updated(self._file['qalib']) + elif ext == '.cc': + ed.remove_value('list', + r'\$\{CMAKE_CURRENT_SOURCE_DIR\}/%s' % filename, + to_ignore_start='APPEND test_{}_sources'.format(self.info['modname'])) + self.scm.mark_file_updated(ed.filename) + elif self.info['version'] == '38': + (base, ext) = os.path.splitext(filename) + if ext == '.cc': + ed.remove_value( + 'list', filename, + to_ignore_start='APPEND test_{}_sources'.format(self.info['modname'])) + self.scm.mark_file_updated(ed.filename) + 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() + self.scm.mark_file_updated(ed.filename) + + 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*.include\s*"({}/)?{}"\s*)'.format \ + (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', 'list'), + 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)) + self.scm.mark_file_updated(self._file['swig']) + 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'], r'.*import\s+{}.*'.format(f[:-3])) + remove_pattern_from_file(self._file['pyinit'], r'.*from\s+{}\s+import.*\n'.format(f[:-3])) + if not self.skip_subdirs['grc']: + self._run_subdir('grc', ('*.yml',), ('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. *.yml) + 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 + """ + if self.cli: + from ..cli import cli_input + # 1. Create a filtered list + files = [] + for g in globs: + files = files + sorted(glob.glob("{}/{}".format(path, g))) + files_filt = [] + logger.info("Searching for matching files in {}/:".format(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: + logger.info("None found.") + return [] + # 2. Delete files, Makefile entries and other occurrences + files_deleted = [] + ed = CMakeFileEditor('{}/CMakeLists.txt'.format(path)) + yes = self.info['yes'] + for f in files_filt: + b = os.path.basename(f) + if not yes and self.cli: + ans = cli_input("Really delete {}? [Y/n/a/q]: ".format(f)).lower().strip() + if ans == 'a': + yes = True + if ans == 'q': + sys.exit(0) + if ans == 'n': + continue + files_deleted.append(b) + logger.info("Deleting {}.".format(f)) + self.scm.remove_file(f) + os.unlink(f) + logger.info("Deleting occurrences of {} from {}/CMakeLists.txt...".format(b, path)) + for var in makefile_vars: + ed.remove_value(var, b) + if cmakeedit_func is not None: + cmakeedit_func(b, ed) + ed.write() + self.scm.mark_files_updated(('{}/CMakeLists.txt'.format(path))) + return files_deleted |