Skip to content

Commit

Permalink
[cmd] Eliminate files of subcmd directory
Browse files Browse the repository at this point in the history
There were python scripts in cc_bin/subcmd directory for every
CodeChecker subcommands. The whole content of these files was a dead
code, so these were never run. Their only use was assembling the content
of cc_bin/commands.json file based on their names. However, the list of
subcommands can be collected based on the files of "cmd" directories.
  • Loading branch information
bruntib committed Nov 11, 2020
1 parent 737b063 commit eab2116
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 339 deletions.
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ package: package_dir_structure set_git_commit_template package_plist_to_html pac

mkdir -p $(CC_BUILD_DIR)/cc_bin && \
./scripts/build/create_commands.py -b $(BUILD_DIR) \
$(ROOT)/bin:codechecker_common/cmd \
$(CC_WEB)/bin:codechecker_web/cmd \
$(CC_SERVER)/bin:codechecker_server/cmd \
$(CC_CLIENT)/bin:codechecker_client/cmd \
$(CC_ANALYZER)/bin:codechecker_analyzer/cmd
--cmd-dir codechecker_common/cmd \
$(CC_WEB)/codechecker_web/cmd \
$(CC_SERVER)/codechecker_server/cmd \
$(CC_CLIENT)/codechecker_client/cmd \
$(CC_ANALYZER)/codechecker_analyzer/cmd \
--bin-file $(ROOT)/bin/CodeChecker \
--cc-bin-file $(ROOT)/bin/CodeChecker.py

# Copy license file.
cp $(ROOT)/LICENSE.TXT $(CC_BUILD_DIR)
Expand Down
6 changes: 4 additions & 2 deletions analyzer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ package: package_plist_to_html package_tu_collector package_analyzer package_mer
# Copy CodeChecker entry point sub-commands.
mkdir -p $(CC_BUILD_DIR)/cc_bin && \
$(ROOT)/scripts/build/create_commands.py -b $(BUILD_DIR) \
$(ROOT)/bin:codechecker_common/cmd \
$(CURRENT_DIR)/bin:codechecker_analyzer/cmd
--cmd-dir codechecker_common/cmd \
$(CC_ANALYZER)/codechecker_analyzer/cmd \
--bin-file $(ROOT)/bin/CodeChecker \
--cc-bin-file $(ROOT)/bin/CodeChecker.py

# Copy license file.
cp $(ROOT)/LICENSE.TXT $(CC_BUILD_DIR)
Expand Down
23 changes: 0 additions & 23 deletions analyzer/bin/codechecker-analyze

This file was deleted.

23 changes: 0 additions & 23 deletions analyzer/bin/codechecker-analyzer-version

This file was deleted.

23 changes: 0 additions & 23 deletions analyzer/bin/codechecker-analyzers

This file was deleted.

3 changes: 0 additions & 3 deletions analyzer/bin/codechecker-check

This file was deleted.

23 changes: 0 additions & 23 deletions analyzer/bin/codechecker-checkers

This file was deleted.

23 changes: 0 additions & 23 deletions analyzer/bin/codechecker-fixit

This file was deleted.

23 changes: 0 additions & 23 deletions analyzer/bin/codechecker-log

This file was deleted.

23 changes: 0 additions & 23 deletions analyzer/bin/codechecker-parse

This file was deleted.

2 changes: 1 addition & 1 deletion analyzer/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_in_env: pycodestyle_in_env pylint_in_env test_unit_in_env test_functional_i

test: pycodestyle pylint test_unit test_functional test_build_logger test_tu_collector

PYCODESTYLE_TEST_CMD = pycodestyle bin codechecker_analyzer tests
PYCODESTYLE_TEST_CMD = pycodestyle codechecker_analyzer tests

pycodestyle:
$(PYCODESTYLE_TEST_CMD)
Expand Down
3 changes: 0 additions & 3 deletions bin/codechecker-version

This file was deleted.

127 changes: 62 additions & 65 deletions scripts/build/create_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,43 @@
LOG.addHandler(log_handler)


def copy_entry_points(input_data, build_dir):
"""
Copy CodeChecker entry point sub-commands.
"""
package_root = os.path.join(build_dir, 'CodeChecker')
package_bin = os.path.join(package_root, 'bin')
package_bin_cmd = os.path.join(package_bin, 'subcmd')
os.makedirs(package_bin_cmd, exist_ok=True)
target_cc = os.path.join(package_root, 'cc_bin')

available_commands = {}
for i in input_data:
module_name = i.split(':')[1]
input_dir = i.split(':')[0]

for input_file in glob.glob(os.path.join(input_dir, '*')):
file_name = os.path.basename(input_file)
if not file_name.endswith(".py"):
# Non-py files use the environment to appear as python
# files, they go into the folder in PATH as they are
# entry points.
if file_name.startswith("codechecker-"):
command_name = file_name.replace("codechecker-", "")

module_path = module_name + '/' + \
command_name.replace('-', '_') + '.py'
available_commands[command_name] = module_path

skip_content = "# DO_NOT_INSTALL_TO_PATH"
with open(input_file, 'r',
encoding="utf-8", errors="ignore") as file:
if file.readline().strip() == skip_content:
# If the file is marked not to install, do not
# install it. This happens with entry points
# whom should not act as "lowercase" entries,
# but the sub-command exists as an available
# command.
continue

# Only the CodeChecker top level script should be copied
# into the bin directory and every other subcommand should be
# called through that.
if file_name.startswith("codechecker-"):
shutil.copy2(input_file, package_bin_cmd)
else:
shutil.copy2(input_file, package_bin)

else:
# .py files are Python code that must run in a valid env.
shutil.copy2(input_file, target_cc)

commands_json = os.path.join(target_cc, 'commands.json')
def collect_subcmd(cmd_dirs, build_dir):
'''
This function collects all CodeChecker subcommands (like analyze, check,
store, etc.) from "cmd_dirs" and writes them to .../cc_bin/commands.json
file so the top-level CodeChecker script knows what subcommands are
allowed. We assume that all python files under "cmd_dirs" belong to a
specific subcommand.
cmd_dirs -- List of directories where the code of subcommands can be found.
build_dir -- CodeChecker build directory where the resulting .json file
is written.
'''
subcmds = {}

for cmd_dir in cmd_dirs:
for cmd_file in glob.glob(os.path.join(cmd_dir, '*')):
cmd_file_name = os.path.basename(cmd_file)
# Exclude files like __init__.py or __pycache__.
if '__' not in cmd_file_name:
# [:-3] removes '.py' extension.
subcmds[cmd_file_name[:-3].replace('_', '-')] = \
os.path.join(*cmd_file.split(os.sep)[-3:])
# In case of an absolute path only the last 3 parts are needed:
# codechecker_<module>/cmd/<cmd_name>.py

commands_json = os.path.join(
build_dir, 'CodeChecker', 'cc_bin', 'commands.json')

with open(commands_json, 'w',
encoding="utf-8", errors="ignore") as commands:
json.dump(available_commands, commands, sort_keys=True, indent=2)
json.dump(subcmds, commands, sort_keys=True, indent=2)


def copy_files(files, target_dir):
'''Copy all files of "files" to "target_dir".'''
for f in files:
shutil.copy2(f, target_dir)


if __name__ == "__main__":
Expand All @@ -82,15 +64,22 @@ def copy_entry_points(input_data, build_dir):
formatter_class=argparse.RawDescriptionHelpFormatter,
description=description)

parser.add_argument('input',
type=str,
nargs='+',
metavar='folder',
help="List of directories which contains CodeChecker "
"sub-commands. The format of sub-commands is: "
"<sub-command-name>:<module-name>, where "
"<module-name> is the module where the "
"sub-command can be found.")
parser.add_argument('--bin-file',
nargs='*',
help="List of files to be copied to 'bin' directory. "
"These are exported to users, so these are "
"directly executable.")

parser.add_argument('--cc-bin-file',
nargs='*',
help="List of files to be copied to 'cc_bin' "
"directory. These are not exported to users, so "
"these are not directly executable.")

parser.add_argument('--cmd-dir',
nargs='*',
help="List of directories which contain CodeChecker "
"sub-commands.")

parser.add_argument('-b', '--build-dir',
required=True,
Expand All @@ -108,7 +97,15 @@ def copy_entry_points(input_data, build_dir):
if 'verbose' in args and args['verbose']:
LOG.setLevel(logging.DEBUG)

if isinstance(args['input'], str):
args.input = [args.input]
if args['cmd_dir']:
collect_subcmd(args['cmd_dir'], args['build_dir'])

if args['bin_file']:
copy_files(
args['bin_file'],
os.path.join(args['build_dir'], 'CodeChecker', 'bin'))

copy_entry_points(args['input'], args['build_dir'])
if args['cc_bin_file']:
copy_files(
args['cc_bin_file'],
os.path.join(args['build_dir'], 'CodeChecker', 'cc_bin'))
10 changes: 6 additions & 4 deletions web/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ package: package_plist_to_html package_report_hash package_web
# Copy CodeChecker entry point sub-commands.
mkdir -p $(CC_BUILD_DIR)/cc_bin && \
$(ROOT)/scripts/build/create_commands.py -b $(BUILD_DIR) \
$(ROOT)/bin:codechecker_common/cmd \
$(CURRENT_DIR)/bin:codechecker_web/cmd \
$(CC_SERVER)/bin:codechecker_server/cmd \
$(CC_CLIENT)/bin:codechecker_client/cmd
--cmd-dir codechecker_common/cmd \
$(CC_WEB)/codechecker_web/cmd \
$(CC_SERVER)/codechecker_server/cmd \
$(CC_CLIENT)/codechecker_client/cmd \
--bin-file $(ROOT)/bin/CodeChecker \
--cc-bin-file $(ROOT)/bin/CodeChecker.py

# Copy license file.
cp $(ROOT)/LICENSE.TXT $(CC_BUILD_DIR)
Expand Down
Loading

0 comments on commit eab2116

Please sign in to comment.