2014-05-18 23:38:59 +03:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
2014-07-27 22:29:32 +03:00
|
|
|
from os import name as os_name
|
2014-11-22 23:55:17 +02:00
|
|
|
from os import getcwd, getenv, makedirs, utime
|
|
|
|
from os.path import dirname, expanduser, isdir, isfile, join, realpath
|
2014-07-31 16:20:31 +03:00
|
|
|
from platform import system, uname
|
2014-06-02 20:59:02 +03:00
|
|
|
from subprocess import PIPE, Popen
|
2014-07-27 22:29:32 +03:00
|
|
|
from time import sleep
|
2014-06-07 13:34:31 +03:00
|
|
|
|
2014-11-22 23:55:17 +02:00
|
|
|
import requests
|
2014-07-27 22:29:32 +03:00
|
|
|
from serial import Serial
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-09-04 18:58:12 +03:00
|
|
|
from platformio import __apiurl__, __version__
|
|
|
|
from platformio.exception import (APIRequestError, GetSerialPortsError,
|
|
|
|
NotPlatformProject)
|
2014-07-27 22:35:40 +03:00
|
|
|
|
2014-05-18 23:38:59 +03:00
|
|
|
try:
|
|
|
|
from configparser import ConfigParser
|
|
|
|
except ImportError:
|
2014-06-07 13:34:31 +03:00
|
|
|
from ConfigParser import ConfigParser
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
|
2014-07-31 16:20:31 +03:00
|
|
|
def get_systype():
|
|
|
|
data = uname()
|
|
|
|
return ("%s_%s" % (data[0], data[4])).lower()
|
2014-06-12 23:29:47 +03:00
|
|
|
|
|
|
|
|
2014-05-18 23:38:59 +03:00
|
|
|
def get_home_dir():
|
2014-11-22 23:55:17 +02:00
|
|
|
home_dir = None
|
|
|
|
|
2014-08-22 17:57:28 +03:00
|
|
|
try:
|
|
|
|
config = get_project_config()
|
|
|
|
if (config.has_section("platformio") and
|
|
|
|
config.has_option("platformio", "home_dir")):
|
2014-11-22 23:55:17 +02:00
|
|
|
home_dir = config.get("platformio", "home_dir")
|
2014-08-22 17:57:28 +03:00
|
|
|
except NotPlatformProject:
|
|
|
|
pass
|
2014-11-22 23:55:17 +02:00
|
|
|
|
|
|
|
if not home_dir:
|
|
|
|
home_dir = expanduser("~/.platformio")
|
|
|
|
|
|
|
|
if not isdir(home_dir):
|
|
|
|
makedirs(home_dir)
|
|
|
|
|
|
|
|
assert isdir(home_dir)
|
|
|
|
return home_dir
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
|
2014-09-04 18:58:12 +03:00
|
|
|
def get_lib_dir():
|
|
|
|
try:
|
|
|
|
config = get_project_config()
|
|
|
|
if (config.has_section("platformio") and
|
|
|
|
config.has_option("platformio", "lib_dir")):
|
2014-10-04 23:30:51 +03:00
|
|
|
lib_dir = config.get("platformio", "lib_dir")
|
|
|
|
if lib_dir.startswith("~"):
|
|
|
|
return expanduser(lib_dir)
|
|
|
|
else:
|
|
|
|
return lib_dir
|
2014-09-04 18:58:12 +03:00
|
|
|
except NotPlatformProject:
|
|
|
|
pass
|
|
|
|
return join(get_home_dir(), "lib")
|
|
|
|
|
|
|
|
|
2014-05-18 23:38:59 +03:00
|
|
|
def get_source_dir():
|
|
|
|
return dirname(realpath(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
def get_project_dir():
|
|
|
|
return getcwd()
|
|
|
|
|
|
|
|
|
2014-07-27 22:35:40 +03:00
|
|
|
def get_pioenvs_dir():
|
|
|
|
return getenv("PIOENVS_DIR", join(get_project_dir(), ".pioenvs"))
|
|
|
|
|
|
|
|
|
2014-05-18 23:38:59 +03:00
|
|
|
def get_project_config():
|
|
|
|
path = join(get_project_dir(), "platformio.ini")
|
|
|
|
if not isfile(path):
|
2014-12-03 12:45:23 +02:00
|
|
|
raise NotPlatformProject(get_project_dir())
|
2014-06-07 13:34:31 +03:00
|
|
|
cp = ConfigParser()
|
|
|
|
cp.read(path)
|
|
|
|
return cp
|
|
|
|
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-06-07 13:34:31 +03:00
|
|
|
def change_filemtime(path, time):
|
|
|
|
utime(path, (time, time))
|
2014-06-03 21:27:36 +03:00
|
|
|
|
|
|
|
|
2014-05-18 23:38:59 +03:00
|
|
|
def exec_command(args):
|
2014-07-31 16:20:31 +03:00
|
|
|
use_shell = system() == "Windows"
|
2014-06-11 21:31:36 +03:00
|
|
|
p = Popen(args, stdout=PIPE, stderr=PIPE, shell=use_shell)
|
2014-05-18 23:38:59 +03:00
|
|
|
out, err = p.communicate()
|
2014-06-07 13:34:31 +03:00
|
|
|
return dict(out=out.strip(), err=err.strip())
|
2014-07-27 22:29:32 +03:00
|
|
|
|
|
|
|
|
|
|
|
def reset_serialport(port):
|
|
|
|
s = Serial(port)
|
|
|
|
s.flushInput()
|
|
|
|
s.setDTR(False)
|
|
|
|
s.setRTS(False)
|
|
|
|
sleep(0.1)
|
|
|
|
s.setDTR(True)
|
|
|
|
s.setRTS(True)
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
|
|
|
|
def get_serialports():
|
|
|
|
if os_name == "nt":
|
|
|
|
from serial.tools.list_ports_windows import comports
|
|
|
|
elif os_name == "posix":
|
|
|
|
from serial.tools.list_ports_posix import comports
|
|
|
|
else:
|
|
|
|
raise GetSerialPortsError(os_name)
|
|
|
|
return[{"port": p, "description": d, "hwid": h} for p, d, h in comports()]
|
2014-09-04 18:58:12 +03:00
|
|
|
|
|
|
|
|
2014-09-08 22:02:57 +03:00
|
|
|
def get_api_result(path, params=None, data=None):
|
2014-09-04 18:58:12 +03:00
|
|
|
result = None
|
|
|
|
r = None
|
2014-12-01 22:45:53 +02:00
|
|
|
|
2014-09-04 18:58:12 +03:00
|
|
|
try:
|
2014-12-24 21:36:11 +02:00
|
|
|
requests.packages.urllib3.disable_warnings()
|
2014-09-04 18:58:12 +03:00
|
|
|
headers = {"User-Agent": "PlatformIO/%s %s" % (
|
2014-11-22 23:55:17 +02:00
|
|
|
__version__, requests.utils.default_user_agent())}
|
2014-12-01 22:45:53 +02:00
|
|
|
# if packages - redirect to SF
|
|
|
|
if path == "/packages":
|
|
|
|
r = requests.get(
|
|
|
|
"https://sourceforge.net/projects/platformio-storage/files/"
|
2014-12-01 23:21:21 +02:00
|
|
|
"packages/manifest.json", params=params, headers=headers)
|
2014-12-01 22:45:53 +02:00
|
|
|
elif data:
|
2014-11-22 23:55:17 +02:00
|
|
|
r = requests.post(__apiurl__ + path, params=params, data=data,
|
|
|
|
headers=headers)
|
2014-09-08 22:02:57 +03:00
|
|
|
else:
|
2014-11-22 23:55:17 +02:00
|
|
|
r = requests.get(__apiurl__ + path, params=params, headers=headers)
|
2014-09-04 18:58:12 +03:00
|
|
|
result = r.json()
|
|
|
|
r.raise_for_status()
|
2014-11-22 23:55:17 +02:00
|
|
|
except requests.exceptions.HTTPError as e:
|
2014-09-04 18:58:12 +03:00
|
|
|
if result and "errors" in result:
|
|
|
|
raise APIRequestError(result['errors'][0]['title'])
|
|
|
|
else:
|
|
|
|
raise APIRequestError(e)
|
2014-11-22 23:55:17 +02:00
|
|
|
except requests.exceptions.ConnectionError:
|
2014-10-19 00:14:11 +03:00
|
|
|
raise APIRequestError(
|
|
|
|
"Could not connect to PlatformIO Registry Service")
|
2014-09-04 18:58:12 +03:00
|
|
|
except ValueError:
|
|
|
|
raise APIRequestError("Invalid response: %s" % r.text)
|
|
|
|
finally:
|
|
|
|
if r:
|
|
|
|
r.close()
|
|
|
|
return result
|