-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_patches.py
72 lines (61 loc) · 2.67 KB
/
update_patches.py
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
import os
import commands
import fnmatch
import re
import subprocess, shlex, shutil
def cmdsplit(args):
if os.sep == '\\':
args = args.replace('\\', '\\\\')
return shlex.split(args)
def cleanDirs(path):
if not os.path.isdir(path):
return
files = os.listdir(path)
if len(files):
for f in files:
fullpath = os.path.join(path, f)
if os.path.isdir(fullpath):
cleanDirs(fullpath)
files = os.listdir(path)
if len(files) == 0:
os.rmdir(path)
def main():
print 'Creating patches'
base = os.path.normpath(os.path.join('..', 'src_base'))
work = os.path.normpath(os.path.join('..', 'src_work'))
timestamp = re.compile(r'[0-9-]* [0-9:\.]* [+-][0-9]*\r?\n')
for path, _, filelist in os.walk(work, followlinks=True):
for cur_file in fnmatch.filter(filelist, '*.java'):
file_base = os.path.normpath(os.path.join(base, path[12:], cur_file)).replace(os.path.sep, '/')
file_work = os.path.normpath(os.path.join(work, path[12:], cur_file)).replace(os.path.sep, '/')
patch = ''
cmd = 'diff -u %s %s -r --strip-trailing-cr --new-file' % (file_base, file_work)
process = subprocess.Popen(cmdsplit(cmd), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=-1)
patch, _ = process.communicate()
patch_dir = os.path.join('patches', path[12:])
patch_file = os.path.join(patch_dir, cur_file + '.patch')
if len(patch) > 0:
print patch_file
patch = timestamp.sub("0000-00-00 00:00:00.000000000 -0000\n", patch)
patch = patch.replace('\r\n', '\n')
if not os.path.exists(patch_dir):
os.makedirs(patch_dir)
with open(patch_file, 'wb') as fh:
fh.write(patch)
else:
if os.path.isfile(patch_file):
print 'Deleting empty patch: ' + patch_file
os.remove(patch_file)
for path, _, filelist in os.walk('patches', followlinks=True):
for cur_file in fnmatch.filter(filelist, '*.patch'):
src_file = os.path.normpath(os.path.join(work, path[8:], cur_file[:-6]))
if not os.path.isfile(src_file):
print 'Deleting empty patch: %s' % os.path.join(path, cur_file)
os.remove(os.path.join(path, cur_file))
cleanDirs('patches')
print 'Grabing copy of Conf'
if os.path.exists('conf'):
shutil.rmtree('conf')
shutil.copytree('../conf', './conf/')
if __name__ == '__main__':
main()