22Author: LetMeFly
33Date: 2022-07-03 11:21:14
44LastEditors: LetMeFly.xyz
5- LastEditTime: 2025-07-31 10:47:13
5+ LastEditTime: 2025-08-03 20:07:03
66Command: python newSolution.py 102. 二叉树的层序遍历
77What's more: 当前仅支持数字开头的题目
88What's more: 代码结构写的很混乱 - 想单文件实现所有操作
@@ -99,6 +99,7 @@ def getPlatform():
9999 issueNum = int (issueCreateResult .split ('\n ' )[0 ].split ('/' )[- 1 ])
100100else :
101101 os .popen (f'gh issue edit { issueNum } --add-label "solving"' ) # 这里暂不read等待popen执行完毕,这里的小异步是被允许的
102+ os .popen (f'gh issue comment { issueNum } -b "hello #{ issueNum } you are not alone now."' )
102103
103104input ('代码写完后按回车生成题解模板:' )
104105
@@ -185,31 +186,33 @@ def refreshPublistTime(solution: str) -> str:
185186print (solution )
186187
187188solutionName = "Solutions/LeetCode {0:04d}.{1}.md" .format (num , title )
188- with open (solutionName , "x" , encoding = "utf-8" ) as f :
189- f .write (solution )
189+ solutionExists = os .path .exists (solutionName )
190+ if not solutionExists :
191+ with open (solutionName , "w" , encoding = "utf-8" ) as f :
192+ f .write (solution )
190193
191- print ("请编辑题解: “{0}”,注意不要更改文件前5行" .format (solutionName ))
194+ print ("请编辑题解: “{0}”,注意不要更改文件前5行" .format (solutionName ))
192195
193- print ("请去掉可能的由其他插件自动生成的头部注释信息,并保存你所编辑的题解" )
194- csdnid = input ("请输入CSDN题解文章的id(11022152):" )
195- solutionURLcs = "https://letmefly.blog.csdn.net/article/details/{0}" .format (csdnid )
196+ print ("请去掉可能的由其他插件自动生成的头部注释信息,并保存你所编辑的题解" )
197+ csdnid = input ("请输入CSDN题解文章的id(11022152):" )
198+ solutionURLcs = "https://letmefly.blog.csdn.net/article/details/{0}" .format (csdnid )
196199
197- with open (solutionName , "r" , encoding = "utf-8" ) as f :
198- solution = f .read ()
200+ with open (solutionName , "r" , encoding = "utf-8" ) as f :
201+ solution = f .read ()
199202
200- solution = solution .replace ("--------------------------" , csdnid )
201- # solution = solution.replace("--------------------------", csdnid)
203+ solution = solution .replace ("--------------------------" , csdnid )
204+ # solution = solution.replace("--------------------------", csdnid)
202205
203- with open (solutionName , "w" , encoding = "utf-8" ) as f :
204- f .write (solution )
206+ with open (solutionName , "w" , encoding = "utf-8" ) as f :
207+ f .write (solution )
205208
206- print ("请重新复制所有的题解内容,并粘贴到CSDN中发布" )
207- print ("请在LeetCode中新建、编辑并发布题解" )
209+ print ("请重新复制所有的题解内容,并粘贴到CSDN中发布" )
210+ print ("请在LeetCode中新建、编辑并发布题解" )
208211
209- solutionURLlc = input ("LeetCode题解的url: " )
212+ solutionURLlc = input ("LeetCode题解的url: " )
210213
211- with open ("README.md" , "r" , encoding = "utf-8" ) as f :
212- readme = f .read ()
214+ with open ("README.md" , "r" , encoding = "utf-8" ) as f :
215+ readme = f .read ()
213216
214217def readmeNewLine (readme : str ) -> str :
215218 splited = readme .split ("\n " )
@@ -260,10 +263,16 @@ def getProblemUrl():
260263 splited .insert (i , generateNewLine ())
261264 return "\n " .join (splited )
262265
263- readme = readmeNewLine (readme )
264- print (readme )
265- with open ("README.md" , "w" , encoding = "utf-8" ) as f :
266- f .write (readme )
266+ if not solutionExists :
267+ readme = readmeNewLine (readme )
268+ print (readme )
269+ with open ("README.md" , "w" , encoding = "utf-8" ) as f :
270+ f .write (readme )
271+
272+ if solutionExists :
273+ gitCommitMsgPrefix = f'update: 添加问题“{ num } .{ title } ”的代码(并更新其题解)'
274+ else :
275+ gitCommitMsgPrefix = f'update: 添加问题“{ num } .{ title } ”的代码和题解'
267276
268277# commit push pr merge delete-branch
269278os .system ('git add .' )
@@ -277,7 +286,7 @@ def getPrOrIssueMaxNum(prOrIssue: str) -> int: # (#811)
277286 return data [0 ]['number' ]
278287lastNum = max (getPrOrIssueMaxNum ('pr' ), getPrOrIssueMaxNum ('issue' ))
279288print (lastNum )
280- commitMsg = f'update: 添加问题“ { num } . { title } ”的代码和题解 (#{ lastNum + 1 } )'
289+ commitMsg = f'{ gitCommitMsgPrefix } (#{ lastNum + 1 } )'
281290if os .path .exists ('.commitmsg' ) and os .path .isfile ('.commitmsg' ): # (#795)
282291 with open ('.commitmsg' , 'r' , encoding = 'utf-8' ) as f :
283292 commitMsgFromfile = f .read ()
@@ -286,7 +295,7 @@ def getPrOrIssueMaxNum(prOrIssue: str) -> int: # (#811)
286295 commitMsg += commitMsgFromfile
287296subprocess .run (['git' , 'commit' , '-s' , '-m' , commitMsg ]) # os.system('git commit -s -m "{msg}"')的话没法评论多行
288297os .system (f'git push --set-upstream origin { num } ' )
289- cmd = f'gh pr create -H { num } -t "添加问题“ { num } . { title } ”的代码和题解 " -b "By [newSolution.py](https://github.com/LetMeFly666/LeetCode/blob/{ lastSHA } /newSolution.py) using GH on { getPlatform ()} | close: #{ issueNum } " -l "题解" -a "@me"' # -H branch可能是 新版/旧版/Mac 所需的属性,并没有默认使用当前分支诶
298+ cmd = f'gh pr create -H { num } -t "{ gitCommitMsgPrefix } " -b "By [newSolution.py](https://github.com/LetMeFly666/LeetCode/blob/{ lastSHA } /newSolution.py) using GH on { getPlatform ()} | close: #{ issueNum } " -l "题解" -a "@me"' # -H branch可能是 新版/旧版/Mac 所需的属性,并没有默认使用当前分支诶
290299print (cmd )
291300prResult = os .popen (cmd ).read ()
292301print (prResult )
0 commit comments