Skip to content

Commit

Permalink
Merge branch 'tests-and-upgrades'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed May 5, 2020
2 parents 2858bc1 + 78e84bd commit a1301ad
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 108 deletions.
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
LIBRARY_VERSION=$(shell cat library/setup.py | grep version | awk -F"'" '{print $$2}')
LIBRARY_NAME=$(shell cat library/setup.py | grep name | awk -F"'" '{print $$2}')
LIBRARY_VERSION=$(shell grep version library/setup.cfg | awk -F" = " '{print $$2}')
LIBRARY_NAME=$(shell grep name library/setup.cfg | awk -F" = " '{print $$2}')

.PHONY: usage install uninstall
usage:
Expand Down Expand Up @@ -36,12 +36,14 @@ check:
tag:
git tag -a "v${LIBRARY_VERSION}" -m "Version ${LIBRARY_VERSION}"

python-readme: library/README.rst
python-readme: library/README.md

python-license: library/LICENSE.txt

library/README.rst: README.md
pandoc --from=markdown --to=rst -o library/README.rst README.md
library/README.md: README.md library/CHANGELOG.txt
cp README.md library/README.md
echo "" >> library/README.md
cat library/CHANGELOG.txt >> library/README.md

library/LICENSE.txt: LICENSE
cp LICENSE library/LICENSE.txt
Expand Down
186 changes: 177 additions & 9 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,193 @@
#!/bin/bash

LIBRARY_VERSION=`cat library/setup.py | grep version | awk -F"'" '{print $2}'`
LIBRARY_NAME=`cat library/setup.py | grep name | awk -F"'" '{print $2}'`
CONFIG=/boot/config.txt
DATESTAMP=`date "+%Y-%M-%d-%H-%M-%S"`
CONFIG_BACKUP=false
APT_HAS_UPDATED=false
USER_HOME=/home/$SUDO_USER
RESOURCES_TOP_DIR=$USER_HOME/Pimoroni
WD=`pwd`

printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n"
user_check() {
if [ $(id -u) -ne 0 ]; then
printf "Script must be run as root. Try 'sudo ./install.sh'\n"
exit 1
fi
}

confirm() {
if [ "$FORCE" == '-y' ]; then
true
else
read -r -p "$1 [y/N] " response < /dev/tty
if [[ $response =~ ^(yes|y|Y)$ ]]; then
true
else
false
fi
fi
}

prompt() {
read -r -p "$1 [y/N] " response < /dev/tty
if [[ $response =~ ^(yes|y|Y)$ ]]; then
true
else
false
fi
}

success() {
echo -e "$(tput setaf 2)$1$(tput sgr0)"
}

inform() {
echo -e "$(tput setaf 6)$1$(tput sgr0)"
}

warning() {
echo -e "$(tput setaf 1)$1$(tput sgr0)"
}

function do_config_backup {
if [ ! $CONFIG_BACKUP == true ]; then
CONFIG_BACKUP=true
FILENAME="config.preinstall-$LIBRARY_NAME-$DATESTAMP.txt"
inform "Backing up $CONFIG to /boot/$FILENAME\n"
cp $CONFIG /boot/$FILENAME
mkdir -p $RESOURCES_TOP_DIR/config-backups/
cp $CONFIG $RESOURCES_TOP_DIR/config-backups/$FILENAME
if [ -f "$UNINSTALLER" ]; then
echo "cp $RESOURCES_TOP_DIR/config-backups/$FILENAME $CONFIG" >> $UNINSTALLER
fi
fi
}

function apt_pkg_install {
PACKAGES=()
PACKAGES_IN=("$@")
for ((i = 0; i < ${#PACKAGES_IN[@]}; i++)); do
PACKAGE="${PACKAGES_IN[$i]}"
printf "Checking for $PACKAGE\n"
dpkg -L $PACKAGE > /dev/null 2>&1
if [ "$?" == "1" ]; then
PACKAGES+=("$PACKAGE")
fi
done
PACKAGES="${PACKAGES[@]}"
if ! [ "$PACKAGES" == "" ]; then
echo "Installing missing packages: $PACKAGES"
if [ ! $APT_HAS_UPDATED ]; then
apt update
APT_HAS_UPDATED=true
fi
apt install -y $PACKAGES
if [ -f "$UNINSTALLER" ]; then
echo "apt uninstall -y $PACKAGES"
fi
fi
}

user_check

apt_pkg_install python-configparser

CONFIG_VARS=`python - <<EOF
from configparser import ConfigParser
c = ConfigParser()
c.read('library/setup.cfg')
p = dict(c['pimoroni'])
# Convert multi-line config entries into bash arrays
for k in p.keys():
fmt = '"{}"'
if '\n' in p[k]:
p[k] = "'\n\t'".join(p[k].split('\n')[1:])
fmt = "('{}')"
p[k] = fmt.format(p[k])
print("""
LIBRARY_NAME="{name}"
LIBRARY_VERSION="{version}"
""".format(**c['metadata']))
print("""
PY3_DEPS={py3deps}
PY2_DEPS={py2deps}
SETUP_CMDS={commands}
CONFIG_TXT={configtxt}
""".format(**p))
EOF`
if [ $(id -u) -ne 0 ]; then
printf "Script must be run as root. Try 'sudo ./install.sh'\n"
if [ $? -ne 0 ]; then
warning "Error parsing configuration...\n"
exit 1
fi
eval $CONFIG_VARS
RESOURCES_DIR=$RESOURCES_TOP_DIR/$LIBRARY_NAME
UNINSTALLER=$RESOURCES_DIR/uninstall.sh
mkdir -p $RESOURCES_DIR
cat << EOF > $UNINSTALLER
printf "It's recommended you run these steps manually.\n"
printf "If you want to run the full script, open it in\n"
printf "an editor and remove 'exit 1' from below.\n"
exit 1
EOF
printf "$LIBRARY_NAME $LIBRARY_VERSION Python Library: Installer\n\n"
cd library
printf "Installing for Python 2..\n"
python setup.py install
apt_pkg_install "${PY2_DEPS[@]}"
python setup.py install > /dev/null
if [ $? -eq 0 ]; then
success "Done!\n"
echo "pip uninstall $LIBRARY_NAME" >> $UNINSTALLER
fi
if [ -f "/usr/bin/python3" ]; then
printf "Installing for Python 3..\n"
python3 setup.py install
apt_pkg_install "${PY3_DEPS[@]}"
python3 setup.py install > /dev/null
if [ $? -eq 0 ]; then
success "Done!\n"
echo "pip3 uninstall $LIBRARY_NAME" >> $UNINSTALLER
fi
fi
cd ..
cd $WD
for ((i = 0; i < ${#SETUP_CMDS[@]}; i++)); do
CMD="${SETUP_CMDS[$i]}"
# Attempt to catch anything that touches /boot/config.txt and trigger a backup
if [[ "$CMD" == *"raspi-config"* ]] || [[ "$CMD" == *"$CONFIG"* ]] || [[ "$CMD" == *"\$CONFIG"* ]]; then
do_config_backup
fi
eval $CMD
done
for ((i = 0; i < ${#CONFIG_TXT[@]}; i++)); do
CONFIG_LINE="${CONFIG_TXT[$i]}"
if ! [ "$CONFIG_LINE" == "" ]; then
do_config_backup
inform "Adding $CONFIG_LINE to $CONFIG\n"
sed -i "s/^#$CONFIG_LINE/$CONFIG_LINE/" $CONFIG
if ! grep -q "^$CONFIG_LINE" $CONFIG; then
printf "$CONFIG_LINE\n" >> $CONFIG
fi
fi
done
if [ -d "examples" ]; then
if confirm "Would you like to copy examples to $RESOURCES_DIR?"; then
inform "Copying examples to $RESOURCES_DIR"
cp -r examples/ $RESOURCES_DIR
echo "rm -r $RESOURCES_DIR" >> $UNINSTALLER
fi
fi
printf "Done!\n"
success "\nAll done!"
inform "If this is your first time installing you should reboot for hardware changes to take effect.\n"
inform "Find uninstall steps in $UNINSTALLER\n"
2 changes: 1 addition & 1 deletion library/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include CHANGELOG.txt
include LICENSE.txt
include README.rst
include README.md
include setup.py
recursive-include ltr559 *.py
48 changes: 48 additions & 0 deletions library/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# LTR559 Proximity/Presence/Light Sensor

[![Build Status](https://travis-ci.com/pimoroni/ltr559-python.svg?branch=master)](https://travis-ci.com/pimoroni/ltr559-python)
[![Coverage Status](https://coveralls.io/repos/github/pimoroni/ltr559-python/badge.svg?branch=master)](https://coveralls.io/github/pimoroni/ltr559-python?branch=master)
[![PyPi Package](https://img.shields.io/pypi/v/ltr559.svg)](https://pypi.python.org/pypi/ltr559-python)
[![Python Versions](https://img.shields.io/pypi/pyversions/ltr559.svg)](https://pypi.python.org/pypi/ltr559-python)

Suitable for detecting proximity of an object at close range the LTR-559 is great for approach detection and ambient light compensation. The range is useful to around 5cm, and this is the type of sensor you might find in a smartphone to determine if you're holding it against your head.

# Installing

Stable library from PyPi:

* Just run `sudo pip3 install ltr559`

Latest/development library from GitHub:

* `git clone https://github.com/pimoroni/ltr559-python`
* `cd ltr559-python`
* `sudo ./install.sh`


0.1.0
-----

* Breaking API change to class for CircuitPython compatibility
* Port to new i2cdevice set/get API

0.0.4
-----

* Removed rogue print() of reset status

0.0.3
-----

* Fix PyPi readme formatting

0.0.2
-----

* Added default light-sensor options
* Fixed bugs & linted

0.0.1
-----

* Initial Release
32 changes: 0 additions & 32 deletions library/README.rst

This file was deleted.

43 changes: 43 additions & 0 deletions library/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
# -*- coding: utf-8 -*-
[metadata]
name = ltr559
version = 0.1.0
author = Philip Howard
author_email = phil@pimoroni.com
description = Python library for the LTR559 light and proximity
long_description = file: README.md
long_description_content_type = text/markdown
keywords = Rasperry Pi
url = https://www.pimoroni.com
project_urls =
GitHub=https://www.github.com/pimoroni/ltr559-python
license = MIT
license_files = LICENSE.txt
classifiers =
Development Status :: 4 - Beta
Operating System :: POSIX :: Linux
License :: OSI Approved :: MIT License
Intended Audience :: Developers
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3
Topic :: Software Development
Topic :: Software Development :: Libraries
Topic :: System :: Hardware

[options]
packages = ltr559
python_requires = >= 2.7
install_requires =
i2cdevice>=0.0.6

[flake8]
exclude =
test.py
Expand All @@ -9,3 +41,14 @@ exclude =
dist
ignore =
E501

[pimoroni]
py2deps =
python-smbus
python-rpi.gpio
py3deps =
python3-smbus
python3-rpi.gpio
configtxt =
commands =
raspi-config nonint do_i2c 0
Loading

0 comments on commit a1301ad

Please sign in to comment.