Skip to content

Instantly share code, notes, and snippets.

@hhsprings
Created March 9, 2019 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhsprings/fd91a1525f2cbff17b11e2bccf1ffff9 to your computer and use it in GitHub Desktop.
Save hhsprings/fd91a1525f2cbff17b11e2bccf1ffff9 to your computer and use it in GitHub Desktop.
pre py2to3 step zero
#! /bin/sh
#
# * fix only print_function, absolute_import
# * keep the target as code working in Python 2.7
#
_2to3="${_2to3:-/c/Python35/Tools/scripts/2to3.py}"
#
test -d "$1" && exit 1 # must be file
test -f "$1" || exit 1 # must be file
test -f "$1".bak && exit 1 # must be fresh
#
(python "${_2to3}" -f print -f import -w "$1" 2>&1) > /dev/null || exit $?
#
pf=`grep "^from __future__ import print_function" "$1"`
ab=`grep "^from __future__ import absolute_import" "$1"`
#
sp=`sed '1,/^[^#].*$/!d' "$1" | sed '$d' | wc -l`
tmp=/tmp/"`basename $1`"
(
exec > "${tmp}"
sed "1,${sp}!d" "$1"
if test -z "${pf}" ; then
echo "from __future__ import print_function"
fi
if test -z "${ab}" ; then
echo "from __future__ import absolute_import"
fi
if test \( -z "${pf}" -o -z "${ab}" \) ; then
echo
fi
sed "`expr ${sp} + 1`,\$!d" "$1"
) && mv -fv "$1" "$1".bak2 && mv -fv "${tmp}" "$1" && diff -u "$1".bak "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment