10
10
import os
11
11
import re
12
12
import sys
13
+ import tempfile
13
14
import types
14
15
16
+ repo = Repo ()
17
+ assert (repo .bare == False )
18
+ git = repo .git
15
19
16
20
LEETCODE_URL_BASE = 'https://leetcode.com/problems'
17
21
GITHUB_URL_BASE = 'https://github.com/cfriedt/leetcode'
18
22
SPDX_LICENSE_IDENTIFIER = 'MIT'
19
- COPYRIGHT_HOLDER = 'Christopher Friedt'
23
+ GIT_USER_NAME = git .config ('user.name' )
24
+ GIT_USER_EMAIL = git .config ('user.email' )
20
25
21
26
NAME_RE = re .compile (r'^[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]+$' )
22
27
@@ -127,7 +132,7 @@ def create_impl_and_test(self):
127
132
128
133
# write the copyright and license info in SPDX format
129
134
f .write ('/*\n ' )
130
- f .write (' * Copyright (c) {}, {}\n ' .format (year , COPYRIGHT_HOLDER ))
135
+ f .write (' * Copyright (c) {}, {}\n ' .format (year , GIT_USER_NAME ))
131
136
f .write (' *\n ' )
132
137
f .write (
133
138
' * SPDX-License-Identifier: {}\n ' .format (SPDX_LICENSE_IDENTIFIER ))
@@ -159,7 +164,7 @@ def create_impl_and_test(self):
159
164
160
165
# write the copyright and license info in SPDX format
161
166
f .write ('/*\n ' )
162
- f .write (' * Copyright (c) {}, {}\n ' .format (year , COPYRIGHT_HOLDER ))
167
+ f .write (' * Copyright (c) {}, {}\n ' .format (year , GIT_USER_NAME ))
163
168
f .write (' *\n ' )
164
169
f .write (
165
170
' * SPDX-License-Identifier: {}\n ' .format (SPDX_LICENSE_IDENTIFIER ))
@@ -195,9 +200,6 @@ def create_impl_and_test(self):
195
200
raise
196
201
197
202
def checkout_branch (self , create = True ):
198
- repo = Repo (os .getcwd ())
199
- git = repo .git
200
-
201
203
try :
202
204
# stash any uncommitted changes
203
205
git .add ('*' )
@@ -206,14 +208,59 @@ def checkout_branch(self, create=True):
206
208
pass
207
209
208
210
git .checkout ('master' )
209
- git .fetch ('-p' )
210
- git .pull ()
211
+
212
+ in_ci = False
213
+ try :
214
+ in_ci = os .environ ['CI' ]
215
+ except :
216
+ pass
217
+
218
+ if not in_ci :
219
+ git .fetch ()
220
+ git .pull ()
211
221
212
222
if create :
213
223
git .checkout ('-b' , self ._branch )
214
224
else :
215
225
git .checkout (self ._branch )
216
226
227
+ def create_commit_message (self ):
228
+ msg = '''\
229
+ Implement {}
230
+
231
+ name: {}
232
+ url: {}
233
+ difficulty: {}
234
+
235
+ time: {} ms
236
+ time-rank: {} %
237
+ time-complexity: {}
238
+
239
+ space: {} MB
240
+ space-rank: {} %
241
+ space-complexity: {}
242
+
243
+ Fixes #{}
244
+
245
+ Signed-off-by: {} <{}>
246
+ ''' .format (self ._name , self ._name , self ._url , self ._difficulty , self ._time , self ._time_rank , self ._time_complexity , self ._space , self ._space_rank , self ._space_complexity , self ._issue , GIT_USER_NAME , GIT_USER_EMAIL )
247
+ return msg
248
+
249
+
250
+ def new_problem_args (name , issue , difficulty , tmpl ):
251
+ args = Problem .default_args ()
252
+ args .name = name
253
+ args .issue = issue
254
+ args .difficulty = difficulty
255
+ args .problem_template = tmpl
256
+
257
+ p = Problem (args )
258
+ p .checkout_branch ()
259
+ p .create_impl_and_test ()
260
+
261
+ return p
262
+
263
+
217
264
def new_problem ():
218
265
args = Problem .default_args ()
219
266
@@ -240,8 +287,92 @@ def new_problem():
240
287
break
241
288
args .problem_template += line
242
289
243
- p = Problem (args )
244
- p .checkout_branch ()
245
- p .create_impl_and_test ()
290
+ p = new_problem_args (args .name , args .issue ,
291
+ args .difficulty , args .problem_template )
246
292
247
293
return p
294
+
295
+
296
+ def commit_solution_args (issue , name , difficulty , time , time_rank , time_complexity , space , space_rank , space_complexity ):
297
+
298
+ args = Problem .default_args ()
299
+ args .issue = issue
300
+ args .name = name
301
+ args .difficulty = difficulty
302
+ args .time = time
303
+ args .time_rank = time_rank
304
+ args .time_complexity = time_complexity
305
+ args .space = space
306
+ args .space_rank = space_rank
307
+ args .space_complexity = space_complexity
308
+
309
+ p = Problem (args )
310
+
311
+ msg = p .create_commit_message ()
312
+ f = tempfile .NamedTemporaryFile (mode = 'w' , delete = False )
313
+ try :
314
+ f .write (msg )
315
+ f .close ()
316
+ git .commit ('-F' , f .name )
317
+ os .remove (f .name )
318
+ except Exception as e :
319
+ try :
320
+ os .remove (f .name )
321
+ except :
322
+ pass
323
+ raise e
324
+
325
+
326
+ def commit_solution ():
327
+ args = Problem .default_args ()
328
+
329
+ branch = git .rev_parse ('--abbrev-ref' , 'HEAD' )
330
+
331
+ if not branch .startswith ('issue/' ):
332
+ raise ValueError ('invalid branch name "{}"' .format (branch ))
333
+
334
+ if len (branch .split ('/' )) != 3 :
335
+ raise ValueError ('invalid branch name "{}"' .format (branch ))
336
+
337
+ args .issue = branch .split ('/' )[1 ]
338
+ args .name = branch .split ('/' )[2 ]
339
+
340
+ impl_cpp = args .name + '.cpp'
341
+
342
+ with open (impl_cpp ) as f :
343
+ for line in f .readlines ():
344
+ if line .startswith ('// difficulty: ' ):
345
+ args .difficulty = line .replace ('// difficulty: ' , '' )
346
+
347
+ print ('Enter the time (ms): ' , end = '' , flush = True )
348
+ for line in sys .stdin :
349
+ args .time = int (line .rstrip ())
350
+ break
351
+
352
+ print ('Enter the time-rank (%): ' , end = '' , flush = True )
353
+ for line in sys .stdin :
354
+ args .time = float (line .rstrip ())
355
+ break
356
+
357
+ print ('Enter the time-complexity: ' , end = '' , flush = True )
358
+ for line in sys .stdin :
359
+ args .time_complexity = line .rstrip ()
360
+ break
361
+
362
+ print ('Enter the space (MB): ' , end = '' , flush = True )
363
+ for line in sys .stdin :
364
+ args .space = float (line .rstrip ())
365
+ break
366
+
367
+ print ('Enter the space-rank (%): ' , end = '' , flush = True )
368
+ for line in sys .stdin :
369
+ args .space_rank = float (line .rstrip ())
370
+ break
371
+
372
+ print ('Enter the space-complexity: ' , end = '' , flush = True )
373
+ for line in sys .stdin :
374
+ args .space_complexity = line .rstrip ()
375
+ break
376
+
377
+ commit_solution_args (args .issue , args .name , args .difficulty , args .time , args .time_rank ,
378
+ args .time_complexity , args .space , args .space_rank , args .space_complexity )
0 commit comments