Files
platformio-core/platformio/util.py

147 lines
3.8 KiB
Python
Raw Normal View History

# Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details.
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
from time import sleep
2014-11-22 23:55:17 +02:00
import requests
from serial import Serial
from platformio import __apiurl__, __version__
from platformio.exception import (APIRequestError, GetSerialPortsError,
NotPlatformProject)
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser
2014-07-31 16:20:31 +03:00
def get_systype():
if system() == "Windows":
return "windows"
data = uname()
return ("%s_%s" % (data[0], data[4])).lower()
def get_home_dir():
2014-11-22 23:55:17 +02:00
home_dir = None
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")
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
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
except NotPlatformProject:
pass
return join(get_home_dir(), "lib")
def get_source_dir():
return dirname(realpath(__file__))
def get_project_dir():
return getcwd()
def get_pioenvs_dir():
return getenv("PIOENVS_DIR", join(get_project_dir(), ".pioenvs"))
def get_project_config():
path = join(get_project_dir(), "platformio.ini")
if not isfile(path):
raise NotPlatformProject()
cp = ConfigParser()
cp.read(path)
return cp
def change_filemtime(path, time):
utime(path, (time, time))
2014-06-03 21:27:36 +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)
out, err = p.communicate()
return dict(out=out.strip(), err=err.strip())
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-08 22:02:57 +03:00
def get_api_result(path, params=None, data=None):
result = None
r = None
try:
headers = {"User-Agent": "PlatformIO/%s %s" % (
2014-11-22 23:55:17 +02:00
__version__, requests.utils.default_user_agent())}
2014-09-08 22:02:57 +03:00
if 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)
result = r.json()
r.raise_for_status()
2014-11-22 23:55:17 +02:00
except requests.exceptions.HTTPError as e:
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:
raise APIRequestError(
"Could not connect to PlatformIO Registry Service")
except ValueError:
raise APIRequestError("Invalid response: %s" % r.text)
finally:
if r:
r.close()
return result