| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#============================================ |
|
2
|
|
|
|
|
|
|
# 便利関数群 |
|
3
|
|
|
|
|
|
|
# 基本的に全関数、exportして使う |
|
4
|
|
|
|
|
|
|
# BASIC認証については、必要に応じてパッケージ変数(BASIC_AUTH_LIST)を上書きする |
|
5
|
|
|
|
|
|
|
# ------------------------------------------- |
|
6
|
|
|
|
|
|
|
# アクセサ |
|
7
|
|
|
|
|
|
|
# xxx_xxx String xxxxxx |
|
8
|
|
|
|
|
|
|
# デフォルトは$XXX_XXX |
|
9
|
|
|
|
|
|
|
#============================================ |
|
10
|
|
|
|
|
|
|
package Sorauta::Utility; |
|
11
|
1
|
|
|
1
|
|
26608
|
use base 'Exporter'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
517
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
38
|
use 5.012003; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
41
|
|
|
14
|
1
|
|
|
1
|
|
6
|
use strict; |
|
|
1
|
|
|
|
|
6
|
|
|
|
1
|
|
|
|
|
35
|
|
|
15
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
35
|
|
|
16
|
1
|
|
|
1
|
|
934
|
use utf8; |
|
|
1
|
|
|
|
|
10
|
|
|
|
1
|
|
|
|
|
5
|
|
|
17
|
1
|
|
|
1
|
|
807
|
use CGI::Carp qw/fatalsToBrowser/; |
|
|
1
|
|
|
|
|
4934
|
|
|
|
1
|
|
|
|
|
7
|
|
|
18
|
1
|
|
|
1
|
|
1420
|
use Data::Dumper; |
|
|
1
|
|
|
|
|
17568
|
|
|
|
1
|
|
|
|
|
89
|
|
|
19
|
1
|
|
|
1
|
|
1363
|
use LWP::UserAgent; |
|
|
1
|
|
|
|
|
57083
|
|
|
|
1
|
|
|
|
|
39
|
|
|
20
|
1
|
|
|
1
|
|
13
|
use Time::Local; # for timegm |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
1234
|
|
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
|
25
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
|
26
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# This allows declaration use Sorauta::Utility ':all'; |
|
29
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
|
30
|
|
|
|
|
|
|
# will save memory. |
|
31
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
) ] ); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# 利用可能なサブルーチン一覧 |
|
38
|
|
|
|
|
|
|
our @EXPORT = qw/get_from_http save_file create_get_url get_timestamp get_date get_epoch_from_formated_http cat is_hidden_file is_unnecessary_copying_file/; |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Preloaded methods go here. |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# ベーシック認証情報 |
|
45
|
|
|
|
|
|
|
# override: %Sorauta::Utility::BASIC_AUTH_LIST = (); |
|
46
|
|
|
|
|
|
|
our %BASIC_AUTH_LIST = ( |
|
47
|
|
|
|
|
|
|
#'host.example.com' => { |
|
48
|
|
|
|
|
|
|
# id => 'user_id', |
|
49
|
|
|
|
|
|
|
# pass => 'user_pass', |
|
50
|
|
|
|
|
|
|
#}, |
|
51
|
|
|
|
|
|
|
); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# HTTPリクエストのタイムアウト時間 |
|
54
|
|
|
|
|
|
|
our $TIMEOUT = 20; |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# HTTPリクエスト失敗時のリトライ回数 |
|
57
|
|
|
|
|
|
|
our $RETRY_COUNT = 1; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# デバッグ出力するか |
|
60
|
|
|
|
|
|
|
our $DEBUG = 0; |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
#========================================== |
|
63
|
|
|
|
|
|
|
# httpリクエスト送る |
|
64
|
|
|
|
|
|
|
# req: |
|
65
|
|
|
|
|
|
|
# url : リクエスト先のURL |
|
66
|
|
|
|
|
|
|
# timeout : リクエストのタイムアウト時間 |
|
67
|
|
|
|
|
|
|
# retry_count: リクエスト失敗時にリトライする回数 |
|
68
|
|
|
|
|
|
|
# res: |
|
69
|
|
|
|
|
|
|
# response: LWP::UserAgentのレスポンスオブジェクト |
|
70
|
|
|
|
|
|
|
#========================================== |
|
71
|
|
|
|
|
|
|
sub get_from_http { |
|
72
|
2
|
|
|
2
|
0
|
524
|
my($url, $timeout, $retry_count) = @_; |
|
73
|
2
|
50
|
|
|
|
8
|
unless ($timeout) { |
|
74
|
2
|
|
|
|
|
6
|
$timeout = $TIMEOUT; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
2
|
50
|
|
|
|
5
|
unless ($retry_count) { |
|
77
|
2
|
|
|
|
|
4
|
$retry_count = $RETRY_COUNT; |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
|
|
80
|
2
|
50
|
|
|
|
9
|
if ($DEBUG) { |
|
81
|
0
|
|
|
|
|
0
|
print '[Sorauta::Utility][get_from_http]', $url, $/; |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# create agent |
|
85
|
2
|
|
|
|
|
19
|
my $ua = LWP::UserAgent->new( |
|
86
|
|
|
|
|
|
|
); |
|
87
|
2
|
|
|
|
|
4601
|
$ua->timeout($timeout); |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# create request |
|
90
|
2
|
|
|
|
|
37
|
my $req = HTTP::Request->new( |
|
91
|
|
|
|
|
|
|
GET => $url |
|
92
|
|
|
|
|
|
|
); |
|
93
|
2
|
|
|
|
|
11174
|
while (my($key, $basic_auth) = each(%BASIC_AUTH_LIST)) { |
|
94
|
0
|
0
|
|
|
|
0
|
if ($url =~ /$key/) { |
|
95
|
0
|
|
|
|
|
0
|
$req->authorization_basic($basic_auth->{id}, $basic_auth->{pass}); |
|
96
|
0
|
|
|
|
|
0
|
last; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
|
|
100
|
2
|
|
|
|
|
6
|
my $try_count = 0; |
|
101
|
2
|
|
|
|
|
3
|
my $response; |
|
102
|
2
|
|
|
|
|
11
|
while ($try_count++ < $retry_count) { |
|
103
|
2
|
|
|
|
|
17
|
$response = $ua->request($req); |
|
104
|
2
|
100
|
|
|
|
934859
|
if ($response->is_success) { |
|
105
|
1
|
|
|
|
|
12
|
last; |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
else { |
|
108
|
1
|
50
|
|
|
|
19
|
if ($DEBUG) { |
|
109
|
0
|
|
|
|
|
0
|
print '[Sorauta::Utility][get_from_http]', $url, ' got ', $response->status_line, ' ... retry', $/; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
2
|
|
|
|
|
64
|
return $response; |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
#========================================== |
|
118
|
|
|
|
|
|
|
# ファイルを保存 |
|
119
|
|
|
|
|
|
|
# req: |
|
120
|
|
|
|
|
|
|
# path: 保存ファイルのパス |
|
121
|
|
|
|
|
|
|
# content: ファイルの内容 |
|
122
|
|
|
|
|
|
|
# is_binary: バイナリの場合は1、それ以外は0 |
|
123
|
|
|
|
|
|
|
# res: |
|
124
|
|
|
|
|
|
|
# result: 成功時は1、失敗時は2 |
|
125
|
|
|
|
|
|
|
#========================================== |
|
126
|
|
|
|
|
|
|
sub save_file { |
|
127
|
1
|
|
|
1
|
0
|
860
|
my($path, $content, $is_binary) = @_; |
|
128
|
|
|
|
|
|
|
|
|
129
|
1
|
50
|
|
|
|
8
|
if ($DEBUG) { |
|
130
|
0
|
|
|
|
|
0
|
print "[Sorauta::Utility][save_file]", $path, $/; |
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
|
|
133
|
1
|
50
|
|
|
|
185
|
open(my $F, '>', $path) or die 'Can\'t open('.$path.'): '.$!; |
|
134
|
1
|
50
|
|
|
|
4
|
if ($is_binary) { |
|
135
|
0
|
|
|
|
|
0
|
binmode($F); |
|
136
|
|
|
|
|
|
|
} |
|
137
|
1
|
|
|
|
|
7
|
print $F $content; |
|
138
|
1
|
|
|
|
|
68
|
close $F; |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
#========================================== |
|
142
|
|
|
|
|
|
|
# パラメータをGET引数に変換 |
|
143
|
|
|
|
|
|
|
# ※文字列エスケープ等は引数に渡す前に既に行っている前提 |
|
144
|
|
|
|
|
|
|
# req: |
|
145
|
|
|
|
|
|
|
# url: 元になるURL |
|
146
|
|
|
|
|
|
|
# ex) http://localhost/index.pl |
|
147
|
|
|
|
|
|
|
# params: 引数をハッシュで表現 |
|
148
|
|
|
|
|
|
|
# ex) { id => 1, name => "test" } |
|
149
|
|
|
|
|
|
|
# res: |
|
150
|
|
|
|
|
|
|
# url_str: 文字列化されたURL |
|
151
|
|
|
|
|
|
|
# ex) http://localhost/index.pl?id=1&name=test |
|
152
|
|
|
|
|
|
|
#========================================== |
|
153
|
|
|
|
|
|
|
sub create_get_url { |
|
154
|
1
|
|
|
1
|
0
|
15
|
my($url, $params) = @_; |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# GET引数に変換 |
|
157
|
1
|
|
|
|
|
15
|
my $get_param = q{}; |
|
158
|
1
|
|
|
|
|
41
|
while (my($key, $val) = each(%$params)) { |
|
159
|
2
|
|
|
|
|
86
|
$get_param .= $key . '=' . $val . '&'; |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
162
|
1
|
|
|
|
|
60
|
return $url . '?' . $get_param; |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
#========================================== |
|
166
|
|
|
|
|
|
|
# 指定した時刻をタイムスタンプで取得 |
|
167
|
|
|
|
|
|
|
# req: |
|
168
|
|
|
|
|
|
|
# unix_time: 取得したい時刻のエポックタイム、指定しなければ現在時刻 |
|
169
|
|
|
|
|
|
|
# res: |
|
170
|
|
|
|
|
|
|
# timestamp: タイムスタンプを文字列に変換したもの |
|
171
|
|
|
|
|
|
|
#========================================== |
|
172
|
|
|
|
|
|
|
sub get_timestamp { |
|
173
|
2
|
|
33
|
2
|
0
|
2393
|
my $time = shift || time; |
|
174
|
2
|
|
|
|
|
96
|
my($sec, $min, $hour, $day, $month, $year) = (localtime($time))[0..5]; |
|
175
|
|
|
|
|
|
|
|
|
176
|
2
|
|
|
|
|
34
|
return sprintf("%04d/%02d/%02d %02d:%02d:%02d", |
|
177
|
|
|
|
|
|
|
$year + 1900, $month + 1, $day, $hour, $min, $sec); |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
#------------------------------------------ |
|
181
|
|
|
|
|
|
|
# 時刻情報取得 |
|
182
|
|
|
|
|
|
|
# req: |
|
183
|
|
|
|
|
|
|
# unix_time: 指定したい時刻のエポックタイム、指定しなければ現在時刻 |
|
184
|
|
|
|
|
|
|
# res: |
|
185
|
|
|
|
|
|
|
# date_obj: 時刻情報をハッシュリファレンスにしたもの |
|
186
|
|
|
|
|
|
|
#------------------------------------------ |
|
187
|
|
|
|
|
|
|
sub get_date { |
|
188
|
1
|
|
33
|
1
|
0
|
963
|
my $time = shift || time; |
|
189
|
1
|
|
|
|
|
33
|
my($sec, $min, $hour, $day, $month, $year) = (localtime($time))[0..5]; |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
return { |
|
192
|
1
|
|
|
|
|
29
|
year => $year + 1900, |
|
193
|
|
|
|
|
|
|
month => $month + 1, |
|
194
|
|
|
|
|
|
|
day => $day, |
|
195
|
|
|
|
|
|
|
hour => $hour, |
|
196
|
|
|
|
|
|
|
min => $min, |
|
197
|
|
|
|
|
|
|
sec => $sec, |
|
198
|
|
|
|
|
|
|
}; |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
#========================================== |
|
202
|
|
|
|
|
|
|
# http経由で取得した時間からエポックタイムを抽出 |
|
203
|
|
|
|
|
|
|
# req: |
|
204
|
|
|
|
|
|
|
# date_str: httpリクエストのヘッダにあるlast-modified等 |
|
205
|
|
|
|
|
|
|
# ex)"Fri, 13 Jan 2012 23:49:21 GMT" |
|
206
|
|
|
|
|
|
|
# res: |
|
207
|
|
|
|
|
|
|
# unix_time: エポックタイム |
|
208
|
|
|
|
|
|
|
#========================================== |
|
209
|
|
|
|
|
|
|
sub get_epoch_from_formated_http { |
|
210
|
1
|
|
|
1
|
0
|
576
|
my(@MoY, %MoY); |
|
211
|
1
|
|
|
|
|
6
|
@MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); |
|
212
|
1
|
|
|
|
|
30
|
@MoY{@MoY} = (1..12); |
|
213
|
|
|
|
|
|
|
|
|
214
|
1
|
50
|
|
|
|
18
|
if ($_[0] =~ /^ |
|
215
|
|
|
|
|
|
|
[SMTWF][a-z][a-z], |
|
216
|
|
|
|
|
|
|
\ (\d\d) |
|
217
|
|
|
|
|
|
|
\ ([JFMAJSOND][a-z][a-z]) |
|
218
|
|
|
|
|
|
|
\ (\d\d\d\d) |
|
219
|
|
|
|
|
|
|
\ (\d\d):(\d\d):(\d\d) |
|
220
|
|
|
|
|
|
|
\ GMT$/x) { |
|
221
|
1
|
|
|
|
|
30
|
return timegm($6, $5, $4, $1, $MoY{$2} - 1, $3 - 1900); |
|
222
|
|
|
|
|
|
|
} |
|
223
|
|
|
|
|
|
|
} |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
#========================================== |
|
226
|
|
|
|
|
|
|
# パスを配列で表現したものから文字列のパスを作る |
|
227
|
|
|
|
|
|
|
# req: |
|
228
|
|
|
|
|
|
|
# path_list: ('/Users', 'user', 'Desktop', 'Hoge.txt') |
|
229
|
|
|
|
|
|
|
# res: |
|
230
|
|
|
|
|
|
|
# path_str: '/Users/user/Desktop/Hoge.txt' |
|
231
|
|
|
|
|
|
|
#========================================== |
|
232
|
|
|
|
|
|
|
sub cat { |
|
233
|
|
|
|
|
|
|
#return join('/', @_); |
|
234
|
1
|
|
|
1
|
0
|
770
|
return File::Spec->catfile(@_); |
|
235
|
|
|
|
|
|
|
} |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
#========================================== |
|
238
|
|
|
|
|
|
|
# 隠しファイルか判定 |
|
239
|
|
|
|
|
|
|
# is_skip_file |
|
240
|
|
|
|
|
|
|
# req: |
|
241
|
|
|
|
|
|
|
# file_path: ファイルまでのパス |
|
242
|
|
|
|
|
|
|
# ex) /Users/user/Desktop/.svn |
|
243
|
|
|
|
|
|
|
# res: |
|
244
|
|
|
|
|
|
|
# result: 隠しファイルの場合は1、それ以外は0 |
|
245
|
|
|
|
|
|
|
#========================================== |
|
246
|
|
|
|
|
|
|
sub is_hidden_file { |
|
247
|
2
|
|
|
2
|
0
|
634
|
my $file_path = shift; |
|
248
|
|
|
|
|
|
|
|
|
249
|
2
|
50
|
33
|
|
|
68
|
return 1 if( |
|
|
|
|
66
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
250
|
|
|
|
|
|
|
$file_path eq '.' || |
|
251
|
|
|
|
|
|
|
$file_path eq '..' || |
|
252
|
|
|
|
|
|
|
$file_path =~ /.svn$/ || |
|
253
|
|
|
|
|
|
|
$file_path =~ /desktop\.ini$/ || |
|
254
|
|
|
|
|
|
|
$file_path =~ /.DS_Store$/ || |
|
255
|
|
|
|
|
|
|
$file_path =~ /Thumbs\.db$/ |
|
256
|
|
|
|
|
|
|
); |
|
257
|
|
|
|
|
|
|
|
|
258
|
1
|
|
|
|
|
9
|
return 0; |
|
259
|
|
|
|
|
|
|
} |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
#------------------------------------------ |
|
262
|
|
|
|
|
|
|
# コピー不要なファイルか |
|
263
|
|
|
|
|
|
|
# copy_filter |
|
264
|
|
|
|
|
|
|
# req: |
|
265
|
|
|
|
|
|
|
# file_name: ファイル名 |
|
266
|
|
|
|
|
|
|
# ex).svn, desktop.ini |
|
267
|
|
|
|
|
|
|
# res: |
|
268
|
|
|
|
|
|
|
# result: コピーすべきファイルの場合は0、それ以外は1 |
|
269
|
|
|
|
|
|
|
#------------------------------------------ |
|
270
|
|
|
|
|
|
|
sub is_unnecessary_copying_file { |
|
271
|
2
|
|
|
2
|
0
|
606
|
my $file_name = shift; |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
# .や..等,階層を指定している場合は無視 |
|
274
|
2
|
100
|
|
|
|
17
|
if ($file_name =~ /^\./) { |
|
275
|
1
|
|
|
|
|
5
|
return 1; |
|
276
|
|
|
|
|
|
|
} |
|
277
|
|
|
|
|
|
|
# サムネイル用ファイルなどは無視 |
|
278
|
1
|
50
|
33
|
|
|
11
|
if ($file_name eq 'Thumbs.db' || !($file_name =~ /[^\.]/)) { |
|
279
|
0
|
|
|
|
|
0
|
return 1; |
|
280
|
|
|
|
|
|
|
} |
|
281
|
|
|
|
|
|
|
# .svnはコピーしたくない |
|
282
|
1
|
50
|
33
|
|
|
24
|
if ($file_name eq '.svn' || $file_name eq 'desktop.ini' || $file_name eq '.DS_Store') { |
|
|
|
|
33
|
|
|
|
|
|
283
|
0
|
|
|
|
|
0
|
return 1; |
|
284
|
|
|
|
|
|
|
} |
|
285
|
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
# ログファイルのディレクトリは無視 |
|
287
|
1
|
50
|
|
|
|
5
|
if ($file_name eq 'log') { |
|
288
|
0
|
|
|
|
|
0
|
return 1; |
|
289
|
|
|
|
|
|
|
} |
|
290
|
|
|
|
|
|
|
|
|
291
|
1
|
|
|
|
|
5
|
return 0; |
|
292
|
|
|
|
|
|
|
} |
|
293
|
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
1; |
|
295
|
|
|
|
|
|
|
__END__ |