Export_3ds: Added distance cue chunk export
[blender-addons.git] / mesh_tissue / utils_pip.py
blob9b731de6297cba7bb7026bbb96b3e40e11f34029
1 # SPDX-FileCopyrightText: 2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # ----------------------------------------------------------
6 # Author: Stephen Leger (s-leger)
8 # ----------------------------------------------------------
9 import bpy
10 import subprocess
11 import sys
14 PYPATH = sys.executable #bpy.app.binary_path_python
17 class Pip:
19 def __init__(self):
20 self._ensurepip()
22 @staticmethod
23 def _ensure_user_site_package():
24 import os
25 import site
26 import sys
27 site_package = site.getusersitepackages()
28 if not os.path.exists(site_package):
29 site_package = bpy.utils.user_resource('SCRIPTS', path="site_package", create=True)
30 site.addsitedir(site_package)
31 if site_package not in sys.path:
32 sys.path.append(site_package)
33 '''
34 @staticmethod
35 def _ensure_user_site_package():
36 import os
37 import site
38 import sys
39 site_package = site.getusersitepackages()
40 if os.path.exists(site_package):
41 if site_package not in sys.path:
42 sys.path.append(site_package)
43 else:
44 site_package = bpy.utils.user_resource('SCRIPTS', "site_package", create=True)
45 site.addsitedir(site_package)
46 '''
47 def _cmd(self, action, options, module):
48 if options is not None and "--user" in options:
49 self._ensure_user_site_package()
51 cmd = [PYPATH, "-m", "pip", action]
53 if options is not None:
54 cmd.extend(options.split(" "))
56 cmd.append(module)
57 return self._run(cmd)
59 def _popen(self, cmd):
60 popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
61 for stdout_line in iter(popen.stdout.readline, ""):
62 yield stdout_line
63 popen.stdout.close()
64 popen.wait()
66 def _run(self, cmd):
67 res = False
68 status = ""
69 for line in self._popen(cmd):
70 if "ERROR:" in line:
71 status = line.strip()
72 if "Error:" in line:
73 status = line.strip()
74 print(line)
75 if "Successfully" in line:
76 status = line.strip()
77 res = True
78 return res, status
80 def _ensurepip(self):
81 pip_not_found = False
82 try:
83 import pip
84 except ImportError:
85 pip_not_found = True
86 pass
87 if pip_not_found:
88 self._run([PYPATH, "-m", "ensurepip", "--default-pip"])
90 @staticmethod
91 def upgrade_pip():
92 return Pip()._cmd("install", "--upgrade", "pip")
94 @staticmethod
95 def uninstall(module, options=None):
96 """
97 :param module: string module name with requirements see:[1]
98 :param options: string command line options see:[2]
99 :return: True on uninstall, False if already removed, raise on Error
100 [1] https://pip.pypa.io/en/stable/reference/pip_install/#id29
101 [2] https://pip.pypa.io/en/stable/reference/pip_install/#id47
103 if options is None or options.strip() == "":
104 # force confirm
105 options = "-y"
106 return Pip()._cmd("uninstall", options, module)
108 @staticmethod
109 def install(module, options=None):
111 :param module: string module name with requirements see:[1]
112 :param options: string command line options see:[2]
113 :return: True on install, False if already there, raise on Error
114 [1] https://pip.pypa.io/en/stable/reference/pip_install/#id29
115 [2] https://pip.pypa.io/en/stable/reference/pip_install/#id47
117 if options is None or options.strip() == "":
118 # store in user writable directory, use wheel, without deps
119 options = "--user --only-binary all --no-deps"
120 return Pip()._cmd("install", options, module)
122 @staticmethod
123 def blender_version():
125 :return: blender version tuple
127 return bpy.app.version
129 @staticmethod
130 def python_version():
132 :return: python version object
134 import sys
135 # version.major, version.minor, version.micro
136 return sys.version_info