使用Makefile一键管理Python项目。
# 伪目标 .PHONY: init_db dev pro build clean upload publish status stop # 定义目标静默执行 .SILENT: .PHONY: status RED=\033[0;31m # 红色的ANSI颜色码 NC=\033[0m # 恢复默认颜色的ANSI码 Tips := "初始化数据库,只执行一次,再次执行会清空数据库数据,慎重!" # 初始化数据库,只在第一次运行项目时执行! init_db: @echo "${RED}$(Tips)${NC}" @echo "" @while [ -z "$$CONTINUE" ]; do \ read -r -p "确定初始化数据库吗?输入Y 或 y 执行,其余退出. [y/n]: " CONTINUE; \ done ; \ [ $$CONTINUE = "y" ] || [ $$CONTINUE = "Y" ] || (echo "\n已取消初始化数据库操作"; exit 1 ;) @echo "\n初始化数据库中..." @echo python manage.py migrate @echo "初始化完成" # 运行开发环境 dev: nohup python main.py > /tmp/main_`date +%Y%m%d`.log 2>&1 & nohup python chegva_monitor_kfk.py > /tmp/chegva_monitor_kfk_`date +%Y%m%d`.log 2>&1 & nohup python chegva_monitor_flow_kfk.py > /tmp/chegva_monitor_flow_kfk_`date +%Y%m%d`.log 2>&1 & celery -A qcelery.main beat -S redbeat.RedBeatScheduler -l info -f /tmp/celery_beat_`date +%Y%m%d`.log --detach celery -A qcelery.main worker -l info -f /tmp/celery_worker_`date +%Y%m%d`.log --detach @echo "开发环境运行成功..." make status # 运行生产环境 pro: @echo prod # 查看运行环境(-符号来忽略命令的错误信息) .PHONY: status status: @echo "--------- 运行程序(pid,启动时间,运行时间,启动用户,CPU占用,MEM占用,启动命令) ---------" -ps axo pid,lstart,etime,user,%cpu,%mem,command | egrep -w "main.py|chegva_monitor_kfk.py|chegva_monitor_flow_kfk.py|celery" | grep -v grep @echo "--------- 日志文件 ---------" -ls /tmp/{main,celery}*.log 2>/dev/null # 关闭运行环境 .PHONY: stop stop: for i in 9000 9001 9100;do lsof -i:$$i | awk 'NR>1 { print $$2 }' | xargs kill;done ps aux | grep celery | grep -v grep | awk '{print $$2}' | xargs kill make clean @echo "运行环境关闭成功!" make status # 发布 make release .PHONY: release release: echo release # 打包 build: echo build # 制作 docker 镜像 .PHONY: docker-build docker-build: echo docker build -t xxx:latest -f Dockerfile . # 构建并运行 docker 镜像 .PHONY: docker-run docker-run: echo docker run -p 8000:8000 xxx:latest # 构建并运行 docker 镜像 .PHONY: docker-build-run docker-build-run: make docker-build make docker-run # 清空打包产物 clean: rm -rf temp logs/* .pytest_cache /tmp/*.log rm -rf dist build *.egg-info # 上传打包产物到 pypi upload: twine check dist/* twine upload dist/* # 发布 make publish publish: make clean make build make upload make clean # 运行所有测试 .PHONY: test test: pytest -c pytest.ini tests/api/test.py # 安装开发环境依赖 make install-require .PHONY: install-require install-require: pip install -r requirements.txt # 快速提交 make fix .PHONY: fix fix: git add . && git commit -m 'fix' && git push
使用Makefile一键管理GO项目。
.PHONY: all build run gotool clean help # 根据不同平台编译生成不同格式的二进制包 ifeq ($(shell uname),Darwin) PLATFORM="darwin" else ifeq ($(OS),Windows_NT) PLATFORM="windows" else PLATFORM="linux" endif endif BINARY="thor" LOGDIR = "log" all: gotool build build: CGO_ENABLED=0 GOOS=${PLATFORM} GOARCH=amd64 go build -o ${BINARY} run: @go run ./main.go conf/config.dev.toml gotool: go fmt ./ go vet ./ clean: @if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi @if [ -d ${LOGDIR} ] ; then rm -rf ${LOGDIR} ; fi help: @echo "make - 格式化 Go 代码, 并编译生成二进制文件" @echo "make build - 编译 Go 代码, 生成二进制文件" @echo "make run - 直接运行 Go 代码" @echo "make clean - 移除二进制文件和日志目录" @echo "make gotool - 运行 Go 工具 'fmt' and 'vet'"
参考: