Files
platformio-core/platformio/commands/home/helpers.py
T

61 lines
1.7 KiB
Python
Raw Normal View History

2019-04-22 21:07:28 +03:00
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import socket
2019-04-22 21:07:28 +03:00
import requests
from starlette.concurrency import run_in_threadpool
2019-04-22 21:07:28 +03:00
from platformio import util
2021-03-19 00:21:44 +02:00
from platformio.compat import IS_WINDOWS
2019-05-27 22:25:22 +03:00
from platformio.proc import where_is_program
2019-04-22 21:07:28 +03:00
class AsyncSession(requests.Session):
async def request( # pylint: disable=signature-differs,invalid-overridden-method
self, *args, **kwargs
):
2022-04-15 14:44:30 +03:00
func = super().request
return await run_in_threadpool(func, *args, **kwargs)
2019-04-22 21:07:28 +03:00
2019-06-06 00:13:04 +03:00
@util.memoized(expire="60s")
2019-04-22 21:07:28 +03:00
def requests_session():
return AsyncSession()
2019-04-22 21:07:28 +03:00
2019-06-06 00:13:04 +03:00
@util.memoized(expire="60s")
2019-04-22 21:07:28 +03:00
def get_core_fullpath():
2021-03-19 00:21:44 +02:00
return where_is_program("platformio" + (".exe" if IS_WINDOWS else ""))
def is_port_used(host, port):
socket.setdefaulttimeout(1)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2021-03-19 00:21:44 +02:00
if IS_WINDOWS:
try:
s.bind((host, port))
s.close()
return False
except (OSError, socket.error):
pass
else:
try:
s.connect((host, port))
s.close()
except socket.error:
return False
return True