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 all commits
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
96 changes: 96 additions & 0 deletions build_support/format_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
# encoding: utf-8

# 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

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 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")

# DEFAULT DIRS
DEFAULT_DIRS = []
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 += "// 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) 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_line_6 = header_comment_line_6 + \
header_comment_line_7+header_comment_line_8+header_comment_line_9

header_comment_line_1 += header_comment_line_2


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

dir_flag = False
for deafult_dir in DEFAULT_DIRS:
if file_path.startswith(deafult_dir):
dir_flag = True
break
if not dir_flag:
return

file_name = os.path.basename(file)

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

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)


if __name__ == '__main__':
for file in sys.argv[1:]:
add_file_header(file)