首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GitLab CI Python格式化程序说:将重新格式化,而运行黑色不会重新格式化。

GitLab CI Python格式化程序说:将重新格式化,而运行黑色不会重新格式化。
EN

Stack Overflow用户
提问于 2022-04-03 10:05:05
回答 2查看 3.2K关注 0票数 3

当我在GitLab上运行此承诺 CI时

用这个gitlab-ci.yml

代码语言:javascript
运行
复制
stages:
    - format
    - test

black_formatting:
  image: python:3.6
  stage: format
  
  before_script:
    # Perform an update to make sure the system is up to date.
    - sudo apt-get update --fix-missing
    # Download miniconda.
    - wget -q https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; bash miniconda.sh -b -f -p $HOME/miniconda;
    # Ensure the (mini) conda environment can be activated.
    - export PATH="$HOME/miniconda/bin:$PATH"
    # (Re)create the environment.yml file for the repository.
    - conda env create -q -f environment.yml -n checkstyle-for-bash --force
    # Activate the environment of the repository.
    - source activate checkstyle-for-bash

  script:
    # Verify the Python code is black formatting compliant.
    - black . --check --exclude '\.venv/|\.local/|\.cache/|\.git/'
    # Verify the Python code is flake8 formatting compliant.
    - flake8 .
  allow_failure: false


test:pytest:36:
  stage: test
  image: python:3.6
  script:
    # Ensure the (mini) conda environment can be activated.
    - export PATH="$HOME/miniconda/bin:$PATH"
    # Activate the environment of the repository.
    - source activate checkstyle-for-bash
    # Run the python tests.
    - python -m pytest

它的产出如下:

代码语言:javascript
运行
复制
Running with gitlab-runner 14.8.0 (565b6c0b)
  on trucolrunner DS42qHSq
Preparing the "shell" executor
00:00
Using Shell executor...
Preparing environment
00:00
Running on pcname...
Getting source from Git repository
00:02
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /home/gitlab-runner/builds/DS42qHSq/0/root/checkstyle-for-bash/.git/
Checking out 001577c3 as main...
Removing miniconda.sh
Skipping Git submodules setup
Executing "step_script" stage of the job script
02:55
$ sudo apt-get update --fix-missing
Hit:1 http://nl.archive.ubuntu.com/ubuntu impish InRelease
Get:2 http://security.ubuntu.com/ubuntu impish-security InRelease [110 kB]
Get:3 http://nl.archive.ubuntu.com/ubuntu impish-updates InRelease [115 kB]
Hit:4 https://repo.nordvpn.com/deb/nordvpn/debian stable InRelease
Get:5 http://nl.archive.ubuntu.com/ubuntu impish-backports InRelease [101 kB]
Hit:6 https://brave-browser-apt-release.s3.brave.com stable InRelease
Get:7 http://security.ubuntu.com/ubuntu impish-security/main amd64 DEP-11 Metadata [20,3 kB]
Get:8 http://security.ubuntu.com/ubuntu impish-security/universe amd64 DEP-11 Metadata [3.624 B]
Get:9 http://nl.archive.ubuntu.com/ubuntu impish-updates/main amd64 DEP-11 Metadata [25,8 kB]
Get:10 http://nl.archive.ubuntu.com/ubuntu impish-updates/universe amd64 DEP-11 Metadata [35,4 kB]
Get:11 http://nl.archive.ubuntu.com/ubuntu impish-updates/multiverse amd64 DEP-11 Metadata [940 B]
Get:12 http://nl.archive.ubuntu.com/ubuntu impish-backports/universe amd64 DEP-11 Metadata [16,4 kB]
Fetched 428 kB in 2s (235 kB/s)
Reading package lists...
$ wget -q https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; bash miniconda.sh -b -f -p $HOME/miniconda;
PREFIX=/home/gitlab-runner/miniconda
Unpacking payload ...
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... done
# All requested packages already installed.
installation finished.
$ export PATH="$HOME/miniconda/bin:$PATH"
$ conda env create -q -f environment.yml -n checkstyle-for-bash --force
Collecting package metadata (repodata.json): ...working... done
Solving environment: ...working... done
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
Installing pip dependencies: ...working... done
$ source activate checkstyle-for-bash
$ black . --check --exclude '\.venv/|\.local/|\.cache/|\.git/'
would reformat src/arg_parser.py
would reformat src/helper_text_parsing.py
Oh no!   
2 files would be reformatted, 10 files would be left unchanged.
ERROR: Job failed: exit status 1

但是,如果我在black src/**存储库上运行GitHub,黑色返回:

代码语言:javascript
运行
复制
~/git/checkstyle-for-bash$ git pull
Already up to date.
(base) some@name:~/git/checkstyle-for-bash$ black src/**
All done! ✨  ✨
8 files left unchanged.

为了防止我没有克隆正确的存储库,我还手动地将src/arg_parser.py文件的内容从GitLab复制到~/git/checkstyle-for-bash/src/arg_parser.py中,然后再次运行black。但是,输出是相同的,它不会改变任何东西。

为了完整起见,这是src/arg_parser.py文件的内容:

代码语言:javascript
运行
复制
# This is the main code of this project nr, and it manages running the code and
# outputting the results to LaTex.
import argparse


def parse_cli_args():
    # Instantiate the parser
    parser = argparse.ArgumentParser(description="Optional app description")

    # Include argument parsing for default code.
    # Allow user to load a graph from file.
    parser.add_argument(
        "--ggl",
        dest="google_style_guide",
        action="store_true",
        help=(
            "boolean flag, determines whether the Google Style Guide for "
            "Bash rules are followed."
        ),
    )

    # Allow user to specify an infile.
    parser.add_argument("infile", nargs="?", type=argparse.FileType("r"))

    # Specify default argument values for the parser.
    parser.set_defaults(google_style_guide=True,)

    # Load the arguments that are given.
    args = parser.parse_args()
    return args

问题

什么会导致GitLab CI说这些文件将被重新设置为黑色(即使文件在它们上运行时没有被重新格式化(在相同的设备上(在不同的conda环境中)?

设置

我在测试GitLab黑命令的同一台设备上运行自己的conda CI。GitLab CI复制存储库的GitHub提交,每次一个,在其上运行CI,然后将结果报告给GitHub。

我目前无法在clearnet上公开我的GitLab服务器,因为我在我目前无法控制的网关后面。

疑虑

我很肯定这是一个“愚蠢”的错误,但我还没有弄清楚它是什么。特别是我手动复制粘贴了两次src/arg_parser.py文件的src/arg_parser.py文件内容,并运行了两次黑色来验证黑色确实不会更改“那个”文件。另外,为了确保它不是尾随换行符或任何东西,我在鼠标上使用了“复制”按钮,而不是手动选择:

此外,该文件与flake8兼容。我目前的猜测是,在存储库的最近一次提交时,CI并不是运行的。但是,要验证我单击了失败提交的GitLab,该提交实际上重定向到GitLab中的"05e85fd54f93ccfc427023b21f9cdb0c0cd6db2e“提交(副本):

正是在此提交之后,我复制-粘贴了两次src/arg_parser.py文件。

另一种猜测是,gitlab-ci.yml加载一个迷你环境,而我的本地版本的黑色则使用完整的conda环境。也许他们有一个不同的换行符,这将导致格式的差异。(尽管我对此表示怀疑)。

问题

我再次运行CI,同时将black . --diff命令包含在gitlab-ci.yml脚本中,区别在于:

代码语言:javascript
运行
复制
''' Return

以及:

代码语言:javascript
运行
复制
'''Return

如所附输出中所示:

代码语言:javascript
运行
复制
$ black . --diff --exclude '\.venv/|\.local/|\.cache/|\.git/'
--- src/arg_parser.py   2022-04-03 10:13:10.751289 +0000
+++ src/arg_parser.py   2022-04-03 11:11:26.297995 +0000
@@ -24,10 +24,12 @@
 
     # Allow user to specify an infile.
     parser.add_argument("infile", nargs="?", type=argparse.FileType("r"))
 
     # Specify default argument values for the parser.
-    parser.set_defaults(google_style_guide=True,)
+    parser.set_defaults(
+        google_style_guide=True,
+    )
 
     # Load the arguments that are given.
     args = parser.parse_args()
     return args
would reformat src/arg_parser.py
--- src/helper_text_parsing.py  2022-04-02 19:35:45.142619 +0000
+++ src/helper_text_parsing.py  2022-04-03 11:11:26.342908 +0000
@@ -5,11 +5,11 @@
 def add_two(x):
     return x + 2
 
 
 def get_function_line_nrs(filecontent, rules):
-    """ Returns two lists containing the starting and ending line numbers of
+    """Returns two lists containing the starting and ending line numbers of
     the functions respectively.
 
     :param filecontent: The content of the bash file that is being analysed.
     :param rules: The Bash formatting rules that are chosen by the user.
     """
would reformat src/helper_text_parsing.py
All done! ✨  ✨
2 files would be reformatted, 10 files would be left unchanged.

我不太清楚为什么会发生这种情况,因为我认为python总是会收敛到给定的有效python文件的完全相同的格式。我认为这是因为在同一个设备上的miniconda环境和anaconda环境之间使用了两个不同的黑色版本。

为了验证这个假设,我在black --version中包含了一个gitlab-ci.yml命令。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-03 11:43:28

GitLab CI中的迷你环境使用了python black版本:

代码语言:javascript
运行
复制
black, 22.3.0 (compiled: yes)

而本地环境使用的是python black版本:

代码语言:javascript
运行
复制
black, version 19.10b0

更新本地black版本,根据最新的python black版本推送格式化代码,并在该GitHub提交上运行GitLab CI,可以成功地运行GitLab CI。

票数 3
EN

Stack Overflow用户

发布于 2022-12-01 09:21:03

另一种确保您有您想要的版本的方法,您可以在before_script.中执行例如pip install black==22.1.0

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71724842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档