Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit 29d5d1b

Browse files
Kiuk Chungfacebook-github-bot
Kiuk Chung
authored andcommitted
add formatter_python.sh for external contributor, and setup circleci to run linter on builds (#41)
Summary: Pull Request resolved: #41 External contributors can run: ``` scripts/formatter_python.sh ``` to simulate `arc lint` Setup lint job in circleci credit: ClassyVision (https://github.com/facebookresearch/ClassyVision/blob/master/scripts/formatter.sh) Reviewed By: mannatsingh Differential Revision: D19656921 fbshipit-source-id: 8aac65c2be02eeee104f869aeda155a42ae9a45a
1 parent f8d2b6b commit 29d5d1b

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

.circleci/config.yml

+20
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ run_tests: &run_tests
9696
# tests
9797
# ------------------------------------------------------------------------------
9898
jobs:
99+
linter:
100+
<<: *cpu
101+
working_directory: ~/torchelastic
102+
steps:
103+
- checkout
104+
105+
- <<: *install_python
106+
107+
- run:
108+
name: setup lint
109+
command: |
110+
pip install --upgrade pip
111+
pip install --progress-bar off black isort
112+
113+
- run:
114+
name: run isort and black
115+
command: |
116+
bash ./scripts/formatter_python.sh
117+
99118
cpu_tests:
100119
<<: *cpu
101120

@@ -172,6 +191,7 @@ workflows:
172191
version: 2
173192
build_and_test:
174193
jobs:
194+
- linter
175195
- cpu_tests
176196
# no gpu tests for now; uncomment to enable
177197
# - gpu_tests

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ We actively welcome your pull requests.
1212
2. If you've added code that should be tested, add tests.
1313
3. If you've changed APIs, update the documentation.
1414
4. Ensure the test suite passes.
15-
5. Make sure your code lints.
15+
5. Make sure your code lints (run `scripts/formatter_python.sh`).
1616
6. If you haven't already, complete the Contributor License Agreement ("CLA").
1717

1818
## Contributor License Agreement ("CLA")

scripts/formatter_python.sh

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/sh
2+
# Copyright (c) Facebook, Inc. and its affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
if [ ! "$(black --version)" ]
9+
then
10+
echo "Please install black."
11+
exit 1
12+
fi
13+
if [ ! "$(isort --version)" ]
14+
then
15+
echo "Please install isort."
16+
exit 1
17+
fi
18+
19+
# cd to the project directory
20+
cd "$(dirname "$0")/.." || exit 1
21+
22+
GIT_URL_1="https://github.com/pytorch/elastic.git"
23+
GIT_URL_2="[email protected]:pytorch/elastic.git"
24+
25+
UPSTREAM_URL="$(git config remote.upstream.url)"
26+
27+
if [ -z "$UPSTREAM_URL" ]
28+
then
29+
echo "Setting upstream remote to $GIT_URL_1"
30+
git remote add upstream "$GIT_URL_1"
31+
elif [ "$UPSTREAM_URL" != "$GIT_URL_1" ] && \
32+
[ "$UPSTREAM_URL" != "$GIT_URL_2" ]
33+
then
34+
echo "upstream remote set to $UPSTREAM_URL."
35+
echo "Please delete the upstream remote or set it to $GIT_URL_1 to use this script."
36+
exit 1
37+
fi
38+
39+
# fetch upstream
40+
git fetch upstream
41+
42+
43+
CHANGED_FILES="$(git diff --diff-filter=ACMRT --name-only upstream/master | grep '\.py$' | tr '\n' ' ')"
44+
45+
if [ "$CHANGED_FILES" != "" ]
46+
then
47+
echo "Running isort..."
48+
isort "$CHANGED_FILES" --recursive --multi-line 3 --trailing-comma --force-grid-wrap 0 \
49+
--line-width 88 --lines-after-imports 2 --combine-as --section-default THIRDPARTY
50+
51+
echo "Running black..."
52+
black "$CHANGED_FILES"
53+
else
54+
echo "No changes made to any Python files. Nothing to do."
55+
exit 0
56+
fi
57+
58+
# Check if any files were modified by running isort + black
59+
# If so, then the files were formatted incorrectly (e.g. did not pass lint)
60+
CHANGED_FILES="$(git diff --name-only | grep '\.py$' | tr '\n' ' ')"
61+
if [ "$CHANGED_FILES" != "" ]
62+
then
63+
# need this so that CircleCI fails
64+
exit 1
65+
fi

0 commit comments

Comments
 (0)