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-07-27 22:35:40 +03:00
|
|
|
from os import getcwd, getenv, listdir, utime
|
2014-06-02 20:59:02 +03:00
|
|
|
from os.path import dirname, expanduser, 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-07-27 22:29:32 +03:00
|
|
|
from serial import Serial
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-07-27 22:35:40 +03:00
|
|
|
from platformio.exception import GetSerialPortsError, NotPlatformProject
|
|
|
|
|
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():
|
|
|
|
if system() == "Windows":
|
|
|
|
return "windows"
|
|
|
|
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-08-22 17:57:28 +03:00
|
|
|
try:
|
|
|
|
config = get_project_config()
|
|
|
|
if (config.has_section("platformio") and
|
|
|
|
config.has_option("platformio", "home_dir")):
|
|
|
|
return config.get("platformio", "home_dir")
|
|
|
|
except NotPlatformProject:
|
|
|
|
pass
|
2014-08-23 11:51:18 +03:00
|
|
|
return expanduser("~/.platformio")
|
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-06-07 13:34:31 +03:00
|
|
|
raise NotPlatformProject()
|
|
|
|
cp = ConfigParser()
|
|
|
|
cp.read(path)
|
|
|
|
return cp
|
|
|
|
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-06-12 23:29:47 +03:00
|
|
|
def get_platforms():
|
|
|
|
platforms = []
|
|
|
|
for p in listdir(join(get_source_dir(), "platforms")):
|
2014-07-31 16:58:49 +03:00
|
|
|
if p in ("__init__.py", "base.py") or not p.endswith(".py"):
|
2014-06-12 23:29:47 +03:00
|
|
|
continue
|
|
|
|
platforms.append(p[:-3])
|
|
|
|
return platforms
|
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()]
|