Repository: conan-io/conan

Issue:
Issues with the JSON created by conan create
Conan version: 1.2.0

Command: `conan create --json ./foo.json . myteam/unstable`

The generated JSON does not separate the dependency packages of the built project, from the built package. This makes it difficult to parse.

Discussion:
lasote: Pushed again, please check

Patch:
diff --git a/conans/client/action_recorder.py b/conans/client/action_recorder.py
index bf573d9cb..d771bf075 100644
--- a/conans/client/action_recorder.py
+++ b/conans/client/action_recorder.py
@@ -7,6 +7,8 @@ from datetime import datetime
 from collections import namedtuple, OrderedDict
 
 # Install actions
+from conans.model.ref import ConanFileReference, PackageReference
+
 INSTALL_CACHE = 0
 INSTALL_DOWNLOADED = 1
 INSTALL_BUILT = 2
@@ -32,8 +34,11 @@ class ActionRecorder(object):
     def __init__(self):
         self._inst_recipes_actions = OrderedDict()
         self._inst_packages_actions = OrderedDict()
+        self._inst_recipes_develop = set()  # Recipes being created (to set dependency=False)
 
     # ###### INSTALL METHODS ############
+    def add_recipe_being_developed(self, reference):
+        self._inst_recipes_develop.add(reference)
 
     def _add_recipe_action(self, reference, action):
         if reference not in self._inst_recipes_actions:
@@ -90,6 +95,12 @@ class ActionRecorder(object):
                 ret.append((_package_ref, _package_action))
         return ret
 
+    def in_development_recipe(self, reference):
+        return reference in self._inst_recipes_develop
+
+    def get_info(self):
+        return self.get_install_info()
+
     def get_install_info(self):
         ret = {"error": self.install_errored,
                "installed": []}
@@ -98,11 +109,15 @@ class ActionRecorder(object):
             error = None if the_action.type != INSTALL_ERROR else the_action.doc
             doc = {"id": str(the_ref),
                    "downloaded": the_action.type == INSTALL_DOWNLOADED,
-                   "built": the_action.type == INSTALL_BUILT,
                    "cache": the_action.type == INSTALL_CACHE,
                    "error": error,
                    "remote": the_action.doc.get("remote", None),
                    "time": the_action.time}
+            if isinstance(the_ref, ConanFileReference):
+                doc["dependency"] = not self.in_development_recipe(the_ref)
+            else:
+                doc["built"] = the_action.type == INSTALL_BUILT
+
             if doc["remote"] is None and error:
                 doc["remote"] = error.get("remote", None)
             return doc
@@ -111,7 +126,6 @@ class ActionRecorder(object):
             # Could be a download and then an access to cache, we want the first one
             action = actions[0]
             recipe_doc = get_doc_for_ref(ref, action)
-            del recipe_doc["built"]  # Avoid confusions
             packages = self._get_installed_packages(ref)
             tmp = {"recipe": recipe_doc,
                    "packages": []}
diff --git a/conans/client/command.py b/conans/client/command.py
index 19513161d..f805caf51 100644
--- a/conans/client/command.py
+++ b/conans/client/command.py
@@ -220,17 +220,21 @@ class Command(object):
 
         cwd = os.getcwd()
 
+        info = None
         try:
-            self._conan.create(args.path, name, version, user, channel,
-                               args.profile, args.settings, args.options,
-                               args.env, args.test_folder, args.not_export,
-                               args.build, args.keep_source, args.keep_build, args.verify,
-                               args.manifests, args.manifests_interactive,
-                               args.remote, args.update,
-                               test_build_folder=args.test_build_folder)
+            info = self._conan.create(args.path, name, version, user, channel,
+                                      args.profile, args.settings, args.options,
+                                      args.env, args.test_folder, args.not_export,
+                                      args.build, args.keep_source, args.keep_build, args.verify,
+                                      args.manifests, args.manifests_interactive,
+                                      args.remote, args.update,
+                                      test_build_folder=args.test_build_folder)
+        except ConanException as exc:
+            info = exc.info
+            raise
         finally:
-            if args.json:
-                self._outputer.json_install(self._conan.recorder.get_install_info(), args.json, cwd)
+            if args.json and info:
+                self._outputer.json_install(info, args.json, cwd)
 
     def download(self, *args):
         """Downloads recipe and binaries to the local cache, without using settings. It works
@@ -289,34 +293,38 @@ class Command(object):
         args = parser.parse_args(*args)
         cwd = os.getcwd()
 
+        info = None
         try:
             try:
                 reference = ConanFileReference.loads(args.path_or_reference)
             except ConanException:
-                self._conan.install(path=args.path_or_reference,
-                                    settings=args.settings, options=args.options,
-                                    env=args.env,
-                                    remote=args.remote,
-                                    verify=args.verify, manifests=args.manifests,
-                                    manifests_interactive=args.manifests_interactive,
-                                    build=args.build, profile_name=args.profile,
-                                    update=args.update, generators=args.generator,
-                                    no_imports=args.no_imports,
-                                    install_folder=args.install_folder)
+                info = self._conan.install(path=args.path_or_reference,
+                                           settings=args.settings, options=args.options,
+                                           env=args.env,
+                                           remote=args.remote,
+                                           verify=args.verify, manifests=args.manifests,
+                                           manifests_interactive=args.manifests_interactive,
+                                           build=args.build, profile_name=args.profile,
+                                           update=args.update, generators=args.generator,
+                                           no_imports=args.no_imports,
+                                           install_folder=args.install_folder)
             else:
-                self._conan.install_reference(reference, settings=args.settings,
-                                              options=args.options,
-                                              env=args.env,
-                                              remote=args.remote,
-                                              verify=args.verify, manifests=args.manifests,
-                                              manifests_interactive=args.manifests_interactive,
-                                              build=args.build, profile_name=args.profile,
-                                              update=args.update,
-                                              generators=args.generator,
-                                              install_folder=args.install_folder)
+                info = self._conan.install_reference(reference, settings=args.settings,
+                                                     options=args.options,
+                                                     env=args.env,
+                                                     remote=args.remote,
+                                                     verify=args.verify, manifests=args.manifests,
+                                                     manifests_interactive=args.manifests_interactive,
+                                                     build=args.build, profile_name=args.profile,
+                                                     update=args.update,
+                                                     generators=args.generator,
+                                                     install_folder=args.install_folder)
+        except ConanException as exc:
+            info = exc.info
+            raise
         finally:
-            if args.json:
-                self._outputer.json_install(self._conan.recorder.get_install_info(), args.json, cwd)
+            if args.json and info:
+                self._outputer.json_install(info, args.json, cwd)
 
     def config(self, *args):
         """Manages Conan configuration. Edits the conan.conf or installs config files.
diff --git a/conans/client/conan_api.py b/conans/client/conan_api.py
index 98ee25d5a..dd50689b2 100644
--- a/conans/client/conan_api.py
+++ b/conans/client/conan_api.py
@@ -65,15 +65,21 @@ def api_method(f):
         the_self = args[0]
         try:
             log_command(f.__name__, kwargs)
+            the_self._init_manager()
             with tools.environment_append(the_self._client_cache.conan_config.env_vars):
                 # Patch the globals in tools
-                return f(*args, **kwargs)
+                ret = f(*args, **kwargs)
+                if ret is None:  # FIXME: Probably each method should manage its return
+                    return the_self._recorder.get_info()
+                return ret
         except Exception as exc:
             msg = exception_message_safe(exc)
             try:
                 log_exception(exc, msg)
             except:
                 pass
+            if isinstance(exc, ConanException):
+                exc.info = the_self._recorder.get_info()
             raise
 
     return wrapper
@@ -207,13 +213,22 @@ class ConanAPIV1(object):
         self._user_io = user_io
         self._runner = runner
         self._remote_manager = remote_manager
-        self.recorder = ActionRecorder()
+        self._search_manager = search_manager
+        self._settings_preprocessor = _settings_preprocessor
         self._registry = RemoteRegistry(self._client_cache.registry, self._user_io.out)
-        self._manager = ConanManager(client_cache, user_io, runner, remote_manager, search_manager,
-                                     _settings_preprocessor, self.recorder, self._registry)
+        self._recorder = None
+        self._manager = None
+
         if not interactive:
             self._user_io.disable_input()
 
+    def _init_manager(self):
+        """Every api call gets a new recorder and new manager"""
+        self._recorder = ActionRecorder()
+        self._manager = ConanManager(self._client_cache, self._user_io, self._runner,
+                                     self._remote_manager, self._search_manager,
+                                     self._settings_preprocessor, self._recorder, self._registry)
+
     @api_method
     def new(self, name, header=False, pure_c=False, test=False, exports_sources=False, bare=False,
             cwd=None, visual_versions=None, linux_gcc_versions=None, linux_clang_versions=None,
@@ -319,6 +334,7 @@ class ConanAPIV1(object):
                                          "or it doesn't have a conanfile.py" % tf)
 
         test_conanfile_path = get_test_conanfile_path(test_folder)
+        self._recorder.add_recipe_being_developed(reference)
 
         if test_conanfile_path:
             pt = PackageTester(self._manager, self._user_io)
diff --git a/conans/errors.py b/conans/errors.py
index 256f20a3f..e5b73a087 100644
--- a/conans/errors.py
+++ b/conans/errors.py
@@ -68,7 +68,9 @@ class ConanException(Exception):
     """
          Generic conans exception
     """
-    pass
+    def __init__(self, *args, **kwargs):
+        self.info = None
+        super(ConanException, self).__init__(*args, **kwargs)
 
 
 class NoRemoteAvailable(ConanException):


Test patch:
diff --git a/conans/test/command/json_output_test.py b/conans/test/command/json_output_test.py
index a9f64b87a..0cf3ba030 100644
--- a/conans/test/command/json_output_test.py
+++ b/conans/test/command/json_output_test.py
@@ -23,6 +23,7 @@ class JsonOutputTest(unittest.TestCase):
         my_json = json.loads(load(os.path.join(self.client.current_folder, "myfile.json")))
         self.assertFalse(my_json["error"])
         self.assertEquals(my_json["installed"][0]["recipe"]["id"], "CC/1.0@private_user/channel")
+        self.assertFalse(my_json["installed"][0]["recipe"]["dependency"])
         self.assertTrue(my_json["installed"][0]["recipe"]["cache"])
         self.assertIsNone(my_json["installed"][0]["recipe"]["remote"])
         self.assertTrue(my_json["installed"][0]["packages"][0]["built"])
@@ -37,6 +38,7 @@ class JsonOutputTest(unittest.TestCase):
         self.assertIn("T", the_time_str) # Weak validation of the ISO 8601
         self.assertFalse(my_json["error"])
         self.assertEquals(my_json["installed"][0]["recipe"]["id"], "CC/1.0@private_user/channel")
+        self.assertTrue(my_json["installed"][0]["recipe"]["dependency"])
         self.assertFalse(my_json["installed"][0]["recipe"]["cache"])
         self.assertTrue(my_json["installed"][0]["recipe"]["downloaded"])
         self.assertIsNotNone(my_json["installed"][0]["recipe"]["remote"])
@@ -164,6 +166,10 @@ AA*: CC/1.0@private_user/channel
         my_json = load(os.path.join(self.client.current_folder, "myfile.json"))
         my_json = json.loads(my_json)
 
+        self.assertTrue(my_json["installed"][0]["recipe"]["dependency"])
+        self.assertTrue(my_json["installed"][1]["recipe"]["dependency"])
+        self.assertTrue(my_json["installed"][2]["recipe"]["dependency"])
+
         # Installed the build require CC with two options
         self.assertEquals(len(my_json["installed"][2]["packages"]), 2)
         self.assertEquals(my_json["installed"][2]["recipe"]["id"], "CC/1.0@private_user/channel")
diff --git a/conans/test/model/version_ranges_test.py b/conans/test/model/version_ranges_test.py
index 71012b623..919b29a8c 100644
--- a/conans/test/model/version_ranges_test.py
+++ b/conans/test/model/version_ranges_test.py
@@ -1,4 +1,5 @@
 import unittest
+
 from conans.test.utils.tools import TestBufferConanOutput
 from conans.paths import CONANFILE
 import os
diff --git a/conans/test/util/action_recorder_test.py b/conans/test/util/action_recorder_test.py
index 0a93a1570..ac85817b5 100644
--- a/conans/test/util/action_recorder_test.py
+++ b/conans/test/util/action_recorder_test.py
@@ -19,9 +19,11 @@ class ActionRecorderTest(unittest.TestCase):
     def incomplete_process_test(self):
         tracer = ActionRecorder()
         tracer.recipe_install_error(self.ref1, INSTALL_ERROR_NETWORK, "SSL wtf", "http://drl.com")
-        install_info = tracer.get_install_info()
+        tracer.add_recipe_being_developed(self.ref1)
+        install_info = tracer.get_info()
         self.assertTrue(install_info["error"])
         self.assertEquals(install_info["installed"][0]["packages"], [])
+        self.assertEquals(install_info["installed"][0]["recipe"]["dependency"], False)
 
     def double_actions_test(self):
         tracer = ActionRecorder()
@@ -30,7 +32,7 @@ class ActionRecorderTest(unittest.TestCase):
         tracer.package_downloaded(self.ref_p1, "http://drl.com")
         tracer.package_fetched_from_cache(self.ref_p1)
 
-        install_info = tracer.get_install_info()
+        install_info = tracer.get_info()
         self.assertFalse(install_info["error"])
 
         first_installed = install_info["installed"][0]
@@ -55,12 +57,15 @@ class ActionRecorderTest(unittest.TestCase):
 
         tracer.recipe_fetched_from_cache(self.ref3)
         tracer.package_built(self.ref_p3)
+        tracer.add_recipe_being_developed(self.ref1)
 
-        install_info = tracer.get_install_info()
+        install_info = tracer.get_info()
         self.assertTrue(install_info["error"])
 
         first_installed = install_info["installed"][0]
+
         self.assertTrue(first_installed["recipe"]["cache"])
+        self.assertFalse(first_installed["recipe"]["dependency"])
         self.assertFalse(first_installed["recipe"]["downloaded"])
         self.assertIsNone(first_installed["recipe"]["error"])
         self.assertEquals(str(first_installed["recipe"]["id"]), "lib1/1.0@conan/stable")
@@ -73,6 +78,7 @@ class ActionRecorderTest(unittest.TestCase):
 
         second_installed = install_info["installed"][1]
         self.assertFalse(second_installed["recipe"]["cache"])
+        self.assertTrue(second_installed["recipe"]["dependency"])
         self.assertTrue(second_installed["recipe"]["downloaded"])
         self.assertIsNone(second_installed["recipe"]["error"])
         self.assertEquals(str(second_installed["recipe"]["id"]), "lib2/1.0@conan/stable")
@@ -85,6 +91,7 @@ class ActionRecorderTest(unittest.TestCase):
         self.assertEquals(str(second_installed["packages"][0]["id"]), "2")
 
         third_installed = install_info["installed"][2]
+        self.assertTrue(third_installed["recipe"]["dependency"])
         self.assertFalse(third_installed["packages"][0]["cache"])
         self.assertFalse(third_installed["packages"][0]["error"])
         self.assertTrue(third_installed["packages"][0]["built"])