<commit_before>#!/usr/bin/env python
"""
Script to build Docker image of the project and possibly test it.

Actions are largely driven by environment variables, and some information
about the version of biweeklybudget installed...

If ``DOCKER_TEST_TAG`` is set, just run tests against an existing (local) image
with that tag, and exit.

If ``TRAVIS=="true"``:

  - build the image using the sdist created by ``tox``
  - run the image with test configuration and in-memory SQLite, make sure
    it serves pages (just check that ``GET /`` is 200 OK).

Otherwise:

  - just build the image
  - test it unless ``TEST_DOCKER=="false"``
  - tag it appropriately
  - if checked out to a git tag according to versionfinder, or
    ``DOCKER_BUILD_VER`` is set, build Docker image for that version
    **from PyPI**. Otherwise, build using the sdist created by ``tox``.

The latest version of this package is available at:
<http://github.com/jantman/biweeklybudget>

################################################################################
Copyright 2016 <NAME> <<EMAIL>> <http://www.jasonantman.com>

    This file is part of biweeklybudget, also known as biweeklybudget.

    biweeklybudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    biweeklybudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with biweeklybudget.  If not, see <http://www.gnu.org/licenses/>.

The Copyright and Authors attributions contained herein may not be removed or
otherwise altered, except to add the Author attribution of a contributor to
this work. (Additional Terms pursuant to Section 7b of the AGPL v3)
################################################################################
While not legally required, I sincerely request that anyone who finds
bugs please submit them at <https://github.com/jantman/biweeklybudget> or
to me via email, and that you send any contributions or improvements
either as a pull request on GitHub, or to me via email.
################################################################################

AUTHORS:
<NAME> <<EMAIL>> <http://www.jasonantman.com>
################################################################################
"""

import os
import sys
import logging
import time
from git import Repo
import requests
import docker
from io import BytesIO
import tarfile
from biweeklybudget.version import VERSION
from textwrap import dedent
import subprocess

FORMAT = "[%(asctime)s %(levelname)s] %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger()

for lname in ['versionfinder', 'pip', 'git', 'requests', 'docker']:
    l = logging.getLogger(lname)
    l.setLevel(logging.CRITICAL)
    l.propagate = True

if sys.version_info[0:2] != (3, 6):
    raise SystemExit('ERROR: Docker build can only run under py 3.6')

DOCKERFILE_TEMPLATE = """
# biweeklybudget Dockerfile - http://github.com/jantman/biweeklybudget<commit_msg>pyflakes fix - remove import rendered unused by previous commit<commit_after>#!/usr/bin/env python
"""
Script to build Docker image of the project and possibly test it.

Actions are largely driven by environment variables, and some information
about the version of biweeklybudget installed...

If ``DOCKER_TEST_TAG`` is set, just run tests against an existing (local) image
with that tag, and exit.

If ``TRAVIS=="true"``:

  - build the image using the sdist created by ``tox``
  - run the image with test configuration and in-memory SQLite, make sure
    it serves pages (just check that ``GET /`` is 200 OK).

Otherwise:

  - just build the image
  - test it unless ``TEST_DOCKER=="false"``
  - tag it appropriately
  - if checked out to a git tag according to versionfinder, or
    ``DOCKER_BUILD_VER`` is set, build Docker image for that version
    **from PyPI**. Otherwise, build using the sdist created by ``tox``.

The latest version of this package is available at:
<http://github.com/jantman/biweeklybudget>

################################################################################
Copyright 2016 <NAME> <<EMAIL>> <http://www.jasonantman.com>

    This file is part of biweeklybudget, also known as biweeklybudget.

    biweeklybudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    biweeklybudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with biweeklybudget.  If not, see <http://www.gnu.org/licenses/>.

The Copyright and Authors attributions contained herein may not be removed or
otherwise altered, except to add the Author attribution of a contributor to
this work. (Additional Terms pursuant to Section 7b of the AGPL v3)
################################################################################
While not legally required, I sincerely request that anyone who finds
bugs please submit them at <https://github.com/jantman/biweeklybudget> or
to me via email, and that you send any contributions or improvements
either as a pull request on GitHub, or to me via email.
################################################################################

AUTHORS:
<NAME> <<EMAIL>> <http://www.jasonantman.com>
################################################################################
"""

import os
import sys
import logging
import time
from git import Repo
import requests
import docker
from io import BytesIO
import tarfile
from biweeklybudget.version import VERSION
import subprocess

FORMAT = "[%(asctime)s %(levelname)s] %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger()

for lname in ['versionfinder', 'pip', 'git', 'requests', 'docker']:
    l = logging.getLogger(lname)
    l.setLevel(logging.CRITICAL)
    l.propagate = True

if sys.version_info[0:2] != (3, 6):
    raise SystemExit('ERROR: Docker build can only run under py 3.6')

DOCKERFILE_TEMPLATE = """
# biweeklybudget Dockerfile - http://github.com/jantman/biweeklybudget
{'fraction_non_alphanumeric': 0.1653902798232695, 'fraction_numerical': 0.00471281296023564, 'mean_word_length': 4.701931150293871, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3501469', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1756, 'n_words': 799}