之前已实现crifan的gitbook的template
可以自动
make deploy
发布内容到
和
中。现在有个需求是:
之前,更新一下对应仓库
的readme
目前最新内容是:
希望是:
每次make deploy,都自动更新时间,以及把目录中没有的book,加过去
其中:现有哪些电子书
的列表顺序,按照bookName排序
现在去写python脚本,以及最后合并到
中的
期间还涉及到:
makefile中调用python代码时
之前已经可以用
python xxx.py
调用,但是此处希望传入 本地的github.io的目录路径过去
不知道是否可以直接传入
去试试
另外,此处本地
/Users/limao/dev/crifan/crifan.github.io/README.md
内容是:
# Crifan的电子书大全 最后更新:`20190717` 。。。 * 现有哪些电子书 * [老少皆宜的运动:羽毛球](https://crifan.github.io/all_age_sports_badminton/website) 。。。 * [有道云笔记和云协作使用总结](https://crifan.github.io/youdao_note_summary/website) * 其他独立内容 。。。
看来待会需要去用正则去解析,再去逻辑判断,生成新列表,再去替换。
先去想办法看看解析传入参数:
【已解决】Python中解析命令行传入的参数
期间还涉及到:
【已解决】Makefile中如何嵌套使用ifeq条件判断逻辑
可以继续写代码处理README.md了。
写代码期间,发现暂时加到book的list时,无需排序,就加到最后就好:
还能区分出,哪些book先写的,哪些是后写的
不过此次发现此处的book的title,比如:移动网络演化史
没法直接获取到。
看来需要再去解析出来才行。。。
经过代码:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Author: Crifan Li Update: 20200918 Function: Update crifan.github.io README.md Note: should run this python file from single gitbook foler eg: /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/books/gitbook_demo """ from datetime import datetime,timedelta import os import codecs import json import argparse import re ################################################################################ # Global Config ################################################################################ ReadmeMdFilename = "README.md" ReadmeCurrentJsonFilename = "README_current.json" ################################################################################ # Internal Function ################################################################################ def datetimeToStr(inputDatetime, format="%Y%m%d_%H%M%S"): """Convert datetime to string Args: inputDatetime (datetime): datetime value Returns: str Raises: Examples: datetime.datetime(2020, 4, 21, 15, 44, 13, 2000) -> '20200421_154413' """ datetimeStr = inputDatetime.strftime(format=format) # print("inputDatetime=%s -> datetimeStr=%s" % (inputDatetime, datetimeStr)) # 2020-04-21 15:08:59.787623 return datetimeStr def getCurDatetimeStr(outputFormat="%Y%m%d_%H%M%S"): """ get current datetime then format to string eg: 20171111_220722 :param outputFormat: datetime output format :return: current datetime formatted string """ curDatetime = datetime.now() # 2017-11-11 22:07:22.705101 # curDatetimeStr = curDatetime.strftime(format=outputFormat) #'20171111_220722' curDatetimeStr = datetimeToStr(curDatetime, format=outputFormat) return curDatetimeStr def loadTextFromFile(fullFilename, fileEncoding="utf-8"): """load file text content from file""" with codecs.open(fullFilename, 'r', encoding=fileEncoding) as fp: allText = fp.read() # logging.debug("Complete load text from %s", fullFilename) return allText def loadJsonFromFile(fullFilename, fileEncoding="utf-8"): """load and parse json dict from file""" with codecs.open(fullFilename, 'r', encoding=fileEncoding) as jsonFp: jsonDict = json.load(jsonFp) # logging.debug("Complete load json from %s", fullFilename) return jsonDict def saveTextToFile(fullFilename, text, fileEncoding="utf-8"): """save text content into file""" with codecs.open(fullFilename, 'w', encoding=fileEncoding) as fp: fp.write(text) fp.close() ################################################################################ # Main Part ################################################################################ parser = argparse.ArgumentParser(description='Update crifan.github.io README.md') parser.add_argument('--curBookRepoName', type=str, help='current gitbook repo name, eg: mobile_network_evolution_history') # parser.add_argument('--curBookTitle', type=str, help='current gitbook title, eg: 移动网络演化史') parser.add_argument('--localGithubIoPath', type=str, help='local github.io path') args = parser.parse_args() print("args=%s" % args) curBookRepoName = args.curBookRepoName print("curBookRepoName=%s" % curBookRepoName) localGithubIoPath = args.localGithubIoPath print("localGithubIoPath=%s" % localGithubIoPath) readmeMdFullPath = os.path.join(localGithubIoPath, ReadmeMdFilename) print("readmeMdFullPath=%s" % readmeMdFullPath) curReadmeMdStr = loadTextFromFile(readmeMdFullPath) print("curReadmeMdStr=%s" % curReadmeMdStr) curDatetimeStr = getCurDatetimeStr("%Y%m%d") # print("curDatetimeStr=%s" % curDatetimeStr) newLastUpdateStr = "最后更新:`%s`" % curDatetimeStr print("newLastUpdateStr=%s" % newLastUpdateStr) newReamdMdStr = re.sub("最后更新:`(\d+)`", newLastUpdateStr, curReadmeMdStr) print("newReamdMdStr=%s" % newReamdMdStr) foundGitbookList = re.search("\* 现有哪些电子书\s(?P<curGitbookListMd>.+)\* 其他独立内容", newReamdMdStr, re.DOTALL) print("foundGitbookList=%s" % foundGitbookList) if foundGitbookList: curGitbookListMd = foundGitbookList.group("curGitbookListMd") print("curGitbookListMd=%s" % curGitbookListMd) # allBookList = re.finditer("\[(?P<bookTitle>[^\]+)\]\(https://crifan\.github\.io/(?P<bookRepoName>\w+)/website\)", curGitbookListMd) # allBookMatchList = re.finditer("\[(?P<bookTitle>[^]]+)\]", curGitbookListMd) allBookMatchList = re.finditer("\[(?P<bookTitle>[^]]+)\]\(https://crifan\.github\.io/(?P<bookRepoName>\w+)/website\)", curGitbookListMd) print("allBookMatchList=%s" % allBookMatchList) curBookDict = {} for curIdx, eachBookMatch in enumerate(allBookMatchList): # print("eachBookMatch=%s" % eachBookMatch) print("%s %s %s" % ("-"*10, curIdx, "-"*10)) bookTitle = eachBookMatch.group("bookTitle") print("bookTitle=%s" % bookTitle) bookRepoName = eachBookMatch.group("bookRepoName") print("bookRepoName=%s" % bookRepoName) curBookDict[bookRepoName] = bookTitle print("curBookDict=%s" % curBookDict) if curBookRepoName in curBookDict.keys(): print("%s already in current readme -> no need add, do nothing" % curBookRepoName) else: print("%s not in current readme -> need add it" % curBookRepoName) currPath = os.getcwd() print("currPath=%s" % currPath) readmeCurrentJsonFullPath = os.path.join(currPath, ReadmeCurrentJsonFilename) print("readmeCurrentJsonFullPath=%s" % readmeCurrentJsonFullPath) readmeCurrentJson = loadJsonFromFile(readmeCurrentJsonFullPath) print("readmeCurrentJson=%s" % readmeCurrentJson) curBookTitle = readmeCurrentJson["bookName"] print("curBookTitle=%s" % curBookTitle) curBookDict[curBookRepoName] = curBookTitle print("updated curBookDict=%s" % curBookDict) # generate new gitbook list md newSingleBookMdList = [] for eachBookReoName, eachBookTitle in curBookDict.items(): singleBookMd = " * [%s](https://crifan.github.io/%s/website)" % (eachBookTitle, eachBookReoName) print("singleBookMd=%s" % singleBookMd) newSingleBookMdList.append(singleBookMd) print("newSingleBookMdList=%s" % newSingleBookMdList) newGitbookListMd = "\n".join(newSingleBookMdList) print("newGitbookListMd=%s" % newGitbookListMd) newGitbookListMd += "\n" newReamdMdStr = newReamdMdStr.replace(curGitbookListMd, newGitbookListMd) print("newReamdMdStr=%s" % newReamdMdStr) saveTextToFile(readmeMdFullPath, newReamdMdStr) print("Updated %s" % readmeMdFullPath)
调试可以更新成希望的内容:
即:
- 更新了:最后更新日期
- 对于新的gitbook的book
- 如果已存在,则不更新
- 如果不存在,则添加
- 放到列表最后
关于readme的内容,详见:
的
然后去真实的make deploy看看是否有问题
。。。 ---------- 27 ---------- bookTitle=有道云笔记和云协作使用总结 bookRepoName=youdao_note_summary ---------- 28 ---------- bookTitle=移动网络演化史 bookRepoName=mobile_network_evolution_history curBookDict={'all_age_sports_badminton': '老少皆宜的运动:羽毛球', 'app_capture_package_tool_charles': 'app抓包利器:Charles', 'android_app_security_crack': '安卓应用的安全和破解', 'api_tool_postman': 'API开发利器:Postman', 'automobile_sales_summary': '汽车销售领域知识总结', 'best_editor_vscode': '史上最好用的编辑器:VSCode', 'common_logic_hardware_embedded': '硬件和嵌入式通用逻辑知识概念', 'crawl_your_data_spider_technology': '爬取你要的数据:爬虫技术', 'doc_format_markdown': '最流行的文档格式:Markdown', 'ebook_system_gitbook': '电子书制作利器:GitBook', 'gitbook_demo': 'Gitbook演示', 'good_automation_tool_makefile': '自动化利器:Makefile', 'http_restful_api': 'HTTP后台端:RESTful API接口设计', 'http_summary': 'HTTP知识总结', 'ic_chip_industry_chain_summary': '芯片产业链总结', 'improve_work_efficiency': '如何提高工作效率', 'mobile_app_summary': '移动端APP开发总结', 'program_code_style': '编程习惯和代码风格', 'program_common_logic': '计算机编程通用逻辑知识概念', 'python_spider_pyspider': 'Python爬虫框架:PySpider', 'resident_life_experience_summary': '居民生活经验总结', 'scientific_network_summary': '科学上网相关知识总结', 'selenium_summary': 'Selenium知识总结', 'smart_speaker_disassemble_summary': '智能音箱拆解总结', 'use_python_write_spider': '如何用Python写爬虫', 'work_job_summary': '工作和职业相关知识', 'xpath_summary': 'XPath知识总结', 'youdao_note_summary': '有道云笔记和云协作使用总结', 'mobile_network_evolution_history': '移动网络演化史'} mobile_network_evolution_history already in current readme -> no need add, do nothing
是没问题的。
然后去更新到gitbook的template
不过另外注意到,本地的GitHub.io的readme.md,竟然没有被push上传更新掉,改动还在。
自己手动去上传了:
待下次,github.io的readme.md被改动后,看看push是否能正常上传
难道是:python脚本还没执行完毕,就允许后续git命令了?
异步了?导致没有push掉改动?
想起来了,可能是刚才的,为了在线版本默认false,而改为了:
ENABLE_COMMIT_GITHUB_IO = false # ENABLE_COMMIT_GITHUB_IO = true
导致此处实际上没用到commit
改为:
# ENABLE_COMMIT_GITHUB_IO = false ENABLE_COMMIT_GITHUB_IO = true
再去试试另外一个book的make deploy,估计可以。
结果报错了:
localGithubIoPath=/Users/limao/dev/crifan/crifan.github.io newLastUpdateStr=最后更新:`20200918` multimedia_core_system_ims not in current readme -> need add it Traceback (most recent call last): File "/Users/limao/dev/crifan/gitbook/gitbook_template/common/tools/update_crifan_github_io_readme.py", line 132, in <module> readmeCurrentJson = loadJsonFromFile(readmeCurrentJsonFullPath) File "/Users/limao/dev/crifan/gitbook/gitbook_template/common/tools/update_crifan_github_io_readme.py", line 68, in loadJsonFromFile with codecs.open(fullFilename, 'r', encoding=fileEncoding) as jsonFp: File "/Users/limao/.pyenv/versions/3.6.5/lib/python3.6/codecs.py", line 897, in open file = builtins.open(filename, mode, buffering) FileNotFoundError: [Errno 2] No such file or directory: '/Users/limao/dev/crifan/crifan.github.io/README_current.json' On branch master Your branch is up to date with 'origin/master'.
找到原因了:
rsync $(RSYNC_PARAMS) $(RELEASE_PATH) $(GITHUB_IO_PATH) cd $(GITHUB_IO_PATH) && \ pwd && \ ls -la && \ git pull && \ if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \ python $(UPDATE_GITHUB_IO_README_FILE) --curBookRepoName $(BOOK_NAME) --localGithubIoPath $(GITHUB_IO_PATH); \ else \ echo "Ignored update README.md before commit $(BOOK_NAME) to github.io"; \ fi; \
是运行python之前,后来又cd切换到github_io,目录了。。。
看log,感觉是对的了:
push modifed content to github.io cd /Users/limao/dev/crifan/crifan.github.io && \ pwd && \ git status && \ pwd && \ git add rcs_tech_dev_summary/* && \ pwd && \ git status && \ pwd && \ git commit -m "1. update book rcs_tech_dev_summary" && \ pwd && \ git status && \ pwd && \ git push && \ pwd && \ cd /Users/limao/dev/crifan/gitbook/gitbook_template/books/rcs_tech_dev_summary && \ pwd /Users/limao/dev/crifan/crifan.github.io On branch master Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: README.md modified: rcs_tech_dev_summary/epub/rcs_tech_dev_summary.epub
但是发现更新了的readme还是没add进去。。。
发现原来之前没有把readme加进去。。。
去加上:
git add README.md && \ git add $(BOOK_NAME)/* && \
再去试试,终于可以了
push modifed content to github.io cd /Users/limao/dev/crifan/crifan.github.io && \ pwd && \ git status && \ pwd && \ git add README.md && \ git add 5g_message_rcs_tech_summary/* && \ pwd && \ git status && \ pwd && \ git commit -m "1. update book 5g_message_rcs_tech_summary" && \ pwd && \ git status && \ pwd && \ git push && \ pwd && \ cd /Users/limao/dev/crifan/gitbook/gitbook_template/books/5g_message_rcs_tech_summary && \ pwd /Users/limao/dev/crifan/crifan.github.io On branch master Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: 5g_message_rcs_tech_summary/epub/5g_message_rcs_tech_summary.epub modified: 5g_message_rcs_tech_summary/mobi/5g_message_rcs_tech_summary.mobi modified: 5g_message_rcs_tech_summary/pdf/5g_message_rcs_tech_summary.pdf 。。。 modified: 5g_message_rcs_tech_summary/website/search_plus_index.json modified: README.md no changes added to commit (use "git add" and/or "git commit -a") /Users/limao/dev/crifan/crifan.github.io /Users/limao/dev/crifan/crifan.github.io On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: 5g_message_rcs_tech_summary/epub/5g_message_rcs_tech_summary.epub modified: 5g_message_rcs_tech_summary/mobi/5g_message_rcs_tech_summary.mobi modified: 5g_message_rcs_tech_summary/pdf/5g_message_rcs_tech_summary.pdf 。。。 modified: 5g_message_rcs_tech_summary/website/search_plus_index.json modified: README.md
其中git commit去提交更新就有README.md了。
【总结】
最后相关commit部分内容是:
## Commit generated files to github io commit: all @echo ================================================================================ ifeq ($(ENABLE_COMMIT_GITHUB_IO), true) @echo Commit for $(BOOK_NAME) @echo pull github.io cd $(GITHUB_IO_PATH) && \ pwd && \ ls -la && \ pwd && \ git pull pwd @echo update readme.md of local github.io if [ $(ENABLE_UPDATE_GITHUB_IO_README) == true ]; then \ python $(UPDATE_GITHUB_IO_README_FILE) --curBookRepoName $(BOOK_NAME) --localGithubIoPath $(GITHUB_IO_PATH); \ else \ echo "Ignored update README.md before commit $(BOOK_NAME) to github.io"; \ fi; @echo copy current book all generated files to local github.io rsync $(RSYNC_PARAMS) $(RELEASE_PATH) $(GITHUB_IO_PATH) @echo push modifed content to github.io cd $(GITHUB_IO_PATH) && \ pwd && \ git status && \ pwd && \ git add README.md && \ git add $(BOOK_NAME)/* && \ pwd && \ git status && \ pwd && \ git commit -m $(COMMIT_COMMENT) && \ pwd && \ git status && \ pwd && \ git push && \ pwd && \ cd $(CURRENT_DIR) && \ pwd else @echo Ignored commit $(BOOK_NAME) to github.io endif
调用的代码是:
common/tools/update_crifan_github_io_readme.py
#!/usr/bin/python # -*- coding: utf-8 -*- """ Author: Crifan Li Update: 20200918 Function: Update crifan.github.io README.md Note: should run this python file from single gitbook foler eg: /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/books/gitbook_demo """ from datetime import datetime,timedelta import os import codecs import json import argparse import re ################################################################################ # Global Config ################################################################################ ReadmeMdFilename = "README.md" ReadmeCurrentJsonFilename = "README_current.json" ################################################################################ # Internal Function ################################################################################ def datetimeToStr(inputDatetime, format="%Y%m%d_%H%M%S"): """Convert datetime to string Args: inputDatetime (datetime): datetime value Returns: str Raises: Examples: datetime.datetime(2020, 4, 21, 15, 44, 13, 2000) -> '20200421_154413' """ datetimeStr = inputDatetime.strftime(format=format) # print("inputDatetime=%s -> datetimeStr=%s" % (inputDatetime, datetimeStr)) # 2020-04-21 15:08:59.787623 return datetimeStr def getCurDatetimeStr(outputFormat="%Y%m%d_%H%M%S"): """ get current datetime then format to string eg: 20171111_220722 :param outputFormat: datetime output format :return: current datetime formatted string """ curDatetime = datetime.now() # 2017-11-11 22:07:22.705101 # curDatetimeStr = curDatetime.strftime(format=outputFormat) #'20171111_220722' curDatetimeStr = datetimeToStr(curDatetime, format=outputFormat) return curDatetimeStr def loadTextFromFile(fullFilename, fileEncoding="utf-8"): """load file text content from file""" with codecs.open(fullFilename, 'r', encoding=fileEncoding) as fp: allText = fp.read() # logging.debug("Complete load text from %s", fullFilename) return allText def loadJsonFromFile(fullFilename, fileEncoding="utf-8"): """load and parse json dict from file""" with codecs.open(fullFilename, 'r', encoding=fileEncoding) as jsonFp: jsonDict = json.load(jsonFp) # logging.debug("Complete load json from %s", fullFilename) return jsonDict def saveTextToFile(fullFilename, text, fileEncoding="utf-8"): """save text content into file""" with codecs.open(fullFilename, 'w', encoding=fileEncoding) as fp: fp.write(text) fp.close() ################################################################################ # Main Part ################################################################################ parser = argparse.ArgumentParser(description='Update crifan.github.io README.md') parser.add_argument('--curBookRepoName', type=str, help='current gitbook repo name, eg: mobile_network_evolution_history') # parser.add_argument('--curBookTitle', type=str, help='current gitbook title, eg: 移动网络演化史') parser.add_argument('--localGithubIoPath', type=str, help='local github.io path') args = parser.parse_args() # print("args=%s" % args) curBookRepoName = args.curBookRepoName print("curBookRepoName=%s" % curBookRepoName) localGithubIoPath = args.localGithubIoPath print("localGithubIoPath=%s" % localGithubIoPath) readmeMdFullPath = os.path.join(localGithubIoPath, ReadmeMdFilename) # print("readmeMdFullPath=%s" % readmeMdFullPath) curReadmeMdStr = loadTextFromFile(readmeMdFullPath) # print("curReadmeMdStr=%s" % curReadmeMdStr) curDatetimeStr = getCurDatetimeStr("%Y%m%d") # print("curDatetimeStr=%s" % curDatetimeStr) newLastUpdateStr = "最后更新:`%s`" % curDatetimeStr print("newLastUpdateStr=%s" % newLastUpdateStr) newReamdMdStr = re.sub("最后更新:`(\d+)`", newLastUpdateStr, curReadmeMdStr) # print("newReamdMdStr=%s" % newReamdMdStr) foundGitbookList = re.search("\* 现有哪些电子书\s(?P<curGitbookListMd>.+)\* 其他独立内容", newReamdMdStr, re.DOTALL) # print("foundGitbookList=%s" % foundGitbookList) if foundGitbookList: curGitbookListMd = foundGitbookList.group("curGitbookListMd") # print("curGitbookListMd=%s" % curGitbookListMd) allBookMatchList = re.finditer("\[(?P<bookTitle>[^]]+)\]\(https://crifan\.github\.io/(?P<bookRepoName>\w+)/website\)", curGitbookListMd) # print("allBookMatchList=%s" % allBookMatchList) curBookDict = {} for curIdx, eachBookMatch in enumerate(allBookMatchList): # print("eachBookMatch=%s" % eachBookMatch) # print("%s %s %s" % ("-"*10, curIdx, "-"*10)) bookTitle = eachBookMatch.group("bookTitle") # print("bookTitle=%s" % bookTitle) bookRepoName = eachBookMatch.group("bookRepoName") # print("bookRepoName=%s" % bookRepoName) curBookDict[bookRepoName] = bookTitle # print("curBookDict=%s" % curBookDict) if curBookRepoName in curBookDict.keys(): print("%s already in current readme -> no need add, do nothing" % curBookRepoName) else: print("%s not in current readme -> need add it" % curBookRepoName) currPath = os.getcwd() # print("currPath=%s" % currPath) readmeCurrentJsonFullPath = os.path.join(currPath, ReadmeCurrentJsonFilename) # print("readmeCurrentJsonFullPath=%s" % readmeCurrentJsonFullPath) readmeCurrentJson = loadJsonFromFile(readmeCurrentJsonFullPath) # print("readmeCurrentJson=%s" % readmeCurrentJson) curBookTitle = readmeCurrentJson["bookName"] # print("curBookTitle=%s" % curBookTitle) curBookDict[curBookRepoName] = curBookTitle # print("updated curBookDict=%s" % curBookDict) # generate new gitbook list md newSingleBookMdList = [] for eachBookReoName, eachBookTitle in curBookDict.items(): singleBookMd = " * [%s](https://crifan.github.io/%s/website)" % (eachBookTitle, eachBookReoName) # print("singleBookMd=%s" % singleBookMd) newSingleBookMdList.append(singleBookMd) # print("newSingleBookMdList=%s" % newSingleBookMdList) newGitbookListMd = "\n".join(newSingleBookMdList) # print("newGitbookListMd=%s" % newGitbookListMd) newGitbookListMd += "\n" newReamdMdStr = newReamdMdStr.replace(curGitbookListMd, newGitbookListMd) print("newReamdMdStr=%s" % newReamdMdStr) saveTextToFile(readmeMdFullPath, newReamdMdStr) print("Updated %s" % readmeMdFullPath)
更新代码,详见:
的:
转载请注明:在路上 » 【已解决】给crifan的gitbook的template添加部署时更新github.io的README