32 '\x1b[101mCRITICAL %s\x1b[0m'
34 'Cannot find package "%s".'
39 '\x1b[37mINFO %s\x1b[0m'
41 'Install it using "pip". Then try again.')
54def run(args: argparse.Namespace) -> int:
65 version: str = args.version
67 except AttributeError:
71 log.error(
'Cannot determine the version number of the "pyenv" Python version.')
72 log.info(
'Trying next, set the version number explicitly. See --help.')
77 version = version.strip()
79 props: bool = args.props
81 log.info(
'Creating Python virtual environment in "pyenv":')
82 log.info(
' * Version: {} ({})'.format(version, realm))
84 log.info(
' * Set project properties: {}'.format(props))
93 selected_version = os.path.basename(selected_version_dir)
95 envs_dir = os.path.join(selected_version_dir,
'envs')
96 if not os.path.isdir(envs_dir):
97 log.verbose(
'Creating "{}" folder ...'.format(envs_dir))
100 venv_dir = os.path.join(envs_dir, name)
101 if os.path.isdir(venv_dir):
103 log.verbose(
'Overriding folder "{}" ...'.format(venv_dir))
104 shutil.rmtree(venv_dir)
106 log.verbose(
'Creating folder "{}" ...'.format(venv_dir))
108 python_exe_path = os.path.join(
109 selected_version_dir,
120 log.info(
'This will take some seconds ...')
124 stdout=subprocess.DEVNULL,
125 stderr=subprocess.DEVNULL,
131 'Unexpectedly cannot create ' +
132 'Python virtual environment.'
134 log.info(
'Check/repair Python and "pyenv". Then try again.')
140 venv_version_dir = os.path.join(
141 os.environ[
'PYENV_ROOT'],
143 '{}-{}'.format(name, selected_version)
148 'Overriding junction "{}" → "{}" ...'.format(
153 os.remove(venv_version_dir)
156 'Creating junction "{}" → "{}" ...'.format(
162 if ctypes.windll.shell32.IsUserAnAdmin() == 0:
165 s = input(
"\x1b[94mDo you permit needed 'Administrator' privileges?\x1b[0m [\x1b[92mY\x1b[0m|\x1b[91mn\x1b[0m]: ").strip().upper()
174 cmd =
'powershell -command "Start-Process -FilePath \'{}\' -ArgumentList \'{}\', \'{}\' -Verb runAs -Wait"'.format(
176 os.path.dirname(os.path.abspath(__file__)),
177 'create_junction.bat'
190 cmd =
'powershell -file "{}" "{}" "{}"'.format(
192 os.path.dirname(os.path.abspath(__file__)),
193 'create_junction.ps1'
205 log.success(
'Junction "{}" → "{}" has been created.'.format(
211 'Unexpectedly cannot create junction to Python virtual environment directory.'
214 'Analyze/configure your file access/permissions of the junction or decide to call this script as "Administrator". Then try again.'
223 log.verbose(
'Project properties successfully set.')
225 log.error(
'Cannot set project properties.')
229 'Virtual environment "{}" is installed '.format(name) +
230 'in "pyenv", depending on "Python {}".'.format(selected_version)
258 parser = argparse.ArgumentParser(
260 prog=
'pyenv virtualenv',
261 description=
'Create a version-assigned and named ' +
262 'Python virtual environment in "pyenv".'
268 action=argparse.BooleanOptionalAction,
269 help=
'As project properties, add the files ' +
270 '`.python-version` and `.python-env` to CWD. Default: --no_props.'
275 help=
'Short name of the new Python virtual environment.'
278 return parser.parse_args(), rc
298 parser = argparse.ArgumentParser(
300 prog=
'pyenv virtualenv',
301 description=
'Create a version-assigned and named ' +
302 'Python virtual environment in "pyenv".'
308 action=argparse.BooleanOptionalAction,
309 help=
'As project properties, add the files ' +
310 '`.python-version` and `.python-env` to CWD. Default: --no_props.'
315 help=
'Python version, which must be already installed ' +
321 help=
'Short name of the new Python virtual environment.'
324 return parser.parse_args(), rc
338 os.environ[
'PYENV_ROOT'],
342 'open_doxygen_docs.bat'
351 log.error(
'Displaying Doxygen documentation failed (RC = {}).'.format(rc))
352 log.info(
'Check/reinstall "pyenv-virtualenv". Then try again. ')
366 file_path = os.path.join(
367 os.environ[
'PYENV_ROOT'],
372 if not os.path.isfile(file_path):
373 log.error(
'Cannot display version number.')
374 log.info(
'Reinstall the missing file "{}".'.format(file_path))
377 with open(file_path,
'r')
as f:
378 version = f.read().strip()
380 print(
'"\x1b[92mpyenv-virtualenv\x1b[0m" for Windows \x1b[96m{}\x1b[0m (\x1b[93mpython -m venv\x1b[0m).'.format(
420 args_list = sys.argv.copy()
425 help_requested =
False
426 version_requested =
False
427 docs_requested =
False
429 props_requested =
False
430 for i
in reversed(range(len(args_list))):
432 if arg
in [
'-h',
'--help']:
433 help_requested =
True
435 elif arg
in [
'-v',
'--version']:
436 version_requested =
True
438 elif arg
in [
'-d',
'--docs']:
439 docs_requested =
True
441 elif arg
in [
'-p',
'--props']:
443 props_requested =
True
446 positional_count = len(args_list)
450 elif version_requested:
456 (positional_count
not in [1, 2])
459Usage: pyenv virtualenv [-h] [-v] [-p | --props | --no-props ] [version] [name]
461Create a version-assigned and named Python virtual environment in "pyenv".
463Positional arguments (which can be omitted):
464 [version] Python version, which must be already installed in
465 "pyenv". Default: The global Python version.
466 [name] Short name of the new Python virtual environment.
469 -h, --help Show this help message and exit.
470 -p, --props, --no-props
471 Add the files `.python-version` and `.python-env`
472 as project properties to CWD. Default: --no_props.
473 -v, --version Display the version number of this "pyenv-virtualenv"
474 release and ignore all other arguments.
477 elif positional_count == 1:
480 elif positional_count == 2:
495 except Exception
as exc:
500 '\x1b[91mERROR: Unexpected error "%s".\x1b[0m'
508if __name__ ==
"__main__":
tuple[(str, None), int] selectVersionDir(str ver, str realm, venv_capable=False)
Find the best version, matching the requirements.
int auditPyEnv(str min_ver)
Check if "pyenv" version is greater or equal the given minimal version.
int auditGlobalPythonVersion(str min_ver)
Check if Python version is greater or equal the given minimal version.
str fName(str natural_name)
Convert a natural name into stripped functional name, without spaces, which is lowercase,...
int auditPlatform(str name)
Check if the program in running on the required platform.
(str, str) getPythonVersion()
Get selected global/local Python version in "pyenv".
bool isJunction(str path)
Check if path is a junction, which has been created e.g.
int setProjectProperties(str ver, str env)
Set/override project property files.
notice((str, tuple) msg)
Log notice message colored to console only.
verbose((str, tuple) msg)
Log verbose message colored to console only.
initLogging()
Initialize the logging.
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.
tuple[(argparse.Namespace, None), int] parseCliArguments2()
Parse CLI arguments for this application.
tuple[(argparse.Namespace, None), int] parseCliArguments1()
Parse CLI arguments for this application.
int displayVersionNumber()
Display the version number and generating procedure for "pyenv-virtualenv" for Windows.
int displayDocumentation()
Display the version number and generating procedure for "pyenv-virtualenv" for Windows.
int main()
Main routine of the application.
int run(argparse.Namespace args)
Sub routine to run the application.