[ILUGC] Re: How to edit a file in a remote git repo automatically?

  • From: Siva Karthikeyan <seesiva@xxxxxxxxx>
  • To: ilugc@xxxxxxxxxxxxx
  • Date: Wed, 23 May 2018 10:25:17 +0530

Commit to the git repository after adding or appending content using
gitpython
"""
#!/bin/python
from git import Repo
import os.path as osp
from lxml import etree

join = osp.join

# rorepo is a Repo instance pointing to the git-python repository.
# For all you know, the first argument to Repo is a path to the repository
you want to work with
if __name__=="__main__":
repo = Repo("D:\\Free-Tamil-Ebooks")
print repo.working_dir
filename="Test.xml"
xml_file_path = osp.join(repo.working_tree_dir, filename)
print xml_file_path
doc = etree.parse(xml_file_path) # retreive and parse the XML file using
lxml
## Sample Additional content
content 
="<book><bookid>e86ad2d0-55c1-4644-874f-4e88c0a98168</bookid><title>எழுதுகோல்
கவிதைகள்</title><author>ச. 
ரவிச்சந்திரன்</author><image>
https://i0.wp.com/freetamilebooks.com/wp-content/uploads/2018/03/ezhuthikol-kavithaigal.jpg?fit€9%2C1200
</image><link>http://freetamilebooks.com/ebooks/ezhuthukol_kavithaigal/
</link><epub>http://freetamilebooks.com/download/%e0%ae%8e%e0%ae%b4%e0%af
%81%e0%ae%a4%e0%af%81%e0%ae%95%e0%af%8b%e0%ae%b2%e0%af%8d-%e0%ae%95%e0%ae%b5
%e0%ae%bf%e0%ae%a4%e0%af%88%e0%ae%95%e0%ae%b3%e0%af%8d-epub/</epub><pdf
/><category>கவிதைகள்</category><date/></book>"
root=doc.getroot() # Get root node
# print etree.tostring(root) # Print the root and its contents
newnode=etree.XML(content) # Create a new node from the content
root.append(newnode) # Append the new node
# print etree.tostring(doc.getroot()) # After insert verify its content
added to root
# Open the file and update it
f = open(xml_file_path, 'wb')
f.write(etree.tostring(root))
f.close()
f = open(xml_file_path, 'rb')
repo.index.add(f)
f.close()
repo.index.commit('Added new node custom message')

I just did a try, Mohan's solution looked elegant.

Regards
siva.

On Wed, May 23, 2018 at 6:27 AM, Mohan L <l.mohanphy@xxxxxxxxx> wrote:

On Wed, May 23, 2018 at 1:25 AM, Shrinivasan T <tshrinivasan@xxxxxxxxx>
wrote:
Hello all,

For FreeTamilEbooks.com we use a xml file with list of all books info.
We use the file for android, ios apps and download reports.

For each book we release, we have to add the book details in this file.
https://github.com/kishorek/Free-Tamil-Ebooks/blob/master/booksdb.xml

I am working on automating the entire publishing process.
I am done with ebook making and all wordpress automation with
wordpress-xmlrpc-api and selenium.

here is the repo - https://github.com/tshrinivasan/make_ebooks

Now, want to update the booksdb.xml file automatically.

Need help on this.

Seems GitPython can be used.
Got this link as reference for modifying files.
http://gitpython.readthedocs.io/en/stable/tutorial.html#
modifying-references

But, cant get an example of how to add some content to existing file and
commit.

Please help on finding a code/solution for this.


May be try with Dulwich. It comes with both a lower-level API and higher
level interface porcelain. The dulwich.porcelain module in Dulwich is aimed
to closely resemble the Git command-line API that you are familiar with.

Porcelain:
https://www.samba.org/~jelmer/dulwich/docs/tutorial/porcelain.html

dulwich.porcelain module documentation:
https://www.dulwich.io/apidocs/dulwich.porcelain.html#add


# pip install dulwich

# pwd
/root/

Just a sample script to clone, modify a file, stage, commit and push.

# cat example.py (here: http://paste.openstack.org/show/721676/)

#!//usr/bin/python
import os
from dulwich import porcelain

def open_repo(repo_dir):
"""
Open a repo.
"""
repo = porcelain.open_repo(repo_dir)
return repo

def git_clone(repo_url, repo_dir):
"""
Clones the remote Git repository at supplied URL into the local directory
at supplied path.
"""
porcelain.clone(repo_url, repo_dir)

def git_add(repo):
"""
Add files to the staging area.
"""
#repo = porcelain.open_repo(repo_dir)

status = porcelain.status(repo)

repo.stage(status.unstaged + status.untracked)

def git_commit(repo, message=None, author=None, committer=None):
"""
Create a new commit.
"""
porcelain.commit(repo, message, author, committer)
def git_push(repo, remote_location, refspecs):
"""
Push changes.
"""
porcelain.push(repo, remote_location, refspecs)

def modify_files(file_path):
"""
Write your logic here to modify the file.
"""
fh = open(file_path, "a")
fh.write("appended some text")
fh.close()

def main():

# You may need to add username and password in repo url in case clone and
push requires authendication.
https://usename:password@xxxxxxxxxx/username/repo_name.git
repo_url = 'https://github.com/username/repo_name.git'

cwd = os.getcwd()

        # modify /docs with what ever target directory you want to clone
the repo.
repo_dir = cwd + '/docs'
# There may be better way to to clone only not there.
git_dir = repo_dir + '/.git'
if not os.path.exists(git_dir):
git_clone(repo_url, repo_dir)

# modify a file in your repo
file_path = repo_dir + '/myfile.txt'
modify_files(file_path)

# stage the file to commit.
repo = open_repo(repo_dir)
git_add(repo)

# commit the change
git_commit(repo, "your commit message")
# push to remote repo
git_push(repo, repo_url, 'master')


main()​

Thanks
Mohan L

_____________________________________
ILUGC List: http://www.freelists.org/list/ilugc
ILUGC Web: http://ilugc.in/



_____________________________________
ILUGC List: http://www.freelists.org/list/ilugc
ILUGC Web: http://ilugc.in/

Other related posts: