def get_org_metadata(raw=False, selectBasedOnPackageXml=False, selectedIds=[], keyword=None, **kwargs): project = config.project if project.get_is_metadata_indexed(): if raw: org_metadata_raw = util.get_file_as_string(os.path.join(project.location,"config",".org_metadata")) org_index = json.loads(org_metadata_raw) if selectBasedOnPackageXml: project.select_metadata_based_on_package_xml(org_index) elif len(selectedIds) > 0 or keyword != None: if keyword != None: crawlJson.setVisibility(org_index, keyword) if len(selectedIds) > 0: crawlJson.setChecked(org_index, selectedIds) return json.dumps(org_index) else: org_index = util.parse_json_from_file(os.path.join(project.location,"config",".org_metadata")) if selectBasedOnPackageXml: project.select_metadata_based_on_package_xml(org_index) elif len(selectedIds) > 0 or keyword != None: if keyword != None: crawlJson.setVisibility(org_index, keyword) if len(selectedIds) > 0: crawlJson.setChecked(org_index, selectedIds) return org_index else: IndexMetadataCommand(params=self.params).execute() org_index = util.parse_json_from_file(os.path.join(project.location,"config",".org_metadata")) project.select_metadata_based_on_package_xml(org_index) return org_index
def get_plugin_client_settings(): settings = {} settings['default'] = util.parse_json_from_file( os.path.join(os.path.dirname(__file__), "default_client_settings.json")) settings['user'] = util.parse_json_from_file( os.path.join(os.path.dirname(__file__), "user_client_settings.json")) return settings
def test_02_bad_compile(self): client_settings = mmutil.parse_json_from_file( os.path.join(test_helper.base_test_directory, "user_client_settings.json")) commandOut = self.redirectStdOut() src = open( os.path.join(client_settings["mm_workspace"], "unit test metadata project", "src", "classes", "unittestapexclass.cls"), "w") src.write( 'public class unittestapexclass { public unittestapexclass() { String foo } }' ) src.close() test_helper.compile("unit test metadata project", [ os.path.join(client_settings["mm_workspace"], "unit test metadata project", "src", "classes", "unittestapexclass.cls") ]) mm_response = commandOut.getvalue() sys.stdout = self.saved_stdout print mm_response mm_json_response = test_util.parse_mm_response(mm_response) self.assertTrue(mm_json_response['State'] == 'Failed') commandOut = self.redirectStdOut() test_helper.delete_apex_metadata("unit test metadata project", [ os.path.join(client_settings["mm_workspace"], "unit test metadata project", "src", "classes", "unittestapexclass.cls") ])
def test_03_delete_apex_class(self): commandOut = self.redirectStdOut() client_settings = mmutil.parse_json_from_file( os.path.join(test_helper.base_test_directory, "user_client_settings.json") ) stdin = { "files": [ os.path.join( client_settings["mm_workspace"], "unit test metadata project", "src", "classes", "unittestapexclass.cls", ) ], "project_name": "unit test metadata project", } request.get_request_payload = mock.Mock(return_value=stdin) sys.argv = ["mm.py", "-o", "delete"] MavensMateRequestHandler().execute() mm_response = commandOut.getvalue() sys.stdout = self.saved_stdout print mm_response mm_json_response = test_util.parse_mm_response(mm_response) self.assertTrue(mm_json_response["success"] == True)
def test_02_should_deploy_to_server(self): client_settings = mmutil.parse_json_from_file( os.path.join(test_helper.base_test_directory, "user_client_settings.json")) org_connections = test_util.parse_json_from_file( os.path.join(client_settings["mm_workspace"], "unit test deploy project", "config", ".org_connections")) stdin = { "project_name": "unit test deploy project", "destinations": [{ "id": org_connections[0]["id"], "username": org_connections[0]["username"], "org_type": org_connections[0]["environment"] }], "check_only": True, "run_tests": False, "rollback_on_error": True, "package": { "ApexClass": ["CompileAndTest"] }, "debug_categories": "" } mm_response = self.runCommand(['mm.py', '-o', 'deploy', '--html'], stdin) self.assertTrue(mm_response['success'] == True)
def test_02_deploy(self): commandOut = self.redirectStdOut() client_settings = mmutil.parse_json_from_file(os.path.join(test_helper.base_test_directory, "user_client_settings.json")) org_connections = test_util.parse_json_from_file(os.path.join(client_settings["mm_workspace"],"unit test deploy project","config",".org_connections")) stdin = { "project_name" : "unit test deploy project", "destinations" : [ { "id" : org_connections[0]["id"], "username" : org_connections[0]["username"], "org_type" : org_connections[0]["environment"] } ], "check_only" : True, "run_tests" : False, "rollback_on_error" : True, "package" : { "ApexClass" : ["CompileAndTest"] }, "debug_categories" : "" } request.get_request_payload = mock.Mock(return_value=stdin) sys.argv = ['mm.py', '-o', 'deploy', '--html'] MavensMateRequestHandler().execute() mm_response = commandOut.getvalue() sys.stdout = self.saved_stdout print mm_response mm_json_response = test_util.parse_mm_response(mm_response) self.assertTrue(mm_json_response['success'] == True)
def test_01_compile_with_tooling_api(self): client_settings = mmutil.parse_json_from_file( os.path.join(test_helper.base_test_directory, "user_client_settings.json") ) test_helper.create_project("unit test metadata project") test_helper.create_apex_metadata("unit test metadata project", "ApexClass", "unittestapexclass") commandOut = self.redirectStdOut() test_helper.compile( "unit test metadata project", [ os.path.join( client_settings["mm_workspace"], "unit test metadata project", "src", "classes", "unittestapexclass.cls", ) ], ) mm_response = commandOut.getvalue() sys.stdout = self.saved_stdout print mm_response mm_json_response = test_util.parse_mm_response(mm_response) self.assertTrue(mm_json_response["State"] == "Completed") self.assertTrue(mm_json_response["CompilerErrors"] == "[]")
def test_03_should_delete_apex_class(self): client_settings = mmutil.parse_json_from_file(os.path.join(test_helper.base_test_directory, "user_client_settings.json")) stdin = { "files": [os.path.join(test_helper.base_test_directory,"test_workspace","unit test metadata project","src","classes","unittestapexclass.cls")], "project_name": "unit test metadata project" } mm_response = self.runCommand('delete', stdin) self.assertTrue(mm_response['success'] == True)
def test_03_should_delete_org_connection(self): client_settings = mmutil.parse_json_from_file(os.path.join(test_helper.base_test_directory, "user_client_settings.json")) org_connections = test_util.parse_json_from_file(os.path.join(client_settings["mm_workspace"],"unit test deploy project","config",".org_connections")) stdin = { "id" : org_connections[0]["id"], "project_name" : "unit test deploy project" } mm_response = self.runCommand('delete_connection', stdin) self.assertTrue(mm_response['success'] == True)
def test_02_should_compile_apex_class(self): test_helper.create_project(self, "unit test metadata project") client_settings = mmutil.parse_json_from_file(os.path.join(test_helper.base_test_directory, "user_client_settings.json")) stdin = { "project_name": "unit test metadata project", "files": [os.path.join(test_helper.base_test_directory,"test_workspace","unit test metadata project","src","classes","unittestapexclass.cls")] } mm_response = self.runCommand('compile', stdin) self.assertTrue(mm_response['State'] == "Completed") self.assertTrue(mm_response['ErrorMsg'] == None)
def execute(self): if 'script_name' in self.params: #running an apex script self.params["body"] = util.get_file_as_string( os.path.join(config.project.location, "apex-scripts", self.params["script_name"])) if 'debug_categories' not in self.params and not os.path.isfile( os.path.join(config.project.location, "config", ".apex_script")): self.params["debug_categories"] = [{ "category": "Apex_code", "level": "DEBUG" }] elif os.path.isfile( os.path.join(config.project.location, "config", ".apex_script")): log_settings = util.parse_json_from_file( os.path.join(config.project.location, "config", ".apex_script")) categories = [] levels = log_settings["levels"] for category in levels.keys(): categories.append({ "category": category, "level": levels[category] }) self.params["debug_categories"] = categories elif 'debug_categories' not in self.params: self.params["debug_categories"] = [{ "category": "Apex_code", "level": "DEBUG" }] return_log = self.params.get("return_log", True) execute_result = config.sfdc_client.execute_apex(self.params) result = { 'column': execute_result['column'], 'compileProblem': execute_result['compileProblem'], 'compiled': execute_result['compiled'], 'exceptionMessage': execute_result['exceptionMessage'], 'exceptionStackTrace': execute_result['exceptionStackTrace'], 'line': execute_result['line'], 'success': execute_result['success'], } if 'log' in execute_result and return_log: result['log'] = execute_result['log'] if result['success']: log_apex = config.connection.get_plugin_client_setting( 'mm_log_anonymous_apex', False) if log_apex: location = config.project.log_anonymous_apex( self.params['body'], execute_result['log'], self.params.get("script_name", None)) result["log_location"] = location return util.generate_response(result)
def test_03_should_delete_org_connection(self): client_settings = mmutil.parse_json_from_file( os.path.join(test_helper.base_test_directory, "user_client_settings.json")) org_connections = test_util.parse_json_from_file( os.path.join(client_settings["mm_workspace"], "unit test deploy project", "config", ".org_connections")) stdin = { "id": org_connections[0]["id"], "project_name": "unit test deploy project" } mm_response = self.runCommand('delete_connection', stdin) self.assertTrue(mm_response['success'] == True)
def test_03_should_delete_apex_class(self): client_settings = mmutil.parse_json_from_file( os.path.join(test_helper.base_test_directory, "user_client_settings.json")) stdin = { "files": [ os.path.join(test_helper.base_test_directory, "test_workspace", "unit test metadata project", "src", "classes", "unittestapexclass.cls") ], "project_name": "unit test metadata project" } mm_response = self.runCommand('delete', stdin) self.assertTrue(mm_response['success'] == True)
def test_03_delete_org_connection(self): commandOut = self.redirectStdOut() client_settings = mmutil.parse_json_from_file(os.path.join(test_helper.base_test_directory, "user_client_settings.json")) org_connections = test_util.parse_json_from_file(os.path.join(client_settings["mm_workspace"],"unit test deploy project","config",".org_connections")) stdin = { "id" : org_connections[0]["id"], "project_name" : "unit test deploy project" } request.get_request_payload = mock.Mock(return_value=stdin) sys.argv = ['mm.py', '-o', 'delete_connection'] MavensMateRequestHandler().execute() mm_response = commandOut.getvalue() sys.stdout = self.saved_stdout print mm_response mm_json_response = test_util.parse_mm_response(mm_response) self.assertTrue(mm_json_response['success'] == True)
def execute(self): if 'script_name' in self.params: #running an apex script self.params["body"] = util.get_file_as_string(os.path.join(config.project.location,"apex-scripts",self.params["script_name"])) if 'debug_categories' not in self.params and not os.path.isfile(os.path.join(config.project.location,"config",".apex_script")): self.params["debug_categories"] = [ { "category" : "Apex_code", "level" : "DEBUG" } ] elif os.path.isfile(os.path.join(config.project.location,"config",".apex_script")): log_settings = util.parse_json_from_file(os.path.join(config.project.location,"config",".apex_script")) categories = [] levels = log_settings["levels"] for category in levels.keys(): categories.append({ "category" : category, "level" : levels[category] }) self.params["debug_categories"] = categories elif 'debug_categories' not in self.params: self.params["debug_categories"] = [ { "category" : "Apex_code", "level" : "DEBUG" } ] return_log = self.params.get("return_log", True) execute_result = config.sfdc_client.execute_apex(self.params) result = { 'column' : execute_result['column'], 'compileProblem' : execute_result['compileProblem'], 'compiled' : execute_result['compiled'], 'exceptionMessage' : execute_result['exceptionMessage'], 'exceptionStackTrace' : execute_result['exceptionStackTrace'], 'line' : execute_result['line'], 'success' : execute_result['success'], } if 'log' in execute_result and return_log: result['log'] = execute_result['log'] if result['success']: log_apex = config.connection.get_plugin_client_setting('mm_log_anonymous_apex', False) if log_apex: location = config.project.log_anonymous_apex(self.params['body'], execute_result['log'], self.params.get("script_name", None)) result["log_location"] = location return util.generate_response(result)
def test_02_should_compile_apex_class(self): test_helper.create_project(self, "unit test metadata project") client_settings = mmutil.parse_json_from_file( os.path.join(test_helper.base_test_directory, "user_client_settings.json")) stdin = { "project_name": "unit test metadata project", "files": [ os.path.join(test_helper.base_test_directory, "test_workspace", "unit test metadata project", "src", "classes", "unittestapexclass.cls") ] } mm_response = self.runCommand('compile', stdin) self.assertTrue(mm_response['State'] == "Completed") self.assertTrue(mm_response['ErrorMsg'] == None)
def test_02_compile_apex_class(self): test_helper.create_project("unit test metadata project") commandOut = self.redirectStdOut() client_settings = mmutil.parse_json_from_file(os.path.join(test_helper.base_test_directory, "user_client_settings.json")) stdin = { "project_name": "unit test metadata project", "files": [os.path.join(client_settings["mm_workspace"],"unit test metadata project","src","classes","unittestapexclass.cls")] } request.get_request_payload = mock.Mock(return_value=stdin) sys.argv = ['mm.py', '-o', 'compile'] MavensMateRequestHandler().execute() mm_response = commandOut.getvalue() sys.stdout = self.saved_stdout print mm_response mm_json_response = test_util.parse_mm_response(mm_response) self.assertTrue(mm_json_response['State'] == "Completed") self.assertTrue(mm_json_response['ErrorMsg'] == None)
def test_02_bad_compile(self): client_settings = mmutil.parse_json_from_file( os.path.join(test_helper.base_test_directory, "user_client_settings.json") ) commandOut = self.redirectStdOut() src = open( os.path.join( client_settings["mm_workspace"], "unit test metadata project", "src", "classes", "unittestapexclass.cls" ), "w", ) src.write("public class unittestapexclass { public unittestapexclass() { String foo } }") src.close() test_helper.compile( "unit test metadata project", [ os.path.join( client_settings["mm_workspace"], "unit test metadata project", "src", "classes", "unittestapexclass.cls", ) ], ) mm_response = commandOut.getvalue() sys.stdout = self.saved_stdout print mm_response mm_json_response = test_util.parse_mm_response(mm_response) self.assertTrue(mm_json_response["State"] == "Failed") commandOut = self.redirectStdOut() test_helper.delete_apex_metadata( "unit test metadata project", [ os.path.join( client_settings["mm_workspace"], "unit test metadata project", "src", "classes", "unittestapexclass.cls", ) ], )
def test_01_compile_with_tooling_api(self): client_settings = mmutil.parse_json_from_file( os.path.join(test_helper.base_test_directory, "user_client_settings.json")) test_helper.create_project("unit test metadata project") test_helper.create_apex_metadata("unit test metadata project", "ApexClass", "unittestapexclass") commandOut = self.redirectStdOut() test_helper.compile("unit test metadata project", [ os.path.join(client_settings["mm_workspace"], "unit test metadata project", "src", "classes", "unittestapexclass.cls") ]) mm_response = commandOut.getvalue() sys.stdout = self.saved_stdout print mm_response mm_json_response = test_util.parse_mm_response(mm_response) self.assertTrue(mm_json_response['State'] == 'Completed') self.assertTrue(mm_json_response['CompilerErrors'] == '[]')
def test_02_should_deploy_to_server(self): client_settings = mmutil.parse_json_from_file(os.path.join(test_helper.base_test_directory, "user_client_settings.json")) org_connections = test_util.parse_json_from_file(os.path.join(client_settings["mm_workspace"],"unit test deploy project","config",".org_connections")) stdin = { "project_name" : "unit test deploy project", "destinations" : [ { "id" : org_connections[0]["id"], "username" : org_connections[0]["username"], "org_type" : org_connections[0]["environment"] } ], "check_only" : True, "run_tests" : False, "rollback_on_error" : True, "package" : { "ApexClass" : ["CompileAndTest"] }, "debug_categories" : "" } mm_response = self.runCommand(['mm.py', '-o', 'deploy', '--html'], stdin) self.assertTrue(mm_response['success'] == True)
def test_03_delete_apex_class(self): commandOut = self.redirectStdOut() client_settings = mmutil.parse_json_from_file( os.path.join(test_helper.base_test_directory, "user_client_settings.json")) stdin = { "files": [ os.path.join(client_settings["mm_workspace"], "unit test metadata project", "src", "classes", "unittestapexclass.cls") ], "project_name": "unit test metadata project" } request.get_request_payload = mock.Mock(return_value=stdin) sys.argv = ['mm.py', '-o', 'delete'] MavensMateRequestHandler().execute() mm_response = commandOut.getvalue() sys.stdout = self.saved_stdout print mm_response mm_json_response = test_util.parse_mm_response(mm_response) self.assertTrue(mm_json_response['success'] == True)
def execute(self): archive_deployments = config.connection.get_plugin_client_setting( "mm_archive_deployments", True) finish_deploy = self.params.get('finish', False) compare = config.connection.get_plugin_client_setting( "mm_compare_before_deployment", True) destinations = self.params['destinations'] deploy_metadata = config.sfdc_client.retrieve( package=self.params['package']) deploy_name = self.params.get('new_deployment_name', None) threads = [] if not finish_deploy and compare: source_retrieve_result = config.sfdc_client.retrieve( package=self.params['package']) debug('source_retrieve_result') debug(source_retrieve_result) source_dict = {} for fp in source_retrieve_result.fileProperties: source_dict[fp.fileName] = fp debug('source_dict') debug(source_dict) #need to compare package.xml to destination orgs here for destination in destinations: thread = CompareHandler(config.project, destination, self.params, self.params['package']) threads.append(thread) thread.start() compare_results = [] for thread in threads: thread.join() compare_results.append(thread.result) debug('compare_results') debug(compare_results) destination_dict = {} for cr in compare_results: cr_dict = {} for fpfp in cr.fileProperties: cr_dict[fpfp.fileName] = fpfp destination_dict[cr.username] = cr_dict debug('destination_dict') debug(destination_dict) final_compare_result = {} for d in destinations: final_compare_result[d['username']] = {} for file_name, file_details in source_dict.iteritems(): if 'package.xml' in file_name: continue for username, username_value in destination_dict.iteritems(): destination_retrieve_details = destination_dict[username] if 'package.xml' in file_name: continue short_file_name = file_name.split('/')[-1] mtype = util.get_meta_type_by_suffix( short_file_name.split('.')[-1]) if file_name not in destination_retrieve_details: final_compare_result[username][file_name] = { 'name': short_file_name, 'type': mtype['xmlName'], 'action': 'insert', 'message': 'Create' } else: destination_file_detail = destination_retrieve_details[ file_name] source_file_detail = source_dict[file_name] if source_file_detail.lastModifiedDate >= destination_file_detail.lastModifiedDate: final_compare_result[username][file_name] = { 'name': short_file_name, 'type': mtype['xmlName'], 'action': 'update', 'message': 'You will overwrite this file' } else: final_compare_result[username][file_name] = { 'name': short_file_name, 'type': mtype['xmlName'], 'action': 'update_conflict', 'message': 'Destination file is newer than source file' } # final_compare_result = {} # for d in destinations: # final_compare_result[d['username']] = {} # for username, username_value in destination_dict.iteritems(): # #destination_dict = destination_dict[username] # for file_name, file_details in username_value.iteritems(): # if 'package.xml' in file_name: # continue; # short_file_name = file_name.split('/')[-1] # mtype = util.get_meta_type_by_suffix(short_file_name.split('.')[-1]) # if file_name not in source_dict: # final_compare_result[username][file_name] = { # 'name' : short_file_name, # 'type' : mtype['xmlName'], # 'action': 'insert', # 'message' : 'Create' # } # else: # destination_file_detail = username_value[file_name] # source_file_detail = source_dict[file_name] # if source_file_detail.lastModifiedDate >= destination_file_detail.lastModifiedDate: # final_compare_result[username][file_name] = { # 'name' : short_file_name, # 'type' : mtype['xmlName'], # 'action' : 'update', # 'message' : 'You will overwrite this file' # } # else: # final_compare_result[username][file_name] = { # 'name' : short_file_name, # 'type' : mtype['xmlName'], # 'action' : 'update_conflict', # 'message' : 'Destination file is newer than source file' # } debug('final_compare_result') debug(final_compare_result) if self.args.respond_with_html == True: html = util.generate_html_response('deploy_compare', final_compare_result, self.params) response = json.loads( util.generate_success_response(html, "html")) response['compare_success'] = True # if deployment to one org fails, the entire deploy was not successful # for result in final_compare_result: # if result['success'] == False: # response['compare_success'] = False # break return json.dumps(response) else: return json.dumps(final_compare_result, indent=4) for destination in destinations: if archive_deployments: deploy_path = os.path.join(config.project.location, "deploy", destination['username']) if not os.path.exists(deploy_path): os.makedirs(deploy_path) if not os.path.isfile( os.path.join(config.project.location, "deploy", '.config')): config_file = open( os.path.join(config.project.location, "deploy", '.config'), 'wb') config_file_contents = { 'deployments': { 'named': [], 'timestamped': [] } } config_file.write(json.dumps(config_file_contents)) config_file.close() ts = time.time() if not config.is_windows: timestamp = datetime.datetime.fromtimestamp(ts).strftime( '%Y-%m-%d %H:%M:%S') else: timestamp = datetime.datetime.fromtimestamp(ts).strftime( '%Y-%m-%d %H %M %S') if deploy_name: if os.path.isdir( os.path.join(config.project.location, "deploy", destination['username'], deploy_name)): shutil.rmtree( os.path.join(config.project.location, "deploy", destination['username'], deploy_name)) os.makedirs( os.path.join(config.project.location, "deploy", destination['username'], deploy_name)) util.extract_base64_encoded_zip( deploy_metadata.zipFile, os.path.join(config.project.location, "deploy", destination['username'], deploy_name)) config_file_json = util.parse_json_from_file( os.path.join(config.project.location, "deploy", '.config')) named_deployment = { 'destination': destination['username'], 'name': deploy_name, 'timestamp': timestamp, 'id': util.get_random_string(30), 'package': os.path.join(config.project.location, "deploy", destination['username'], deploy_name, 'unpackaged', 'package.xml') } config_file_json['deployments']['named'].append( named_deployment) config_file = open( os.path.join(config.project.location, "deploy", '.config'), 'wb') config_file.write(json.dumps(config_file_json)) config_file.close() else: os.makedirs( os.path.join(config.project.location, "deploy", destination['username'], timestamp)) util.extract_base64_encoded_zip( deploy_metadata.zipFile, os.path.join(config.project.location, "deploy", destination['username'], timestamp)) config_file_json = util.parse_json_from_file( os.path.join(config.project.location, "deploy", '.config')) timestamped_deployment = { 'destination': destination['username'], 'timestamp': timestamp, 'id': util.get_random_string(30), 'package': os.path.join(config.project.location, "deploy", destination['username'], timestamp, 'unpackaged', 'package.xml') } config_file_json['deployments']['timestamped'].append( timestamped_deployment) config_file = open( os.path.join(config.project.location, "deploy", '.config'), 'wb') config_file.write(json.dumps(config_file_json)) config_file.close() thread = DeploymentHandler(config.project, destination, self.params, deploy_metadata) threads.append(thread) thread.start() deploy_results = [] for thread in threads: thread.join() deploy_results.append(thread.result) if self.args.respond_with_html == True: html = util.generate_html_response(self.args.operation, deploy_results, self.params) response = json.loads(util.generate_success_response(html, "html")) response['deploy_success'] = True # if deployment to one org fails, the entire deploy was not successful for result in deploy_results: if result['success'] == False: response['deploy_success'] = False break return json.dumps(response) else: return json.dumps(deploy_results, index=4)
def get_plugin_client_settings(): settings = {} settings['default'] = util.parse_json_from_file(os.path.join(os.path.dirname(__file__),"default_client_settings.json")) settings['user'] = util.parse_json_from_file(os.path.join(os.path.dirname(__file__),"user_client_settings.json")) return settings
def execute(self): archive_deployments = config.connection.get_plugin_client_setting("mm_archive_deployments", True) finish_deploy = self.params.get('finish', False) compare = config.connection.get_plugin_client_setting("mm_compare_before_deployment", True) destinations = self.params['destinations'] deploy_metadata = config.sfdc_client.retrieve(package=self.params['package']) deploy_name = self.params.get('new_deployment_name', None) threads = [] if not finish_deploy and compare: source_retrieve_result = config.sfdc_client.retrieve(package=self.params['package']) debug('source_retrieve_result') debug(source_retrieve_result) source_dict = {} for fp in source_retrieve_result.fileProperties: source_dict[fp.fileName] = fp debug('source_dict') debug(source_dict) #need to compare package.xml to destination orgs here for destination in destinations: thread = CompareHandler(config.project, destination, self.params, self.params['package']) threads.append(thread) thread.start() compare_results = [] for thread in threads: thread.join() compare_results.append(thread.result) debug('compare_results') debug(compare_results) destination_dict = {} for cr in compare_results: cr_dict = {} for fpfp in cr.fileProperties: cr_dict[fpfp.fileName] = fpfp destination_dict[cr.username] = cr_dict debug('destination_dict') debug(destination_dict) final_compare_result = {} for d in destinations: final_compare_result[d['username']] = {} for file_name, file_details in source_dict.iteritems(): if 'package.xml' in file_name: continue; for username, username_value in destination_dict.iteritems(): destination_retrieve_details = destination_dict[username] if 'package.xml' in file_name: continue short_file_name = file_name.split('/')[-1] mtype = util.get_meta_type_by_suffix(short_file_name.split('.')[-1]) if file_name not in destination_retrieve_details: final_compare_result[username][file_name] = { 'name' : short_file_name, 'type' : mtype['xmlName'], 'action': 'insert', 'message' : 'Create' } else: destination_file_detail = destination_retrieve_details[file_name] source_file_detail = source_dict[file_name] if source_file_detail.lastModifiedDate >= destination_file_detail.lastModifiedDate: final_compare_result[username][file_name] = { 'name' : short_file_name, 'type' : mtype['xmlName'], 'action' : 'update', 'message' : 'You will overwrite this file' } else: final_compare_result[username][file_name] = { 'name' : short_file_name, 'type' : mtype['xmlName'], 'action' : 'update_conflict', 'message' : 'Destination file is newer than source file' } # final_compare_result = {} # for d in destinations: # final_compare_result[d['username']] = {} # for username, username_value in destination_dict.iteritems(): # #destination_dict = destination_dict[username] # for file_name, file_details in username_value.iteritems(): # if 'package.xml' in file_name: # continue; # short_file_name = file_name.split('/')[-1] # mtype = util.get_meta_type_by_suffix(short_file_name.split('.')[-1]) # if file_name not in source_dict: # final_compare_result[username][file_name] = { # 'name' : short_file_name, # 'type' : mtype['xmlName'], # 'action': 'insert', # 'message' : 'Create' # } # else: # destination_file_detail = username_value[file_name] # source_file_detail = source_dict[file_name] # if source_file_detail.lastModifiedDate >= destination_file_detail.lastModifiedDate: # final_compare_result[username][file_name] = { # 'name' : short_file_name, # 'type' : mtype['xmlName'], # 'action' : 'update', # 'message' : 'You will overwrite this file' # } # else: # final_compare_result[username][file_name] = { # 'name' : short_file_name, # 'type' : mtype['xmlName'], # 'action' : 'update_conflict', # 'message' : 'Destination file is newer than source file' # } debug('final_compare_result') debug(final_compare_result) if self.args.respond_with_html == True: html = util.generate_html_response('deploy_compare', final_compare_result, self.params) response = json.loads(util.generate_success_response(html, "html")) response['compare_success'] = True # if deployment to one org fails, the entire deploy was not successful # for result in final_compare_result: # if result['success'] == False: # response['compare_success'] = False # break return json.dumps(response) else: return json.dumps(final_compare_result,indent=4) for destination in destinations: if archive_deployments: deploy_path = os.path.join(config.project.location,"deploy",destination['username']) if not os.path.exists(deploy_path): os.makedirs(deploy_path) if not os.path.isfile(os.path.join(config.project.location,"deploy",'.config')): config_file = open(os.path.join(config.project.location,"deploy",'.config'), 'wb') config_file_contents = { 'deployments' : { 'named' : [], 'timestamped' : [] } } config_file.write(json.dumps(config_file_contents)) config_file.close() ts = time.time() if not config.is_windows: timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') else: timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H %M %S') if deploy_name: if os.path.isdir(os.path.join(config.project.location,"deploy",destination['username'],deploy_name)): shutil.rmtree(os.path.join(config.project.location,"deploy",destination['username'],deploy_name)) os.makedirs(os.path.join(config.project.location,"deploy",destination['username'],deploy_name)) util.extract_base64_encoded_zip(deploy_metadata.zipFile, os.path.join(config.project.location,"deploy",destination['username'],deploy_name)) config_file_json = util.parse_json_from_file(os.path.join(config.project.location,"deploy",'.config')) named_deployment = { 'destination' : destination['username'], 'name' : deploy_name, 'timestamp' : timestamp, 'id' : util.get_random_string(30), 'package' : os.path.join(config.project.location,"deploy",destination['username'],deploy_name,'unpackaged','package.xml') } config_file_json['deployments']['named'].append(named_deployment) config_file = open(os.path.join(config.project.location,"deploy",'.config'), 'wb') config_file.write(json.dumps(config_file_json)) config_file.close() else: os.makedirs(os.path.join(config.project.location,"deploy",destination['username'],timestamp)) util.extract_base64_encoded_zip(deploy_metadata.zipFile, os.path.join(config.project.location,"deploy",destination['username'],timestamp)) config_file_json = util.parse_json_from_file(os.path.join(config.project.location,"deploy",'.config')) timestamped_deployment = { 'destination' : destination['username'], 'timestamp' : timestamp, 'id' : util.get_random_string(30), 'package' : os.path.join(config.project.location,"deploy",destination['username'],timestamp,'unpackaged','package.xml') } config_file_json['deployments']['timestamped'].append(timestamped_deployment) config_file = open(os.path.join(config.project.location,"deploy",'.config'), 'wb') config_file.write(json.dumps(config_file_json)) config_file.close() thread = DeploymentHandler(config.project, destination, self.params, deploy_metadata) threads.append(thread) thread.start() deploy_results = [] for thread in threads: thread.join() deploy_results.append(thread.result) if self.args.respond_with_html == True: html = util.generate_html_response(self.args.operation, deploy_results, self.params) response = json.loads(util.generate_success_response(html, "html")) response['deploy_success'] = True # if deployment to one org fails, the entire deploy was not successful for result in deploy_results: if result['success'] == False: response['deploy_success'] = False break return json.dumps(response) else: return json.dumps(deploy_results,index=4)
def execute(self): project = config.project sfdc_client = config.sfdc_client if "files" in self.params: if "type" in self.params: open_type = self.params.get("type", None) else: open_type = "edit" files = self.params.get("files", None) if len(files) > 0: apex_file_properties = util.parse_json_from_file(os.path.join(project.location,"config",".local_store")) opened = [] for fileabs in files: basename = os.path.basename(fileabs) if basename not in apex_file_properties: # make sure we have meta data and then get the object type if os.path.isfile(fileabs+"-meta.xml"): xmldoc = minidom.parse(fileabs+"-meta.xml") root = xmldoc.firstChild object_type = root.nodeName else: continue object_id = sfdc_client.get_apex_entity_id_by_name(object_type=object_type, name=basename) if not object_id: continue else: props = apex_file_properties[basename] object_type = props['type'] object_id = props['id'] # only ApexClasses that are global and have webservice scope have WSDL files if open_type == "wsdl": if object_type != "ApexClass": continue with open(fileabs, 'r') as content_file: content = content_file.read() p = re.compile("global\s+(abstract\s+)?class\s", re.I + re.M) if not p.search(content): continue p = re.compile("\swebservice\s", re.I + re.M) if not p.search(content): continue # get the server instance url and set the redirect url frontdoor = "https://" + sfdc_client.server_url.split('/')[2] + "/secur/frontdoor.jsp?sid=" + sfdc_client.sid + "&retURL=" if open_type == "wsdl": f, e = os.path.splitext(basename) ret_url = "/services/wsdl/class/" + f else: f, ext = os.path.splitext(basename) if object_type == "CustomObject" and not f.endswith('__c'): # standard object? ret_url = "/p/setup/layout/LayoutFieldList?type=" + f + "%23CustomFieldRelatedList_target" else: ret_url = "/" + object_id # open the browser window for this file and track it webbrowser.open(frontdoor+ret_url, new=2) opened.append(basename) if len(opened) == 0: return util.generate_error_response("There were no valid files to open.") return util.generate_success_response("Opened "+(", ".join(opened))+" on server.") return util.generate_error_response("Unable to open file on server.") else: raise MMException("To open on Salesforce, you must provide an array of 'files'")