Add diff mode to freebsd-to-rtems.py

This commit is contained in:
Joel Sherrill
2012-03-12 07:58:24 -05:00
parent 560eccfb92
commit 7326d96457
2 changed files with 48 additions and 29 deletions

View File

@@ -39,12 +39,14 @@ import re
import sys import sys
import getopt import getopt
import filecmp import filecmp
import difflib
RTEMS_DIR = "not_set" RTEMS_DIR = "not_set"
FreeBSD_DIR = "not_set" FreeBSD_DIR = "not_set"
isVerbose = False isVerbose = False
isForward = True isForward = True
isDryRun = False isDryRun = False
isDiffMode = False
isEarlyExit = False isEarlyExit = False
isOnlyMakefile = False isOnlyMakefile = False
tempFile = "/tmp/tmp_FBRT" tempFile = "/tmp/tmp_FBRT"
@@ -54,6 +56,7 @@ def usage():
print "freebsd-to-rtems.py [args]" print "freebsd-to-rtems.py [args]"
print " -?|-h|--help print this and exit" print " -?|-h|--help print this and exit"
print " -d|--dry-run run program but no modifications" print " -d|--dry-run run program but no modifications"
print " -D|--diff provide diff of files between trees"
print " -e|--early-exit evaluate arguments, print results, and exit" print " -e|--early-exit evaluate arguments, print results, and exit"
print " -m|--makefile just generate Makefile" print " -m|--makefile just generate Makefile"
print " -R|--reverse default FreeBSD -> RTEMS, reverse that" print " -R|--reverse default FreeBSD -> RTEMS, reverse that"
@@ -64,12 +67,13 @@ def usage():
# Parse the arguments # Parse the arguments
def parseArguments(): def parseArguments():
global RTEMS_DIR, FreeBSD_DIR global RTEMS_DIR, FreeBSD_DIR
global isVerbose, isForward, isEarlyExit, isOnlyMakefile global isVerbose, isForward, isEarlyExit, isOnlyMakefile, isDiffMode
try: try:
opts, args = getopt.getopt(sys.argv[1:], "?hdemRr:f:v", opts, args = getopt.getopt(sys.argv[1:], "?hdDemRr:f:v",
["help", ["help",
"help", "help",
"dry-run" "dry-run"
"diff"
"early-exit" "early-exit"
"makefile" "makefile"
"reverse" "reverse"
@@ -91,6 +95,8 @@ def parseArguments():
sys.exit() sys.exit()
elif o in ("-d", "--dry-run"): elif o in ("-d", "--dry-run"):
isForward = False isForward = False
elif o in ("-D", "--diff"):
isDiffMode = True
elif o in ("-e", "--early-exit"): elif o in ("-e", "--early-exit"):
isEarlyExit = True isEarlyExit = True
elif o in ("-m", "--makefile"): elif o in ("-m", "--makefile"):
@@ -109,6 +115,7 @@ def parseArguments():
parseArguments() parseArguments()
print "Verbose: " + ("no", "yes")[isVerbose] print "Verbose: " + ("no", "yes")[isVerbose]
print "Dry Run: " + ("no", "yes")[isDryRun] print "Dry Run: " + ("no", "yes")[isDryRun]
print "Diff Mode Enabled: " + ("no", "yes")[isDiffMode]
print "Only Generate Makefile: " + ("no", "yes")[isOnlyMakefile] print "Only Generate Makefile: " + ("no", "yes")[isOnlyMakefile]
print "RTEMS Libbsd Directory: " + RTEMS_DIR print "RTEMS Libbsd Directory: " + RTEMS_DIR
print "FreeBSD SVN Directory: " + FreeBSD_DIR print "FreeBSD SVN Directory: " + FreeBSD_DIR
@@ -130,9 +137,9 @@ wasDirectorySet( "FreeBSD", FreeBSD_DIR )
# Are we generating or reverting? # Are we generating or reverting?
if isForward == True: if isForward == True:
print "Generating into", RTEMS_DIR print "Forward from FreeBSD SVN into ", RTEMS_DIR
else: else:
print "Reverting from", RTEMS_DIR print "Reverting from ", RTEMS_DIR
if isOnlyMakefile == True: if isOnlyMakefile == True:
print "Only Makefile Mode and Reverse are contradictory" print "Only Makefile Mode and Reverse are contradictory"
sys.exit(2) sys.exit(2)
@@ -155,19 +162,25 @@ def mapContribPath(path):
def mapCPUDependentPath(path): def mapCPUDependentPath(path):
return path.replace("include/", "include/freebsd/machine/") return path.replace("include/", "include/freebsd/machine/")
# compare and overwrite destination file only if different # compare and process file only if different
def copyIfDifferent(new, old, desc, src): # + copy or diff depending on execution mode
global filesChanged def processIfDifferent(new, old, desc, src):
# print new + " " + old + " X" + desc + "X " + src global filesChanged
if not os.path.exists(old) or \ # print new + " " + old + " X" + desc + "X " + src
filecmp.cmp(new, old, shallow=False) == False: if not os.path.exists(old) or \
filesChanged += 1 filecmp.cmp(new, old, shallow=False) == False:
print "Install " + desc + src + " => " + dst filesChanged += 1
if isDryRun == False: if isDiffMode == False:
shutil.move(new, old) # print "Move " + new + " to " + old
# print "Move " + new + " to " + old if isDryRun == False:
return True shutil.move(new, old)
return False else:
#print "Diff " + src
old_contents = open(old).readlines()
new_contents = open(new).readlines()
for line in difflib.unified_diff( \
old_contents, new_contents, fromfile=src, tofile=new, n=5):
sys.stdout.write(line)
# generate an empty file as a place holder # generate an empty file as a place holder
def installEmptyFile(src): def installEmptyFile(src):
@@ -181,7 +194,7 @@ def installEmptyFile(src):
out = open(tempFile, 'w') out = open(tempFile, 'w')
out.write('/* EMPTY */\n') out.write('/* EMPTY */\n')
out.close() out.close()
copyIfDifferent(tempFile, dst, "empty file ", "" ) processIfDifferent(tempFile, dst, "empty file ", "" )
# fix include paths inside a C or .h file # fix include paths inside a C or .h file
def fixIncludes(data): def fixIncludes(data):
@@ -218,7 +231,7 @@ def installHeaderFile(org, target):
data = fixIncludes(data) data = fixIncludes(data)
out.write(data) out.write(data)
out.close() out.close()
copyIfDifferent(tempFile, dst, "Header ", src) processIfDifferent(tempFile, dst, "Header ", src)
# Copy a source file from FreeBSD to the RTEMS BSD tree # Copy a source file from FreeBSD to the RTEMS BSD tree
@@ -239,7 +252,7 @@ def installSourceFile(org):
out.write('#include <' + PREFIX + '/machine/rtems-bsd-config.h>\n\n') out.write('#include <' + PREFIX + '/machine/rtems-bsd-config.h>\n\n')
out.write(data) out.write(data)
out.close() out.close()
copyIfDifferent(tempFile, dst, "Source ", src) processIfDifferent(tempFile, dst, "Source ", src)
# Revert a header file from the RTEMS BSD tree to the FreeBSD tree # Revert a header file from the RTEMS BSD tree to the FreeBSD tree
def revertHeaderFile(org, target): def revertHeaderFile(org, target):
@@ -260,7 +273,7 @@ def revertHeaderFile(org, target):
data = revertFixIncludes(data) data = revertFixIncludes(data)
out.write(data) out.write(data)
out.close() out.close()
copyIfDifferent(tempFile, dst, "Header ", src) processIfDifferent(tempFile, dst, "Header ", src)
# Revert a source file from the RTEMS BSD tree to the FreeBSD tree # Revert a source file from the RTEMS BSD tree to the FreeBSD tree
def revertSourceFile(org, target): def revertSourceFile(org, target):
@@ -281,7 +294,7 @@ def revertSourceFile(org, target):
data = revertFixIncludes(data) data = revertFixIncludes(data)
out.write(data) out.write(data)
out.close() out.close()
copyIfDifferent(tempFile, dst, "Source ", src) processIfDifferent(tempFile, dst, "Source ", src)
# Remove the output directory # Remove the output directory
def deleteOutputDirectory(): def deleteOutputDirectory():
@@ -407,7 +420,7 @@ class ModuleManager:
out.write(data) out.write(data)
out.close() out.close()
makefile = RTEMS_DIR + '/Makefile' makefile = RTEMS_DIR + '/Makefile'
copyIfDifferent(tempFile, makefile, "Makefile ", "") processIfDifferent(tempFile, makefile, "Makefile ", "")
# Module - logical group of related files we can perform actions on # Module - logical group of related files we can perform actions on
class Module: class Module:
@@ -1857,13 +1870,15 @@ mm.addModule(sparc64Dependent)
# Perform the actual file manipulation # Perform the actual file manipulation
if isForward == True: if isForward == True:
if isOnlyMakefile == False: if isOnlyMakefile == False:
mm.copyFiles() mm.copyFiles()
mm.createMakefile() mm.createMakefile()
else: else:
mm.revertFiles() mm.revertFiles()
if filesChanged == 1: # Print a summary if changing files
if isDiffMode == False:
if filesChanged == 1:
print str(filesChanged) + " file was changed." print str(filesChanged) + " file was changed."
else: else:
print str(filesChanged) + " files were changed." print str(filesChanged) + " files were changed."

View File

@@ -207,6 +207,7 @@ on the FreeBSD code. Its command line arguments are shown below:
freebsd-to-rtems.py [args] freebsd-to-rtems.py [args]
-?|-h|--help print this and exit -?|-h|--help print this and exit
-d|--dry-run run program but no modifications -d|--dry-run run program but no modifications
-D|--diff provide diff of files between trees
-e|--early-exit evaluate arguments, print results, and exit -e|--early-exit evaluate arguments, print results, and exit
-m|--makefile just generate Makefile -m|--makefile just generate Makefile
-R|--reverse default FreeBSD -> RTEMS, reverse that -R|--reverse default FreeBSD -> RTEMS, reverse that
@@ -248,6 +249,9 @@ Generating into /home/joel/newbsd/git/libbsd-8.2
0 files were changed. 0 files were changed.
---- ----
The script may also be used to generate a diff in either forward or reverse
direction.
== Initialization of RTEMS Libbsd == Initialization of RTEMS Libbsd
The initialization of the RTEMS Libbsd is based on the FreeBSD SYSINIT The initialization of the RTEMS Libbsd is based on the FreeBSD SYSINIT