# Copyright 2014 ARM Limited
#
# Licensed under the Apache License, Version 2.0
# See LICENSE file for details.

# standard library modules, , ,
from __future__ import print_function
from collections import defaultdict
import logging

# validate, , validate things, internal
from .lib import validate

def addOptions(parser):
    parser.add_argument('--all', '-a', dest='list_all', default=False, action='store_true',
        help='List all licenses, not just each unique license.'
    )

def execCommand(args, following_args):
    c = validate.currentDirectoryModule()
    if not c:
        return 1

    if not args.target:
        logging.error('No target has been set, use "yotta target" to set one.')
        return 1

    target, errors = c.satisfyTarget(args.target)
    if errors:
        for error in errors:
            logging.error(error)
        return 1

    dependencies = c.getDependenciesRecursive(
                      target = target,
        available_components = [(c.getName(), c)]
    )

    if args.list_all:
        for dep in dependencies.values():
            print(u'%s: %s' % (dep.getName(), u', '.join(dep.licenses())))
    else:
        licenses = defaultdict(list)
        for dep in dependencies.values():
            for lic in dep.licenses():
                licenses[lic].append(dep.getName())
        for lic in licenses:
            print(lic)


@@ -1005,16 +1005,32 @@
 %0A    )%0A%0A
+    errors = %5B%5D%0A
     if a
@@ -1046,32 +1046,38 @@
 all:%0A        for
+ name,
  dep in dependen
@@ -1077,34 +1077,152 @@
 ependencies.
-values():%0A
+items():%0A            if not dep:%0A                errors.append(u'%25s is missing: license unknown!' %25 name)%0A            else:%0A    
             
@@ -1240,29 +1240,20 @@
  %25s' %25 (
-dep.getN
+n
 ame
-()
 , u', '.
@@ -1333,16 +1333,22 @@
      for
+ name,
  dep in 
@@ -1364,16 +1364,130 @@
 ies.
-values()
+items():%0A            if not dep:%0A                errors.append(u'%25s is missing: license unknown!' %25 name)%0A            else
 :%0A  
@@ -1488,32 +1488,36 @@
 se:%0A            
+ 
+   
 for lic in dep.l
@@ -1535,32 +1535,36 @@
                 
+    
 licenses%5Blic%5D.ap
@@ -1572,21 +1572,12 @@
 end(
-dep.getN
+n
 ame
-()
 )%0A  
@@ -1627,8 +1627,117 @@
 t(lic)%0A%0A
+    if len(errors):%0A        for err in errors:%0A            logging.error(err)%0A        return 1%0A    return 0%0A%0A

Fix crash in "yotta licenses" subcommand