From 2ac6d63ffc776af3b24d28de863f3020274383c8 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Mon, 23 Feb 2015 11:50:14 +0200 Subject: [PATCH] Fix Python 2.6 support --- .travis.yml | 5 +++-- platformio/downloader.py | 9 +++++---- platformio/libmanager.py | 2 +- platformio/util.py | 4 ++-- scripts/get-platformio.py | 32 ++++++++++++++++++++++++++------ tests/commands/test_init.py | 21 ++++++++++++--------- 6 files changed, 49 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index 911131bf..323dbf4c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ cache: directories: - - $HOME/.platformio + - $HOME/.platformio language: python python: @@ -9,7 +9,8 @@ python: env: - TOX_ENV=docs - TOX_ENV=lint - - TOX_ENV=py27 + - TOX_ENV=py26 + - TOX_ENV=py27 install: - pip install tox diff --git a/platformio/downloader.py b/platformio/downloader.py index 5b58fdb7..df9f5b64 100644 --- a/platformio/downloader.py +++ b/platformio/downloader.py @@ -4,7 +4,6 @@ from email.utils import parsedate_tz from math import ceil from os.path import getsize, join -from subprocess import check_output from time import mktime from click import progressbar @@ -12,7 +11,7 @@ from requests import get from platformio.exception import (FDSHASumMismatch, FDSizeMismatch, FDUnrecognizedStatusCode) -from platformio.util import change_filemtime +from platformio.util import change_filemtime, exec_command class FileDownloader(object): @@ -67,10 +66,12 @@ class FileDownloader(object): dlsha1 = None try: - dlsha1 = check_output(["sha1sum", self._destination]) + result = exec_command(["sha1sum", self._destination]) + dlsha1 = result['out'] except OSError: try: - dlsha1 = check_output(["shasum", "-a", "1", self._destination]) + result = exec_command(["shasum", "-a", "1", self._destination]) + dlsha1 = result['out'] except OSError: pass diff --git a/platformio/libmanager.py b/platformio/libmanager.py index b669a74b..ae577d15 100644 --- a/platformio/libmanager.py +++ b/platformio/libmanager.py @@ -90,7 +90,7 @@ class LibraryManager(object): info = self.get_info(id_) rename(tmplib_dir, join(self.lib_dir, "%s_ID%d" % ( - re.sub(r"[^\da-z]+", "_", info['name'], flags=re.I), id_))) + re.sub(r"[^\da-zA-Z]+", "_", info['name']), id_))) telemetry.on_event( category="LibraryManager", action="Install", diff --git a/platformio/util.py b/platformio/util.py index ca4d4512..ea48f79b 100644 --- a/platformio/util.py +++ b/platformio/util.py @@ -146,14 +146,14 @@ def exec_command(*args, **kwargs): result['out'], result['err'] = p.communicate() result['returncode'] = p.returncode except KeyboardInterrupt: + raise exception.AbortedByUser() + finally: for s in ("stdout", "stderr"): if isinstance(kwargs[s], AsyncPipe): kwargs[s].close() - raise exception.AbortedByUser() for s in ("stdout", "stderr"): if isinstance(kwargs[s], AsyncPipe): - kwargs[s].close() result[s[3:]] = "\n".join(kwargs[s].get_buffer()) for k, v in result.iteritems(): diff --git a/scripts/get-platformio.py b/scripts/get-platformio.py index be38fcde..16c7cd1d 100644 --- a/scripts/get-platformio.py +++ b/scripts/get-platformio.py @@ -2,13 +2,14 @@ # See LICENSE for details. import os +import subprocess import sys -from subprocess import check_output +from platform import system from tempfile import NamedTemporaryFile CURINTERPRETER_PATH = os.path.normpath(sys.executable) -IS_WINDOWS = sys.platform.startswith("win") +IS_WINDOWS = system() == "Windows" def fix_winpython_pathenv(): @@ -51,8 +52,21 @@ def fix_winpython_pathenv(): return True +def exec_command(*args, **kwargs): + kwargs['stdout'] = subprocess.PIPE + kwargs['stderr'] = subprocess.PIPE + kwargs['shell'] = IS_WINDOWS + + p = subprocess.Popen(*args, **kwargs) + out, err = p.communicate() + + if p.returncode != 0: + raise Exception(err) + return out + + def exec_python_cmd(args): - return check_output([CURINTERPRETER_PATH] + args, shell=IS_WINDOWS).strip() + return exec_command([CURINTERPRETER_PATH] + args, shell=IS_WINDOWS).strip() def install_pip(): @@ -74,7 +88,10 @@ def install_pip(): def install_pypi_packages(packages): for pipargs in packages: - print (exec_python_cmd(["-m", "pip", "install", "-U"] + pipargs)) + print (exec_python_cmd([ + "-m", + "pip.__main__" if sys.version_info < (2, 7, 0) else "pip", + "install", "-U"] + pipargs)) def main(): @@ -113,10 +130,13 @@ def main(): "successfully FINISHED! <==\n") try: - print (check_output("platformio", shell=IS_WINDOWS)) + print (exec_command("platformio", shell=IS_WINDOWS)) except: try: - print (exec_python_cmd(["-m", "platformio"])) + print (exec_python_cmd([ + "-m", + "platformio.__main__" if sys.version_info < (2, 7, 0) else + "platformio"])) finally: print ("\n Please RESTART your Terminal Application and run " "`platformio --help` command.") diff --git a/tests/commands/test_init.py b/tests/commands/test_init.py index 4364bfe3..3dd9e318 100644 --- a/tests/commands/test_init.py +++ b/tests/commands/test_init.py @@ -40,13 +40,15 @@ def test_init_special_board(platformio_setup, clirunner, validate_cliresult): uno = util.get_boards("uno") config = util.get_project_config() expected_result = [ - ('platform', uno['platform']), - ('framework', uno['framework']), - ('board', 'uno'), - ('targets', 'upload') + ("platform", str(uno['platform'])), + ("framework", str(uno['framework'])), + ("board", "uno"), + ("targets", "upload") ] + assert config.has_section("env:autogen_uno") - assert config.items("env:autogen_uno") == expected_result + assert len(set(expected_result).symmetric_difference( + set(config.items("env:autogen_uno")))) == 0 def test_init_disable_auto_uploading(platformio_setup, clirunner, @@ -58,12 +60,13 @@ def test_init_disable_auto_uploading(platformio_setup, clirunner, validate_pioproject(getcwd()) config = util.get_project_config() expected_result = [ - ('platform', 'atmelavr'), - ('framework', 'arduino'), - ('board', 'uno') + ("platform", "atmelavr"), + ("framework", "arduino"), + ("board", "uno") ] assert config.has_section("env:autogen_uno") - assert config.items("env:autogen_uno") == expected_result + assert len(set(expected_result).symmetric_difference( + set(config.items("env:autogen_uno")))) == 0 def test_init_incorrect_board(clirunner):