/
zprofile.util
196 lines (156 loc) · 4.93 KB
/
zprofile.util
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
export LOOP_EXEC_MAX_COUNT=50
export CAREFUL_SYNC_OPTS='--exclude=".*"'
# {{{ careful-sync
function loop-exec() {
function init-loop-exec() {
if [ $2 -gt $LOOP_EXEC_MAX_COUNT ]; then
echo "loop-exec reached MAX_COUNT(${LOOP_EXEC_MAX_COUNT}). exit."
return 1
fi
eval "$1"
if [ $? -ne 0 ]; then
echo "[$2]: Exit code != 0. execute command again."
init-loop-exec "$1" $(expr $2 + 1)
fi
}
if [ $# -ne 1 ]; then
echo 'Please specify only a command.'
echo 'If command has arguments or options, Please quote it.'
echo "Example: $ loop-exec 'exec_command --option'"
return 1
fi
init-loop-exec "$1" 1
unfunction init-loop-exec
}
function careful-sync {
if [ $# -lt 2 -o $# -gt 3 ]; then
echo 'Usage : careful-sync <rsync-additional-opts> <source-dir> <destination-dir>'
return 1
fi
if [ $# -eq 2 ]; then
local rsync_opts=''
local src=$1
local dest=$2
elif [ $# -eq 3 ]; then
local rsync_opts=$1
local src=$2
local dest=$3
fi
loop-exec "rsync -avx ${CAREFUL_SYNC_OPTS} ${rsync_opts} '${src}' '${dest}'"
if [ $? -ne 0 ]; then
echo 'Failed synchronization without checksum validation. Validation skipped.'
return 1
fi
loop-exec "rsync -acvx ${CAREFUL_SYNC_OPTS} ${rsync_opts} '${src}' '${dest}'"
}
# }}}
# {{{ compress-directory(using rar)
function compress-directory() {
rar a -r -rr5 $1.rar $1
}
function archive-directory() {
find . -name '*.db' -print0 | xargs -0 rm
find . -name '.*' -print0 | xargs -0 rm
find . -name '_*' -print0 | xargs -0 rm
find . -type d -print0 | xargs -0 rmdir
mkdir /tmp/original_files
local dir
for dir in *; do
compress-directory "$dir" && mv "$dir" /tmp/original_files
done
}
function rearchive-directory() {
mkdir /tmp/original_archives
local file
for file in *; do
unar -d "$file" && mv "$file" /tmp/original_archives
local dirname="${file%.*}"
mv "${dirname}"/**/*.* "${dirname}"
done
archive-directory
}
# }}}
# {{{ git-privatize
function git-privatize() {
if [ ! -d .git ]; then
echo '.git not found.'
echo 'Please execute in repository-root directory.'
return 1
fi
echo-and-eval 'rm -rf /tmp/git-stash-hooks'
echo-and-eval 'mkdir -p /tmp/git-stash-hooks'
echo-and-eval 'mv .git/hooks/* /tmp/git-stash-hooks'
echo-and-eval 'echo "#!/bin/sh" > .git/hooks/pre-push'
echo-and-eval 'echo "exit 1" >> .git/hooks/pre-push'
echo-and-eval 'chmod 755 .git/hooks/pre-push'
}
# }}}
# {{{ note-calc-times
function note-calc-times() {
local basepath=/tmp/note-calc-times
mkdir -p $basepath
local filespath="$basepath/files"
ls -1 $@ |& egrep -v '[^0-9]:' > $filespath
local timeregexp='^[0-9/]{10} [0-9:]{8}$'
local timespath="$basepath/times"
cat $filespath | xargs cat |& egrep -v '^cat:' | egrep $timeregexp | sort > $timespath
local size=$((`cat $timespath | wc -l`))
function line() { printf "%55s\n" | tr ' ' '-' }; line
echo "detected time $size counts. listing..."; line
cat $filespath | xargs egrep -Hn $timeregexp; line
if [ $size -eq 0 ]; then
echo 'formatted time not detected. abort.'
return 1
elif [ $(($size % 2)) -eq 1 ]; then
echo "detected time $size counts, not-even. abort."
return 1
fi
echo 'formatted time calculating...'; line
function to_unixtime() {
if [ `uname` = 'Darwin' ]; then
date -j -u -f '%Y/%m/%d %H:%M:%S' "$1" '+%s'
elif which busybox >& /dev/null; then
date -D '%Y/%m/%d %H:%M:%S' -d "$1" '+%s'
else
date -d "$1" '+%s' # Maybe GNU Linux
fi
}
function print_time() {
local hour=$(($2 / 3600))
local minute=$((($2 % 3600) / 60))
local second=$(($2 % 60))
printf "$1%02d:%02d:%02d\n" $hour $minute $second
}
function calc_time_diff() {
local before=`to_unixtime $1`
local after=`to_unixtime $2`
local diff=$(($after - $before))
print_time "$1 -> $2 = " $diff
total=$(($total + $diff))
}
declare -a lines; lines=( "${(@f)"$(<$timespath)"}" )
local total=0
local i=1
while true ; do
local i1=$(($i+1))
if [ $i1 -gt $size ]; then break; fi
calc_time_diff ${lines[$i]} ${lines[$i1]}
i=$(($i+2))
done
line; print_time 'Total time: ' $total
unfunction line to_unixtime print_time calc_time_diff
rm -rf $basepath
}
# }}}
# {{{ pip3-install
function pip3-install() {
pip3 install --no-color --progress-bar off --upgrade $1
pip3 check $1
}
# }}}
# {{{ skk-cleanup-regexp
function skk-cleanup-regexp() {
echo "^[0-9a-z\u3042-\u3093\u30fc]*\\ \\/[0-9a-z\u3042-\u3093\u30fc\u3001]*\\/$"
}
# }}}
# vim: set filetype=zsh: