From 5496c02f9f6c06af34987c6b1e9d33243c75bdc4 Mon Sep 17 00:00:00 2001
From: Kenneth Reitz <me@kennethreitz.org>
Date: Tue, 30 May 2017 20:03:57 -0400
Subject: [PATCH] Don't install packages that could mess packaging up (#397)

* updated changelog

* remove setuptools sanity check

* update changelog

* pip-clean

* changelog note

* refactor codebase to improve package name detection

* fix version string

* cleanup

* add messaging, improve execution

* do uninstall first
---
 CHANGELOG.md                    | 12 ++++++++++++
 bin/compile                     | 12 ++++++++----
 bin/steps/setuptools            | 11 +++++++++++
 vendor/pip-pop/pip-clean        | 33 +++++++++++++++++++++++++++++++++
 vendor/pip-pop/requirements.txt | 17 +++++++++++++++++
 5 files changed, 81 insertions(+), 4 deletions(-)
 create mode 100755 bin/steps/setuptools
 create mode 100755 vendor/pip-pop/pip-clean
 create mode 100644 vendor/pip-pop/requirements.txt

diff --git a/CHANGELOG.md b/CHANGELOG.md
index c6b13c56..80d88287 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,17 @@
 # Python Buildpack Changelog
 
+# 106
+
+Don't install packages that could mess up packaging.
+
+- The Python buildpack will automatically remove `six`, `pyparsing`, `appdirs`,
+  `setuptools`, and `distribute` from a `requirements.txt` file now, as these
+  packages are provided by the Python buildpack.
+
+# 105
+
+Improvements to output messaging.
+
 # 104
 
 General improvements.
diff --git a/bin/compile b/bin/compile
index ab60dbe7..78b63bcd 100755
--- a/bin/compile
+++ b/bin/compile
@@ -171,15 +171,19 @@ sub-env $BIN_DIR/steps/geo-libs
 # GDAL support.
 source $BIN_DIR/steps/gdal
 
+# Uninstall removed dependencies with Pip.
+let start=$(nowms)
+source $BIN_DIR/steps/pip-uninstall
+mtime "pip.uninstall.time" "${start}"
+
+# Cleanup requirements.txt
+source $BIN_DIR/steps/setuptools
+
 # Install dependencies with Pip (where the magic happens).
 let start=$(nowms)
 source $BIN_DIR/steps/pip-install
 mtime "pip.install.time" "${start}"
 
-# Uninstall removed dependencies with Pip.
-let start=$(nowms)
-source $BIN_DIR/steps/pip-uninstall
-mtime "pip.uninstall.time" "${start}"
 
 # Support for NLTK corpora.
 let start=$(nowms)
diff --git a/bin/steps/setuptools b/bin/steps/setuptools
new file mode 100755
index 00000000..3bfc8ed3
--- /dev/null
+++ b/bin/steps/setuptools
@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+
+# Syntax sugar.
+source $BIN_DIR/utils
+
+puts-step "Removing packaging utilities from requirements.txt."
+
+if [[ -f requirements.txt ]]; then
+    pip-clean requirements.txt
+fi
+
diff --git a/vendor/pip-pop/pip-clean b/vendor/pip-pop/pip-clean
new file mode 100755
index 00000000..b17e4619
--- /dev/null
+++ b/vendor/pip-pop/pip-clean
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""Usage:
+  pip-clean <req-file>
+
+Options:
+  -h --help     Show this screen.
+"""
+from docopt import docopt
+
+BAD_PACKAGES = ['appdirs', 'packaging', 'pyparsing', 'six', 'setuptools', 'distribute']
+
+
+def good_package(line):
+    package_name = line.split('=')[0].split('<')[0].split('>')[0].split(' ')[0].split('#')[0].split('\n')[0]
+    return package_name not in BAD_PACKAGES
+
+def main():
+    args = docopt(__doc__, version='pip-clean')
+    req_file = args['<req-file>']
+
+    with open(req_file, 'r') as f:
+        # Iterate over every line in the requirements file.
+        lines = [line for line in f if good_package(line)]
+
+    # Write the requirements file to disk.
+    with open(req_file, 'w') as f:
+        f.write(''.join(lines))
+
+
+if __name__ == '__main__':
+    main()
diff --git a/vendor/pip-pop/requirements.txt b/vendor/pip-pop/requirements.txt
new file mode 100644
index 00000000..e2541fa2
--- /dev/null
+++ b/vendor/pip-pop/requirements.txt
@@ -0,0 +1,17 @@
+certifi==2017.4.17
+chardet==3.0.3
+dateparser
+humanize==0.5.1
+idna==2.5
+maya==0.3.2
+pendulum==1.2.1
+python-dateutil==2.6.0
+pytz==2017.2
+pytzdata==2017.2
+regex==2017.5.26
+requests==2.17.3
+ruamel.ordereddict==0.4.9
+ruamel.yaml==0.14.12
+tzlocal==1.4
+urllib3==1.21.1
+
-- 
GitLab