How do I apply a patch from gist to the Django source? - python

I want to try out a patch on gist that modifies the source code of Django:
gist: 550436
How do I do it? I have never used git so a step by step instruction would be greatly appreciated.

You can use patch to apply diffs. Make sure you're in your django source directory (or wherever you want to apply the patch), and run something like patch -p1 < downloaded-patch.diff.
You may want to experiment with the -p argument if it fails; -p tells patch to strip some of the directory prefix for each file in the diff (look at the first line in the diff).

Related

Pdoc - Change template directory programatically

I have a script that generates documentation using pdoc for all of my classes in my project. I want to further customize my documentation by making changes to pdoc templates. I want to do this inside of my project so I can track changes in git, therefor I need to change --template-dir. This is easy enough as a cmd line argument but I have not been able to make this change when using pdoc inside a python script. My question is where can I pass in this argument and or if I make an object of pdoc is there a function or parameter to make this change. Thanks in advance!
The documentation says:
If working with pdoc programmatically, prepend the directory with modified templates into the directories list of the tpl_lookup object.
This is the object in question so something like this should work:
import pdoc
pdoc.tpl_lookup.directories.insert(0, MY_TEMPLATE_DIR)

GitPython: how to commit updated submodule

I have been at this for hours now, and although I have a feeling I'm close I can't seem to figure this out.
I'm trying to make a script that takes a git repository, updates a submodule in that repository to a specified version, and commits that change.
What works:
I can find the repository, get the submodule and check out the commit I want.
What doesn't work:
I can't seem to add the updated submodule hash so I can commit it.
My Code:
repos = Repo('path/to/repos')
submodule = repos.submodule('submodule-name')
submodule.module().git.checkout('wanted commit')
diff = repos.index.diff(None)
At this point I can see the submodule-change. If I check sourcetree, I can see the changed submodule in the 'unstaged files'.
The thing is, I have no clue how to stage the change so I can commit it.
What I have tried:
If I commit using repos.index.commit(''), it creates an empty commit.
If I try to add the path of the submodule using repos.index.add([submodule.path]), all files in the submodule are added to the repository, which is definately not what I want.
If I try to add the submodule itself (which should be possible according to the docs) using repos.index.add([submodule]), nothing seems to happen.
There are two ways to add new submodule commits to the parent repository. One will use the git command directly, the other one will be implemented in pure-python.
All examples are based on the code presented in the question.
Simple
repos.git.add(submodule.path)
repos.index.commit("updated submodule to 'wanted commit'")
The code above will invoke the git command, which is similar to doing a git add <submodule.path> in a shell.
Pythonic
submodule.binsha = submodule.module().head.commit.binsha
repos.index.add([submodule])
repos.index.commit("updated submodule to 'wanted commit'")
The IndexFile.add(...) implementation adds whichever binsha it finds in the submodule object. This one would be the one in the parent repository's current commit, and not the commit that was checked out in the submodule. One can see the Submodule object as a singular snapshot of the submodule, which does not change, nor does not it know about changes to the submodule's repository.
In this case it seems easiest to just overwrite the binsha field of the submodule object to the one that is actually checked out in its repository, so adding it to the index will have the desired effect.

Using git behind a proxy in python scripts

I work with a proxy which doesn't like git. In most of the cases, I can use export http_proxy and git config --global url."http://".insteadOf git://.
But when I use Yocto's python script, this workaround doesn't work anymore. I'm systematically stopped at Getting branches from remote repo git://git.yoctoproject.org/linux-yocto-3.14.git.... I suspect these lines to be responsible :
gitcmd = "git ls-remote %s *heads* 2>&1" % (giturl)
tmp = subprocess.Popen(gitcmd, shell=True, stdout=subprocess.PIPE).stdout.read()
I think that after these lines, others will try to connect to git url. The script I use (yocto-bsp) calls others scripts, which call scripts, so it's difficult to say.
I have tried to add os.system(git config --global url."http://".insteadOf git://) just before, but it does peanuts.
Of course, I could try and modify all the url manually (or with a parsing script) to replace git:// by http:// manually, but this solution is... hideous. I'd like the modification(s) to be as small as possible and reproductible easily. But most of all, I'd like a working script.
EDIT : according to this page, the git url is git://git.yoctoproject.org/linux-yocto-3.14 but the correspondant http url is http://git.yoctoproject.org/git/linux-yocto-3.14, so I can't just parse to replace git:// by http://. Definitely not cool.
Well, rewriting the git url does indeed work, also when using YP.
However, you're rewriting scheme doesn't work that well... You're just replacing the git:// part or the url with http://, but if you look at e.g. linux-yocto-3.14, you'll see that this repo is available through the following two URL's:
git://git.yoctoproject.org/linux-yocto-3.14
http://git.yoctoproject.org/git/linux-yocto-3.14
That is you need to rewrite git://git.yoctoproject.org to http://git.yoctoproject.org/git. Thus, you'll need to do this instead:
git config --global url."http://git.yoctoproject.org/git".insteadOf git://git.yoctoproject.org
Which means that you'll have to repeat this exercise for all repositories that are accessed through the git protocol.

Redefine class/model of python module

I want to use django-achievements (link) module in my app, but it lack some fields in it's model. For example, I want to add CharField to it with path to picture of the badge/achievement. Also I will need to modify module's engine.py file for that.
What is the right way to do that? Download that module to my main app' folder and modify original files, or i can somehow redefine some methods/classes of original models.py and engine.py locally without modifing original files?
I'd say fork it and make your own modifications directly to the source. If it's an improvement you can create a Pull Request and contribute your code to the actual repository (not required, you can always just keep it for your own use).

Adding customized functions to Buildbot

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. :)

Categories