Python版的xpclr的艰辛debug之旅

2020-06-22  本文已影响0人  xuzhougeng

这个软件的安装非常波折,根据安装官方的教程,我在Python3.7.0中进行安装

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple scikit-allel
git clone https://github.com/hardingnj/xpclr.git
cd xpclr
git clone https://github.com/hardingnj/xpclr.git

使用python -c "import xpclr"并没有报错信息,说明至少软件是成功安装了。但是在运行的时候会出现如下的报错

Traceback (most recent call last):
  File "/opt/sysoft/Python-3.7.0/bin/xpclr", line 4, in <module>
    __import__('pkg_resources').run_script('xpclr==1.1.2', 'xpclr')
  File "/opt/sysoft/Python-3.7.0/lib/python3.7/site-packages/pkg_resources/__init__.py", line 658, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/opt/sysoft/Python-3.7.0/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1445, in run_script
    exec(script_code, namespace, namespace)
  File "/opt/sysoft/Python-3.7.0/lib/python3.7/site-packages/xpclr-1.1.2-py3.7.egg/EGG-INFO/scripts/xpclr", line 196, in <module>
  File "/opt/sysoft/Python-3.7.0/lib/python3.7/site-packages/xpclr-1.1.2-py3.7.egg/EGG-INFO/scripts/xpclr", line 88, in main
AssertionError: No permission to write in the specified directory:

根据报错信息,我想去检查/opt/sysoft/Python-3.7.0/lib/python3.7/site-packages/xpclr-1.1.2-py3.7.egg/EGG-INFO/scripts/xpclr这个文件中的代码,然而xpclr-1.1.2-py3.7.egg是一个目录,而非一个文件夹。通过一波检索,我觉得或许是Python版本的原因,导致原本是一个目录的xpclr-1.1.2-py3.7.egg被搞成了一个文件夹,于是我测试了不同的Python版本,2.7, 3.4.0, 3.5.6,我都测试了一遍,发现都报错。

甚至我尝试了下面这个诡异的操作,新建一个目录,然后把原来的文件给重命名为xplr

mv xpclr-1.1.2-py3.7.egg xpclr
mkdir xpclr-1.1.2-py3.7.egg/EGG0INFO/scripts
mkdir -p  xpclr-1.1.2-py3.7.egg/EGG0INFO/scripts
mv xpclr xpclr-1.1.2-py3.7.egg/EGG0INFO/scripts/

当然以上的尝试都失败了。后来我就想着要不就别装了吧,直接调用这个软件吧,于是我做了下面这个操作

mkdir pyxpclr
sed 's:#! /usr/bin/python:#!/usr/bin/env python: ' bin/xpclr > pyxpclr/pyxpclr
chmod +x pyxpclr/pyxpclr
cp -r  xpclr pyxpclr

于是我就就可以通过pyxpclr/pyxpclr的方式进行运行,然后我遇到了如下的报错

Traceback (most recent call last):
  File "/opt/biosoft/pyxpclr/pyxpclr", line 197, in <module>
    main()
  File "/opt/biosoft/pyxpclr/pyxpclr", line 88, in main
    "No permission to write in the specified directory: {0}".format(outdir)
AssertionError: No permission to write in the specified directory

虽然还是报错,但是我似乎看到了胜利的曙光,因为我这次能够定位到引起错误的实际地方,就可以用我的print解决报错大法

    assert os.access(outdir, os.W_OK), \
        print(outdir)
        #"No permission to write in the specified directory: {0}".format(outdir)

继续运行之后,我得到了如下的报错

Traceback (most recent call last):
  File "/opt/biosoft/pyxpclr/pyxpclr", line 197, in <module>
    main()
  File "/opt/biosoft/pyxpclr/pyxpclr", line 88, in main
    print(outdir)
AssertionError: None

这里None表明我的outdir参数没有设置,但实际上我设置了outdir,所以到底是哪里出错了呢?经过我的排除,终于将报错定位到了下面这一句中。

outdir = os.path.dirname(fn)

看似平平无奇,实际上却是我掉头发的罪魁祸首。作者的原来目的是想获取我们给定输出文件路径的文件路径,也就是os.path.dirname("a/b/c")会输出a/b,是os.path.dirname("c")的结果是空,也就是None。显然None是没有读写权限的,因此一定会报错。

换句话说,这个软件的输入要么是以/开头的绝对路径,要么是相对路径,总之不能是一个字符串。

我目前已经把代码fork了一份,并且将对应的代码进行了优化, 对应代码在 https://github.com/xuzhougeng/xpclr, 并向原作者提了PR,估计这个问题之后就会修复了。

上一篇下一篇

猜你喜欢

热点阅读