-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm.py
executable file
·279 lines (237 loc) · 12 KB
/
mm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python
import os.path
import sys
import argparse
import traceback
import inspect
import json
import lib.config as config
import lib.mm_util as util
import time #TODO: remove
import urllib
from suds.client import Client
from lib.mm_connection import MavensMatePluginConnection
from lib.mm_client import MavensMateClient
from lib.mm_exceptions import MMException
request_payload = util.get_request_payload()
#config.logger.debug('\n\n\n>>>>>>>>\nhandling request with payload >>>>>')
#config.logger.debug(request_payload)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--operation') #name of the operation being requested
parser.add_argument('-c', '--client') #name of the plugin client ("SUBLIME_TEXT_2", "SUBLIME_TEXT_3", "TEXTMATE", "NOTEPAD_PLUS_PLUS", "BB_EDIT", etc.)
parser.add_argument('-p', '--projectname') #name of the project
parser.add_argument('-d', '--projectdirectory') #name of the project
parser.add_argument('--callback') #some terminal script to run upon completion of a command
parser.add_argument('--ui', action='store_true', default=False,
dest='ui_switch', help='Include flag to launch the default UI for the operation')
parser.add_argument('--html', action='store_true', default=False,
dest='respond_with_html', help='Include flag if you want the response in HTML')
args = parser.parse_args()
operation = args.operation
try:
setup_connection(args)
except Exception as e:
print util.generate_error_response(e.message)
return
#if the arg switch argument is included, the request is to launch the out of box
#MavensMate UI, so we generate the HTML for the UI and launch the process
#example: mm -o new_project --ui
if args.ui_switch == True:
#os.system('killAll MavensMateWindowServer') #TODO: try/except?
tmp_html_file = util.generate_ui(operation,request_payload)
util.launch_ui(tmp_html_file)
print util.generate_success_response('UI Generated Successfully')
else:
requested_function = operation_dict[operation]
fspec = inspect.getargspec(requested_function)
if type(fspec.args) is list and len(fspec.args) == 1 and fspec.args[0] == 'args':
requested_function(eval(fspec.args[0]))
elif type(fspec.args) is list and len(fspec.args) > 0:
print util.generate_error_response('Invalid operation requested')
else:
requested_function()
if args.callback != None:
os.system(args.callback)
# each operation sets up a single connection
# the connection holds information about the plugin running it
# and establishes a project object
def setup_connection(args):
if 'project_name' in request_payload or 'project_directory' in request_payload:
#project_name = request_payload.get('project_name', args.projectname)
#project_directory = request_payload.get('project_directory', args.projectdirectory)
config.connection = MavensMatePluginConnection(
client=args.client or 'SUBLIME_TEXT_2',
ui=args.ui_switch,
params=request_payload,
operation=args.operation)
else:
config.connection = MavensMatePluginConnection(
client=args.client or 'SUBLIME_TEXT_2',
params=request_payload,
operation=args.operation)
# echo '{ "username" : "", "password" : "", "metadata_type" : "ApexClass" ' | joey2 mavensmate.py -o 'list_metadata'
def list_metadata():
client = MavensMateClient(credentials={
"sid" : request_payload.get('sid', None),
"metadata_server_url" : urllib.unquote(request_payload.get('metadata_server_url', None)),
"server_url" : urllib.unquote(request_payload.get('server_url', None)),
})
print json.dumps(client.list_metadata(request_payload['metadata_type']))
def open_sfdc_url():
print config.connection.project.open_selected_metadata(request_payload);
def list_connections():
print config.connection.project.get_org_connections()
def new_connection():
print config.connection.project.new_org_connection(request_payload)
def delete_connection():
print config.connection.project.delete_org_connection(request_payload)
def compile_selected_metadata():
print config.connection.project.compile_selected_metadata(request_payload)
def delete_selected_metadata():
print config.connection.project.delete_selected_metadata(request_payload)
def index_metadata(args):
if 'metadata_type' in request_payload:
index_result = config.connection.project.index_metadata(request_payload['metadata_type'])
else:
index_result = config.connection.project.index_metadata()
if args.respond_with_html == True:
html = util.generate_html_response(args.operation, index_result, request_payload)
print util.generate_success_response(html, "html")
else:
print util.generate_success_response(index_result)
def get_metadata_index():
print config.connection.project.get_org_metadata(None, True)
def new_project():
print config.connection.new_project(request_payload,action='new')
def new_project_from_existing_directory():
print config.connection.new_project(request_payload,action='existing')
def edit_project():
print config.connection.project.edit(request_payload)
def upgrade_project():
print config.connection.project.upgrade()
def checkout_project():
print config.connection.new_project(request_payload,action='checkout')
def compile_project():
print config.connection.project.compile()
def clean_project():
print config.connection.project.clean()
def synchronize():
print config.connection.project.synchronize_selected_metadata(request_payload)
def refresh():
print config.connection.project.refresh_selected_metadata(request_payload)
def refresh_properties():
config.connection.project.refresh_selected_properties(request_payload)
print util.generate_success_response("Refreshed Apex file properties.")
def new_metadata():
print config.connection.project.new_metadata(request_payload)
def execute_apex():
print config.connection.project.execute_apex(request_payload)
def fetch_logs():
print config.connection.project.fetch_logs(request_payload)
# echo '{ "project_name" : "bloat", "classes" : [ "MyTester" ] }' | joey2 mavensmate.py -o 'test'
def run_unit_tests(args):
test_result = config.connection.project.run_unit_tests(request_payload)
if args.respond_with_html == True:
html = util.generate_html_response(args.operation, test_result, request_payload)
print util.generate_success_response(html, "html")
else:
print test_result
def deploy_to_server(args):
deploy_result = config.connection.project.deploy_to_server(request_payload)
if args.respond_with_html == True:
html = util.generate_html_response(args.operation, deploy_result, request_payload)
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_result:
if result['success'] == False:
response['deploy_success'] = False
break
print json.dumps(response)
else:
print deploy_result
# echo '{ "username" : "mm@force.com", "password" : "force", "org_type" : "developer" }' | joey2 mavensmate.py -o 'get_active_session'
def get_active_session():
try:
if 'username' not in request_payload or request_payload['username'] == None or request_payload['username'] == '':
raise MMException('Please enter a Salesforce.com username')
if 'password' not in request_payload or request_payload['password'] == None or request_payload['password'] == '':
raise MMException('Please enter a Salesforce.com password')
if 'org_type' not in request_payload or request_payload['org_type'] == None or request_payload['org_type'] == '':
raise MMException('Please select an org type')
client = MavensMateClient(credentials={
"username" : request_payload['username'],
"password" : request_payload['password'],
"org_type" : request_payload['org_type']
})
response = {
"sid" : client.sid,
"user_id" : client.user_id,
"metadata_server_url" : client.metadata_server_url,
"server_url" : client.server_url,
"metadata" : client.get_org_metadata(),
"success" : True
}
print util.generate_response(response)
except BaseException, e:
print util.generate_error_response(e.message)
def index_apex_overlays():
print config.connection.project.index_apex_overlays(request_payload)
def new_apex_overlay():
print config.connection.project.new_apex_overlay(request_payload)
def delete_apex_overlay():
print config.connection.project.delete_apex_overlay(request_payload)
def update_credentials():
try:
config.connection.project.username = request_payload['username']
config.connection.project.password = request_payload['password']
config.connection.project.org_type = request_payload['org_type']
config.connection.project.update_credentials()
print util.generate_success_response('Your credentials were updated successfully')
except BaseException, e:
print util.generate_error_response(e.message)
def get_symbol_table():
print config.connection.project.get_symbol_table(request_payload)
def index_apex_file_properties():
#print util.generate_error_response("Operation not currently supported")
print config.connection.project.index_apex_file_properties()
operation_dict = {
'new_project' : new_project,
'edit_project' : edit_project,
'upgrade_project' : upgrade_project,
'checkout_project' : checkout_project,
'compile_project' : compile_project,
'new_metadata' : new_metadata,
'synchronize' : synchronize,
'refresh' : refresh,
'clean_project' : clean_project,
'refresh_properties' : refresh_properties,
'compile' : compile_selected_metadata,
'delete' : delete_selected_metadata,
'get_active_session' : get_active_session,
'update_credentials' : update_credentials,
'execute_apex' : execute_apex,
'deploy_to_server' : deploy_to_server,
'deploy' : deploy_to_server,
'unit_test' : run_unit_tests,
'test' : run_unit_tests,
'list_metadata' : list_metadata,
'index_metadata' : index_metadata,
'get_indexed_metadata' : get_metadata_index,
'list_connections' : list_connections,
'new_connection' : new_connection,
'delete_connection' : delete_connection,
'index_apex_overlays' : index_apex_overlays,
'new_apex_overlay' : new_apex_overlay,
'delete_apex_overlay' : delete_apex_overlay,
'fetch_logs' : fetch_logs,
'new_project_from_existing_directory' : new_project_from_existing_directory,
'open_sfdc_url' : open_sfdc_url,
'get_symbols' : get_symbol_table,
'index_apex_file_properties' : index_apex_file_properties,
'index_apex' : index_apex_file_properties
#'debug_log' : todo
}
if __name__ == '__main__':
main()