Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit d20431c

Browse files
Merge branch 'master' into tbb
2 parents 29b16ee + 4eb7191 commit d20431c

File tree

115 files changed

+3228
-1536
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+3228
-1536
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ docs/dev
306306
# Protobuf files
307307
*.pb.cc
308308
*.pb.h
309+
*.pb
309310

310311
# Third party
311312
third_party/gflags/

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: c++
2+
python: 3.6
23
matrix:
34
fast_finish: true
45

@@ -141,7 +142,7 @@ install:
141142

142143
before_script:
143144
# first, run source_validator
144-
- python ./script/validators/source_validator.py
145+
- python script/validators/source_validator.py
145146

146147
# build peloton (override this value to execute tests)
147148
script:

Jenkinsfile

Lines changed: 56 additions & 56 deletions
Large diffs are not rendered by default.

cmake/Dependencies.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ include(cmake/CapnProtoMacros.cmake)
3636
# ---[ Google-protobuf
3737
include(cmake/ProtoBuf.cmake)
3838

39+
# --[ tensorflow
40+
find_library(TFlowC
41+
NAMES tensorflow
42+
PATHS "/usr/local/lib")
43+
list(APPEND Peloton_LINKER_LIBS ${TFlowC})
44+
3945
# ---[ Libevent
4046
find_package(Libevent REQUIRED)
4147
include_directories(SYSTEM ${LIBEVENT_INCLUDE_DIRS})

cmake/ProtoBuf.cmake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Finds Google Protocol Buffers library and compilers and extends
22
# the standard cmake script with version and python generation support
3-
4-
find_package( Protobuf REQUIRED )
3+
find_package( Protobuf REQUIRED)
54
include_directories(SYSTEM ${PROTOBUF_INCLUDE_DIR})
65
list(APPEND Peloton_LINKER_LIBS ${PROTOBUF_LIBRARIES})
76

script/formatting/formatter.py

Lines changed: 84 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
# encoding: utf-8
3-
3+
"""Format the ill-formatted code."""
44
## ==============================================
55
## GOAL : Format code, Update headers
66
## ==============================================
@@ -12,8 +12,15 @@
1212
import sys
1313
import datetime
1414
import subprocess
15-
import distutils.spawn
1615

16+
from functools import reduce
17+
18+
# Following is done so that we can import from ../helpers.py
19+
sys.path.append(
20+
os.path.abspath(os.path.dirname(__file__)).replace('/formatting', '')
21+
)
22+
from helpers import CLANG_FORMAT, PELOTON_DIR, CLANG_FORMAT_FILE, LOG,\
23+
clang_format
1724

1825
## ==============================================
1926
## CONFIGURATION
@@ -22,8 +29,6 @@
2229
# NOTE: absolute path to peloton directory is calculated from current directory
2330
# directory structure: peloton/scripts/formatting/<this_file>
2431
# PELOTON_DIR needs to be redefined if the directory structure is changed
25-
CODE_SOURCE_DIR = os.path.abspath(os.path.dirname(__file__))
26-
PELOTON_DIR = reduce(os.path.join, [CODE_SOURCE_DIR, os.path.pardir, os.path.pardir])
2732

2833
#other directory paths used are relative to peloton_dir
2934
PELOTON_SRC_DIR = os.path.join(PELOTON_DIR, "src")
@@ -34,15 +39,12 @@
3439
DEFAULT_DIRS.append(PELOTON_SRC_DIR)
3540
DEFAULT_DIRS.append(PELOTON_TESTS_DIR)
3641

37-
CLANG_FORMAT = None
38-
CLANG_FORMAT_FILE = os.path.join(PELOTON_DIR, ".clang-format")
39-
4042
## ==============================================
4143
## HEADER CONFIGURATION
4244
## ==============================================
4345

4446
#header framework, dynamic information will be added inside function
45-
header_comment_line_1 = "//===----------------------------------------------------------------------===//\n"
47+
header_comment_line_1 = "//===----------------------------------------------------------------------===//\n"
4648
header_comment_line_1 += "//\n"
4749
header_comment_line_1 += "// Peloton\n"
4850
header_comment_line_2 = "//\n"
@@ -56,81 +58,61 @@
5658

5759
header_comment_1 = header_comment_line_1 + header_comment_line_2
5860
header_comment_3 = header_comment_line_4
59-
header_comment_5 = header_comment_line_6 + header_comment_line_7 + header_comment_line_8 \
60-
+ header_comment_line_9
61+
header_comment_5 = header_comment_line_6 + header_comment_line_7 \
62+
+ header_comment_line_8 + header_comment_line_9
6163

6264
#regular expresseion used to track header
63-
header_regex = re.compile("((\/\/===-*===\/\/\n(\/\/.*\n)*\/\/===-*===\/\/[\n]*)\n\n)*")
64-
65-
## ==============================================
66-
## LOGGING CONFIGURATION
67-
## ==============================================
68-
69-
LOG = logging.getLogger(__name__)
70-
LOG_handler = logging.StreamHandler()
71-
LOG_formatter = logging.Formatter(
72-
fmt='%(asctime)s [%(funcName)s:%(lineno)03d] %(levelname)-5s: %(message)s',
73-
datefmt='%m-%d-%Y %H:%M:%S'
74-
)
75-
LOG_handler.setFormatter(LOG_formatter)
76-
LOG.addHandler(LOG_handler)
77-
LOG.setLevel(logging.INFO)
65+
HEADER_REGEX = re.compile(r"((\/\/===-*===\/\/\n(\/\/.*\n)*\/\/===-*===\/\/[\n]*)\n\n)*")
7866

7967
## ==============================================
8068
## UTILITY FUNCTION DEFINITIONS
8169
## ==============================================
8270

83-
#format the file passed as argument
84-
def format_file(file_path, update_header, clang_format_code):
8571

72+
def format_file(file_path, update_header, clang_format_code):
73+
"""Formats the file passed as argument."""
8674
file_name = os.path.basename(file_path)
8775
abs_path = os.path.abspath(file_path)
88-
rel_path_from_peloton_dir = os.path.relpath(abs_path,PELOTON_DIR)
76+
rel_path_from_peloton_dir = os.path.relpath(abs_path, PELOTON_DIR)
8977

90-
with open(file_path, "r+") as fd:
91-
file_data = fd.read()
78+
with open(file_path, "r+") as file:
79+
file_data = file.read()
9280

9381
if update_header:
9482
# strip old header if it exists
95-
header_match = header_regex.match(file_data)
83+
header_match = HEADER_REGEX.match(file_data)
9684
if not header_match is None:
97-
LOG.info("Strip header from %s", file_name)
98-
header_comment = header_match.group()
99-
LOG.debug("Header comment : %s", header_comment)
100-
file_data = file_data.replace(header_comment,"")
101-
85+
LOG.info("Strip header from %s", file_name)
86+
header_comment = header_match.group()
87+
LOG.debug("Header comment : %s", header_comment)
88+
file_data = file_data.replace(header_comment,"")
89+
10290
# add new header
10391
LOG.info("Add header to %s", file_name)
10492
header_comment_2 = header_comment_line_3 + file_name + "\n"
105-
header_comment_4 = header_comment_line_5 + rel_path_from_peloton_dir + "\n"
106-
header_comment = header_comment_1 + header_comment_2 + header_comment_3 \
107-
+ header_comment_4 + header_comment_5
93+
header_comment_4 = header_comment_line_5\
94+
+ rel_path_from_peloton_dir + "\n"
95+
header_comment = header_comment_1 + header_comment_2 \
96+
+ header_comment_3 + header_comment_4 \
97+
+ header_comment_5
10898
#print header_comment
10999

110100
file_data = header_comment + file_data
111101

112-
fd.seek(0,0)
113-
fd.truncate()
114-
fd.write(file_data)
102+
file.seek(0, 0)
103+
file.truncate()
104+
file.write(file_data)
115105

116106
elif clang_format_code:
117-
if CLANG_FORMAT is None:
118-
LOG.error("clang-format seems not installed")
119-
exit("clang-format seems not installed")
120-
121-
formatting_command = CLANG_FORMAT + " -style=file -i " + file_path
122-
LOG.info(formatting_command)
123-
subprocess.call([CLANG_FORMAT, "-style=file", "-i", file_path])
107+
clang_format(file_path)
124108

125109
#END WITH
126-
127-
fd.close()
128110
#END FORMAT__FILE(FILE_NAME)
129111

130112

131-
#format all the files in the dir passed as argument
132113
def format_dir(dir_path, update_header, clang_format_code):
133-
for subdir, dirs, files in os.walk(dir_path):
114+
"""Formats all the files in the dir passed as argument."""
115+
for subdir, _, files in os.walk(dir_path): # _ is for directories.
134116
for file in files:
135117
#print os.path.join(subdir, file)
136118
file_path = subdir + os.path.sep + file
@@ -142,53 +124,66 @@ def format_dir(dir_path, update_header, clang_format_code):
142124
#END FOR [os.walk]
143125
#END ADD_HEADERS_DIR(DIR_PATH)
144126

145-
#find clang-format executable
146-
def find_clangformat():
147-
global CLANG_FORMAT
148-
#check for possible clang-format versions
149-
for exe in ["clang-format", "clang-format-3.6", "clang-format-3.7", "clang-format-3.8"]:
150-
path = distutils.spawn.find_executable(exe)
151-
if not path is None:
152-
CLANG_FORMAT = path
153-
return
154-
155127

156128
## ==============================================
157129
## Main Function
158130
## ==============================================
159131

160132
if __name__ == '__main__':
161133

162-
parser = argparse.ArgumentParser(description='Update headers and/or format source code')
163-
164-
parser.add_argument("-u", "--update-header", help='Action: Update existing headers or add new ones', action='store_true')
165-
parser.add_argument("-c", "--clang-format-code", help='Action: Apply clang-format to source code', action='store_true')
166-
parser.add_argument("-f", "--staged-files", help='Action: Apply the selected action(s) to all staged files (git)', action='store_true')
167-
parser.add_argument('paths', metavar='PATH', type=str, nargs='*',
168-
help='Files or directories to (recursively) apply the actions to')
169-
170-
args = parser.parse_args()
171-
172-
find_clangformat()
173-
174-
if args.staged_files:
175-
targets = [os.path.abspath(os.path.join(PELOTON_DIR, f)) for f in subprocess.check_output(["git", "diff", "--name-only", "HEAD", "--cached", "--diff-filter=d"]).split()]
176-
if not targets:
177-
LOG.error("no staged files or not calling from a repository -- exiting")
134+
PARSER = argparse.ArgumentParser(
135+
description='Update headers and/or format source code'
136+
)
137+
138+
PARSER.add_argument(
139+
"-u", "--update-header",
140+
help='Action: Update existing headers or add new ones',
141+
action='store_true'
142+
)
143+
PARSER.add_argument(
144+
"-c", "--clang-format-code",
145+
help='Action: Apply clang-format to source code',
146+
action='store_true'
147+
)
148+
PARSER.add_argument(
149+
"-f", "--staged-files",
150+
help='Action: Apply the selected action(s) to all staged files (git)',
151+
action='store_true'
152+
)
153+
PARSER.add_argument(
154+
'paths', metavar='PATH', type=str, nargs='*',
155+
help='Files or directories to (recursively) apply the actions to'
156+
)
157+
158+
ARGS = PARSER.parse_args()
159+
160+
if ARGS.staged_files:
161+
PELOTON_DIR_bytes = bytes(PELOTON_DIR, 'utf-8')
162+
TARGETS = [
163+
str(os.path.abspath(os.path.join(PELOTON_DIR_bytes, f)), 'utf-8') \
164+
for f in subprocess.check_output(
165+
["git", "diff", "--name-only", "HEAD", "--cached",
166+
"--diff-filter=d"
167+
]
168+
).split()]
169+
170+
if not TARGETS:
171+
LOG.error(
172+
"no staged files or not calling from a repository -- exiting"
173+
)
178174
sys.exit("no staged files or not calling from a repository")
179-
elif not args.paths:
175+
elif not ARGS.paths:
180176
LOG.error("no files or directories given -- exiting")
181177
sys.exit("no files or directories given")
182178
else:
183-
targets = args.paths
184-
185-
for x in targets:
179+
TARGETS = ARGS.paths
180+
181+
for x in TARGETS:
186182
if os.path.isfile(x):
187-
LOG.info("Scanning file: " + x)
188-
format_file(x, args.update_header, args.clang_format_code)
183+
LOG.info("Scanning file: %s", x)
184+
format_file(x, ARGS.update_header, ARGS.clang_format_code)
189185
elif os.path.isdir(x):
190-
LOG.info("Scanning directory " + x)
191-
format_dir(x, args.update_header, args.clang_format_code)
186+
LOG.info("Scanning directory %s", x)
187+
format_dir(x, ARGS.update_header, ARGS.clang_format_code)
192188
## FOR
193189
## IF
194-

script/git-hooks/pre-commit

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
# To enable, symlink this file to '.git/hooks/pre-commit' like so:
99
# ln -s ../../script/git-hooks/pre-commit .git/hooks/pre-commit
1010

11-
FORMATTER_PATH="./script/formatting/formatter.py"
11+
FORMATTER_COMMAND="./script/formatting/formatter.py"
1212

1313
FILES=$(git diff --name-only HEAD --cached --diff-filter=d | grep '\.\(cpp\|h\)$')
1414
if [ -n "$FILES" ]; then
15-
python script/validators/source_validator.py --files $FILES
15+
./script/validators/source_validator.py --files $FILES
1616
RESULT=$?
1717
if [ $RESULT -ne 0 ]; then
1818
echo "***************************************"
1919
echo "******* Peloton Pre-Commit Hook *******"
2020
echo "***************************************"
2121
echo "Use \"$FORMATTER_PATH -c -f\" to format all staged files."
2222
echo "Or use \"git commit --no-verify\" to temporarily bypass the pre-commit hook."
23+
2324
echo
2425
echo "Be aware that changed files have to be staged again!"
2526
echo "***************************************"

0 commit comments

Comments
 (0)