source_code_file.py
Go to the documentation of this file.
1 import re
2 import os
3 from ros_introspection.resource_list import is_package, get_python_dependency
4 
5 PKG = '([^\.;]+)(\.?[^;]*)?'
6 PYTHON1 = '^import ' + PKG
7 PYTHON2 = 'from ' + PKG + ' import .*'
8 CPLUS = re.compile('#include\s*[<\\"]([^/]*)/?([^/]*)[>\\"]') # Zero or one slash
9 CPLUS2 = re.compile('#include\s*[<\\"]([^/]*)/([^/]*)/([^/]*)[>\\"]') # Two slashes
10 ROSCPP = re.compile('#include\s*<ros/ros.h>')
11 
12 EXPRESSIONS = [re.compile(PYTHON1), re.compile(PYTHON2), CPLUS, CPLUS2]
13 
14 
16  return s[0:2] == '#!' and 'python' in s
17 
18 
20  def __init__(self, rel_fn, file_path):
21  self.rel_fn = rel_fn
22  self.file_path = file_path
23  self.tags = set()
24  self.changed_contents = None
25 
26  self.lines = map(str.strip, self.get_contents().split('\n'))
27  if '.py' in self.file_path or (len(self.lines) > 0 and is_python_hashbang_line(self.lines[0])):
28  self.language = 'python'
29  else:
30  self.language = 'c++'
31 
32  parts = os.path.split(rel_fn)
33  if parts and parts[0] == 'test':
34  self.tags.add('test')
35 
36  def get_contents(self):
37  if self.changed_contents:
38  return self.changed_contents
39  return open(self.file_path).read()
40 
41  def replace_contents(self, contents):
42  self.changed_contents = contents
43  self.lines = map(unicode.strip, unicode(contents).split('\n'))
44 
45  def search_for_patterns(self, patterns):
46  matches = []
47  contents = self.get_contents()
48  for pattern in patterns:
49  matches += pattern.findall(contents)
50  return matches
51 
52  def search_lines_for_patterns(self, patterns):
53  matches = []
54  for line in self.lines:
55  for pattern in patterns:
56  m = pattern.search(line)
57  if m:
58  matches.append(m.groups())
59  return matches
60 
61  def search_lines_for_pattern(self, pattern):
62  return self.search_lines_for_patterns([pattern])
63 
65  pkgs = set()
66  for match in self.search_lines_for_patterns(EXPRESSIONS):
67  pkgs.add(match[0])
68  if len(self.search_lines_for_pattern(ROSCPP)) > 0:
69  pkgs.add('roscpp')
70  return sorted(list(pkgs))
71 
72  def get_dependencies(self):
73  deps = []
74  for pkg in self.get_import_packages():
75  if is_package(pkg):
76  deps.append(pkg)
77  return deps
78 
80  deps = []
81  if self.language != 'python':
82  return deps
83 
84  for pkg in self.get_import_packages():
85  p_dep = get_python_dependency(pkg)
86  if p_dep:
87  deps.append(p_dep)
88  return deps
89 
90  def is_executable(self):
91  return os.access(self.file_path, os.X_OK)
92 
93  def __lt__(self, other):
94  return self.rel_fn < other.rel_fn
95 
96  def __repr__(self):
97  attribs = [self.language] + list(self.tags)
98  return '%s (%s)' % (self.rel_fn, ', '.join(attribs))
99 
100  def write(self):
101  if self.changed_contents:
102  with open(self.file_path, 'w') as f:
103  f.write(self.changed_contents)


ros_introspection
Author(s):
autogenerated on Wed Jun 19 2019 19:56:52