source_code_file.py
Go to the documentation of this file.
1 import os
2 import re
3 
4 from .resource_list import get_python_dependency, is_package
5 
6 PKG = r'([^\.;]+)(\.?[^;]*)?'
7 PYTHON1 = '^import ' + PKG
8 PYTHON2 = 'from ' + PKG + ' import .*'
9 CPLUS = re.compile(r'#include\s*[<\\"]([^/]*)/?([^/]*)[>\\"]') # Zero or one slash
10 CPLUS2 = re.compile(r'#include\s*[<\\"]([^/]*)/([^/]*)/([^/]*)[>\\"]') # Two slashes
11 ROSCPP = re.compile(r'#include\s*<ros/ros.h>')
12 
13 EXPRESSIONS = [re.compile(PYTHON1), re.compile(PYTHON2), CPLUS, CPLUS2]
14 
15 
17  return s[0:2] == '#!' and 'python' in s
18 
19 
21  def __init__(self, rel_fn, file_path):
22  self.rel_fn = rel_fn
23  self.file_path = file_path
24  self.tags = set()
25  self.changed_contents = None
26 
27  self.lines = list(map(str.strip, self.get_contents().split('\n')))
28  if '.py' in self.file_path or (len(self.lines) > 0 and is_python_hashbang_line(self.lines[0])):
29  self.language = 'python'
30  else:
31  self.language = 'c++'
32 
33  parts = os.path.split(rel_fn)
34  if parts and parts[0] == 'test':
35  self.tags.add('test')
36 
37  def get_contents(self):
38  if self.changed_contents:
39  return self.changed_contents
40  return open(self.file_path).read()
41 
42  def replace_contents(self, contents):
43  self.changed_contents = contents
44  try:
45  self.lines = map(unicode.strip, unicode(contents).split('\n'))
46  except NameError:
47  # Python3 Case
48  self.lines = list(map(str.strip, contents.split('\n')))
49 
50  def search_for_patterns(self, patterns):
51  matches = []
52  contents = self.get_contents()
53  for pattern in patterns:
54  matches += pattern.findall(contents)
55  return matches
56 
57  def search_lines_for_patterns(self, patterns):
58  matches = []
59  for line in self.lines:
60  for pattern in patterns:
61  m = pattern.search(line)
62  if m:
63  matches.append(m.groups())
64  return matches
65 
66  def search_lines_for_pattern(self, pattern):
67  return self.search_lines_for_patterns([pattern])
68 
69  def modify_with_patterns(self, patterns, verbose):
70  """
71  Given a map of patterns, replace all instances in the source code.
72 
73  The key in the map (needle) is a regular expression string literal.
74  If there are no groups, then the matching string is replaced with the map value.
75  If there are groups, then the literals of the form $0, $1, etc in the map value are replaced with the groups
76  """
77  s = self.get_contents()
78  changed = False
79  for needle, replacement in patterns.items():
80  pattern = re.compile(needle)
81  m = pattern.search(s)
82  while m:
83  this_replacement = replacement
84  if len(m.groups()) > 0:
85  for i, chunk in enumerate(m.groups()):
86  key = '$%d' % i
87  this_replacement = this_replacement.replace(key, chunk)
88  before, middle, after = s.partition(m.group(0))
89  if verbose:
90  print('In %s, replacing %s with %s' % (self.rel_fn, middle, this_replacement))
91  s = before + this_replacement + after
92 
93  changed = True
94  m = pattern.search(s)
95  if changed:
96  self.replace_contents(s)
97 
99  pkgs = set()
100  for match in self.search_lines_for_patterns(EXPRESSIONS):
101  pkgs.add(match[0])
102  if len(self.search_lines_for_pattern(ROSCPP)) > 0:
103  pkgs.add('roscpp')
104  return sorted(pkgs)
105 
106  def get_dependencies(self):
107  deps = []
108  for pkg in self.get_import_packages():
109  if is_package(pkg):
110  deps.append(pkg)
111  return deps
112 
114  deps = []
115  if self.language != 'python':
116  return deps
117 
118  for pkg in self.get_import_packages():
119  p_dep = get_python_dependency(pkg)
120  if p_dep:
121  deps.append(p_dep)
122  return deps
123 
124  def is_executable(self):
125  return os.access(self.file_path, os.X_OK)
126 
127  def __lt__(self, other):
128  return self.rel_fn < other.rel_fn
129 
130  def __repr__(self):
131  attribs = [self.language] + list(self.tags)
132  return '%s (%s)' % (self.rel_fn, ', '.join(attribs))
133 
134  def write(self):
135  if self.changed_contents:
136  with open(self.file_path, 'w') as f:
137  f.write(self.changed_contents)


ros_introspection
Author(s):
autogenerated on Wed Mar 3 2021 03:56:00