/
metamodel.py
2137 lines (1789 loc) · 67.8 KB
/
metamodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
#Copyright (c) 2017 André Santos
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
###############################################################################
# Imports
###############################################################################
from builtins import range
from collections import Counter, namedtuple
from itertools import chain
import os
import magic as file_cmd
###############################################################################
# Notes
###############################################################################
# It seems that it is better to store string names for source objects, instead
# of RosName. If necessary, store namespace also as a string. Source objects
# will be used as generators for runtime objects, so we must have access to
# the values provided originally.
# Source objects define various types of dependencies (files, packages, etc.).
# The objects exist, but only work properly if the dependencies are met.
# Runtime objects do not really have dependencies (only the node instances
# depend on the node executable). Instead, they have conditions, boolean
# expressions that determine whether the objects will be instantiated.
###############################################################################
# Base Metamodel Object
###############################################################################
class MetamodelObject(object):
pass
###############################################################################
# Analysis Properties
###############################################################################
class DependencySet(object):
def __init__(self):
self.files = set()
self.packages = set()
self.arguments = set()
self.environment = set()
def __str__(self):
return str(self.__dict__)
def __eq__(self, other):
if not isinstance(self, other.__class__):
return False
return (self.files == other.files
and self.packages == other.packages
and self.arguments == other.arguments
and self.environment == other.environment)
def __ne__(self, other):
return not self.__eq__(other)
class SourceCondition(object):
def __init__(self, condition, location=None, statement="if"):
self.statement = statement
self.condition = condition
self.location = location
self.location2 = loc_to_loc2(location)
@property
def language(self):
if self.location and self.location.file:
return self.location.file.language
return "unknown"
def to_JSON_object(self):
return {
"statement": self.statement,
"condition": str(self.condition),
"location": loc2_to_JSON(self.location2)
}
def __str__(self):
return "{} {}".format(self.statement, str(self.condition))
def __repr__(self):
return self.__str__()
class _UnknownValue(object):
__slots__ = ("name", "_call", "_attr", "_fun")
def __init__(self, name, call, attr, fun):
self.name = name
self._call = call
self._attr = attr
self._fun = fun
@property
def attr(self):
return self._attr
def resolve(self, value):
if value is not None:
setattr(self._call, self._attr, self._fun(value))
return self.name in self._call._vars
return False
def __str__(self):
return self.name
class RosPrimitiveCall(MetamodelObject):
KEY = ""
""""Base class for calls to ROS primitives."""
def __init__(self, name, namespace, msg_type, control_depth = None,
repeats = False, conditions = None, location = None):
self._vars = {}
self.name = name
self.namespace = namespace
self.type = msg_type
self.conditions = conditions if not conditions is None else []
self.control_depth = control_depth or len(self.conditions)
self.repeats = repeats and self.control_depth >= 1
self.location = location
self.location2 = loc_to_loc2(location)
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._set_str_attr("name", value)
@property
def namespace(self):
return self._namespace
@namespace.setter
def namespace(self, value):
self._set_str_attr("namespace", value)
@property
def type(self):
return self._type
@type.setter
def type(self, value):
self._set_str_attr("type", value)
@property
def location(self):
return self._location
@location.setter
def location(self, value):
if value is None or value.package is None or value.file is None:
if "location" not in self._vars:
self._vars["location"] = self._new_var("location", fpid)
else:
if "location" in self._vars:
del self._vars["location"]
self._location = value
self.location2 = loc_to_loc2(value)
@property
def conditions(self):
return self._conditions
@conditions.setter
def conditions(self, value):
if value:
if "conditions" not in self._vars:
self._vars["conditions"] = self._new_var(
"conditions", _bool_to_conditions)
else:
if "conditions" in self._vars:
del self._vars["conditions"]
self._conditions = value
@property
def full_name(self):
if not self.namespace:
return self.name
if self.namespace.endswith("/"):
return self.namespace + self.name
return self.namespace + "/" + self.name
@property
def rostype(self):
return self.type
def variables(self):
return list(self._vars.values())
def refine_from_JSON_specs(self, data):
if "?" in self.name:
self.name = data["name"]
if "?" in self.namespace:
self.namespace = data.get("ns", self.namespace)
self.conditions = []
self.control_depth = 0
self.repeats = False
self.location = data.get("location", self.location)
def to_JSON_object(self):
return {
"name": self.name,
"namespace": self.namespace,
"type": self.type,
"depth": self.control_depth,
"repeats": self.repeats,
"conditions": [c.to_JSON_object() for c in self.conditions],
"location": loc2_to_JSON(self.location2),
"variables": {attr: uv.name for attr, uv in self._vars.items()}
}
def _set_str_attr(self, attr, value):
if value is None or value == "?":
if attr not in self._vars:
self._vars[attr] = self._new_var(attr, str)
else:
if attr in self._vars:
del self._vars[attr]
setattr(self, "_" + attr, value)
_var_counter = 0
def _new_var(self, attr, tf):
type(self)._var_counter += 1
name = "{}@{}{}".format(self.KEY, attr, type(self)._var_counter)
return _UnknownValue(name, self, attr, tf)
def __str__(self):
return "RosPrimitiveCall({}, {}, {}) {} (depth {})".format(
self.name, self.namespace, self.type,
self.location2, self.control_depth
)
def __repr__(self):
return self.__str__()
class AdvertiseCall(RosPrimitiveCall):
KEY = "advertise"
_var_counter = 0
def __init__(self, name, namespace, msg_type, queue_size, latched=False,
control_depth=None, repeats=False, conditions=None,
location=None):
RosPrimitiveCall.__init__(self, name, namespace, msg_type,
control_depth = control_depth,
repeats = repeats,
conditions = conditions, location = location)
self.queue_size = queue_size
self.latched = latched
@property
def queue_size(self):
return self._queue_size
@queue_size.setter
def queue_size(self, value):
key = "queue_size"
if value is None:
if key not in self._vars:
self._vars[key] = self._new_var(key, int)
else:
if key in self._vars:
del self._vars[key]
self._queue_size = value
@property
def latched(self):
return self._latched
@latched.setter
def latched(self, value):
key = "latched"
if value is None:
if key not in self._vars:
self._vars[key] = self._new_var(key, bool)
else:
if key in self._vars:
del self._vars[key]
self._latched = value
@classmethod
def from_JSON_specs(cls, datum):
name = datum["name"]
ns = datum.get("namespace", "")
msg_type = datum["msg_type"]
queue = datum["queue_size"]
latched = datum.get("latched", False)
return cls(name, ns, msg_type, queue, latched=latched)
def refine_from_JSON_specs(self, data):
RosPrimitiveCall.refine_from_JSON_specs(self, data)
self.type = data["msg_type"]
self.queue_size = data.get("queue_size", self.queue_size)
self.latched = data.get("latched", self.latched)
def to_JSON_object(self):
data = RosPrimitiveCall.to_JSON_object(self)
data["queue"] = self.queue_size
data["latched"] = self.latched
return data
def clone(self):
call = AdvertiseCall(self.name, self.namespace, self.type,
self.queue_size, latched=self.latched,
control_depth=self.control_depth, repeats=self.repeats,
conditions=list(self.conditions), location=self.location)
call.location2 = self.location2
return call
Publication = AdvertiseCall
class SubscribeCall(RosPrimitiveCall):
KEY = "subscribe"
_var_counter = 0
def __init__(self, name, namespace, msg_type, queue_size,
control_depth = None, repeats = False, conditions = None,
location = None):
RosPrimitiveCall.__init__(self, name, namespace, msg_type,
control_depth = control_depth,
repeats = repeats,
conditions = conditions, location = location)
self.queue_size = queue_size
@property
def queue_size(self):
return self._queue_size
@queue_size.setter
def queue_size(self, value):
key = "queue_size"
if value is None:
if key not in self._vars:
self._vars[key] = self._new_var(key, int)
else:
if key in self._vars:
del self._vars[key]
self._queue_size = value
@classmethod
def from_JSON_specs(cls, datum):
name = datum["name"]
ns = datum.get("namespace", "")
msg_type = datum["msg_type"]
queue = datum["queue_size"]
return cls(name, ns, msg_type, queue)
def refine_from_JSON_specs(self, data):
RosPrimitiveCall.refine_from_JSON_specs(self, data)
self.type = data["msg_type"]
self.queue_size = data.get("queue_size", self.queue_size)
def to_JSON_object(self):
data = RosPrimitiveCall.to_JSON_object(self)
data["queue"] = self.queue_size
return data
def clone(self):
call = SubscribeCall(self.name, self.namespace, self.type,
self.queue_size, control_depth=self.control_depth,
repeats=self.repeats, conditions=list(self.conditions),
location=self.location)
call.location2 = self.location2
return call
Subscription = SubscribeCall
class AdvertiseServiceCall(RosPrimitiveCall):
KEY = "advertiseService"
_var_counter = 0
def refine_from_JSON_specs(self, data):
RosPrimitiveCall.refine_from_JSON_specs(self, data)
self.type = data["srv_type"]
@classmethod
def from_JSON_specs(cls, datum):
name = datum["name"]
ns = datum.get("namespace", "")
srv_type = datum["srv_type"]
return cls(name, ns, srv_type)
def clone(self):
call = AdvertiseServiceCall(self.name, self.namespace, self.type,
control_depth=self.control_depth, repeats=self.repeats,
conditions=list(self.conditions), location=self.location)
call.location2 = self.location2
return call
ServiceServerCall = AdvertiseServiceCall
class ServiceClientCall(RosPrimitiveCall):
KEY = "serviceClient"
_var_counter = 0
def refine_from_JSON_specs(self, data):
RosPrimitiveCall.refine_from_JSON_specs(self, data)
self.type = data["srv_type"]
@classmethod
def from_JSON_specs(cls, datum):
name = datum["name"]
ns = datum.get("namespace", "")
srv_type = datum["srv_type"]
return cls(name, ns, srv_type)
def clone(self):
call = ServiceClientCall(self.name, self.namespace, self.type,
control_depth=self.control_depth, repeats=self.repeats,
conditions=list(self.conditions), location=self.location)
call.location2 = self.location2
return call
class GetParamCall(RosPrimitiveCall):
KEY = "getParam"
_var_counter = 0
def __init__(self, name, namespace, param_type, default_value=None,
control_depth=None, repeats=False, conditions=None,
location=None):
RosPrimitiveCall.__init__(self, name, namespace, param_type,
control_depth=control_depth, repeats=repeats,
conditions=conditions, location=location)
self.default_value = default_value
@property
def default_value(self):
return self._default_value
@default_value.setter
def default_value(self, value):
key = "default_value"
if value is None:
if key not in self._vars:
self._vars[key] = self._new_var(key, fpid)
else:
if key in self._vars:
del self._vars[key]
self._default_value = value
@classmethod
def from_JSON_specs(cls, datum):
name = datum["name"]
ns = datum.get("namespace", "")
param_type = datum["param_type"]
v = datum.get("default_value")
return cls(name, ns, param_type, default_value=v)
def refine_from_JSON_specs(self, data):
RosPrimitiveCall.refine_from_JSON_specs(self, data)
self.type = data["param_type"]
self.default_value = data.get("default_value", self.default_value)
def to_JSON_object(self):
data = RosPrimitiveCall.to_JSON_object(self)
data["default_value"] = self.default_value
return data
def clone(self):
call = GetParamCall(self.name, self.namespace, self.type,
default_value=self.default_value,
control_depth=self.control_depth, repeats=self.repeats,
conditions=list(self.conditions), location=self.location)
call.location2 = self.location2
return call
ReadParameterCall = GetParamCall
class SetParamCall(RosPrimitiveCall):
KEY = "setParam"
_var_counter = 0
def __init__(self, name, namespace, param_type, value=None,
control_depth=None, repeats=False, conditions=None,
location=None):
RosPrimitiveCall.__init__(self, name, namespace, param_type,
control_depth=control_depth, repeats=repeats,
conditions=conditions, location=location)
self.value = value
@property
def value(self):
return self._value
@value.setter
def value(self, value):
key = "value"
if value is None:
if key not in self._vars:
self._vars[key] = self._new_var(key, fpid)
else:
if key in self._vars:
del self._vars[key]
self._value = value
@classmethod
def from_JSON_specs(cls, datum):
name = datum["name"]
ns = datum.get("namespace", "")
param_type = datum["param_type"]
v = datum.get("value")
return cls(name, ns, param_type, value=v)
def refine_from_JSON_specs(self, data):
RosPrimitiveCall.refine_from_JSON_specs(self, data)
self.type = data["param_type"]
self.value = data.get("value", self.value)
def to_JSON_object(self):
data = RosPrimitiveCall.to_JSON_object(self)
data["value"] = self.value
return data
def clone(self):
call = SetParamCall(self.name, self.namespace, self.type,
value=self.value,
control_depth=self.control_depth, repeats=self.repeats,
conditions=list(self.conditions), location=self.location)
call.location2 = self.location2
return call
WriteParameterCall = SetParamCall
###############################################################################
# Source Code Structures
###############################################################################
class Person(object):
"""Represents a person (author/maintainer)."""
def __init__(self, name, email = "email@example.com"):
self.id = email
self.name = name or "Unknown"
self.email = email
def __eq__(self, other):
if not isinstance(self, other.__class__):
return False
return self.id == other.id
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return self.id.__hash__()
Location2 = namedtuple("Location2", ("package", "file", "line", "column"))
# in the process of deprecation
class Location(object):
"""A location to report (package, file, line)."""
def __init__(self, pkg, file=None, line=None, col=None, fun=None, cls=None):
self.package = pkg
self.file = file
self.line = line # should start at 1
self.column = col # should start at 1
self.function = fun
self.class_ = cls
@property
def largest_scope(self):
return self.package
@property
def smallest_scope(self):
return self.file or self.package
def to_JSON_object(self):
return {
"package": self.package.name,
"file": self.file.full_name if self.file else None,
"line": self.line,
"column": self.column,
"function": self.function,
"class": self.class_
}
def __str__(self):
s = "in " + self.package.name
if not self.file:
return s
s += "/" + self.file.full_name
if not self.line is None:
s += ":" + str(self.line)
if not self.column is None:
s += ":" + str(self.column)
if self.function:
s += ", in function " + self.function
if self.class_:
s += ", in class " + self.class_
return s
class SourceObject(MetamodelObject):
"""Base class for objects subject to analysis."""
SCOPES = ("file", "node", "package", "repository", "project")
def __init__(self, id, name):
self.id = id
self.name = name
self.dependencies = DependencySet()
self._analyse = True
@property
def location(self):
return None
def __lt__(self, scope):
if isinstance(scope, basestring):
return self.SCOPES.index(self.scope) < self.SCOPES.index(scope)
return self.SCOPES.index(self.scope) < self.SCOPES.index(scope.scope)
def __le__(self, scope):
if isinstance(scope, basestring):
return self.SCOPES.index(self.scope) <= self.SCOPES.index(scope)
return self.SCOPES.index(self.scope) <= self.SCOPES.index(scope.scope)
def __gt__(self, scope):
if isinstance(scope, basestring):
return self.SCOPES.index(self.scope) > self.SCOPES.index(scope)
return self.SCOPES.index(self.scope) > self.SCOPES.index(scope.scope)
def __ge__(self, scope):
if isinstance(scope, basestring):
return self.SCOPES.index(self.scope) >= self.SCOPES.index(scope)
return self.SCOPES.index(self.scope) >= self.SCOPES.index(scope.scope)
def __eq__(self, other):
if not isinstance(self, other.__class__):
return False
return self.id == other.id
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return self.id.__hash__()
def bound_to(self, scope):
return self == scope
def accepts_scope(self, scope):
return self.scope == scope
def __str__(self):
return self.id
class SourceFile(SourceObject):
"""Represents a source code file."""
CPP = ('c source', 'c++ source')
CPP_ALT = ('.c', '.h', '.hpp', '.hxx', '.cpp', '.cc', '.cxx')
PYTHON = 'python script'
PYTHON_ALT = '.py'
PKG_XML = 'package.xml'
LAUNCH = ('.launch', '.launch.xml')
MSG = '.msg'
SRV = '.srv'
ACTION = '.action'
YAML = ('.yaml', '.yml')
CMAKELISTS = 'CMakeLists.txt'
def __init__(self, name, directory, pkg):
id = ("file:" + pkg.name + "/" + directory.replace(os.path.sep, "/")
+ "/" + name)
SourceObject.__init__(self, id, name)
self.directory = directory
self.full_name = os.path.join(directory, name)
self.dir_path = os.path.join(pkg.path, directory)
self.path = os.path.join(pkg.path, directory, name)
self.package = pkg
self.language = self._get_language()
self.tree = None
self.size = 0
self.lines = 0
self.sloc = 0
self.timestamp = 0
@property
def scope(self):
return "file"
@property
def location(self):
return Location(self.package, file=self)
def bound_to(self, other):
if other.scope == "node":
return self in other.source_files
if other.scope == "package":
return self.package == other
if other.scope == "repository" or other.scope == "project":
return self.package in other.packages
return other.scope == "file" and self == other
def accepts_scope(self, scope):
return self >= scope
def set_file_stats(self):
self.size = os.path.getsize(self.path)
self.timestamp = os.path.getmtime(self.path)
self.lines = 0
self.sloc = 0
ignore_all = []
to_ignore = {"*": ignore_all}
ilp, inlp = self._ignore_parsers()
with open(self.path, "r") as handle:
for line in handle:
self.lines += 1
sline = line.strip()
if sline:
self.sloc += 1
if ilp(sline):
ignore_all.append(self.lines)
elif inlp(sline):
ignore_all.append(self.lines + 1)
return to_ignore
def to_JSON_object(self):
return {
"name": self.name,
"directory": self.directory,
"package": self.package.name,
"language": self.language,
"size": self.size,
"timestamp": self.timestamp,
"lines": self.lines,
"sloc": self.sloc
}
def _get_language(self):
try:
file_type = file_cmd.from_file(self.path).lower()
if file_type.startswith(self.CPP):
return 'cpp'
if self.PYTHON in file_type:
return 'python'
except IOError:
if self.name.endswith(self.CPP_ALT):
return 'cpp'
if self.name.endswith(self.PYTHON_ALT):
return 'python'
if self.name.endswith(self.LAUNCH):
return 'launch'
if self.name == self.PKG_XML:
return 'package'
if self.name.endswith(self.MSG):
return 'msg'
if self.name.endswith(self.SRV):
return 'srv'
if self.name.endswith(self.ACTION):
return 'action'
if self.name.endswith(self.YAML):
return 'yaml'
if self.name == self.CMAKELISTS:
return 'cmake'
return 'unknown'
def __str__(self):
return self.__repr__()
def __repr__(self):
return self.id
def _ignore_parsers(self):
if self.language == "cpp":
return (_cpp_ignore_line, _cpp_ignore_next_line)
elif self.language == "python":
return (_py_ignore_line, _py_ignore_next_line)
return (_no_parser, _no_parser)
class Package(SourceObject):
"""Represents a ROS package."""
def __init__(self, name, repo = None, proj = None):
SourceObject.__init__(self, "package:" + name, name)
# public:
self.project = proj
self.repository = repo
self.authors = set()
self.maintainers = set()
self.is_metapackage = False
self.description = ""
self.version = "0.0.0"
self.licenses = set()
self.website = None
self.vcs_url = None
self.bug_url = None
self.path = None
self.source_files = []
self.nodes = []
self.size = 0 # sum of file sizes
self.lines = 0 # sum of physical file lines
self.sloc = 0 # sum of file source lines of code
self.topological_tier = 0
@property
def scope(self):
return "package"
@property
def location(self):
return Location(self)
@property
def file_count(self):
return len(self.source_files)
def bound_to(self, other):
if other.scope == "file" or other.scope == "node":
return other.package == self
if other.scope == "repository":
return self.repository == other
if other.scope == "project":
return self.project == other
if other.scope == "package":
if self == other:
return True
for dep in self.dependencies.packages:
if dep.type == "package" and dep.value == other.name:
return True
return False
def to_JSON_object(self):
return {
"name": self.name,
"metapackage": self.is_metapackage,
"description": self.description,
"version": self.version,
"wiki": self.website,
"repository": self.vcs_url,
"bugTracker": self.bug_url,
"authors": [person.name for person in self.authors],
"maintainers": [person.name for person in self.maintainers],
"dependencies": [pkg for pkg in self.dependencies.packages],
"files": [f.full_name for f in self.files],
"nodes": [n.node_name for n in self.nodes]
}
def __str__(self):
return self.__repr__()
def __repr__(self):
return self.id
class Repository(SourceObject):
"""Represents a source code repository."""
def __init__(self, name, vcs = None, url = None, version = None,
status = None, path = None, proj = None):
SourceObject.__init__(self, "repository:" + name, name)
self.project = None
self.vcs = vcs
self.url = url
self.version = version
self.status = status
self.path = path
self.packages = []
self.declared_packages = []
self.commits = 1
self.contributors = 1
@property
def scope(self):
return "repository"
def bound_to(self, other):
if other.scope == "package":
return other.repository == self
if other.scope == "file" or other.scope == "node":
return other.package in self.packages
if other.scope == "project":
if self.project == other:
return True
for package in self.packages:
if not package in other.packages:
return False
return True
return other.scope == "repository" and self == other
def __str__(self):
return self.__repr__()
def __repr__(self):
return self.id
class Project(SourceObject):
"""A project is a custom grouping of packages, not necessarily
corresponding to a repository, and not even requiring the
existence of one.
"""
def __init__(self, name):
if name == "all":
raise ValueError("Forbidden project name: all")
SourceObject.__init__(self, "project:" + name, name)
self.packages = []
self.repositories = []
self.configurations = []
self.node_specs = {}
@property
def scope(self):
return "project"
def get_node(self, node_name):
parts = node_name.split("/")
if not len(parts) == 2:
raise ValueError("Expected '<pkg>/<node>' string, got {}.".format(
repr(node_name)))
for pkg in self.packages:
if pkg.name == parts[0]:
break
else:
raise ValueError("Package is not part of the project: " + parts[0])
for node in pkg.nodes:
if node.name == parts[1]:
break
else:
raise ValueError("Unknown node: " + node_name)
return node
def bound_to(self, other):
if other.scope == "package":
return other.project == self
if other.scope == "file" or other.scope == "node":
return other.package in self.packages
if other.scope == "repository":
if other.project == self:
return True
for package in other.packages:
if not package in self.packages:
return False
return True
return other.scope == "project" and self == other
def to_JSON_object(self):
return {
"id": self.name,
"packages": [pkg.name for pkg in self.packages],
"repositories": [repo.name for repo in self.repositories]
}
def __str__(self):
return self.__repr__()
def __repr__(self):
return self.id
class Node(SourceObject):
def __init__(self, name, pkg, rosname = None, nodelet = None):
id = "node:" + pkg.name + "/" + (nodelet or name)
SourceObject.__init__(self, id, name)
self.package = pkg
if isinstance(rosname, basestring):
rosname = RosName(rosname)
self.rosname = rosname
self.nodelet_class = nodelet
self.source_files = []
self.source_tree = None
self.instances = []
self.advertise = []
self.subscribe = []
self.service = []
self.client = []
self.read_param = []
self.write_param = []
self.hpl_properties = [] # [string | HplAstObject]
self.hpl_assumptions = [] # [string | HplAstObject]