def get_plugin_client_settings(self): user_path = self.get_plugin_settings_path("User") def_path = self.get_plugin_settings_path("MavensMate") settings = {} workspace = self.params.get('workspace', None) if self.project_name != None and workspace != None: try: settings['project'] = util.parse_json_from_file( os.path.join(workspace, self.project_name, self.project_name + '.sublime-settings')) except: debug('Project settings could not be loaded') if not user_path == None: try: settings['user'] = util.parse_json_from_file(user_path) except: debug('User settings could not be loaded') if not def_path == None: try: settings['default'] = util.parse_json_from_file(def_path) except: raise MMException( 'Could not load default MavensMate settings.') if settings == {}: raise MMException( 'Could not load MavensMate settings. Please ensure they contain valid JSON' ) return settings
def get_plugin_client_settings(self): settings = {} user_path = self.get_plugin_settings_path("User") def_path = self.get_plugin_settings_path("MavensMate") ''' if the default path for settings is none, we're either dealing with a bad client setup or a new client like Atom.io. Let's load the settings from the default cache and optionally allow them to pipe settings in via STDIN ''' if def_path == None: if 'ATOM' in self.plugin_client: file_name = 'atom' elif 'SUBLIME_TEXT' in self.plugin_client: file_name = 'st3' elif 'BRACKETS' in self.plugin_client: file_name = 'brackets' settings['default'] = util.parse_json_from_file(config.base_path + "/lib/config/" + file_name + ".json") if config.plugin_client_settings != None: settings['user'] = config.plugin_client_settings else: workspace = self.params.get('workspace', None) if self.project_name != None and workspace != None: try: settings['project'] = util.parse_json_from_file( os.path.join(workspace, self.project_name, self.project_name + '.sublime-settings')) except: debug('Project settings could not be loaded') if not user_path == None: try: settings['user'] = util.parse_json_from_file(user_path) except: debug('User settings could not be loaded') if not def_path == None: try: settings['default'] = util.parse_json_from_file(def_path) except: raise MMException( 'Could not load default MavensMate settings.') if settings == {}: raise MMException( 'Could not load MavensMate settings. Please ensure they contain valid JSON' ) return settings
def get_password_by_key(key): if sys.platform == 'linux2': try: items = gnomekeyring.find_network_password_sync(key, 'MavensMate: '+key) return items[0]['password'] except gnomekeyring.CancelledError: raise MMException('Unable to retrieve password') else: return keyring.get_password('MavensMate: '+key, key)
def put_password_by_key(key, password): if sys.platform == 'linux2': try: gnomekeyring.set_network_password_sync(None, key, 'MavensMate: '+key, None, None, None, None, 0, password) except gnomekeyring.CancelledError: raise MMException('Unable to set password') else: keyring.set_password('MavensMate: '+key, key, password)
def get_workspace(self): mm_workspace_path = None mm_workspace_setting = self.get_plugin_client_setting('mm_workspace') if type(mm_workspace_setting) is list and len( mm_workspace_setting) > 0: mm_workspace_path = mm_workspace_setting[0] #grab the first path else: mm_workspace_path = mm_workspace_setting #otherwise, it's a string, set it if mm_workspace_path == None or mm_workspace_path == '': raise MMException( "Please set mm_workspace to the location where you'd like your mavensmate projects to reside" ) elif not os.path.exists(mm_workspace_path): try: os.makedirs(mm_workspace_path) except: raise MMException("Unable to create mm_workspace location") return mm_workspace_path
def get_workspaces(self): workspaces = [] mm_workspace_setting = self.get_plugin_client_setting('mm_workspace') if type(mm_workspace_setting) is list and len( mm_workspace_setting) == 0: raise MMException("mm_workspace not properly set") if type(mm_workspace_setting) is list and len( mm_workspace_setting) > 0: workspaces = mm_workspace_setting else: workspaces = [mm_workspace_setting] return workspaces
def get_password_by_key(key): use_keyring = config.connection.get_plugin_client_setting('mm_use_keyring', False) if use_keyring: if sys.platform == 'linux2': try: items = gnomekeyring.find_network_password_sync(key, 'MavensMate: '+key) return items[0]['password'] except gnomekeyring.CancelledError: raise MMException('Unable to retrieve password') else: return keyring.get_password('MavensMate: '+key, key) else: #not recommend! we only use this for CI file_body = get_file_as_string(os.path.join(config.connection.get_app_settings_directory(),key+'.json')) file_body_json = json.loads(file_body) return decode(str(key), str(file_body_json['value']))
def put_password_by_key(key, password): use_keyring = config.connection.get_plugin_client_setting('mm_use_keyring', False) if use_keyring: if sys.platform == 'linux2': try: gnomekeyring.set_network_password_sync(None, key, 'MavensMate: '+key, None, None, None, None, 0, password) except gnomekeyring.CancelledError: raise MMException('Unable to set password') else: keyring.set_password('MavensMate: '+key, key, password) else: #not recommend! we only use this for CI encoded = encode(key, password) src = open(os.path.join(config.connection.get_app_settings_directory(),key+'.json'), "wb") src.write(json.dumps({'value':encoded})) src.close()
def get_plugin_client_settings(self): user_path = self.get_plugin_settings_path("User") def_path = self.get_plugin_settings_path("MavensMate") settings = {} if not user_path == None: try: settings['user'] = util.parse_json_from_file(user_path) except: debug('User settings could not be loaded') if not def_path == None: try: settings['default'] = util.parse_json_from_file(def_path) except: raise MMException( 'Could not load default MavensMate settings.') return settings
def run_subl_command(self, command, params): if self.plugin_client != self.PluginClients.SUBLIME_TEXT_3: raise MMException('unsupported operation') if sys.platform == 'darwin': client_location = self.get_plugin_client_setting( 'mm_plugin_client_location') plugin_app_name = self.get_plugin_client_setting( 'mm_osx_plugin_client_app_name') if client_location == None: client_location = '/Applications' if plugin_app_name == None: plugin_app_name = 'Sublime Text 3.app' if os.path.exists( os.path.join('{0}/{1}'.format(client_location, plugin_app_name))): os.system( "'{0}/{1}/Contents/SharedSupport/bin/subl' --command '{2} {3}'" .format(client_location, plugin_app_name, command, params)) elif os.path.exists( os.path.join( '{0}/Sublime Text 3.app'.format(client_location))): os.system( "'{0}/Sublime Text 3.app/Contents/SharedSupport/bin/subl' --command '{1} {2}'" .format(client_location, command, params)) else: os.system( "'{0}/Sublime Text.app/Contents/SharedSupport/bin/subl' --command '{1} {2}'" .format(client_location, command, params)) elif 'linux' in sys.platform: subl_location = self.get_plugin_client_setting( 'mm_subl_location', '/usr/local/bin/subl') os.system("'{0}' --command '{1} {2}'".format( subl_location, os.path.join(self.project.location, command, params))) else: subl_location = self.get_plugin_client_setting( 'mm_windows_subl_location') if not os.path.isfile( subl_location) and "x86" not in subl_location: subl_location = subl_location.replace("Program Files", "Program Files (x86)") params = params.replace('"', '\"') cmd = '"{0}" --command "{1} {2}"'.format( subl_location, os.path.join(self.project.location, command, params)) subprocess.call(cmd)
def generate_ui(operation,params={}): template_path = config.base_path + "/lib/ui/templates" env = Environment(loader=FileSystemLoader(template_path),trim_blocks=True) env.globals['platform'] = platform env.globals['play_sounds'] = play_sounds env.globals['project_settings'] = project_settings env.globals['metadata_types'] = metadata_types env.globals['client_subscription_list'] = client_subscription_list env.globals['base_local_server_url'] = base_local_server_url env.globals['operation'] = operation env.globals['project_location'] = config.project.location env.globals['base_path_normal'] = base_path_normal temp = tempfile.NamedTemporaryFile(delete=False, prefix="mm", suffix=".html") if operation == 'new_project': template = env.get_template('/project/new.html') file_body = template.render( user_action='new', workspace=config.connection.workspace, client=config.connection.plugin_client, workspaces=config.connection.get_workspaces() ).encode('UTF-8') elif operation == 'checkout_project': template = env.get_template('/project/new.html') file_body = template.render(user_action='checkout',workspace=config.connection.workspace,client=config.connection.plugin_client).encode('UTF-8') elif operation == 'upgrade_project': template = env.get_template('/project/upgrade.html') creds = config.project.get_creds() org_url = creds.get('org_url', None) if org_url == None: org_url = '' file_body = template.render( name=config.project.project_name, project_location=config.project.location, client=config.connection.plugin_client, username=creds['username'], org_type=creds['org_type'], org_url=org_url, workspace=config.connection.workspace ).encode('UTF-8') elif operation == 'edit_project': template = env.get_template('/project/edit.html') creds = config.project.get_creds() org_url = creds.get('org_url', None) if org_url == None: org_url = '' file_body = template.render( name=config.project.project_name, username=creds['username'], password=creds['password'], org_type=creds['org_type'], org_url=org_url, has_indexed_metadata=config.project.is_metadata_indexed, project_location=config.project.location, client=config.connection.plugin_client ).encode('UTF-8') elif operation == 'unit_test': if int(float(SFDC_API_VERSION)) < 29 or config.connection.get_plugin_client_setting("mm_use_legacy_test_ui", False): template = env.get_template('/unit_test/index28.html') else: template = env.get_template('/unit_test/index.html') istest = re.compile(r"@istest", re.I) testmethod = re.compile(r"testmethod", re.I) apex_classes = [] for dirname, dirnames, filenames in os.walk(config.project.location+"/src/classes"): for f in filenames: if f == "." or f == ".." or '-meta.xml' in f or ".svn" in f: continue try: full_file_path = os.path.join(dirname,f) if istest.search(open(full_file_path).read()) or testmethod.search(open(full_file_path).read()): apex_classes.append(f.split(".")[0]) except: continue if "selected" in params: selected = params["selected"] else: selected = [] file_body = template.render( name=config.project.project_name, classes=apex_classes, selected=selected, client=config.connection.plugin_client).encode('UTF-8') elif operation == 'deploy': template = env.get_template('/deploy/index.html') file_body = template.render( name=config.project.project_name, has_indexed_metadata=config.project.is_metadata_indexed, project_location=config.project.location, connections=config.project.get_org_connections(), operation=operation, client=config.connection.plugin_client).encode('UTF-8') elif operation == 'execute_apex': template = env.get_template('/execute_apex/index.html') file_body = template.render( name=config.project.project_name, project_location=config.project.location, client=config.connection.plugin_client).encode('UTF-8') elif operation == 'new_project_from_existing_directory': project_name = os.path.basename(params['directory']) template = env.get_template('/project/new_from_existing.html') file_body = template.render( project_name=project_name, directory=params['directory'], workspaces=config.connection.get_workspaces(), client=config.connection.plugin_client).encode('UTF-8') elif operation == 'debug_log': template = env.get_template('/debug_log/index.html') file_body = template.render( project_name=config.project.project_name, users=config.project.get_org_users_list(), user_id=config.sfdc_client.user_id, apex_items=config.sfdc_client.get_apex_classes_and_triggers(), #logs=config.project.get_org_logs(), client=config.connection.plugin_client).encode('UTF-8') elif operation == 'github': template = env.get_template('/github/index.html') file_body = template.render( client=config.connection.plugin_client).encode('UTF-8') elif operation == 'project_health_check': template = env.get_template('/project/health_check.html') file_body = template.render( client=config.connection.plugin_client, name=config.project.project_name ).encode('UTF-8') else: raise MMException('Unsupported UI Command') temp.write(file_body) temp.close() return temp.name
def generate_ui(operation,params={},args={}): template_path = config.base_path + "/lib/ui/templates" env = Environment(loader=FileSystemLoader(template_path),trim_blocks=True) env.globals['uid'] = args.uid env.globals['platform'] = platform env.globals['play_sounds'] = play_sounds env.globals['project_settings'] = project_settings env.globals['metadata_types'] = metadata_types env.globals['client_subscription_list'] = client_subscription_list env.globals['base_local_server_url'] = base_local_server_url env.globals['operation'] = operation env.globals['project_location'] = config.project.location env.globals['static_resource_path'] = static_resource_path env.globals['client'] = config.connection.plugin_client temp = tempfile.NamedTemporaryFile(delete=False, prefix="mm", suffix=".html") if operation == 'new_project': template = env.get_template('/project/new.html') file_body = template.render( user_action='new', workspace=config.connection.workspace, workspaces=config.connection.get_workspaces() ).encode('UTF-8') elif operation == 'upgrade_project': template = env.get_template('/project/upgrade.html') creds = config.project.get_creds() org_url = creds.get('org_url', None) if org_url == None: org_url = '' file_body = template.render( name=config.project.project_name, project_location=config.project.location, username=creds['username'], org_type=creds['org_type'], org_url=org_url, workspace=config.connection.workspace ).encode('UTF-8') elif operation == 'edit_project': template = env.get_template('/project/edit.html') creds = config.project.get_creds() org_url = creds.get('org_url', None) if org_url == None: org_url = '' file_body = template.render( name=config.project.project_name, username=creds['username'], password=creds['password'], org_type=creds['org_type'], org_url=org_url, has_indexed_metadata=config.project.is_metadata_indexed, project_location=config.project.location ).encode('UTF-8') elif operation == 'unit_test': if int(float(SFDC_API_VERSION)) < 29 or config.connection.get_plugin_client_setting("mm_use_legacy_test_ui", False): template = env.get_template('/unit_test/index28.html') else: template = env.get_template('/unit_test/index.html') istest = re.compile(r"@istest", re.I) testmethod = re.compile(r"testmethod", re.I) apex_classes = [] for dirname, dirnames, filenames in os.walk(config.project.location+"/src/classes"): for f in filenames: if f == "." or f == ".." or '-meta.xml' in f or ".svn" in f: continue try: full_file_path = os.path.join(dirname,f) if istest.search(open(full_file_path).read()) or testmethod.search(open(full_file_path).read()): apex_classes.append(f.split(".")[0]) except: continue if "selected" in params: selected = params["selected"] else: selected = [] file_body = template.render( name=config.project.project_name, classes=apex_classes, selected=selected ).encode('UTF-8') elif operation == 'deploy': compare = config.connection.get_plugin_client_setting("mm_compare_before_deployment", True) template = env.get_template('/deploy/index.html') file_body = template.render( name=config.project.project_name, has_indexed_metadata=config.project.is_metadata_indexed, project_location=config.project.location, connections=config.project.get_org_connections(), operation=operation, compare=compare, deployments=config.project.get_deployments() ).encode('UTF-8') elif operation == 'execute_apex': template = env.get_template('/execute_apex/index.html') file_body = template.render( name=config.project.project_name, project_location=config.project.location ).encode('UTF-8') elif operation == 'new_project_from_existing_directory': project_name = os.path.basename(params['directory']) template = env.get_template('/project/new_from_existing.html') file_body = template.render( project_name=project_name, directory=params['directory'], workspaces=config.connection.get_workspaces() ).encode('UTF-8') elif operation == 'debug_log': template = env.get_template('/debug_log/index.html') file_body = template.render( project_name=config.project.project_name, users=config.project.get_org_users_list(), user_id=config.sfdc_client.user_id, apex_items=config.sfdc_client.get_apex_classes_and_triggers() ).encode('UTF-8') elif operation == 'github': template = env.get_template('/github/index.html') file_body = template.render().encode('UTF-8') elif operation == 'project_health_check': template = env.get_template('/project/health_check.html') file_body = template.render( name=config.project.project_name ).encode('UTF-8') elif operation == 'new_metadata': template_source = config.connection.get_plugin_client_setting('mm_template_source', 'joeferraro/MavensMate-Templates/master') template_location = config.connection.get_plugin_client_setting('mm_template_location', 'remote') try: if template_location == 'remote': if 'linux' in sys.platform: template_package = os.popen("wget https://raw.github.com/{0}/package.json -q -O -".format(template_source)).read() else: template_package = urllib2.urlopen("https://raw.github.com/{0}/package.json".format(template_source)).read() else: template_package = get_file_as_string(os.path.join(template_source,'package.json')) except: template_package = get_file_as_string(os.path.join(config.base_path,"lib","templates","github-local","package.json")) metadata_type = params['metadata_type'] template_package_json = json.loads(template_package) template = env.get_template('/metadata/new.html') file_body = template.render( name=config.project.project_name, template_list=template_package_json[metadata_type], templates=json.dumps(template_package_json[metadata_type]) ).encode('UTF-8') else: raise MMException('Unsupported UI Command') temp.write(file_body) temp.close() return temp.name
def get_retrieve_result(self, params): if 'directories' in params and len( params['directories']) > 0 and 'files' in params and len( params['files']) > 0: raise MMException( "Please select either directories or files to refresh, not both" ) elif 'directories' in params and len(params['directories']) > 0: metadata = {} types = [] for d in params['directories']: basename = os.path.basename(d) # refresh all if it's the project base or src directory if basename == config.project.project_name or basename == "src": data = util.get_default_metadata_data() if type(data) is dict and 'metadataObjects' in data: data = data['metadataObjects'] for item in data: if 'directoryName' in item: types.append(item['xmlName']) else: metadata_type = util.get_meta_type_by_dir(basename) if metadata_type: types.append(metadata_type['xmlName']) if 'childXmlNames' in metadata_type: for child in metadata_type['childXmlNames']: types.append(child) custom_fields = [] for val in self.project.get_package_types(): package_type = val['name'] members = val['members'] if package_type not in types: continue metadata[package_type] = members if package_type == 'CustomObject': for member in members: if members == "*": for item in self.project.get_org_metadata(): if item['xmlName'] == 'CustomObject': for child in item['children']: if not child['title'].endswith("__c"): for props in child['children']: if props['title'] == 'fields': for field in props[ 'children']: custom_fields.append( child['title'] + '.' + field['title']) break if member != "*": break break if len(custom_fields): if 'CustomField' not in metadata: metadata['CustomField'] = [] metadata['CustomField'] = list( set(metadata['CustomField'] + custom_fields)) if len(metadata) == 0: raise MMException("Could not find metadata types to refresh") elif 'files' in params and len(params['files']) > 0: metadata = util.get_metadata_hash(params['files']) else: raise MMException( "Please provide either an array of 'directories' or an array of 'files'" ) #retrieves a fresh set of metadata based on the files that have been requested retrieve_result = self.project.sfdc_client.retrieve(package=metadata) return retrieve_result
def __init__(self, params={}, **kwargs): params = dict(params.items() + kwargs.items()) #join params and kwargs self.params = params self.operation = params.get('operation', None) self.args = params.get('args', None) self.plugin_client = params.get( 'client', 'SUBLIME_TEXT_3') #=> "Sublime Text", "Notepad++", "TextMate" if self.plugin_client not in self.currently_supported_clients: self.plugin_client = 'SUBLIME_TEXT_3' self.project_name = params.get('project_name', None) self.project_location = params.get('project_location', None) self.plugin_client_settings = self.get_plugin_client_settings() if self.project_location == None: self.workspace = params.get('workspace', self.get_workspace()) else: self.workspace = os.path.dirname(self.project_location) if self.project_name != None and self.project_location == None: self.project_location = os.path.join(self.workspace, self.project_name) self.project_id = params.get('project_id', None) self.project = None self.sfdc_api_version = self.get_sfdc_api_version() self.ui = params.get( 'ui', False ) #=> whether this connection was created for the purposes of generating a UI self.verbose = params.get('verbose', False) if 'wsdl_path' in params: util.WSDL_PATH = params.get('wsdl_path') self.setup_logging() if self.get_plugin_client_setting('mm_timeout', None) != None: socket.setdefaulttimeout( self.get_plugin_client_setting('mm_timeout')) debug('') debug('--------------------------------------------') debug('---------- NEW OPERATION REQUESTED ---------') debug('--------------------------------------------') debug('') debug(self.operation) debug(self.args) debug(params) debug('') debug('--------------------------------------------') if self.sfdc_api_version != None: util.SFDC_API_VERSION = self.sfdc_api_version #setting api version based on plugin settings util.set_endpoints() if self.operation != 'new_project' and self.operation != 'upgrade_project' and self.operation != 'new_project_from_existing_directory' and self.project_location != None: if not os.path.exists(os.path.join(self.project_location)): raise MMException('Could not find project in workspace: ' + self.workspace) if not os.path.exists( os.path.join(self.project_location, "config", ".settings")): raise MMException( 'This does not seem to be a valid MavensMate project, missing config/.settings' ) #if not os.path.exists(os.path.join(self.project_location,"src","package.xml")): # raise MMException('This does not seem to be a valid MavensMate project, missing package.xml') if self.project_name != None and self.project_name != '' and not os.path.exists( self.project_location ) and self.operation != 'new_project_from_existing_directory' and self.operation != 'new_project': raise MMException('The project could not be found') elif self.project_name != None and self.project_name != '' and os.path.exists( os.path.join( self.workspace, self.project_name)) and self.operation == 'new_project': raise MMException( 'A project with this name already exists in your workspace. To create a MavensMate project from an existing non-MavensMate Force.com project, open the project directory in Sublime Text, right click the project name in the sidebar and select "Create MavensMate Project"' ) elif self.project_name != None and self.project_name != '' and os.path.exists( os.path.join(self.workspace, self.project_name) ) and self.operation != 'new_project_from_existing_directory': params['location'] = self.project_location params['ui'] = self.ui