diff.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 import collections
4 import difflib
5 import inspect
6 import os
7 import shutil
8 import tempfile
9 from filecmp import dircmp
10 
11 from ros_introspection.package import Package
12 from ros_introspection.util import get_sibling_packages
13 
14 from .terminal import color_diff, color_header
15 
16 
17 def get_diff_helper(dcmp, folder=''):
18  D = collections.defaultdict(list)
19  for name in dcmp.diff_files:
20  D['diff'].append(os.path.join(folder, name))
21  for name in dcmp.left_only:
22  D['deleted'].append(os.path.join(folder, name))
23  for name in dcmp.right_only:
24  D['added'].append(os.path.join(folder, name))
25 
26  for sub_dcmp in dcmp.subdirs.values():
27  sub_f = sub_dcmp.left.replace(dcmp.left, '')
28  if sub_f[0] == '/':
29  sub_f = sub_f[1:]
30  D2 = get_diff_helper(sub_dcmp, os.path.join(folder, sub_f))
31  for key in D2:
32  D[key] += D2[key]
33  return dict(D)
34 
35 
36 def get_diff(original_folder, new_folder):
37  dcmp = dircmp(original_folder, new_folder)
38  return get_diff_helper(dcmp)
39 
40 
41 def get_lines(folder, filename):
42  return open(os.path.join(folder, filename)).readlines()
43 
44 
45 def print_diff(filename, left_folder=None, right_folder=None):
46  if left_folder is None:
47  left = []
48  else:
49  left = get_lines(left_folder, filename)
50 
51  if right_folder is None:
52  right = []
53  else:
54  right = get_lines(right_folder, filename)
55 
56  diff = difflib.unified_diff(left, right, fromfile=filename, tofile='%s (modified)' % filename)
57  print(''.join(color_diff(diff)))
58 
59 
60 def preview_changes(package, fn_name, fne, use_package_name=False):
61  try:
62  temp_dir = tempfile.mkdtemp()
63  new_package_root = os.path.join(temp_dir, package.name)
64  shutil.copytree(package.root, new_package_root)
65  new_pkg = Package(new_package_root)
66 
67  # Special case for metapackage rules, since the sibling packages
68  # require knowing the names of packages outside of the package's file root
69  # thus will not be copied with the above copytree operation
70  if 'sibling_packages' in inspect.getargspec(fne).args:
71  fne(new_pkg, sibling_packages=get_sibling_packages(package))
72  else:
73  fne(new_pkg)
74  new_pkg.write()
75  the_diff = get_diff(package.root, new_package_root)
76  if len(the_diff) == 0:
77  return False
78 
79  if use_package_name:
80  print(color_header(fn_name + ' (' + package.name + ')'))
81  else:
82  print(color_header(fn_name))
83 
84  for filename in the_diff.get('diff', []):
85  print_diff(filename, package.root, new_package_root)
86  for filename in the_diff.get('deleted', []):
87  print_diff(filename, left_folder=package.root)
88  for filename in the_diff.get('added', []):
89  print_diff(filename, right_folder=new_package_root)
90  finally:
91  shutil.rmtree(new_package_root)
92  shutil.copytree(package.root, new_package_root)
93  shutil.rmtree(temp_dir)
94  return True
95 
96 
97 def prepare_diff_lines(string_a, string_b):
98  a_lines = string_a.split('\n')
99  b_lines = string_b.split('\n')
100  while len(a_lines) < len(b_lines):
101  a_lines.append(None)
102  while len(b_lines) < len(a_lines):
103  b_lines.append(None)
104  return zip(a_lines, b_lines)
def get_diff_helper(dcmp, folder='')
Definition: diff.py:17
def color_diff(diff)
Definition: terminal.py:12
def prepare_diff_lines(string_a, string_b)
Definition: diff.py:97
def preview_changes(package, fn_name, fne, use_package_name=False)
Definition: diff.py:60
def get_diff(original_folder, new_folder)
Definition: diff.py:36
def color_header(s, fore='WHITE', back='BLUE')
Definition: terminal.py:27
def get_lines(folder, filename)
Definition: diff.py:41
def print_diff(filename, left_folder=None, right_folder=None)
Definition: diff.py:45


roscompile
Author(s):
autogenerated on Wed Mar 3 2021 03:56:01