Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add format_header #549

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[fix] use format_header to gen header.
Signed-off-by: clundro <clusternlh@gmail.com>
infdahai committed Apr 2, 2023
commit 4123448f3ce22d4cbe800a2e3fc3f844e93e6c96
256 changes: 71 additions & 185 deletions build_support/format_header.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,48 @@
#!/usr/bin/env python3
# encoding: utf-8
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# Modified from the Apache Arrow project for the Terrier project.

"""Format the ill-formatted code."""
# ==============================================
# GOAL : Format code, Update headers
# ==============================================

# ===----------------------------------------------------------------------===//
#
# CMU-DB Project (15-445/645)
# ***DO NO SHARE PUBLICLY***
#
# Identification: src/include/page/b_plus_tree_page.h
#
# Copyright (c) 2023, Carnegie Mellon University Database Group
#
# ===----------------------------------------------------------------------===//
''' template
//===----------------------------------------------------------------------===//
//
// BusTub
//
// rid.h
//
// Identification: src/include/common/rid.h
//
// Copyright (c) 2015-2019, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
'''
# ref: https://github.com/cmu-db/bustub/blob/master/script/formatting/formatter.py

'''
Usage: example
python format_header.py src/include/storage/page/b_plus_tree_page.h \
src/include/storage/page/b_plus_tree_leaf_page.h

# ref: https://github.com/cmu-db/bustub/blob/master/script/formatting/formatter.py
then you will get the header in b_plus_tree_page.h.
//===----------------------------------------------------------------------===//
//
// BusTub
//
// b_plus_tree_page.h
//
// Identification: src/include/storage/page/b_plus_tree_page.h
//
// Copyright (c) 2015-2023, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

similar results insert in b_plus_tree_leaf_page.h.

import argparse
import logging
import os
import re
import sys
import datetime
import subprocess
from functools import reduce
from helpers import CLANG_FORMAT, BUSTUB_DIR, CLANG_FORMAT_FILE, LOG,\
clang_format, hunks_from_staged_files, hunks_from_last_commits
'''


import os
import datetime
import sys
BUSTUB_DIR = os.path.abspath(os.path.dirname(
__file__)).replace('/build_support', '')
BUSTUB_SRC_DIR = os.path.join(BUSTUB_DIR, "src")
BUSTUB_TESTS_DIR = os.path.join(BUSTUB_DIR, "test")

@@ -60,163 +51,58 @@
DEFAULT_DIRS.append(BUSTUB_SRC_DIR)
DEFAULT_DIRS.append(BUSTUB_TESTS_DIR)


# header framework, dynamic information will be added inside function
header_comment_line_1 = "//===----------------------------------------------------------------------===//\n"
header_comment_line_1 += "//\n"
header_comment_line_1 += "// CMU-DB Project (15-445/645)\n"
header_comment_line_2 = "// ***DO NO SHARE PUBLICLY***\n"
header_comment_line_1 += "// BusTub\n"
header_comment_line_2 = "//\n"
header_comment_line_3 = "// "
header_comment_line_4 = "//\n"
header_comment_line_5 = "// Identification: "
header_comment_line_6 = "//\n"
header_comment_line_7 = "// Copyright (c) %d, Carnegie Mellon University Database Group\n" % datetime.datetime.now().year
header_comment_line_7 = "// Copyright (c) 2015-%d, Carnegie Mellon University Database Group\n" % datetime.datetime.now().year
header_comment_line_8 = "//\n"
header_comment_line_9 = "//===----------------------------------------------------------------------===//\n\n"

header_comment_1 = header_comment_line_1 + header_comment_line_2
header_comment_3 = header_comment_line_4
header_comment_5 = header_comment_line_6 + header_comment_line_7 \
+ header_comment_line_8 + header_comment_line_9
header_comment_line_6 = header_comment_line_6 + \
header_comment_line_7+header_comment_line_8+header_comment_line_9

HEADER_REGEX = re.compile(
r"((\/\/===-*===\/\/\n(\/\/.*\n)*\/\/===-*===\/\/[\n]*)\n\n)*")
header_comment_line_1 += header_comment_line_2


# ==============================================
# UTILITY FUNCTION DEFINITIONS
# ==============================================
def add_file_header(file: str):
"""add header to this file."""
if not file.endswith('.h'):
return
file_path = os.path.join(BUSTUB_DIR, file)

if not os.path.isfile(file_path):
return

def format_file(file_path, file_hunks, update_header, clang_format_code):
"""Formats the file passed as argument."""
file_name = os.path.basename(file_path)
abs_path = os.path.abspath(file_path)
rel_path_from_bustub_dir = os.path.relpath(abs_path, BUSTUB_DIR)
dir_flag = False
for deafult_dir in DEFAULT_DIRS:
if file_path.startswith(deafult_dir):
dir_flag = True
break
if not dir_flag:
return

with open(file_path, "r+") as file:
file_data = file.read()
file_name = os.path.basename(file)

if update_header:
# strip old header if it exists
header_match = HEADER_REGEX.match(file_data)
if not header_match is None:
LOG.info("Strip header from %s", file_name)
header_comment = header_match.group()
LOG.debug("Header comment : %s", header_comment)
file_data = file_data.replace(header_comment, "")
header_comment_filename = header_comment_line_3+file_name+'\n'
header_comment_relpath = header_comment_line_5+file + '\n'
header_commnt = header_comment_line_1 + header_comment_filename + \
header_comment_line_4+header_comment_relpath+header_comment_line_6

# add new header
LOG.info("Add header to %s", file_name)
header_comment_2 = header_comment_line_3 + file_name + "\n"
header_comment_4 = header_comment_line_5\
+ rel_path_from_bustub_dir + "\n"
header_comment = header_comment_1 + header_comment_2 \
+ header_comment_3 + header_comment_4 \
+ header_comment_5
# print header_comment
with open(file_path, 'r+') as f:
# maybe use sed -i '' file
old = f.read()
f.seek(0)
f.write(header_commnt)
f.write(old)

file_data = header_comment + file_data

file.seek(0, 0)
file.truncate()
file.write(file_data)

elif clang_format_code:
clang_format(file_path, file_hunks)

# END WITH
# END FORMAT__FILE(FILE_NAME)


def format_dir(dir_path, update_header, clang_format_code):
"""Formats all the files in the dir passed as argument."""
for subdir, _, files in os.walk(dir_path): # _ is for directories.
for file in files:
# print os.path.join(subdir, file)
file_path = subdir + os.path.sep + file

if file_path.endswith(".h") or file_path.endswith(".cpp"):
format_file(file_path, None, update_header, clang_format_code)
# END IF
# END FOR [file]
# END FOR [os.walk]
# END ADD_HEADERS_DIR(DIR_PATH)


# ==============================================
# Main Function
# ==============================================

if __name__ == '__main__':

PARSER = argparse.ArgumentParser(
description='Update headers and/or format source code'
)

PARSER.add_argument(
"-u", "--update-header",
help='Action: Update existing headers or add new ones',
action='store_true'
)
PARSER.add_argument(
"-c", "--clang-format-code",
help='Action: Apply clang-format to source code',
action='store_true'
)
PARSER.add_argument(
"-f", "--staged-files",
help='Action: Apply the selected action(s) to all staged files (git). ' +
'(clang-format will only touch the staged lines)',
action='store_true'
)
PARSER.add_argument(
"-n", "--number-commits",
help='Action: Apply the selected action(s) to all changes of the last ' +
'<n> commits (clang-format will only touch the changed lines)',
type=int, default=0
)
PARSER.add_argument(
'paths', metavar='PATH', type=str, nargs='*',
help='Files or directories to (recursively) apply the actions to'
)

ARGS = PARSER.parse_args()

# TARGETS is a list of files with an optional list of hunks, represented as
# pair (start, end) of line numbers, 1 based.
# element of TARGETS: (filename, None) or (filename, [(start,end)])

if ARGS.staged_files:
TARGETS = hunks_from_staged_files()

if not TARGETS:
LOG.error(
"no staged files or not calling from a repository -- exiting"
)
sys.exit("no staged files or not calling from a repository")

elif ARGS.number_commits > 0:
TARGETS = hunks_from_last_commits(ARGS.number_commits)

if not TARGETS:
LOG.error(
"no changes could be extracted for formatting -- exiting"
)
sys.exit("no changes could be extracted for formatting")

elif not ARGS.paths:
LOG.error("no files or directories given -- exiting")
sys.exit("no files or directories given")

else:
TARGETS = [(f, None) for f in ARGS.paths]

for f, hunks in TARGETS:
if os.path.isfile(f):
LOG.info("Scanning file: %s", f)
format_file(f, hunks, ARGS.update_header, ARGS.clang_format_code)
elif os.path.isdir(f):
LOG.info("Scanning directory %s", f)
format_dir(f, ARGS.update_header, ARGS.clang_format_code)
# FOR
# IF
for file in sys.argv[1:]:
add_file_header(file)
194 changes: 0 additions & 194 deletions build_support/helpers.py

This file was deleted.