OSDN Git Service

ヘッダコメントやらかした
[rabbit-bts/srcHead.git] / delete.py
1 #! /usr/bin/python
2 # coding:utf-8
3
4 #   Copyright 2009 senju@users.sourceforge.jp
5 #
6 #   Licensed under the Apache License, Version 2.0 (the "License");
7 #   you may not use this file except in compliance with the License.
8 #   You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #   Unless required by applicable law or agreed to in writing, software
13 #   distributed under the License is distributed on an "AS IS" BASIS,
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #   See the License for the specific language governing permissions and
16 #   limitations under the License.
17
18
19 # 標準入力からファイルのリストを読み込み、
20 # ファイルの先頭のコメントを削除する。
21
22 import sys,re
23
24 commentStart = re.compile(r"^\s*/\*")
25 commentEnd = re.compile(r"\*/\s*$")
26
27 def readToCommentStart():
28         """コメント開始まで読み込む。見つからない場合falseを返す。"""
29         for line in file:
30                 if line.strip() == "":
31                         # 空行よみとばし
32                         continue
33                 if commentStart.search(line):
34                         return True
35                 else:
36                         # 空行でもコメントでもない場合、ファイルの処理を終了
37                         return False
38         # すべて空行だった場合
39         return False
40
41
42 def readToCommentEnd():
43         """コメント終了まで読み込む。見つからない場合、falseを返す。"""
44         for line in file:
45                 if commentEnd.search(line):
46                         return True
47                 if commentStart.search(line):
48                         # コメント開始が見つかった場合、多分異常
49                         return False
50
51         # コメントの終了が見つからない場合
52         return False
53
54 for path in sys.stdin:
55         path = path.strip()
56         file = open(path)
57
58         # コメント開始まで読み飛ばし
59         if not readToCommentStart():
60                 print "%s does not have head comment." % path
61                 file.close()
62                 continue
63         # コメント終了まで読み飛ばし
64         if not readToCommentEnd():
65                 print "%s does not have head comment.(No '*/')" % path
66                 file.close()
67                 continue
68
69
70
71         buf = ""
72         # コメント終了後の空行を読み飛ばし
73         for line in file:
74                 if line.strip() == "":
75                         continue
76                 else: # 空行以外に遭遇したら
77                         buf += line
78                         break
79
80         # ソースを読み込む
81         for line in file:
82                 buf += line
83         # ソースを書き込む
84         file.close()
85         file = open(path, "w")
86         file.write(buf)
87         file.close()
88
89 None