求帮忙把一个简单的 Python 改成 bash shell 的

2018-06-21 10:37:38 +08:00
 coderstory

因为实习生老提交一堆乱七八糟的代码,所以决定写个 git hook 禁止提交语法检查不同用过的代码。

初步发现 git 的 hook 机制配合 checkstyle 满足需求,但网上的例子是 python 写的,而 windwos 的 git 运行在 mingw64 环境下,整合起来比较麻烦,开了某软件也下载不不下来那些资源。

跪求懂 python 和 shell 的朋友帮忙转一下。TAT

脚本

https://blog.coderstory.cn/wp-content/uploads/2018/06/pre-commit.txt

代码如下

` #! /usr/bin/env python

-- encoding: utf-8 --

import sys,os,re

print '\n.......................Code Style Checking....................\n'

#the count of level, like ERROR,WARN def get_level(content, level): return len(re.compile(r"[%s]" % level).findall(content))

#get the commit file name (whole path) def get_file_name(content, postfix=None): content = content.replace("\t", " ") line_divided = content.split("\n") space_divided = [[j for j in i.split(" ") if j.strip()]for i in line_divided] filenames = [i[5] for i in space_divided if i] if not postfix: return filenames return [i for i in filenames if ".%s" % postfix in i]

jarpath = os.popen('git config --get checkstyle.jar').read() checkfilepath = os.popen('git config --get checkstyle.checkfile').read()

#check code command command = 'java -jar ' + jarpath[:-1] + ' -c ' + checkfilepath[:-1]

#the file to check files = os.popen('git diff-index --cached HEAD').read()

#the result of command content = get_file_name(files, 'java')

resultsum = 0

for i in content: result = os.popen(command + ' ' + i).read() print result resultsum += get_level(result,'ERROR') resultsum += get_level(result,'WARN')

if resultsum > 0: print '\n.......................You must fix the errors and warnings first, then excute commit command again...........\n' sys.exit(-1) else: print '\n.................................Code is very good...................\n' sys.exit(0) `

3405 次点击
所在节点    Python
19 条回复
mseasons
2018-06-21 10:43:00 +08:00
写一个 CLI 不行嘛。为啥非要转 Bash
congeec
2018-06-21 10:59:33 +08:00
你听说过 ci 么?
e9e499d78f
2018-06-21 11:02:52 +08:00
简单你就自己写啊
coderstory
2018-06-21 11:18:09 +08:00
@e9e499d78f 关键是我都不会啊 TAT
coderstory
2018-06-21 11:19:20 +08:00
@congeec 我要的是 commit 的时候 禁止提交 ci 是服务端的 tfs 貌似也不能拒绝吧
coderstory
2018-06-21 11:19:48 +08:00
@mseasons cli ? cli 不是命令行么
omph
2018-06-21 11:31:50 +08:00
别人没你的环境,怎么写?
起码也得放字符串的例子,让人知道字符串是什么格式
coderstory
2018-06-21 11:39:40 +08:00
@omph 脚本是 git 自己调用的

git 项目中存在.git 隐藏目录,里面有个 hooks 目录。直接把我提供的脚本去掉扩展名扔到这个 hooks 目录 然后在项目里 git commit 就会自动被 git 调用。hooks 里面也有很多例子

在此谢过
ifaii
2018-06-21 11:48:24 +08:00
都不知道你那些的命令结果,天知道返回什么内容,不知道内容 怎么写
bumz
2018-06-21 11:50:34 +08:00
在脚本开头加 #!/usr/bin/python 即可
omph
2018-06-21 11:51:45 +08:00
shell 语法很简单,楼主看看教程,半天就搞定了
http://www.runoob.com/linux/linux-shell.html

bash 语法大概这样:
-----------------------------------
#!/bin/bash

echo -e '\n.......................Code Style Checking....................\n'

#the count of level, like ERROR,WARN
function get_level {
content=$1
level=$2
return grep -o "[$level]" "$content" | wc -l
}

jarpath=$(git config --get checkstyle.jar)
checkfilepath=$(git config --get checkstyle.checkfile)
ryd994
2018-06-21 11:52:17 +08:00
直接用 wsl 是最简单的办法
araraloren
2018-06-21 12:04:57 +08:00
@omph
~~ LZ 上面说了都不会,意思应该是这个 python 脚本他也看不懂
uorz
2018-06-21 12:07:13 +08:00
```
#!/bin/bash

echo '.......................Code Style Checking....................'

git diff-index --cached HEAD |grep '\.java$'|\
while read -r FILE_NAME; do
java -jar $(git config --get checkstyle.jar) -c $FILE_NAME |tee| grep -Eq '(ERROR|WARN)' && \
echo '.......................You must fix the errors and warnings first, then excute commit command again...........'
done

echo '.................................Code is very good...................'
```

Not promised to work.
congeec
2018-06-21 12:28:50 +08:00
@coderstory #5
ci 不就是这样么,有改动的时候跑一遍测试,测试不通过就不 merge
uorz
2018-06-21 12:41:59 +08:00
Forgetting exit with status

```
#!/bin/bash

echo '.......................Code Style Checking....................'

git diff-index --cached HEAD |grep '\.java$'|\
while read -r FILE_NAME; do
java -jar $(git config --get checkstyle.jar) -c $FILE_NAME |tee| grep -Eq '(ERROR|WARN)' && \
echo '.......................You must fix the errors and warnings first, then excute commit command again...........' && \
exit -1
done

echo '.................................Code is very good...................'
```
coderstory
2018-06-21 14:30:33 +08:00
感谢各位
我现在直接在 maven 中集成 checkstyle 使用 mvn checkstyle:check 调用插件检查代码
根据返回值是 0 还是 1 判断是否通过
beginor
2018-06-21 18:44:40 +08:00
gitlab ci 了解一下
0312birdzhang
2018-06-21 22:34:16 +08:00
谷歌的 fire 了解一下?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/464633

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX