Skip to content

Instantly share code, notes, and snippets.

@nzbr
Last active May 15, 2019 00:34
Show Gist options
  • Save nzbr/895791518c50a6f361e4e7964e152d2d to your computer and use it in GitHub Desktop.
Save nzbr/895791518c50a6f361e4e7964e152d2d to your computer and use it in GitHub Desktop.
Makefile that creates precompiled python bytecode (.pyc) and bundles it in an exectable zipfile
## Options
PY=python3
ZIP=zip -0 -q
PYLINT=pylint -E
PYC=$(PY) -c "from py_compile import compile; from sys import argv; compile(argv[1], cfile=argv[1]+'c', doraise=True, optimize=2)"
PREFIX=/usr/bin
##Default uses direcory name, replace if neccessary
NAME=$(shell basename $$PWD)
OUT=$(NAME).pyz
## Files
SRC = $(wildcard *.py)
OBJ = $(SRC:%.py=%.pyc)
CLEAN = $(wildcard *.pyc) $(wildcard *.pyz.zip) $(wildcard *.pyz) $(wildcard *.py.lint)
## Build Targets
$(OUT) : $(OUT).zip
@printf " PYZ\t$@ <= $<\n"
@echo "#!$$(which $(PY))" | cat - $(OUT).zip >$(OUT)
@chmod +x $(OUT)
$(OUT).zip : $(OBJ)
@printf " ZIP\t$@ <= $+\n"
@$(ZIP) $(OUT).zip $(OBJ)
%.py.lint : %.py
@printf " LINT $<\n"
@$(PYLINT) $<
%.pyc : %.py %.py.lint
@printf " PYC\t$@\n"
@$(PYC) $<
## Phony Targets
.PHONY: clean all dist check install
clean :
@for x in $(CLEAN); do printf " RM\t$$x\n"; rm -rf $$x; done
all : dist
dist : $(OUT)
check : dist
@printf " CHK\t$(OUT).zip\n"
@zip -T $(OUT).zip
install : dist
@printf " INST\t$(OUT) => $(PREFIX)/$(NAME)\n"
@install -m 755 $(OUT) $(PREFIX)/$(NAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment