Skip to content

Commit

Permalink
Fix python2
Browse files Browse the repository at this point in the history
Python 2 reached EOL on January 1, 2020. Homebrew removed support for Python 2 about 2 weeks ago (can't find the specific commit.)  This PR will continue to lint existing python2 scripts against best practices, but will only allow Python3 moving forward for new PRs.  This also means that an updated Python 2 scripts will need to be converted to Python 3.

I've tried to make it simple to remove Python2 support completely once this is designed to support Catalina only (since it doesn't include Python by default anyway.)
  • Loading branch information
tresni committed Apr 7, 2020
1 parent e92140f commit 8d581ac
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
44 changes: 28 additions & 16 deletions .test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import re
import os
import subprocess
import urllib2
import argparse
import warnings
from distutils.spawn import find_executable
from tempfile import NamedTemporaryFile
from urllib.request import urlopen

ignore_file_types = ['.md']
ignore_file_names = ['package.json', 'package-lock.json']
Expand All @@ -18,25 +19,25 @@
def debug(s):
global args
if args.debug:
print "\033[1;44mDBG!\033[0;0m %s\n" % s
print("\033[1;44mDBG!\033[0;0m %s\n" % s)


def passed(s):
global args
if args.verbose:
print "\033[1;42mPASS\033[0;0m %s\n" % s
print("\033[1;42mPASS\033[0;0m %s\n" % s)


def warn(s):
global args
if args.warn:
print "\033[1;43mWRN!\033[0;0m %s\n" % s
print("\033[1;43mWRN!\033[0;0m %s\n" % s)


def error(s):
global error_count
error_count += 1
print "\033[1;41mERR!\033[0;0m %s\n" % s
print("\033[1;41mERR!\033[0;0m %s\n" % s)


class Language(object):
Expand Down Expand Up @@ -81,21 +82,32 @@ def lint(self, file, is_pr):
lines = fp.readlines()[1:]
with warnings.catch_warnings():
warnings.simplefilter("ignore")
tmpfile = os.tmpnam()
with open(tmpfile, 'w') as tp:
tp.writelines(lines)
file = tmpfile
with NamedTemporaryFile(mode='w', delete=False) as t:
file = t.name
t.writelines(lines)
command = list(self.cmd)
if is_pr and self.pr:
command.extend(self.pr)
elif not is_pr and self.full:
command.extend(self.full)
command.append(file)
return subprocess.check_output(command, stderr=subprocess.STDOUT)
result = subprocess.check_output(command, stderr=subprocess.STDOUT)
if self.trim:
os.remove(file)
return result


class Python2(Language):
def lint(self, file, pr):
if pr:
raise subprocess.CalledProcessError(
cmd="", returncode=1,
output=b"python3 support required for all PRs")
else:
return super(Python2, self).lint(file, pr)

Language.registerLanguage(Language(['.sh'], '(bash|ksh|zsh|sh|fish)$', ['shellcheck'], full_options=['-e', 'SC1117,SC2164,SC2183,SC2196,SC2197,SC2206,SC2207,SC2215,SC2219,SC2230,SC2236']))
Language.registerLanguage(Language(['.py', '.py2'], 'python(|2(\.\d+)?)$', ['python2', '-m', 'pyflakes']))
Language.registerLanguage(Python2(['.py', '.py2'], 'python(|2(\.\d+)?)$', ['python2', '-m', 'pyflakes']))
Language.registerLanguage(Language(['.py', '.py3'], 'python(|3(\.\d+)?)$', ['python3', '-m', 'pyflakes']))
Language.registerLanguage(Language(['.rb'], 'ruby$', ['rubocop', '-l'], full_options=['--except', 'Lint/RedundantStringCoercion,Lint/BigDecimalNew']))
Language.registerLanguage(Language(['.js'], 'node$', ['jshint']))
Expand Down Expand Up @@ -170,8 +182,8 @@ def check_file(file_full_path, pr=False):

if metadata.get('image', False):
try:
response = urllib2.urlopen(metadata['image'])
response_content_type = response.info().getheader('Content-Type')
response = urlopen(metadata['image'])
response_content_type = response.info().get('Content-Type')
if response_content_type not in allowed_image_content_types:
error('%s image metadata has bad content type: %s' % (file_full_path, response_content_type))
else:
Expand All @@ -196,7 +208,7 @@ def check_file(file_full_path, pr=False):
for e in errors:
error('%s failed linting with "%s", please correct the following:' %
(file_full_path, " ".join(e['linter'].cmd)))
print e['output']
print(e['output'].decode('UTF-8'))


def boolean_string(string):
Expand Down Expand Up @@ -225,7 +237,7 @@ def boolean_string(string):
warn('No changed files in this PR... weird...')
exit(0)
else:
args.files = output.split('\n')
args.files = output.decode("UTF-8").split('\n')
args.verbose = True
elif not args.files:
for root, dirs, files_in_folder in os.walk("."):
Expand Down
33 changes: 22 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
language: objective-c
osx_image: xcode11.2


addons:
homebrew:
packages:
- python
- shellcheck
- luarocks
- node
- cpanminus
- clisp
- golang
casks:
- racket

before_install:
- brew outdated python || brew upgrade python
- brew outdated python@2 || brew upgrade python@2
- pip2 install pyflakes
- pip3 install virtualenv
- mkdir ~/venv
- virtualenv -p python2 ~/venv

install:
- pip3 install pyflakes
- source ~/venv/bin/activate
- pip install pyflakes
- gem install rubocop
- brew install shellcheck
- brew install luarocks
- luarocks install luacheck
- brew outdated node || brew upgrade node
- npm install -g jshint
- brew install cpanminus && sudo cpanm Mozilla::CA
- brew install clisp
- brew outdated golang || brew upgrade golang
- sudo cpanm Mozilla::CA
- go get -u golang.org/x/lint/golint
- brew cask install racket
- raco pkg install --deps search-auto rackjure
install:
- export PATH="$PATH:${GOROOT:-$HOME/go}/bin"

script: ./.test.py

0 comments on commit 8d581ac

Please sign in to comment.