【问题标题】:Using gitpython to get current hash does not work when using qsub for job submission on a cluster在集群上使用 qsub 提交作业时,使用 gitpython 获取当前哈希不起作用
【发布时间】:2021-10-11 09:20:55
【问题描述】:

我使用 python 进行数据分析,最近我想出了将当前的 git 哈希保存在日志文件中的想法,以便稍后检查哪个代码版本创建了我的结果(以防我发现不一致或其他问题)。

只要我在本地做就可以了。

import git
import os
rep = git.Repo(os.getcwd(), search_parent_directories=True)
git_hash = rep.head.object.hexsha
with open ('logfile.txt', 'w+') as writer:
    writer.write('Code version: {}'.format(git_hash))

但是,我使用 qsub 在集群上运行了大量繁重的计算以加快速度(并行运行主题分析),看起来或多或少像这样:

qsub -l nodes=1:ppn=12 analysis.py -q shared

这总是导致git.exc.InvalidGitRepositoryError

编辑

打印os.getcwd() 告诉我,无论我从哪里提交作业,在集群上当前的工作目录始终是我的 $HOME 目录。 我的下一个解决方案是使用建议的一些解决方案here 获取文件所在的目录。

但是,这些解决方案会导致相同的错误,因为(这就是我的理解)我的文件以某种方式被复制到集群头节点 (/var/spool/torque/mom_priv/jobs) 的根结构深处的目录中。

我当然可以将文件的位置写为硬编码变量,但我想要一个适用于所有脚本的通用解决方案。

【问题讨论】:

    标签: gitpython qsub


    【解决方案1】:

    所以在我向 IT 详细解释了我的问题后,他们可以帮助我解决问题。

    显然 $PBS_O_WORKDIR 变量存储了提交作业的目录。

    所以我调整了对 githash 的访问权限,如下所示:

    try:
        script_file_directory = os.environ["PBS_O_WORKDIR"]
    except KeyError:
        script_file_directory = os.getcwd()
        
    try:
        rep = git.Repo(script_file_directory, search_parent_directories=True)
        git_hash = rep.head.object.hexsha
    except git.InvalidGitRepositoryError:
        git_hash = 'not-found'
        
    # create a log file, that saves some information about the run script
    with open('logfile.txt'), 'w+') as writer:
        writer.write('Codeversion: {} \n'.format(git_hash))
    

    我首先检查 PBS_O_WORKDIR 变量是否存在(因此如果我将脚本作为集群上的作业运行)。如果它不使用当前工作目录,则从该目录获取 gitash。

    非常具体,但也许有一天有人会遇到同样的问题......

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 2023-03-20
      • 2014-12-11
      相关资源
      最近更新 更多