2012-04-02 08:09:00 +00:00
|
|
|
import os
|
2012-08-05 05:50:54 +00:00
|
|
|
import sys
|
2012-04-02 08:09:00 +00:00
|
|
|
import fnmatch
|
2012-08-05 05:50:54 +00:00
|
|
|
import shlex
|
|
|
|
import difflib
|
|
|
|
import time
|
2012-12-04 02:15:38 +00:00
|
|
|
from optparse import OptionParser
|
2012-04-02 08:09:00 +00:00
|
|
|
|
|
|
|
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():
|
2012-08-05 05:50:54 +00:00
|
|
|
print("Creating patches")
|
2012-12-04 02:15:38 +00:00
|
|
|
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option('-m', '--mcp-dir', action='store', dest='mcp_dir', help='Path to MCP', default=None)
|
2013-09-06 20:32:26 +00:00
|
|
|
parser.add_option('-p', '--patch-dir', action='store', dest='patch_dir', help='Folder to place patches in', default=None)
|
2012-12-04 02:15:38 +00:00
|
|
|
options, _ = parser.parse_args()
|
|
|
|
|
|
|
|
forge_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
mcp = os.path.join(forge_dir, 'mcp')
|
|
|
|
if not options.mcp_dir is None:
|
|
|
|
mcp = os.path.abspath(options.mcp_dir)
|
|
|
|
elif os.path.isfile(os.path.join('..', 'runtime', 'commands.py')):
|
|
|
|
mcp = os.path.abspath('..')
|
|
|
|
|
2012-08-05 05:50:54 +00:00
|
|
|
patchd = os.path.normpath(os.path.join(forge_dir, 'patches'))
|
2013-09-06 20:32:26 +00:00
|
|
|
if not options.patch_dir is None:
|
|
|
|
patchd = os.path.normpath(options.patch_dir)
|
|
|
|
print "Patch folder set to '%s'" % patchd
|
2012-08-05 05:50:54 +00:00
|
|
|
base = os.path.normpath(os.path.join(mcp, 'src_base'))
|
|
|
|
work = os.path.normpath(os.path.join(mcp, 'src_work'))
|
2012-04-02 08:09:00 +00:00
|
|
|
|
|
|
|
for path, _, filelist in os.walk(work, followlinks=True):
|
|
|
|
for cur_file in fnmatch.filter(filelist, '*.java'):
|
2012-08-05 05:50:54 +00:00
|
|
|
file_base = os.path.normpath(os.path.join(base, path[len(work)+1:], cur_file)).replace(os.path.sep, '/')
|
|
|
|
file_work = os.path.normpath(os.path.join(work, path[len(work)+1:], cur_file)).replace(os.path.sep, '/')
|
2013-07-08 21:30:25 +00:00
|
|
|
if not os.path.isfile(file_base):
|
|
|
|
print("Missing base file %s"%(file_base))
|
|
|
|
continue
|
2012-08-05 05:50:54 +00:00
|
|
|
fromlines = open(file_base, 'U').readlines()
|
|
|
|
tolines = open(file_work, 'U').readlines()
|
|
|
|
|
|
|
|
patch = ''.join(difflib.unified_diff(fromlines, tolines, '../' + file_base[len(mcp)+1:], '../' + file_work[len(mcp)+1:], '', '', n=3))
|
|
|
|
patch_dir = os.path.join(patchd, path[len(work)+1:])
|
2012-04-02 08:09:00 +00:00
|
|
|
patch_file = os.path.join(patch_dir, cur_file + '.patch')
|
|
|
|
|
|
|
|
if len(patch) > 0:
|
2012-08-05 05:50:54 +00:00
|
|
|
print patch_file[len(patchd)+1:]
|
2012-04-02 08:09:00 +00:00
|
|
|
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):
|
2012-08-05 05:50:54 +00:00
|
|
|
print("Deleting empty patch: %s"%(patch_file))
|
2012-04-02 08:09:00 +00:00
|
|
|
os.remove(patch_file)
|
2012-08-05 05:50:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
cleanDirs(patchd)
|
2012-04-02 08:09:00 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-07-08 21:30:25 +00:00
|
|
|
main()
|