Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a makefile for python scripts update a database?

I have a bunch of Python scripts that I use in a pipeline to read files, and convert the data to create and populate a sqlite3 database.

I use a makefile to do this; as some of my input files are fairly large, i.e. 5GB and therefore take a considerable time to process, I don't want the makefile to rerun the whole pipeline when I edit just one file.

However, because they all edit the same file, i.e. the database file, they're all in effect phony targets. Is there a way to make it so that make only reruns the targets that have had their files edited?

Here is the makefile that I'm using:

.PHONY: all
    all: blogs.db

    blogs.db: create users posts likes blogs blog_likes

    .PHONY: create
    create: create.py
        $(PYTHON) create.py

    .PHONY: users
    users:  users.py
        $(PYTHON) users.py

    .PHONY: posts
    posts:  posts.py
        $(PYTHON) posts.py

    .PHONY: likes
    likes:  likes.py
        $(PYTHON) likes.py

    .PHONY: blogs
    blogs:  blogs.py
        $(PYTHON) blogs.py

    .PHONY: blog_likes
    blog_likes: blog_likes.py
        $(PYTHON) blog_likes.py
like image 451
mercifulhop Avatar asked Mar 16 '26 00:03

mercifulhop


1 Answers

You don't need dummy files, provided nothing other than these scripts is modifying the database:

SCRIPTS = create.py users.py posts.py likes.py blogs.py blog_likes.py

.PHONY: all
all: blogs.db

blogs.db: $(SCRIPTS)
    @for s in $?; do $(PYTHON) $$s; done
like image 185
Beta Avatar answered Mar 18 '26 12:03

Beta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!