diff.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 from ros_introspection.package import Package
00004 from .terminal import color_header, color_diff
00005 import collections
00006 import tempfile
00007 import shutil
00008 import os
00009 import difflib
00010 from filecmp import dircmp
00011 
00012 
00013 def get_diff_helper(dcmp, folder=''):
00014     D = collections.defaultdict(list)
00015     for name in dcmp.diff_files:
00016         D['diff'].append(os.path.join(folder, name))
00017     for name in dcmp.left_only:
00018         D['deleted'].append(os.path.join(folder, name))
00019     for name in dcmp.right_only:
00020         D['added'].append(os.path.join(folder, name))
00021 
00022     for sub_dcmp in dcmp.subdirs.values():
00023         sub_f = sub_dcmp.left.replace(dcmp.left, '')
00024         if sub_f[0] == '/':
00025             sub_f = sub_f[1:]
00026         D2 = get_diff_helper(sub_dcmp, os.path.join(folder, sub_f))
00027         for key in D2:
00028             D[key] += D2[key]
00029     return dict(D)
00030 
00031 
00032 def get_diff(original_folder, new_folder):
00033     dcmp = dircmp(original_folder, new_folder)
00034     return get_diff_helper(dcmp)
00035 
00036 
00037 def get_lines(folder, filename):
00038     return open(os.path.join(folder, filename)).readlines()
00039 
00040 
00041 def print_diff(filename, left_folder=None, right_folder=None):
00042     if left_folder is None:
00043         left = []
00044     else:
00045         left = get_lines(left_folder, filename)
00046 
00047     if right_folder is None:
00048         right = []
00049     else:
00050         right = get_lines(right_folder, filename)
00051 
00052     diff = difflib.unified_diff(left, right, fromfile=filename, tofile='%s (modified)' % filename)
00053     print(''.join(color_diff(diff)))
00054 
00055 
00056 def preview_changes(package, fn_name, fne):
00057     try:
00058         temp_dir = tempfile.mkdtemp()
00059         new_package_root = os.path.join(temp_dir, package.name)
00060         shutil.copytree(package.root, new_package_root)
00061         new_pkg = Package(new_package_root)
00062         fne(new_pkg)
00063         new_pkg.write()
00064         the_diff = get_diff(package.root, new_package_root)
00065         if len(the_diff) == 0:
00066             return False
00067 
00068         print(color_header(fn_name))
00069 
00070         for filename in the_diff.get('diff', []):
00071             print_diff(filename, package.root, new_package_root)
00072         for filename in the_diff.get('deleted', []):
00073             print_diff(filename, left_folder=package.root)
00074         for filename in the_diff.get('added', []):
00075             print_diff(filename, right_folder=new_package_root)
00076     finally:
00077         shutil.rmtree(new_package_root)
00078         shutil.copytree(package.root, new_package_root)
00079         shutil.rmtree(temp_dir)
00080     return True


roscompile
Author(s):
autogenerated on Wed Jun 19 2019 19:21:36