V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
coderstory
V2EX  ›  Python

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

  •  
  •   coderstory ·
    coderstory · 2018-06-21 10:37:38 +08:00 · 3370 次点击
    这是一个创建于 2107 天前的主题,其中的信息可能已经有所发展或是发生改变。

    因为实习生老提交一堆乱七八糟的代码,所以决定写个 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) `

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

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

    在此谢过
    ifaii
        9
    ifaii  
       2018-06-21 11:48:24 +08:00
    都不知道你那些的命令结果,天知道返回什么内容,不知道内容 怎么写
    bumz
        10
    bumz  
       2018-06-21 11:50:34 +08:00
    在脚本开头加 #!/usr/bin/python 即可
    omph
        11
    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
        12
    ryd994  
       2018-06-21 11:52:17 +08:00 via Android
    直接用 wsl 是最简单的办法
    araraloren
        13
    araraloren  
       2018-06-21 12:04:57 +08:00
    @omph
    ~~ LZ 上面说了都不会,意思应该是这个 python 脚本他也看不懂
    uorz
        14
    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
        15
    congeec  
       2018-06-21 12:28:50 +08:00 via iPhone
    @coderstory #5
    ci 不就是这样么,有改动的时候跑一遍测试,测试不通过就不 merge
    uorz
        16
    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
        17
    coderstory  
    OP
       2018-06-21 14:30:33 +08:00
    感谢各位
    我现在直接在 maven 中集成 checkstyle 使用 mvn checkstyle:check 调用插件检查代码
    根据返回值是 0 还是 1 判断是否通过
    beginor
        18
    beginor  
       2018-06-21 18:44:40 +08:00 via Android
    gitlab ci 了解一下
    0312birdzhang
        19
    0312birdzhang  
       2018-06-21 22:34:16 +08:00 via iPhone   ❤️ 1
    谷歌的 fire 了解一下?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4661 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 10:03 · PVG 18:03 · LAX 03:03 · JFK 06:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.