ForgePatch/fml/update_patches.py

65 lines
2.2 KiB
Python
Raw Normal View History

2012-03-30 17:33:29 +00:00
import sys
import os
import commands
import fnmatch
import re
import subprocess, shlex
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")
2012-03-31 00:38:31 +00:00
base = 'src-reference'
work = 'src-work'
2012-03-31 00:43:58 +00:00
patched_dir=os.path.normpath(os.path.join(sys.argv[1],work))
2012-03-30 17:33:29 +00:00
timestamp = re.compile(r'[0-9-]* [0-9:\.]* [+-][0-9]*\r?\n')
2012-03-31 00:38:31 +00:00
for path, _, filelist in os.walk(patched_dir, followlinks=True):
2012-03-30 17:33:29 +00:00
for cur_file in fnmatch.filter(filelist, '*.java'):
2012-03-31 00:38:31 +00:00
file_base = os.path.normpath(os.path.join(base, path[len(patched_dir)+1:], cur_file))
file_work = os.path.normpath(os.path.join(work, path[len(patched_dir)+1:], cur_file))
2012-03-30 17:33:29 +00:00
patch = ''
cmd = 'diff -u %s %s -r --strip-trailing-cr --new-file' % (file_base, file_work)
2012-03-31 00:38:31 +00:00
process = subprocess.Popen(cmdsplit(cmd), cwd=os.path.normpath(sys.argv[1]), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=-1)
2012-03-30 17:33:29 +00:00
patch, _ = process.communicate()
2012-03-31 00:43:58 +00:00
patch_dir = os.path.join(sys.argv[2],path[len(patched_dir)+1:])
2012-03-30 17:33:29 +00:00
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, 'w') as fh:
fh.write(patch)
else:
if os.path.isfile(patch_file):
print("Deleting empty patch: %s"%(patch_file))
os.remove(patch_file)
cleanDirs('patches')
if __name__ == '__main__':
main()