Browse Source

Add ability to list all deps

These changes add a new feature which allows users
to list all dependencies for their platform in csv
format or one dep per line. This is really useful
if people want to take the deps found in bindeps.txt
and use them in an RPM spec file to build an RPM.

USAGE:
bindep -l newline
* lists all dependencies one per line
bindep -l csv
* lists all dependencies in csv format

Change-Id: I0277689ba727737e11b0adcddf6300b58614ac18
tags/2.5.0^0
rybridges 1 year ago
parent
commit
71c58d24c0
3 changed files with 87 additions and 0 deletions
  1. 19
    0
      bindep/depends.py
  2. 9
    0
      bindep/main.py
  3. 59
    0
      bindep/tests/test_depends.py

+ 19
- 0
bindep/depends.py View File

@@ -194,6 +194,25 @@ class Depends(object):
194 194
                 result.append(rule)
195 195
         return result
196 196
 
197
+    def list_all_packages(self, rules, output_format='newline'):
198
+        """Print a list of all packages that are required on this platform
199
+        according to the passed in rules. This is useful if we want to build
200
+        RPMs based on the deps listed in bindeps.txt
201
+
202
+        :param rules: A list of rules, as returned by active_rules.
203
+        :param output_format: The format to print the output in. Currently
204
+        we support newline format which will print 1 package per line, and
205
+        csv format which prints a csv list.
206
+        :return: List of all required packages regardless of whether they are
207
+        missing.
208
+        """
209
+        packages_list = [rule[0] for rule in rules]
210
+        if output_format == 'csv':
211
+            logging.info(','.join(packages_list))
212
+        elif output_format == 'newline':
213
+            logging.info('\n'.join(packages_list))
214
+        return packages_list
215
+
197 216
     def check_rules(self, rules):
198 217
         """Evaluate rules against the local environment.
199 218
 

+ 9
- 0
bindep/main.py View File

@@ -41,6 +41,12 @@ def main(depends=None):
41 41
     parser.add_option(
42 42
         "--profiles", action="store_true",
43 43
         help="List the platform and configuration profiles.")
44
+    parser.add_option(
45
+        "-l", "--list_all", dest="list_all", choices=["newline", "csv"],
46
+        type="choice", action="store",
47
+        help="List all dependencies for this platform and profile."
48
+             " Pass in either 'newline' or 'csv' to specify the format"
49
+             " of the output.")
44 50
     opts, args = parser.parse_args()
45 51
     if depends is None:
46 52
         depends = bindep.depends.get_depends(opts.filename)
@@ -61,6 +67,9 @@ def main(depends=None):
61 67
             profiles = ["default"]
62 68
         profiles = profiles + depends.platform_profiles()
63 69
         rules = depends.active_rules(profiles)
70
+        if opts.list_all:
71
+            depends.list_all_packages(rules, opts.list_all)
72
+            return 0
64 73
         errors = depends.check_rules(rules)
65 74
         for error in errors:
66 75
             if error[0] == 'missing':

+ 59
- 0
bindep/tests/test_depends.py View File

@@ -350,6 +350,65 @@ class TestDepends(TestCase):
350 350
             set(r[0] for r in depends.active_rules(['platform:rpm', 'test'])),
351 351
             Equals({"install5", "install6", "install8", "install9"}))
352 352
 
353
+    def test_list_all(self):
354
+        depends = Depends(dedent("""\
355
+            install1
356
+            install2 [test]
357
+            install3 [platform:rpm]
358
+            install4 [platform:dpkg]
359
+            install5 [quark]
360
+            install6 [platform:dpkg test]
361
+            install7 [quark test]
362
+            install8 [platform:dpkg platform:rpm]
363
+            install9 [platform:dpkg platform:rpm test]
364
+            installA [!platform:dpkg]
365
+            installB [!platform:dpkg test]
366
+            installC [!platform:dpkg !test]
367
+            installD [platform:dpkg !test]
368
+            installE [platform:dpkg !platform:rpm]
369
+            installF [platform:dpkg !platform:rpm test]
370
+            installG [!platform:dpkg !platform:rpm]
371
+            installH [!platform:dpkg !platform:rpm test]
372
+            installI [!platform:dpkg !platform:rpm !test]
373
+            installJ [platform:dpkg !platform:rpm !test]
374
+            """))
375
+
376
+        rules_dpkg = depends.active_rules(['platform:dpkg'])
377
+        result_dpkg = set(r[0] for r in rules_dpkg)
378
+        self.assertEqual(result_dpkg,
379
+                         set(depends.list_all_packages(rules_dpkg,
380
+                             output_format='newline')))
381
+        self.assertEqual(result_dpkg,
382
+                         set(depends.list_all_packages(rules_dpkg,
383
+                             output_format='csv')))
384
+
385
+        rules_dpkg_test = depends.active_rules(['platform:dpkg', 'test'])
386
+        result_dpkg_test = set(r[0] for r in rules_dpkg_test)
387
+        self.assertEqual(result_dpkg_test, set(depends.list_all_packages(
388
+                                               rules_dpkg_test,
389
+                                               output_format='newline')))
390
+        self.assertEqual(result_dpkg_test, set(depends.list_all_packages(
391
+                                               rules_dpkg_test,
392
+                                               output_format='csv')))
393
+
394
+        rules_rpm = depends.active_rules(['platform:rpm'])
395
+        result_rpm = set(r[0] for r in rules_rpm)
396
+        self.assertEqual(result_rpm, set(depends.list_all_packages(rules_rpm,
397
+                                         output_format='newline')))
398
+        self.assertEqual(result_rpm, set(depends.list_all_packages(rules_rpm,
399
+                                         output_format='csv')))
400
+
401
+        rules_rpm_test = depends.active_rules(['platform:rpm', 'test'])
402
+        result_rpm_test = set(r[0] for r in rules_rpm_test)
403
+        self.assertEqual(result_rpm_test,
404
+                         set(depends.list_all_packages(
405
+                             rules_rpm_test,
406
+                             output_format='newline')))
407
+        self.assertEqual(result_rpm_test,
408
+                         set(depends.list_all_packages(
409
+                             rules_rpm_test,
410
+                             output_format='csv')))
411
+
353 412
     def test_platforms(self):
354 413
         depends = Depends(dedent("""\
355 414
             install1

Loading…
Cancel
Save