From be9aaf8902659587542047bf51b240efa5a65b23 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Thu, 17 Oct 2019 20:57:40 +0300 Subject: [PATCH] Be compatible with Python 3.8, on Windows skip HOME and check for USERPROFILE --- platformio/builder/tools/piolib.py | 14 +++----------- platformio/commands/ci.py | 4 ++-- platformio/fs.py | 9 +++++++++ platformio/home/rpc/handlers/app.py | 6 +++--- platformio/home/rpc/handlers/os.py | 6 +++--- platformio/home/rpc/handlers/project.py | 4 ++-- platformio/ide/projectgenerator.py | 4 ++-- platformio/project/config.py | 6 +++--- platformio/project/helpers.py | 6 +++--- platformio/project/options.py | 4 +++- 10 files changed, 33 insertions(+), 30 deletions(-) diff --git a/platformio/builder/tools/piolib.py b/platformio/builder/tools/piolib.py index b27fbf9b..7f5484ce 100644 --- a/platformio/builder/tools/piolib.py +++ b/platformio/builder/tools/piolib.py @@ -22,16 +22,7 @@ import hashlib import os import re import sys -from os.path import ( - basename, - commonprefix, - expanduser, - isdir, - isfile, - join, - realpath, - sep, -) +from os.path import basename, commonprefix, isdir, isfile, join, realpath, sep import click import SCons.Scanner # pylint: disable=import-error @@ -943,7 +934,8 @@ def GetLibSourceDirs(env): items = env.GetProjectOption("lib_extra_dirs", []) items.extend(env["LIBSOURCE_DIRS"]) return [ - env.subst(expanduser(item) if item.startswith("~") else item) for item in items + env.subst(fs.expanduser(item) if item.startswith("~") else item) + for item in items ] diff --git a/platformio/commands/ci.py b/platformio/commands/ci.py index 6b98aacd..7228d001 100644 --- a/platformio/commands/ci.py +++ b/platformio/commands/ci.py @@ -14,7 +14,7 @@ from glob import glob from os import getenv, makedirs, remove -from os.path import abspath, basename, expanduser, isdir, isfile, join +from os.path import abspath, basename, isdir, isfile, join from shutil import copyfile, copytree from tempfile import mkdtemp @@ -34,7 +34,7 @@ def validate_path(ctx, param, value): # pylint: disable=unused-argument value = list(value) for i, p in enumerate(value): if p.startswith("~"): - value[i] = expanduser(p) + value[i] = fs.expanduser(p) value[i] = abspath(value[i]) if not glob(value[i]): invalid_path = p diff --git a/platformio/fs.py b/platformio/fs.py index c81a50db..23edb634 100644 --- a/platformio/fs.py +++ b/platformio/fs.py @@ -171,6 +171,15 @@ def to_unix_path(path): return re.sub(r"[\\]+", "/", path) +def expanduser(path): + """ + Be compatible with Python 3.8, on Windows skip HOME and check for USERPROFILE + """ + if not WINDOWS or not path.startswith("~") or "USERPROFILE" not in os.environ: + return os.path.expanduser(path) + return os.environ["USERPROFILE"] + path[1:] + + def rmtree(path): def _onerror(func, path, __): try: diff --git a/platformio/home/rpc/handlers/app.py b/platformio/home/rpc/handlers/app.py index cc3be81f..1fd49e22 100644 --- a/platformio/home/rpc/handlers/app.py +++ b/platformio/home/rpc/handlers/app.py @@ -14,9 +14,9 @@ from __future__ import absolute_import -from os.path import expanduser, join +from os.path import join -from platformio import __version__, app, util +from platformio import __version__, app, fs, util from platformio.project.helpers import get_project_core_dir, is_platformio_project @@ -54,7 +54,7 @@ class AppRPC(object): for name, data in app.DEFAULT_SETTINGS.items() } - storage["homeDir"] = expanduser("~") + storage["homeDir"] = fs.expanduser("~") storage["projectsDir"] = storage["coreSettings"]["projects_dir"]["value"] # skip non-existing recent projects diff --git a/platformio/home/rpc/handlers/os.py b/platformio/home/rpc/handlers/os.py index 7f5f5ece..92f4a008 100644 --- a/platformio/home/rpc/handlers/os.py +++ b/platformio/home/rpc/handlers/os.py @@ -19,12 +19,12 @@ import glob import os import shutil from functools import cmp_to_key -from os.path import expanduser, isdir, isfile, join +from os.path import isdir, isfile, join import click from twisted.internet import defer # pylint: disable=import-error -from platformio import app, util +from platformio import app, fs, util from platformio.compat import PY2, get_filesystem_encoding from platformio.home import helpers @@ -126,7 +126,7 @@ class OSRPC(object): items = [] if path.startswith("~"): - path = expanduser(path) + path = fs.expanduser(path) if not isdir(path): return items for item in os.listdir(path): diff --git a/platformio/home/rpc/handlers/project.py b/platformio/home/rpc/handlers/project.py index bb320ab9..aa60058e 100644 --- a/platformio/home/rpc/handlers/project.py +++ b/platformio/home/rpc/handlers/project.py @@ -17,7 +17,7 @@ from __future__ import absolute_import import os import shutil import time -from os.path import basename, expanduser, getmtime, isdir, isfile, join, realpath, sep +from os.path import basename, getmtime, isdir, isfile, join, realpath, sep import jsonrpc # pylint: disable=import-error @@ -56,7 +56,7 @@ class ProjectRPC(object): # skip non existing folders and resolve full path for key in ("envLibdepsDirs", "libExtraDirs"): data[key] = [ - expanduser(d) if d.startswith("~") else realpath(d) + fs.expanduser(d) if d.startswith("~") else realpath(d) for d in data[key] if isdir(d) ] diff --git a/platformio/ide/projectgenerator.py b/platformio/ide/projectgenerator.py index b297e37b..d4e13e5a 100644 --- a/platformio/ide/projectgenerator.py +++ b/platformio/ide/projectgenerator.py @@ -15,7 +15,7 @@ import io import os import sys -from os.path import abspath, basename, expanduser, isdir, isfile, join, relpath +from os.path import abspath, basename, isdir, isfile, join, relpath import bottle @@ -64,7 +64,7 @@ class ProjectGenerator(object): "project_name": basename(self.project_dir), "project_dir": self.project_dir, "env_name": self.env_name, - "user_home_dir": abspath(expanduser("~")), + "user_home_dir": abspath(fs.expanduser("~")), "platformio_path": sys.argv[0] if isfile(sys.argv[0]) else where_is_program("platformio"), diff --git a/platformio/project/config.py b/platformio/project/config.py index 988b53c2..4d22ea57 100644 --- a/platformio/project/config.py +++ b/platformio/project/config.py @@ -20,7 +20,7 @@ from hashlib import sha1 import click -from platformio import exception +from platformio import exception, fs from platformio.compat import WINDOWS, hashlib_encode_data from platformio.project.options import ProjectOptions @@ -106,7 +106,7 @@ class ProjectConfigBase(object): # load extra configs for pattern in self.get("platformio", "extra_configs", []): if pattern.startswith("~"): - pattern = os.path.expanduser(pattern) + pattern = fs.expanduser(pattern) for item in glob.glob(pattern): self.read(item) @@ -380,7 +380,7 @@ class ProjectConfigDirsMixin(object): ) if result.startswith("~"): - result = os.path.expanduser(result) + result = fs.expanduser(result) result = os.path.realpath(result) diff --git a/platformio/project/helpers.py b/platformio/project/helpers.py index 5b921e9d..fbf86199 100644 --- a/platformio/project/helpers.py +++ b/platformio/project/helpers.py @@ -16,11 +16,11 @@ import json import os from hashlib import sha1 from os import walk -from os.path import dirname, expanduser, isdir, isfile, join +from os.path import dirname, isdir, isfile, join from click.testing import CliRunner -from platformio import __version__, exception +from platformio import __version__, exception, fs from platformio.compat import WINDOWS, hashlib_encode_data from platformio.project.config import ProjectConfig @@ -90,7 +90,7 @@ def get_project_libdeps_dir(): def get_default_projects_dir(): - docs_dir = join(expanduser("~"), "Documents") + docs_dir = join(fs.expanduser("~"), "Documents") try: assert WINDOWS import ctypes.wintypes # pylint: disable=import-outside-toplevel diff --git a/platformio/project/options.py b/platformio/project/options.py index f7294c40..02762783 100644 --- a/platformio/project/options.py +++ b/platformio/project/options.py @@ -19,6 +19,8 @@ from collections import OrderedDict, namedtuple import click +from platformio import fs + ConfigOptionClass = namedtuple( "ConfigOption", [ @@ -77,7 +79,7 @@ ProjectOptions = OrderedDict( name="core_dir", oldnames=["home_dir"], sysenvvar="PLATFORMIO_CORE_DIR", - default=os.path.join(os.path.expanduser("~"), ".platformio"), + default=os.path.join(fs.expanduser("~"), ".platformio"), ), ConfigPlatformioOption( name="globallib_dir",