-
Notifications
You must be signed in to change notification settings - Fork 0
/
deftsilo
executable file
·256 lines (233 loc) · 7.87 KB
/
deftsilo
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python3
# Copyright (c) 2012-2013,2023 Robert Escriva
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of deftsilo nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import collections
import hashlib
import os
import os.path
import shlex
import subprocess
import sys
import tarfile
import tempfile
import argparse
def get_sha256(root, relativepath):
return hashlib.sha256(open(os.path.join(root, relativepath), 'rb').read()).hexdigest()
def get_history(root, relativepath):
cmdline = ('git', 'whatchanged', '--follow', '--no-abbrev', '--oneline', relativepath)
pipe = subprocess.Popen(cmdline, shell=False, stdout=subprocess.PIPE, cwd=root)
stdout, stderr = pipe.communicate()
ret = []
for line in stdout.decode('utf8').split('\n'):
if not line.startswith(':'):
continue
ref = line.split(' ')[3]
if ref == '0' * 40:
continue
cmdline = ('git', 'cat-file', 'blob', ref)
pipe = subprocess.Popen(cmdline, shell=False, stdout=subprocess.PIPE, cwd=root)
stdout, stderr = pipe.communicate()
ret.append(hashlib.sha256(stdout).hexdigest())
ret.append(get_sha256(root, relativepath))
return sorted(set(ret))
def get_mode(root, path):
mode = os.stat(os.path.join(root, path)).st_mode
mode = '{:o}'.format(mode)[-4:]
assert mode.startswith('0')
return mode
def generate_mkdir(root, directory):
mode = get_mode(root, directory)
return 'deftsilo_mkdir {d} {m}\n'.format(d=shlex.quote(os.path.normpath(directory)), m=mode)
def generate_cp(root, file, refs):
mode = get_mode(root, file)
return 'deftsilo_install {f} {m} {r}\n'.format(f=shlex.quote(os.path.normpath(file)), m=mode, r=' '.join(refs))
def generate_script(root, directories, files):
script = '''#!/bin/sh
set -e
DEFTSILO_ROOT=`dirname $0`
if test "x${DEFTSILO_ROOT}" = x;
then
DEFTSILO_ROOT=.
fi
DEFTSILO_ROOT=`realpath -q ${DEFTSILO_ROOT}`
DEFTSILO_INSTALL=deftsilo_cp
while getopts "l" arg
do
case "$1" in
-l)
echo "linking, not copying"
DEFTSILO_INSTALL=deftsilo_ln
shift
;;
--)
shift
break
;;
esac
done
DEFTSILO_TARGET="$1"
shift
deftsilo_sha256() {
sha256sum "$1" | awk '{print $1}'
}
deftsilo_mkdir() {
d="$1"
shift
m="$1"
shift
dest="${DEFTSILO_TARGET}/$d"
if test -f "$dest"; then
echo cannot mkdir "$dest": would clobber a file
exit 1
elif test '!' -e "$dest"; then
mkdir "$dest"
chmod $m "$dest"
fi
}
deftsilo_cp() {
f="$1"
shift
m="$1"
shift
dest="${DEFTSILO_TARGET}/$f"
if test -d "$dest"; then
echo cannot copy "$dest": would clobber a directory
exit 1
elif test '!' -f "$dest"; then
cp "${DEFTSILO_ROOT}/$f" "$dest"
chmod "$m" "$dest"
else
exp=`deftsilo_sha256 "$dest"`
found=no
for hash in $@
do
if test x"$exp" = x"$hash"; then
cp "${DEFTSILO_ROOT}/$f" "$dest"
chmod "$m" "$dest"
found=yes
fi
done
if test x"$found" = xno; then
echo failed to copy "$f": unsaved changes
exit 1
fi
fi
}
deftsilo_ln() {
f="$1"
shift
m="$1"
shift
dest="${DEFTSILO_TARGET}/$f"
if test -d "$dest"; then
echo cannot link "$dest": would clobber a directory
exit 1
elif test -L "$dest"; then
true
elif test -f "$dest"; then
exp=`deftsilo_sha256 "$dest"`
found=no
for hash in $@
do
if test x"$exp" = x"$hash"; then
unlink "$dest"
ln -s "${DEFTSILO_ROOT}/$f" "$dest"
found=yes
fi
done
if test x"$found" = xno; then
echo failed to link "$f": unsaved changes
exit 1
fi
elif test '!' -L "$dest"; then
ln -s "${DEFTSILO_ROOT}/$f" "$dest"
fi
}
deftsilo_install() {
"$DEFTSILO_INSTALL" $@
}
'''
for d in directories:
script += generate_mkdir(root, d)
for (f, refs) in files:
script += generate_cp(root, f, refs)
return script
def tarball_prefix(output):
output = os.path.basename(output)
SUFFIXES = ['.tar.gz', '.tar.bz2', '.tar.xz']
for suffix in SUFFIXES:
if output.endswith(suffix):
return output[:0-len(suffix)]
return output
def generate_tarball(output, root, directories, files, script):
mode = 'x:' + os.path.splitext(output)[1][1:]
tar_prefix = tarball_prefix(output)
tarf = tarfile.open(output, mode=mode)
for f, refs in sorted(files):
tarf.add(os.path.join(root, f), os.path.normpath(os.path.join(tar_prefix, f)))
tmp = tempfile.NamedTemporaryFile(prefix='.deftsilo')
tmp.write(script.encode('utf8'))
tmp.flush()
tarf.add(tmp.name, os.path.normpath(os.path.join(tar_prefix, 'install.sh')))
tarf.close()
def main(args):
parser = argparse.ArgumentParser(description='deftsilo manages dotfiles')
parser.add_argument('--output', default='dotfiles.tar.gz',
help='tarball output name')
parser.add_argument('--directory', default='.')
args = parser.parse_args(args)
directories = []
files = []
for dirpath, dirnames, filenames in os.walk(args.directory):
dirpath = os.path.relpath(dirpath, args.directory)
if '.git' in dirnames:
dirnames.remove('.git')
if 'install.sh' in filenames:
filenames.remove('install.sh')
for dirname in sorted(dirnames):
dirname = os.path.join(dirpath, dirname)
print('dir', args.directory, dirname)
directories.append(dirname)
for filename in filenames:
filename = os.path.join(dirpath, filename)
refs = get_history(args.directory, filename)
if not refs:
print('file', args.directory, filename, 'new')
else:
for ref in refs:
print('file', args.directory, filename, ref)
files.append((filename, refs))
script = generate_script(args.directory, directories, files)
if os.path.basename(args.output) == 'install.sh':
with open(args.output, 'w') as fout:
fout.write(script)
fout.flush()
else:
if os.path.exists(args.output):
os.remove(args.output)
generate_tarball(args.output, args.directory, directories, files, script)
if __name__ == '__main__':
main(sys.argv[1:])