简体   繁体   English

如何设置 Pycharm 在 Windows 上调试 Fabric fabfile?

[英]How do you set up Pycharm to debug a Fabric fabfile on Windows?

Is is possible to set up Pycharm to step through aa Fabric fabfile.py?是否可以设置 Pycharm 以单步执行 Fabric fabfile.py?

It seems like this would be doable with the run/debug configuration editor but I can't seem to get the settings just right.使用运行/调试配置编辑器似乎可以做到这一点,但我似乎无法正确设置设置。 The editor is asking for a script to run and I've tried the fab-script.py file it is just giving me the fab help options.编辑器要求运行一个脚本,我已经尝试了 fab-script.py 文件,它只是给了我 fab 帮助选项。

It seems like I'm close but not quite there.好像我很接近但不完全在那里。

Here is how I ended up setting this up in case this is useful for someone else.这是我最终设置它的方式,以防这对其他人有用。 As with most things like this, once you know the magic settings, it was very easy.与大多数类似的事情一样,一旦您了解了魔法设置,就很容易了。 All these instructions are through PyCharm but several of them can be done in alternate ways.所有这些指令都是通过 PyCharm 完成的,但其中一些指令可以通过其他方式完成。 However, since this is about debugging in PyCharm, that's what I'm using for the instructions.但是,由于这是关于在 PyCharm 中进行调试,这就是我用于说明的内容。 Also, I'm using Windows.另外,我正在使用Windows。

Install Fabric package to the project environment (using the Settings-->Project Interpreter package installation).将 Fabric 包安装到项目环境中(使用 Settings-->Project Interpreter 包安装)。 This installs Fabric to the virtual environment's site package folder as well as putting a fab.exe and fab-script.py file in the /Scripts folder.这会将 Fabric 安装到虚拟环境的站点包文件夹中,并将 fab.exe 和 fab-script.py 文件放在 /Scripts 文件夹中。 Find the location of the fab-scripts.py file and copy the path (something like this “C:\\\\Scripts\\fab-script.py”)找到 fab-scripts.py 文件的位置并复制路径(类似于“C:\\\\Scripts\\fab-script.py”)

Now, create a run configuration (Run --> Edit Configuration… --> Python) with this script file name.现在,使用此脚本文件名创建运行配置(运行 --> 编辑配置... --> Python)。 The Script parameters point to the fabfile.py and the command to execute/debug. Script 参数指向 fabfile.py 和要执行/调试的命令。 The Script parameters are: -f fabfile dev:"MyBranch1" deploy This allows me to debug the “dev” task with a “MyBranch1” parameter then run the “deploy” task.脚本参数是: -f fabfile dev:"MyBranch1" deploy 这允许我使用“MyBranch1”参数调试“dev”任务,然后运行“deploy”任务。 Replace the dev:"MyBranch1" deploy with whatever your task name is.将 dev:"MyBranch1" deploy 替换为您的任务名称。 The Working directory points to your project folder which is also where the fabfile.py is located (at least with my configuration).工作目录指向您的项目文件夹,这也是 fabfile.py 所在的位置(至少在我的配置中)。 My setup looks like this.我的设置看起来像这样。
运行调试配置

Open the fabfile.py and put a breakpoint where you would like to stop the debugger.打开 fabfile.py 并在要停止调试器的位置放置一个断点。 In this case, since I'm debugging the deploy task, I put the breakpoint there.在这种情况下,因为我正在调试部署任务,所以我将断点放在那里。在此处输入图片说明

Now to debug the fab run, set the active configuration to the one just made and click debug.现在要调试 fab 运行,将活动配置设置为刚刚创建的配置,然后单击调试。在此处输入图片说明

When the breakpoint is hit, you are off and debugging your fabric fabfile.py with PyCharm当断点被击中时,您将关闭并使用 PyCharm 调试您的结构 fabfile.py

When you are ready to run your debugged fabfile, open the Terminal and run the fab command with the parameters used in the debugging.当您准备好运行调试的 fabfile 时,打开终端并使用调试中使用的参数运行 fab 命令。 Again, point the command prompt at the project (working) directory.再次将命令提示符指向项目(工作)目录。 (NOTE: The fab.exe in the Scripts folder needs to be executable from the command line – by having it in the environment variables path property) (注意:Scripts 文件夹中的 fab.exe 需要可以从命令行执行——通过将它放在环境变量路径属性中)在此处输入图片说明

I followed the instructions above in the screenshots.我按照屏幕截图中的上述说明进行操作。 Please be aware that fab-script above should contain:请注意,上面的 fab-script 应包含:

import fabric.main

if __name__ == '__main__':
    fabric.main.main()

The fab executable is nothing but a simple python script. fab可执行文件只不过是一个简单的 python 脚本。 For example, with Fabric 1.10.2 it is just this (although I skipped the shebang and encoding lines):例如,对于 Fabric 1.10.2,它就是这样(尽管我跳过了shebang 和 encoding 行):

import re
import sys

from fabric.main import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

ie: all it is really doing is calling fabric.main.main() .即:它真正做的就是调用fabric.main.main() I'm not sure why it is subbing out the windows extensions in sys.argv[0] , but it doesn't really matter.我不确定它为什么要替换sys.argv[0]的 Windows 扩展,但这并不重要。

If you want to debug a fabfile, either just set the fab.py file as the script to be debugged by your IDE, or make a little stub file that calls fabric's main() and debug that.如果您想调试 fabfile,只需将fab.py文件设置为要由您的 IDE 调试的脚本,或者制作一个调用fabric 的main()的小存根文件并对其进行调试。 You can then put breakpoints anywhere in your fabfile, (or anything your fabfile imports) as you would with any other python program.然后,您可以在 fabfile(或 fabfile 导入的任何内容)中的任何位置放置断点,就像使用任何其他 python 程序一样。

Aside from this, the main part you need is to be able to pass arguments to the executed script so that they show up in sys.argv .除此之外,您需要的主要部分是能够将参数传递给执行的脚本,以便它们显示在sys.argv How to do this depends on your IDE/debugger.如何做到这一点取决于您的 IDE/调试器。 Or, you could just jam them in yourself, something like this:或者,你可以自己把它们塞进去,像这样:

import sys
from fabric.main import main

sys.argv[1:] = ["task1",."task2"] #or whatever you would give fab.
main()

Using your IDE/debugger method is much better, of course.当然,使用您的 IDE/调试器方法要好得多。

for fabric2(2.5) add those code to fabfile.py and debug it as a python script:对于 fabric2(2.5) 将这些代码添加到 fabfile.py 并将其作为 python 脚本进行调试:

if __name__ == '__main__':
    from fabric.main import make_program
    make_program().run("fab [your_task]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM