acidjunk / pyang

Automatically exported from code.google.com/p/pyang
ISC License
0 stars 0 forks source link

Dependencies are not fully recursive #121

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Unpack the attached ZIP file. It contains the following YANG 
module/submodule (M/S) hierarchy:

test(M) -> test-sub(S)
test-sub(S) -> other(M)

other(M) -> other-sub(S) types(M)
other-sub(S)

types(M)

2. Generate test.d using the new --depend-from-submodules option:

% pyang  --format=depend --depend-include-path --depend-target="test.tree" 
--depend-from-submodules test.yang >test.d
other.yang:5: warning: imported module types not used

% cat test.d
test.tree : test-sub.yang other.yang

What is the expected output? What do you see instead?

This output correctly includes the dependency implied by the fact that 
test-sub(S) -> other(M), but it does include show the full dependencies. 
Specifically it does not show the dependencies implied by other(M) -> 
other-sub(S) types(M).

The patch shown below extends --depend-from-submodules to handle submodule and 
module dependencies recursively. With this patch:

% cat test.d
test.tree : test-sub.yang other.yang types.yang other-sub.yang

I believe that these are the correct dependencies that one would want to use in 
make rules.

What version of the product are you using? On what operating system?

The latest version from the Google code site (as of 08-Jan-15) plus the patch 
given below.

Please provide any additional information below.

Here is the patch (git diff output). Note that if this or a similar change is 
accepted, the name of the option should be changed, e.g. to --depend-recursive.

% git diff
diff --git a/pyang/plugins/depend.py b/pyang/plugins/depend.py
index c54a836..6a9c767 100644
--- a/pyang/plugins/depend.py
+++ b/pyang/plugins/depend.py
@@ -63,16 +63,8 @@ def emit_depend(ctx, modules, fd):
             fd.write('%s :' % module.pos.ref)
         else:
             fd.write('%s :' % ctx.opts.depend_target)
-        prereqs = [i.arg for i in module.search("import")]
-        if not ctx.opts.depend_no_submodules:
-            prereqs += [i.arg for i in module.search("include")]
-        if ctx.opts.depend_from_submodules:
-            for i in module.search("include"):
-                subm = ctx.get_module(i.arg)
-                if subm is not None:
-                    for prereq in [i.arg for i in subm.search("import")]:
-                        if prereq not in prereqs:
-                            prereqs.append(prereq)
+        prereqs = []
+        _add_prereqs(ctx, module, prereqs)
         for i in prereqs:
             if i in ctx.opts.depend_ignore:
                 continue
@@ -91,3 +83,22 @@ def emit_depend(ctx, modules, fd):
                     ext = ctx.opts.depend_extension
                 fd.write(' %s%s' % (i, ext))
         fd.write('\n')
+
+# note that --depend-from-submodules semantics has changed:
+# (a) it is now really "from imports and includes"
+# (b) dependencies are now fully recursive
+# therefore perhaps it could be renamed as --depend-recursive?
+def _add_prereqs(ctx, module, _prereqs):
+    imps = module.search("import")
+    if not ctx.opts.depend_no_submodules:
+        imps += module.search("include")
+    prereqs = [i.arg for i in imps]
+    for prereq in prereqs:
+        if prereq not in _prereqs:
+            _prereqs.append(prereq)
+    if ctx.opts.depend_from_submodules:
+        for i in imps:
+            subm = ctx.get_module(i.arg)
+            if subm is not None:
+                # maybe there needs to be a check for infinite recursion?
+                _add_prereqs(ctx, subm, _prereqs)

Original issue reported on code.google.com by william....@gmail.com on 9 Jan 2015 at 10:31

Attachments: