Adding customized functions to Buildbot - python

I have written a function in python and I would like to have Buildbot to execute this function
when it receives a "build" command. I have used the "factory.addStep()" before to add new commands through command line, but I'm not sure how I can add a python function to Buildbot. Thanks and please let me know if I'm being unclear on anything.

Do you want the code to run on the master or the slave?
If you want to run code on the master, then all you need to do is subclass BuildStep, and put your code in .start (see the link in vernomcrp's answer).
If you want to run the code on the slave, things become trickier. The simplest solution is if you can write a python script (rather than function), and execute that script. You can include the script in your repository, or download it to the slave with FileDownload or StringDownload.
If you require the code to run in the slave process it self, you need patch the slave, to add a new command, and create a corresponding BuildStep on the master, that calls your new command. This requires modifying the buildslave code on all slaves that you want to run the code on. This isn't documented anywhere, so you will need to have a look at the code to figure out how to do this.

I think we can customize buildstep to execute python function. Have a look at this link http://buildbot.net/buildbot/docs/latest/manual/customization.html. I think it has something what you want. :)

Related

Modify a python script that runs on terminal with command line input to work in an IDE

I am playing around with a python software-like code that I downloaded. I need to extract some parts of the code and so I am currently tracing it to understand it better. It would make my life much easier to trace the code in an IDE (I use spyder) so that I can put breakpoints and visually inspect the variables. I therefore want to modify the code but I don't know how to proceed.
This is the directory structure of the code. The main directory is "advance" and 2 of its subdirectories are "advance" and "examples". The code was installed using pip and to run the code, one must go to the examples directory, enter a sub-directory which represents a test case, and simply type "advance ." in the terminal (the "." represents pwd).
After some tracing, I found out that doing this calls the file driver.py with address advance/advance/driver.py. The driver.py code has the structure below:
# import stuff here
def main():
parser = argparse.ArgumentParser(description="Read working directory")
# some stuff here
if __name__ == "__main__":
try:
main()
except:
print(traceback.format_exc())
print("Execution failed")
As you can see, the main function takes an input which is the working directory.
My question now is: How can I modify this so that I can run this code in my IDE? Or better yet, can I write a script that calls the main function in driver.py and give it the path of the directory for the example test case I want to run? If so, how can I do it?
Thanks and apologies if it sounds too simple. I am coming from Matlab and transitioning to python.
I found in this reddit post that spyder offers to pass command line options, when running a script. See the following setting:
Run -> Configure -> Command line options
Unfortunately, I am not familiar with Spyder, so that's what I can tell so far. I guess it works similar to PyCharm (which I use). There it is like this: You adapt your run configuration and can set the parameters you want to pass a script. Afterwards, when running that run configuration PyCharm will always pass those particular parameters to the script. This allows to use the IDE's debugger to examine the code. I would bet that Spyder behaves almost the same way. Good luck trying the suggested settings :)

Is there a way in python to start a Powershell "session" / pass multiple commands at different times to a single powershell session?

For example, let's say I wanted to set a variable in powershell (or command line), and then get that variable later using something like subprocess.check_output("echo $var", shell=True) (this isn't something I need to do, just an example). Or, lets say I wanted to have a user 'cd' to a directory, and be able to run commands from that directory. I would be able to have some python variable that saves the current directory, and always run "cd {dir};{command}", but that would be inefficient, and still wouldn't work for every situation, I would need to add some special bit of code for every possible situation where a user could want to run a command, then run another command which depends on the first command.
Sorry if I phrased this question badly, let me know if I should clarify. TIA!
Ok, after some searching, I found a way to do this. Using a module called pexpect, MarkBaggett on GitHub has made a simple way to do this: pxpowershell. here's an example:
ps = pxpowershell()
ps.start_process()
ps.run("$x = 10")
print(ps.run("echo $x"))
ps.stop_process()
The only small problems are that 1. colors don't work, and 2. you need to decode() and strip() the output, though you can just add that into the pxpowershell.py.

How can a Py script call a "setup/configure" function only on first-run?

I've been learning Python and decided to make a note-taking utility that runs in bash. I've worked out the basic 'guts' and I want to add a feature that allows a new user to configure their notes (for instance, set the directory where new note files are stored).
I realize this means running a 'install/config' function that is only called the first time the user runs the script (or until they configure it). I don't know what this concept is called, and after some research, cannot find anything about it w/Python.
I'm using argparse. You call the python script from the shell and can optionally use it with arguments. If it would help to see my code, please let me know and I'll format it (it's long and needs to be edited a bit if I want to post). Thanks.
tl;dr How do you run a function only once in Python (either first time code is executed, or until the function's purpose - in this case, setting a file path - is fulfilled)?

PyCharm "Run configuration" asking for "script parameters"

While writing an application parsing command line arguments I would like to run it with various parameters.
I don't want to create a Run Configuration for every possible command line argument that I want my script to test with. Is there a way in PyCharm (and I guess with any JetBrains IDE) to make a Run Configuration that asks for the Script parameters when executed?
I am currently using PyCharm 3.1 (EAP).
Currently the only possibility is to use the "Before launch | Show this page" option.
I've found today that now is possible to ask for parameters using the "Prompt" macro on the "Run configuration" parameters field.
https://www.jetbrains.com/help/pycharm/code-running-assistance-tutorial.html#parameter-with-macros
Although yole's answer is the de facto way to be prompted for thw arguments before running a program, it is slightly annoying because:
the dialog is visually overwhelming and cluttered instead of focused on what you want to do;
you have to tab to reach the arguments field if you want to use the keyboard exclusively (and why not?);
Nothing you could do about that. (Except maybe file a ticket. Have you done that?)
I'm just adding what I used to do before I knew about Googled for this option for the sake of completeness (obvously, this is a hack in the least glamorous sense of the term). But it did suit my workflow as I often only had discrete lines to test with, and didn't switch that often.
Create a new configuration set to the same file, but with a special 'magic' parameter;
Add code to your script to check if the magic is there;
Use a string variable instead of sys.argv (pass it through lambda args: [__name__] + args.split() to reduce the boilerplate);
???
Profit;
I'm doing this on a Mac, but hopefully this will be helpful for Windows or Linux.
Go to Run > Edit Configurations
There will be a dialog box that opens.
Script: file you want to run (ending with .py)
Script Parameters: the command line arguments
Working Directory: directory where your project is.
My simple answer is adding another wrapper as the cover in the source code which will run on the selection you made through code branch or external command or file, so choosing different branch is just a 'ddp' tap distance in vim(line change for parameter settings). You dont have to depend on pycharm updating by building your own code world:)

How to switch execution to a new script in Python?

Is it possible to have one script call another and then exit (don't know if I'm putting this right), leaving the other script running? Specifically I want an update script to download installer.py and then run it. Since installer.py overwrites the update script I can't just do a subprocess.call() since it will fail at the 'delete old install' stage.
Is this possible or must I instead leave the updater script alone, replace everything else, put the new one in a temporary directory and then replace it next time the program is run? Is this considered a better approach?
Thank you very much and sorry if this is blindingly obvious.
One of the os.exec* family of functions should do what you want.
Since you're downloading another Python script, you could try using exec to run it. In 2.x there is a helper function execfile for this sort of thing (which wraps the use of the exec keyword; in 3.x you must read the file and pass the resulting string to exec (which is now a function, if I'm reading correctly).
You must always be 110% sure you trust the content in cases like this! It seems that this isn't an issue for you, though.

Categories