void-packages

Void Source Packages
git clone git://ezup.dev/void-packages.git
Log | Files | Refs | README | LICENSE

pycompile (3038B)


      1#!/bin/sh
      2#
      3# Trigger to compile python code into native bytecode and remove
      4# generated bytecode files.
      5#
      6# Packages need to set the variable pycompile_dirs with a list
      7# of directories (absolute path) separated by spaces, and WITHOUT
      8# the first slash, e.g:
      9#
     10# pycompile_dirs="usr/blah/foo usr/zoo/d00d"
     11#
     12# or if the code resides in standard site-packages directory,
     13# need to set the pycompile_module variable:
     14#
     15# pycompile_module="blah foo"
     16#
     17# Or if a module is stored in top-level site-packages directory:
     18#
     19# pycompile_module="foo.py"
     20#
     21# Additionally another var can be used to specify the target python version:
     22#
     23# pycompile_version="3.4"
     24#
     25# Arguments:	$ACTION = [run/targets]
     26#		$TARGET = [post-install/pre-remove]
     27#		$PKGNAME
     28#		$VERSION
     29#		$UPDATE = [yes/no]
     30#
     31ACTION="$1"
     32TARGET="$2"
     33PKGNAME="$3"
     34VERSION="$4"
     35UPDATE="$5"
     36
     37export PATH="usr/bin:/usr/sbin:/usr/sbin:/usr/bin:/sbin:/bin"
     38
     39update_ldcache() {
     40	if [ -x sbin/ldconfig -o -x bin/ldconfig ]; then
     41		echo "Updating ldconfig(8) cache..."
     42		ldconfig -X || :
     43	fi
     44}
     45
     46compile()
     47{
     48	for f in ${pycompile_dirs}; do
     49		echo "Byte-compiling python code in ${f}..."
     50		python${pycompile_version} -m compileall -f -q ./${f} && \
     51		python${pycompile_version} -O -m compileall -f -q ./${f}
     52	done
     53	for f in ${pycompile_module}; do
     54		echo "Byte-compiling python${pycompile_version} code for module ${f}..."
     55		if [ -d "usr/lib/python${pycompile_version}/site-packages/${f}" ]; then
     56			python${pycompile_version} -m compileall -f -q \
     57				usr/lib/python${pycompile_version}/site-packages/${f} && \
     58			python${pycompile_version} -O -m compileall -f -q \
     59				usr/lib/python${pycompile_version}/site-packages/${f}
     60		else
     61			python${pycompile_version} -m compileall -f -q \
     62				usr/lib/python${pycompile_version}/site-packages/${f} && \
     63			python${pycompile_version} -O -m compileall -f -q \
     64				usr/lib/python${pycompile_version}/site-packages/${f}
     65		fi
     66	done
     67	update_ldcache
     68}
     69
     70remove()
     71{
     72	for f in ${pycompile_dirs}; do
     73		echo "Removing byte-compiled python${pycompile_version} files in ${f}..."
     74		find ./${f} -type f -name \*.py[co] -delete 2>&1 >/dev/null
     75		find ./${f} -type d -name __pycache__ -delete 2>&1 >/dev/null
     76	done
     77	for f in ${pycompile_module}; do
     78		echo "Removing byte-compiled python${pycompile_version} code for module ${f}..."
     79		if [ -d usr/lib/python${pycompile_version}/site-packages/${f} ]; then
     80			find usr/lib/python${pycompile_version}/site-packages/${f} \
     81				-type f -name \*.py[co] -delete 2>&1 >/dev/null
     82			find usr/lib/python${pycompile_version}/site-packages/${f} \
     83				-type d -name __pycache__ -delete 2>&1 >/dev/null
     84		else
     85			rm -f usr/lib/python${pycompile_version}/site-packages/${f%.py}.py[co]
     86		fi
     87	done
     88	update_ldcache
     89}
     90
     91case "$ACTION" in
     92targets)
     93	echo "post-install pre-remove"
     94	;;
     95run)
     96	[ ! -x usr/bin/python${pycompile_version} ] && exit 0
     97	[ -z "${pycompile_dirs}" -a -z "${pycompile_module}" ] && exit 0
     98
     99	case "$TARGET" in
    100	post-install)
    101		compile
    102		;;
    103	pre-remove)
    104		remove
    105		;;
    106	*)
    107		exit 1
    108		;;
    109	esac
    110	;;
    111*)
    112	exit 1
    113	;;
    114esac
    115
    116exit 0