def do_build(self, args):
        try:
            #add arguments
            doParser = self.arg_build()
            try:
                doArgs = doParser.parse_args(args.split())
            except SystemExit as e:
                return
            #--
            template=validate_json_file(doArgs.file)
            if template is None:
                return 2

            if doArgs.id:
                myAppliance = self.api.Users(self.login).Appliances(doArgs.id).Get()
                myAppliance = myAppliance.appliances.appliance
            else:
                #Get template which correpond to the template file
                myAppliance = self.api.Users(self.login).Appliances().Getall(Query="name=='"+template["stack"]["name"]+"';version=='"+template["stack"]["version"]+"'")
                myAppliance = myAppliance.appliances.appliance
            if myAppliance is None or len(myAppliance)!=1:
                printer.out("No template found on the plateform")
                return 0
            myAppliance=myAppliance[0]
            rInstallProfile = self.api.Users(self.login).Appliances(myAppliance.dbId).Installprofile("").Get()
            if rInstallProfile is None:
                printer.out("No installation found on the template '"+template["stack"]["name"]+"'", printer.ERROR)
                return 0
            try:
                i=1
                if doArgs.junit is not None:
                    test_results=[]
                for builder in template["builders"]:
                    try:
                        printer.out("Generating '"+builder["type"]+"' image ("+str(i)+"/"+str(len(template["builders"]))+")")
                        if doArgs.junit is not None:
                            test = TestCase('Generation '+builder["type"])
                            test_results.append(test)
                            start_time = time.time()

                        format_type = builder["type"]
                        targetFormat = generate_utils.get_target_format_object(self.api, self.login, format_type)
                        if targetFormat is None:
                            printer.out("Builder type unknown: "+format_type, printer.ERROR)
                            return 2

                        myimage = image()
                        myinstallProfile = installProfile()
                        if rInstallProfile.partitionAuto:
                            if "installation" in builder:
                                if "swapSize" in builder["installation"]:
                                    myinstallProfile.swapSize = builder["installation"]["swapSize"]
                                if "diskSize" in builder["installation"]:
                                    myinstallProfile.diskSize = builder["installation"]["diskSize"]
                            else:
                                myinstallProfile.swapSize = rInstallProfile.swapSize
                                myinstallProfile.diskSize = rInstallProfile.partitionTable.disks.disk[0].size

                        func = getattr(generate_utils, "generate_"+generics_utils.remove_special_chars(targetFormat.format.name), None)
                        if func:
                            myimage,myinstallProfile = func(myimage, builder, myinstallProfile, self.api, self.login)
                        else:
                            printer.out("Builder type unknown: "+format_type, printer.ERROR)
                            return 2


                        if myimage is None:
                            return 2

                        myimage.targetFormat = targetFormat
                        myimage.installProfile = myinstallProfile
                        if doArgs.simulated is not None and doArgs.simulated:
                            myimage.simulated=True
                        if doArgs.forced is not None and doArgs.forced:
                            myimage.forceCheckingDeps=True

                        rImage = self.api.Users(self.login).Appliances(myAppliance.dbId).Images().Generate(myimage)

                        status = rImage.status
                        statusWidget = progressbar_widget.Status()
                        statusWidget.status = status
                        widgets = [Bar('>'), ' ', statusWidget, ' ', ReverseBar('<')]
                        progress = ProgressBar(widgets=widgets, maxval=100).start()
                        while not (status.complete or status.error or status.cancelled):
                            statusWidget.status = status
                            progress.update(status.percentage)
                            status = self.api.Users(self.login).Appliances(myAppliance.dbId).Images(rImage.dbId).Status.Get()
                            time.sleep(2)
                        statusWidget.status = status
                        progress.finish()
                        if status.error:
                            printer.out("Generation '"+builder["type"]+"' error: "+status.message+"\n"+status.errorMessage, printer.ERROR)
                            if status.detailedError:
                                printer.out(status.detailedErrorMsg)
                            if doArgs.junit is not None:
                                test.elapsed_sec=time.time() - start_time
                                test.add_error_info("Error", status.message+"\n"+status.errorMessage)
                        elif status.cancelled:
                            printer.out("Generation '"+builder["type"]+"' canceled: "+status.message, printer.WARNING)
                            if doArgs.junit is not None:
                                test.elapsed_sec=time.time() - start_time
                                test.add_failure_info("Canceled", status.message)
                        else:
                            printer.out("Generation '"+builder["type"]+"' ok", printer.OK)
                            printer.out("Image URI: "+rImage.uri)
                            printer.out("Image Id : "+generics_utils.extract_id(rImage.uri))
                            if doArgs.junit is not None:
                                test.elapsed_sec=time.time() - start_time
                                #the downloadUri already contains downloadKey at the end
                                if rImage.downloadUri is not None:
                                    test.stdout=self.api._url+"/"+rImage.downloadUri
                        i+=1
                    except Exception as e:
                        if  is_uforge_exception(e):
                            print_uforge_exception(e)
                            if doArgs.junit is not None and "test_results" in locals() and len(test_results)>0:
                                test=test_results[len(test_results)-1]
                                test.elapsed_sec=time.time() - start_time
                                test.add_error_info("Error", get_uforge_exception(e))
                        else:
                            raise
                if doArgs.junit is not None:
                    testName = myAppliance.distributionName+" "+myAppliance.archName
                    ts = TestSuite("Generation "+testName, test_results)
                    with open(doArgs.junit, 'w') as f:
                        TestSuite.to_file(f, [ts], prettyprint=False)
                return 0
            except __HOLE__ as e:
                printer.out("unknown error in template json file", printer.ERROR)

        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: "+str(e), printer.ERROR)
            self.help_build()
        except KeyboardInterrupt:
            printer.out("\n")
            if generics_utils.query_yes_no("Do you want to cancel the job ?"):
                if 'myAppliance' in locals() and 'rImage' in locals() and hasattr(myAppliance, 'dbId') and hasattr(rImage, 'dbId'):
                    self.api.Users(self.login).Appliances(myAppliance.dbId).Images(rImage.dbId).Status.Cancel()
                else:
                    printer.out("Impossible to cancel", printer.WARNING)
            else:
                printer.out("Exiting command")
        except Exception as e:
            print_uforge_exception(e)
            if doArgs.junit is not None and "test_results" in locals() and len(test_results)>0:
                test=test_results[len(test_results)-1]
                if "start_time" in locals():
                    elapse=time.time() - start_time
                else:
                    elapse=0
                test.elapsed_sec=elapse
                test.add_error_info("Error", get_uforge_exception(e))
            else:
                return 2
        finally:
            if "doArgs" in locals() and doArgs.junit is not None and "test_results" in locals() and len(test_results)>0:
                if "myAppliance" in locals():
                    testName = myAppliance.distributionName+" "+myAppliance.archName
                else:
                    testName = ""
                ts = TestSuite("Generation "+testName, test_results)
                with open(doArgs.junit, 'w') as f:
                    TestSuite.to_file(f, [ts], prettyprint=False)
dataset/ETHPy150Open usharesoft/hammr/src/hammr/commands/template/template.py/Template.do_build