Skip to content

Commit 4123448

Browse files
committed
[fix] use format_header to gen header.
Signed-off-by: clundro <[email protected]>
1 parent 50245bd commit 4123448

File tree

2 files changed

+71
-379
lines changed

2 files changed

+71
-379
lines changed

build_support/format_header.py

+71-185
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,48 @@
11
#!/usr/bin/env python3
22
# encoding: utf-8
3-
#
4-
# Licensed to the Apache Software Foundation (ASF) under one
5-
# or more contributor license agreements. See the NOTICE file
6-
# distributed with this work for additional information
7-
# regarding copyright ownership. The ASF licenses this file
8-
# to you under the Apache License, Version 2.0 (the
9-
# "License"); you may not use this file except in compliance
10-
# with the License. You may obtain a copy of the License at
11-
#
12-
# http://www.apache.org/licenses/LICENSE-2.0
13-
#
14-
# Unless required by applicable law or agreed to in writing,
15-
# software distributed under the License is distributed on an
16-
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17-
# KIND, either express or implied. See the License for the
18-
# specific language governing permissions and limitations
19-
# under the License.
20-
#
21-
# Modified from the Apache Arrow project for the Terrier project.
22-
23-
"""Format the ill-formatted code."""
24-
# ==============================================
25-
# GOAL : Format code, Update headers
26-
# ==============================================
27-
28-
# ===----------------------------------------------------------------------===//
29-
#
30-
# CMU-DB Project (15-445/645)
31-
# ***DO NO SHARE PUBLICLY***
32-
#
33-
# Identification: src/include/page/b_plus_tree_page.h
34-
#
35-
# Copyright (c) 2023, Carnegie Mellon University Database Group
36-
#
37-
# ===----------------------------------------------------------------------===//
3+
''' template
4+
//===----------------------------------------------------------------------===//
5+
//
6+
// BusTub
7+
//
8+
// rid.h
9+
//
10+
// Identification: src/include/common/rid.h
11+
//
12+
// Copyright (c) 2015-2019, Carnegie Mellon University Database Group
13+
//
14+
//===----------------------------------------------------------------------===//
15+
'''
16+
# ref: https://github.com/cmu-db/bustub/blob/master/script/formatting/formatter.py
3817

18+
'''
19+
Usage: example
20+
python format_header.py src/include/storage/page/b_plus_tree_page.h \
21+
src/include/storage/page/b_plus_tree_leaf_page.h
3922
40-
# ref: https://github.com/cmu-db/bustub/blob/master/script/formatting/formatter.py
23+
then you will get the header in b_plus_tree_page.h.
24+
//===----------------------------------------------------------------------===//
25+
//
26+
// BusTub
27+
//
28+
// b_plus_tree_page.h
29+
//
30+
// Identification: src/include/storage/page/b_plus_tree_page.h
31+
//
32+
// Copyright (c) 2015-2023, Carnegie Mellon University Database Group
33+
//
34+
//===----------------------------------------------------------------------===//
4135
36+
similar results insert in b_plus_tree_leaf_page.h.
4237
43-
import argparse
44-
import logging
45-
import os
46-
import re
47-
import sys
48-
import datetime
49-
import subprocess
50-
from functools import reduce
51-
from helpers import CLANG_FORMAT, BUSTUB_DIR, CLANG_FORMAT_FILE, LOG,\
52-
clang_format, hunks_from_staged_files, hunks_from_last_commits
38+
'''
5339

5440

41+
import os
42+
import datetime
43+
import sys
44+
BUSTUB_DIR = os.path.abspath(os.path.dirname(
45+
__file__)).replace('/build_support', '')
5546
BUSTUB_SRC_DIR = os.path.join(BUSTUB_DIR, "src")
5647
BUSTUB_TESTS_DIR = os.path.join(BUSTUB_DIR, "test")
5748

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

54+
6355
# header framework, dynamic information will be added inside function
6456
header_comment_line_1 = "//===----------------------------------------------------------------------===//\n"
6557
header_comment_line_1 += "//\n"
66-
header_comment_line_1 += "// CMU-DB Project (15-445/645)\n"
67-
header_comment_line_2 = "// ***DO NO SHARE PUBLICLY***\n"
58+
header_comment_line_1 += "// BusTub\n"
59+
header_comment_line_2 = "//\n"
6860
header_comment_line_3 = "// "
6961
header_comment_line_4 = "//\n"
7062
header_comment_line_5 = "// Identification: "
7163
header_comment_line_6 = "//\n"
72-
header_comment_line_7 = "// Copyright (c) %d, Carnegie Mellon University Database Group\n" % datetime.datetime.now().year
64+
header_comment_line_7 = "// Copyright (c) 2015-%d, Carnegie Mellon University Database Group\n" % datetime.datetime.now().year
7365
header_comment_line_8 = "//\n"
7466
header_comment_line_9 = "//===----------------------------------------------------------------------===//\n\n"
7567

76-
header_comment_1 = header_comment_line_1 + header_comment_line_2
77-
header_comment_3 = header_comment_line_4
78-
header_comment_5 = header_comment_line_6 + header_comment_line_7 \
79-
+ header_comment_line_8 + header_comment_line_9
68+
header_comment_line_6 = header_comment_line_6 + \
69+
header_comment_line_7+header_comment_line_8+header_comment_line_9
8070

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

8473

85-
# ==============================================
86-
# UTILITY FUNCTION DEFINITIONS
87-
# ==============================================
74+
def add_file_header(file: str):
75+
"""add header to this file."""
76+
if not file.endswith('.h'):
77+
return
78+
file_path = os.path.join(BUSTUB_DIR, file)
8879

80+
if not os.path.isfile(file_path):
81+
return
8982

90-
def format_file(file_path, file_hunks, update_header, clang_format_code):
91-
"""Formats the file passed as argument."""
92-
file_name = os.path.basename(file_path)
93-
abs_path = os.path.abspath(file_path)
94-
rel_path_from_bustub_dir = os.path.relpath(abs_path, BUSTUB_DIR)
83+
dir_flag = False
84+
for deafult_dir in DEFAULT_DIRS:
85+
if file_path.startswith(deafult_dir):
86+
dir_flag = True
87+
break
88+
if not dir_flag:
89+
return
9590

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

99-
if update_header:
100-
# strip old header if it exists
101-
header_match = HEADER_REGEX.match(file_data)
102-
if not header_match is None:
103-
LOG.info("Strip header from %s", file_name)
104-
header_comment = header_match.group()
105-
LOG.debug("Header comment : %s", header_comment)
106-
file_data = file_data.replace(header_comment, "")
93+
header_comment_filename = header_comment_line_3+file_name+'\n'
94+
header_comment_relpath = header_comment_line_5+file + '\n'
95+
header_commnt = header_comment_line_1 + header_comment_filename + \
96+
header_comment_line_4+header_comment_relpath+header_comment_line_6
10797

108-
# add new header
109-
LOG.info("Add header to %s", file_name)
110-
header_comment_2 = header_comment_line_3 + file_name + "\n"
111-
header_comment_4 = header_comment_line_5\
112-
+ rel_path_from_bustub_dir + "\n"
113-
header_comment = header_comment_1 + header_comment_2 \
114-
+ header_comment_3 + header_comment_4 \
115-
+ header_comment_5
116-
# print header_comment
98+
with open(file_path, 'r+') as f:
99+
# maybe use sed -i '' file
100+
old = f.read()
101+
f.seek(0)
102+
f.write(header_commnt)
103+
f.write(old)
117104

118-
file_data = header_comment + file_data
119-
120-
file.seek(0, 0)
121-
file.truncate()
122-
file.write(file_data)
123-
124-
elif clang_format_code:
125-
clang_format(file_path, file_hunks)
126-
127-
# END WITH
128-
# END FORMAT__FILE(FILE_NAME)
129-
130-
131-
def format_dir(dir_path, update_header, clang_format_code):
132-
"""Formats all the files in the dir passed as argument."""
133-
for subdir, _, files in os.walk(dir_path): # _ is for directories.
134-
for file in files:
135-
# print os.path.join(subdir, file)
136-
file_path = subdir + os.path.sep + file
137-
138-
if file_path.endswith(".h") or file_path.endswith(".cpp"):
139-
format_file(file_path, None, update_header, clang_format_code)
140-
# END IF
141-
# END FOR [file]
142-
# END FOR [os.walk]
143-
# END ADD_HEADERS_DIR(DIR_PATH)
144-
145-
146-
# ==============================================
147-
# Main Function
148-
# ==============================================
149105

150106
if __name__ == '__main__':
151-
152-
PARSER = argparse.ArgumentParser(
153-
description='Update headers and/or format source code'
154-
)
155-
156-
PARSER.add_argument(
157-
"-u", "--update-header",
158-
help='Action: Update existing headers or add new ones',
159-
action='store_true'
160-
)
161-
PARSER.add_argument(
162-
"-c", "--clang-format-code",
163-
help='Action: Apply clang-format to source code',
164-
action='store_true'
165-
)
166-
PARSER.add_argument(
167-
"-f", "--staged-files",
168-
help='Action: Apply the selected action(s) to all staged files (git). ' +
169-
'(clang-format will only touch the staged lines)',
170-
action='store_true'
171-
)
172-
PARSER.add_argument(
173-
"-n", "--number-commits",
174-
help='Action: Apply the selected action(s) to all changes of the last ' +
175-
'<n> commits (clang-format will only touch the changed lines)',
176-
type=int, default=0
177-
)
178-
PARSER.add_argument(
179-
'paths', metavar='PATH', type=str, nargs='*',
180-
help='Files or directories to (recursively) apply the actions to'
181-
)
182-
183-
ARGS = PARSER.parse_args()
184-
185-
# TARGETS is a list of files with an optional list of hunks, represented as
186-
# pair (start, end) of line numbers, 1 based.
187-
# element of TARGETS: (filename, None) or (filename, [(start,end)])
188-
189-
if ARGS.staged_files:
190-
TARGETS = hunks_from_staged_files()
191-
192-
if not TARGETS:
193-
LOG.error(
194-
"no staged files or not calling from a repository -- exiting"
195-
)
196-
sys.exit("no staged files or not calling from a repository")
197-
198-
elif ARGS.number_commits > 0:
199-
TARGETS = hunks_from_last_commits(ARGS.number_commits)
200-
201-
if not TARGETS:
202-
LOG.error(
203-
"no changes could be extracted for formatting -- exiting"
204-
)
205-
sys.exit("no changes could be extracted for formatting")
206-
207-
elif not ARGS.paths:
208-
LOG.error("no files or directories given -- exiting")
209-
sys.exit("no files or directories given")
210-
211-
else:
212-
TARGETS = [(f, None) for f in ARGS.paths]
213-
214-
for f, hunks in TARGETS:
215-
if os.path.isfile(f):
216-
LOG.info("Scanning file: %s", f)
217-
format_file(f, hunks, ARGS.update_header, ARGS.clang_format_code)
218-
elif os.path.isdir(f):
219-
LOG.info("Scanning directory %s", f)
220-
format_dir(f, ARGS.update_header, ARGS.clang_format_code)
221-
# FOR
222-
# IF
107+
for file in sys.argv[1:]:
108+
add_file_header(file)

0 commit comments

Comments
 (0)