Skip to content

Commit

Permalink
Adjust for strftime() handling %Y inconsistantly
Browse files Browse the repository at this point in the history
On Debian systems, datetime.strftime("%Y") will produce less than a
4-digit year if the value is < 1000. On OS X and Windows sytems, it
will pad on the left with zeros to create a 4-digit year when the
value is < 1000.

I changed the code to detect whether padding was applied and supply
the correct padding if it was missing.

Also added recipe for a Docker container that could be used for
Ubuntu testing and development.

Bump version to 1.1.2
  • Loading branch information
Dave Hein committed Jun 9, 2016
1 parent f821a73 commit 09a600f
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 7 deletions.
4 changes: 2 additions & 2 deletions dev/build/register-package.sh
Expand Up @@ -12,7 +12,7 @@ pushd src > /dev/null

# Register the project
#
twine register -r pypi dist/authenticator-1.1.1.tar.gz
twine register -r pypi dist/authenticator-1.1.1-py3-none-any.whl
twine register -r pypi dist/authenticator-1.1.2.tar.gz
twine register -r pypi dist/authenticator-1.1.2-py3-none-any.whl

popd > /dev/null
4 changes: 2 additions & 2 deletions dev/build/test-register-package.sh
Expand Up @@ -12,8 +12,8 @@ pushd src > /dev/null

# Register the project
#
twine register -r pypitest dist/authenticator-1.1.1.tar.gz
twine register -r pypitest dist/authenticator-1.1.1-py3-none-any.whl
twine register -r pypitest dist/authenticator-1.1.2.tar.gz
twine register -r pypitest dist/authenticator-1.1.2-py3-none-any.whl
# python setup.py register -r https://testpypi.python.org/pypi

popd > /dev/null
71 changes: 71 additions & 0 deletions dev/docker/Dockerfile
@@ -0,0 +1,71 @@
# Using a base Ubuntu 14.04 that has the latest updates already
#
# Base images can be found here: https://hub.docker.com/_/ubuntu/
# Ubuntu 14.04 LTS (Trusty Tahr) is supported through 2019.
# Ubuntu 16.04 LTS (Xenial Xerus) has just been released and probably should
# not be used yet (because dependent software may need to be updated and
# made compatible).
#
FROM ubuntu:14.04.4
MAINTAINER Dave Hein <dhein@acm.org>

# Setting this environment variable prevents errors during package installs
# that look like:
#
# debconf: unable to initialize frontend: Dialog
# debconf: (TERM is not set, so the dialog frontend is not usable.)
# debconf: falling back to frontend: Readline
#
# As per: http://stackoverflow.com/a/35976127/1392864
#
ARG DEBIAN_FRONTEND=noninteractive

# Update apt package info and upgrade installed packages (base image
# has some packages installed)
#
# Install some basic package fetching tools
#
ENV FETCH_REFRESHED_AT 2016-06-08T05:25-0500
RUN apt-get update && apt-get -y upgrade \
apt-get install -yqq software-properties-common python-software-properties && \
apt-get -yqq install wget

# Install Python 3.5
#
ENV PYTHON35_REFRESHED_AT 2016-06-08T05:25-0500
RUN add-apt-repository ppa:fkrull/deadsnakes && \
apt-get update
RUN apt-get install -yqq python3.5
RUN apt-get install -yqq python3.5-dev
RUN apt-get install -yqq libncurses5-dev
RUN apt-get install -yqq python3.5-venv

# Install pip & setuptools
#
ENV PIP3_REFRESHED_AT 2016-06-08T05:47-0500
RUN wget https://bootstrap.pypa.io/get-pip.py
RUN python3 get-pip.py
RUN pip3 install setuptools --upgrade

# Install git
#
ENV GIT_REFRESHED_AT 2016-06-08T05:47-0500
RUN apt-get install -yqq git-core

# Install gcc
#
ENV GCC_REFRESHED_AT 2016-06-08T05:47-0500
RUN apt-get install -yqq \
autoconf \
automake \
g++ \
gcc \
libffi-dev \
libssl-dev \
make \
patch

# Local stuff
ENV LOCALSTUFF_REFRESHED_AT 2016-06-08T06:07-0500
COPY establish-dev.sh /root/trash/
WORKDIR /root/trash
5 changes: 5 additions & 0 deletions dev/docker/build.sh
@@ -0,0 +1,5 @@
#! /bin/bash
#
# Create the image ... for the python35 dev environment
#
docker build -t datihein/devauthenticator:python35 .
10 changes: 10 additions & 0 deletions dev/docker/establish-dev.sh
@@ -0,0 +1,10 @@
#! /bin/bash
#
git clone https://github.com/JeNeSuisPasDave/authenticator
cd authenticator
python3.5 -m venv --clear --copies venv35
. venv35/bin/activate
pip install --quiet --upgrade pip
pip install --quiet --upgrade setuptools
./dev/venv/provision-venv.sh
deactivate
7 changes: 7 additions & 0 deletions dev/docker/run.sh
@@ -0,0 +1,7 @@
#! /bin/bash
#

# Start the python35 dev container
#
docker run --rm -t -i \
datihein/devauthenticator:python35
2 changes: 1 addition & 1 deletion dev/runonetest.sh
@@ -1,4 +1,4 @@
#! /bin/bash
#
PYTHONPATH="$(pwd)/src" python -m unittest \
tests.test_CLI.CoreCLITests.test_list_with_three_configs_verbose
tests.test_ClientData.CoreClientDataTests.test_string
2 changes: 1 addition & 1 deletion src/authenticator/__init__.py
Expand Up @@ -40,4 +40,4 @@

__author__ = "Dave Hein <dhein@acm.org>"
__license__ = "MIT"
__version__ = "1.1.1"
__version__ = "1.1.2"
26 changes: 26 additions & 0 deletions src/authenticator/data.py
Expand Up @@ -228,6 +228,10 @@ def _init_last_count_update_time(self, kw_args):

self.__last_count_update_time = datetime(
1, 1, 1, 0, 0, 0, 0, ClientData.utz()).strftime(self._isoFmt)
# Fix issue on some systems, e.g. Debian, where %Y doesn't zero-pad
if self.__last_count_update_time[0:3] != "000":
self.__last_count_update_time = "000" + \
self.__last_count_update_time
if 'lastCountUpdateTime' in kw_args:
t = datetime.min
v = kw_args['lastCountUpdateTime']
Expand All @@ -242,6 +246,17 @@ def _init_last_count_update_time(self, kw_args):
if t.tzinfo is None:
t = t.replace(tzinfo=ClientData.utz())
self.__last_count_update_time = t.strftime(self._isoFmt)
# Fix issue on some systems, e.g. Debian, where %Y doesn't zero-pad
tpadding = ""
if 10 > t.year:
tpadding = "000"
elif 100 > t.year:
tpadding = "00"
elif 1000 > t.year:
tpadding = "0"
if "0" != self.__last_count_update_time[0:1]:
self.__last_count_update_time = tpadding + \
self.__last_count_update_time

def _init_period(self, kw_args):
"""Process kw_arg period."""
Expand Down Expand Up @@ -482,6 +497,17 @@ def set_last_count_update_time(self, update_time):

if isinstance(update_time, datetime):
self.__last_count_update_time = update_time.strftime(self._isoFmt)
# Fix issue on some systems, e.g. Debian, where %Y doesn't zero-pad
tpadding = ""
if 10 > update_time.year:
tpadding = "000"
elif 100 > update_time.year:
tpadding = "00"
elif 1000 > update_time.year:
tpadding = "0"
if "0" != self.__last_count_update_time[0:1]:
self.__last_count_update_time = tpadding + \
self.__last_count_update_time
else:
self.__last_count_update_time = update_time

Expand Down
2 changes: 1 addition & 1 deletion src/setup.py
Expand Up @@ -30,7 +30,7 @@
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
#
version='1.1.1',
version='1.1.2',

description='A HOTP/TOTP code generator for the command line.',
long_description=long_description,
Expand Down
3 changes: 3 additions & 0 deletions tests/test_ClientData.py
Expand Up @@ -320,6 +320,9 @@ def test_constructor_last_count_update_time(self):
cut = ClientData(**args)
expected = datetime(
1, 1, 1, 0, 0, 0, 0, self.__utz).strftime(self.isoFmt)
# Fix issue on some systems, e.g. Debian, where %Y doesn't zero-pad
if expected[0:3] != "000":
expected = "000" + expected
self.assertEqual(expected, cut.last_count_update_time())
# unix epoch time
#
Expand Down

0 comments on commit 09a600f

Please sign in to comment.