Skip to content

Commit

Permalink
Add copyright header and pre-commit hook (open-mmlab#872)
Browse files Browse the repository at this point in the history
* Add pre-commit hook to automatically add copyright file header

* update files with copyright header

*  Limit copyright checking in the first 2 lines of a file
* Exclude configs in demo/

* set max-header-lines as 5

* rebase to master and add copyright to new files
* move benchmark_regression into .dev_scripts/benchmark
  • Loading branch information
ly015 authored Aug 27, 2021
1 parent e3658fc commit 4ebbb47
Show file tree
Hide file tree
Showing 298 changed files with 404 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import math
import os
Expand Down Expand Up @@ -31,7 +32,7 @@ def parse_args():
parser.add_argument(
'--config',
help='test config file path',
default='./.dev_scripts/benchmark_regression_cfg_tmpl.yaml')
default='./.dev_scripts/benchmark/benchmark_regression_cfg.yaml')
parser.add_argument(
'--priority',
nargs=2,
Expand Down
100 changes: 100 additions & 0 deletions .dev_scripts/github/update_copyright.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env python
# Copyright (c) OpenMMLab. All rights reserved.

import argparse
import os
import os.path as osp
import re
import sys

HEADER = 'Copyright (c) OpenMMLab. All rights reserved.\n'
HEADER_KEYWORDS = {'Copyright', 'License'}


def contains_header(lines, comment_symbol, max_header_lines):
for line in lines[:max_header_lines]:
if line.startswith('#!'):
# skip shebang line
continue
elif re.match(f'{comment_symbol}.*({"|".join(HEADER_KEYWORDS)})',
line):
return True

return False


def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'files',
type=str,
nargs='*',
help='Files to add copyright header. If an empty list is given, '
'search target files according to "--src", "--exclude" and '
'"--suffixes"')
parser.add_argument(
'--src', type=str, default=None, help='Root path to search files.')
parser.add_argument(
'--exclude', type=str, default=None, help='Path to exclude in search.')
parser.add_argument(
'--suffixes',
type=str,
nargs='+',
default=['.py', '.c', '.cpp', '.cu', '.sh'],
help='Only files with one of the given suffixes will be searched.')
parser.add_argument(
'--max-header-lines',
type=int,
default=5,
help='Only checkout copyright information in the first several lines '
'of a file.')

args = parser.parse_args()
return args


def main():
args = parse_args()

file_list = []
if args.files:
file_list = args.files
else:
assert args.src is not None
for root, _, files in os.walk(args.src):
if args.exclude and osp.realpath(root).startswith(
osp.realpath(args.exclude)):
continue

for file in files:
if osp.splitext(file)[1] in args.suffixes:
file_list.append(osp.join(root, file))

modified = False
for file in file_list:
suffix = osp.splitext(file)[1]
if suffix in {'.py', '.sh'}:
comment_symbol = '# '
elif suffix in {'.c', '.cpp', '.cu'}:
comment_symbol = '// '
else:
raise ValueError(f'Comment symbol of files with suffix {suffix} '
'is unspecified.')

with open(file, 'r') as f:
lines = f.readlines()
if not contains_header(lines, comment_symbol, args.max_header_lines):
if lines and lines[0].startswith('#!'):
lines.insert(1, comment_symbol + HEADER)
else:
lines.insert(0, comment_symbol + HEADER)

with open(file, 'w') as f:
f.writelines(lines)
modified = True

return int(modified)


if __name__ == '__main__':
sys.exit(main())
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# Copyright (c) OpenMMLab. All rights reserved.

# This tool is used to update model-index.yml which is required by MIM, and
# will be automatically called as a pre-commit hook. The updating will be
Expand Down
9 changes: 8 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ repos:
- id: update-model-index
name: update-model-index
description: Collect model information and update model-index.yml
entry: tools/misc/update_model_index.py
entry: .dev_scripts/github/update_model_index.py
additional_dependencies: [mmcv]
language: python
files: ^configs/.*\.md$
require_serial: true
- id: update-copyright
name: update-copyright
description: Add OpenMMLab copyright header to files
entry: .dev_scripts/github/update_copyright.py
language: python
files: ^(demo|docs|docs_zh-CN|mmpose|tests|tools|\.dev_scripts)/.*\.(py|c|cpp|cu|sh)$
exclude: ^demo/mm(detection|tracking)_cfg/.*$
1 change: 1 addition & 0 deletions demo/body3d_two_stage_img_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
import os.path as osp
from argparse import ArgumentParser
Expand Down
1 change: 1 addition & 0 deletions demo/body3d_two_stage_video_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import copy
import os
import os.path as osp
Expand Down
1 change: 1 addition & 0 deletions demo/bottom_up_img_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/bottom_up_pose_tracking_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/bottom_up_video_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/face_img_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/face_video_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/interhand3d_img_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
import os.path as osp
from argparse import ArgumentParser
Expand Down
1 change: 1 addition & 0 deletions demo/mesh_img_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/top_down_img_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/top_down_img_demo_with_mmdet.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/top_down_pose_tracking_demo_with_mmdet.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/top_down_pose_tracking_demo_with_mmtracking.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/top_down_video_demo_full_frame_without_det.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/top_down_video_demo_with_mmdet.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os
from argparse import ArgumentParser

Expand Down
1 change: 1 addition & 0 deletions demo/webcam_demo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import time
from collections import deque
Expand Down
1 change: 1 addition & 0 deletions docs/collect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# Copyright (c) OpenMMLab. All rights reserved.
import os
import re
from glob import glob
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
Expand Down
1 change: 1 addition & 0 deletions docs/merge_docs.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
# Copyright (c) OpenMMLab. All rights reserved.

sed -i '$a\\n' ../demo/docs/*_demo.md
cat ../demo/docs/*_demo.md | sed "s/#/#&/" | sed "s/md###t/html#t/g" | sed '1i\# Demo' | sed 's/](\/docs\//](/g' | sed 's=](/=](https://github.com/open-mmlab/mmpose/tree/master/=g' >demo.md
Expand Down
1 change: 1 addition & 0 deletions docs/stats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# Copyright (c) OpenMMLab. All rights reserved.
import functools as func
import glob
import re
Expand Down
1 change: 1 addition & 0 deletions docs_zh-CN/collect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# Copyright (c) OpenMMLab. All rights reserved.
import os
import re
from glob import glob
Expand Down
1 change: 1 addition & 0 deletions docs_zh-CN/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
Expand Down
1 change: 1 addition & 0 deletions docs_zh-CN/merge_docs.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
# Copyright (c) OpenMMLab. All rights reserved.

sed -i '$a\\n' ../demo/docs/*_demo.md
cat ../demo/docs/*_demo.md | sed "s/#/#&/" | sed "s/md###t/html#t/g" | sed '1i\# 示例' | sed 's/](\/docs\//](/g' | sed 's=](/=](https://github.com/open-mmlab/mmpose/tree/master/=g' >demo.md
Expand Down
1 change: 1 addition & 0 deletions docs_zh-CN/stats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# Copyright (c) OpenMMLab. All rights reserved.
import functools as func
import glob
import re
Expand Down
1 change: 1 addition & 0 deletions mmpose/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import mmcv
from mmcv import digit_version, parse_version_info

Expand Down
1 change: 1 addition & 0 deletions mmpose/apis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from .inference import (inference_bottom_up_pose_model,
inference_top_down_pose_model, init_pose_model,
process_mmdet_results, vis_pose_result)
Expand Down
1 change: 1 addition & 0 deletions mmpose/apis/inference.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os

import cv2
Expand Down
1 change: 1 addition & 0 deletions mmpose/apis/inference_3d.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import numpy as np
import torch
from mmcv.parallel import collate, scatter
Expand Down
1 change: 1 addition & 0 deletions mmpose/apis/inference_tracking.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import warnings

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions mmpose/apis/test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
import pickle
import shutil
Expand Down
1 change: 1 addition & 0 deletions mmpose/apis/train.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import warnings

import torch
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from .camera import * # noqa: F401, F403
from .evaluation import * # noqa: F401, F403
from .fp16 import * # noqa: F401, F403
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/camera/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from .camera_base import CAMERAS
from .single_camera import SimpleCamera

Expand Down
1 change: 1 addition & 0 deletions mmpose/core/camera/camera_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from abc import ABCMeta, abstractmethod

from mmcv.utils import Registry
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/camera/single_camera.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import numpy as np

from .camera_base import CAMERAS, SingleCameraBase
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/distributed_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import torch
import torch.nn as nn
from mmcv.parallel import MODULE_WRAPPERS, MMDistributedDataParallel
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/evaluation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from .bottom_up_eval import (aggregate_results, get_group_preds,
get_multi_stage_outputs)
from .eval_hooks import DistEvalHook, EvalHook
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/evaluation/bottom_up_eval.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import numpy as np
import torch

Expand Down
1 change: 1 addition & 0 deletions mmpose/core/evaluation/eval_hooks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import tempfile
import warnings

Expand Down
1 change: 1 addition & 0 deletions mmpose/core/evaluation/pose3d_eval.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import numpy as np

from .mesh_eval import compute_similarity_transform
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/evaluation/top_down_eval.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import warnings

import cv2
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/fp16/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from .decorators import auto_fp16, force_fp32
from .hooks import Fp16OptimizerHook, wrap_fp16_model
from .utils import cast_tensor_type
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/fp16/decorators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import functools
import warnings
from inspect import getfullargspec
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/fp16/hooks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import copy

import torch
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/fp16/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from collections import abc

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/optimizer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from .builder import OPTIMIZERS, build_optimizers

__all__ = ['build_optimizers', 'OPTIMIZERS']
1 change: 1 addition & 0 deletions mmpose/core/optimizer/builder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from mmcv.runner import build_optimizer
from mmcv.utils import Registry

Expand Down
1 change: 1 addition & 0 deletions mmpose/core/post_processing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from .nms import oks_iou, oks_nms, soft_oks_nms
from .one_euro_filter import OneEuroFilter
from .post_transforms import (affine_transform, flip_back, fliplr_joints,
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from .dist_utils import allreduce_grads
from .regularizations import WeightNormClipHook

Expand Down
1 change: 1 addition & 0 deletions mmpose/core/utils/dist_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from collections import OrderedDict

import torch.distributed as dist
Expand Down
1 change: 1 addition & 0 deletions mmpose/core/utils/regularizations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from abc import ABCMeta, abstractmethod, abstractproperty

import torch
Expand Down
Loading

0 comments on commit 4ebbb47

Please sign in to comment.