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

  • From: Mohan L <l.mohanphy@xxxxxxxxx>
  • To: ilugc@xxxxxxxxxxxxx
  • Date: Wed, 23 May 2018 06:27:10 +0530

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/

Other related posts: