Sub routine to run the application.
56def run(args: argparse.Namespace) -> int:
57 rc: int = 0
58
59 try:
60 while True:
61 corrected: bool = False
62 log.info(
'Checking operation environment.')
63
64
66 if not 'PYENV_ROOT' in os.environ:
67 log.error(
'Cannot find "PYENV_ROOT" in the environment variables.')
68 log.info(
'Possibly "pyenv" for Windows has been uninstalled or damaged.')
69 log.info(
'Possibly you need to install the newest versions of "pyenv" and "pyenv-virtualenv" for Windows.')
70 break
71 log.verbose(
'"PYENV_ROOT" environment variable exists.')
72 pyenv_root_dir = os.environ['PYENV_ROOT'].strip()
73 log.verbose(f
'Checking existence of directory "{pyenv_root_dir}" ...')
74 if not os.path.isdir(pyenv_root_dir):
75 log.error(f
'Cannot find directory "{pyenv_root_dir}".')
76 log.info(
'Possibly "pyenv" for Windows has been uninstalled or damaged.')
77 log.info(
'Possibly you need to install the newest versions of "pyenv" and "pyenv-virtualenv" for Windows.')
78 break
79 log.verbose(f
'Directory "{pyenv_root_dir}" exists.')
81 pve_path1 = os.path.join(
82 pyenv_root_dir,
83 'plugins',
84 'pyenv-virtualenv',
85 'shims'
86 )
87 pve_index = []
88 pve_paths = []
89 paths = os.environ['PATH'].split(';')
90 pev_path1 = os.path.join(
91 pyenv_root_dir,
92 'bin'
93 )
94 pev_path2 = os.path.join(
95 pyenv_root_dir,
96 'shims'
97 )
98 pev_index = []
99 pev_paths = []
100 for i in range(len(paths)):
101 path = paths[i]
102 path = path.strip()
103 if not os.path.isdir(path):
104 log.warning(f
'Directory "{path}" in PATH is not available.')
105 log.info(f
'Please manually correct this deviation afterward, if necessary.')
106 if path == pve_path1:
107 pve_index.append(i)
108 pve_paths.append(path)
109 if path == pev_path1:
110 pev_index.append(i)
111 pev_paths.append(path)
112 if path == pev_path2:
113 pev_index.append(i)
114 pev_paths.append(path)
115
116 if len(pev_index) == 0:
117 log.error(
'Cannot recognize "pyenv" in PATH.')
118 log.info(
'Possibly "pyenv" for Windows has been uninstalled or damaged.')
119 log.info(
'Possibly you need to install the newest versions of "pyenv" and "pyenv-virtualenv" for Windows.')
120 break
121 if len(pve_paths) > 1:
122
123
124
125 for j in reversed(range(len(pve_index[1:]))):
126 removal_index = pve_index[j]
127 path = paths.pop(removal_index)
128 log.warning(f
'Obsolete clone of path "{path}" found in PATH.')
129 log.info(f
'This entry has been automatically removed.')
130
131
132 for k in range(len(pev_index)):
133 index = pev_index[k]
134 if index < removal_index:
135 pev_index[k] -= 1
136
137
138
139 pve_index = pve_index[0:1]
140
141 min_pev_index = min(pev_index)
142 if len(pve_paths) == 0:
143 corrected = True
144
145 cmd = [
146 'setx',
147 'PATH',
148 '{};{}'.format(pve_path1, os.environ['PATH']),
149 '/m'
150 ]
152 cp = subprocess.run(
153 cmd,
154 shell=True
155 )
156 rc = cp.returncode
157 if rc != 0:
158 log.error(f
'Cannot permanently prepend "{pve_path1}" to the PATH environment variable. (RC = {rc}.')
159 log.info(
'Open new console terminal as "Administrator", in which you want to try again.')
160 break
161 elif min_pev_index < pve_index[0]:
162 corrected = True
163
164 paths.pop(pve_index[0])
165
166
167 cmd = [
168 'setx',
169 'PATH',
170 '{};{}'.format(pve_path1, os.environ['PATH']),
171 '/m'
172 ]
174 cp = subprocess.run(
175 cmd,
176 shell=True
177 )
178 rc = cp.returncode
179 if rc != 0:
180 log.error(f
'Cannot permanently prepend "{pve_path1}" to the PATH environment variable. (RC = {rc}.')
181 log.info(
'Open new console terminal as "Administrator", in which you want to try again.')
182 break
183
184
186 if corrected:
187 log.success(f
'Operation environment checked and corrected.')
188 else:
190 log.success(f
'"pyenv-virtualenv" should work as expected.')
191
192 break
193
194 except:
196 rc = 1
197 return rc
198
199
200
201
warning((str, tuple) msg)
Log warning message colored to console only.
verbose((str, tuple) msg)
Log verbose message colored to console only.
success((str, tuple) msg)
Log success message colored to console only.
info((str, tuple) msg)
Log info message colored to console only.
error((str, tuple) msg)
Log error message colored to console only.