pyenv-virtualenv for Windows 1.2
A 'pyenv' plugin to manage Python virtual environments, depending on different Python versions, for various Python projects.
Loading...
Searching...
No Matches
pyenv-virtualenv-props.py
Go to the documentation of this file.
1##
2# @package pyenv-virtualenv-props
3# @file pyenv-virtualenv-props.py
4# @author Michael Paul Korthals
5# @date 2025-07-10
6# @version 1.0.0
7# @copyright © 2025 Michael Paul Korthals. All rights reserved.
8# See License details in the documentation.
9#
10# Utility application to configure the project properties
11# in "pyenv-virtualenv".
12#
13
14# --- IMPORTS ----------------------------------------------------------
15
16# Python
17import argparse
18import os
19import sys
20
21# Avoid colored output problems
22os.system('')
23
24# Community
25try:
26 import virtualenv
27except ImportError():
28 print(
29 '\x1b[101mCRITICAL %s\x1b[0m'
30 %
31 'Cannot find package "%s".'
32 %
33 'virtualenv'
34 )
35 print(
36 '\x1b[37mINFO %s\x1b[0m'
37 %
38 'Install it using "pip". Then try again.')
39 import virtualenv
40
41# My
42import lib.hlp as hlp
43import lib.log as log
44
45
46# --- RUN ---------------------------------------------------------------
47
48## Sub routine to run the application.
49#
50# @param args Parsed command line arguments of this application.
51# @return RC = 0 or other values in case of error.
52def run(args: argparse.Namespace) -> int:
53 rc: int = 0
54 cwd = os.getcwd()
55 # noinspection PyBroadException
56 try:
57 while True:
58 if args.__contains__('select_set'):
59 log.verbose('Setting project properties ...')
60 rc = hlp.setProjectProperties(args.version, args.name)
61 if rc == 0:
62 log.verbose('Project properties successfully set.')
63 else:
64 log.error('Cannot set project properties.')
65 elif args.__contains__('select_unset'):
66 log.verbose('Unsetting project properties ...')
68 if rc == 0:
69 log.verbose('Project properties successfully unset.')
70 else:
71 log.error('Cannot unset project properties.')
72 elif args.__contains__('select_list'):
73 log.verbose('Showing list of project properties ...')
75 if rc == 0:
77 'Project properties list successfully shown.'
78 )
79 else:
80 log.error('Cannot show project properties.')
81 # End if
82 log.verbose(f'Done (RC = {rc}).')
83 # Go on
84 break
85 # End while
86 except:
87 log.error(sys.exc_info())
88 rc = 1
89 finally:
90 os.chdir(cwd)
91 return rc
92
93
94# --- MAIN --------------------------------------------------------------
95
96## Parse CLI arguments for this application.<br>
97# <br>
98# Implement this as required, but don't touch the interface definition
99# for input and output.
100#
101# @return A tuple of:
102# * Namespace to read arguments in "dot" notation or None
103# in case of help or error.
104# * RC = 0 or another value in case of error.
105def parseCliArguments() -> tuple[(argparse.Namespace, None), int]:
106 rc: int = 0
107 # noinspection PyBroadException
108 try:
109 # Create main parser
110 parser = argparse.ArgumentParser(
111# --- BEGIN CHANGE -----------------------------------------------------
112 prog='pyenv virtualenv-props',
113 description='Manage virtual environment-related project ' +
114 'properties.'
115 )
116 # Create subparsers collection
117 subparsers = parser.add_subparsers(help='subcommand help')
118 # Add 1st subparser
119 parser_set = subparsers.add_parser(
120 'set',
121 aliases=['s'],
122 description='Set project properties inside CWD = project folder.'
123 )
124 # Add positional str argument to SET subparser
125 parser_set.add_argument(
126 'version',
127 help='Number of Python version, already installed in "pyenv".'
128 )
129 # Add positional str argument to SET subparser
130 parser_set.add_argument(
131 'name',
132 help='Short name of required Python virtual environment.'
133 )
134 # Add hidden flag
135 parser_set.add_argument(
136 'select_set',
137 action='store_true',
138 help=argparse.SUPPRESS # Don't show in --help
139 )
140 # Add 2nd subparser
141 parser_unset = subparsers.add_parser(
142 'unset',
143 aliases=['u', 'unset', 'unlink', 'd', 'del', 'delete', 'r', 'rm', 'remove'],
144 description='Unset project properties inside CWD = project folder.'
145 )
146 # Add hidden flag
147 parser_unset.add_argument(
148 'select_unset',
149 action='store_true',
150 help=argparse.SUPPRESS # Don't show in --help
151 )
152 # Add 3rd subparser
153 parser_list = subparsers.add_parser(
154 'list',
155 aliases=['l', 'ls'],
156 description='Read and list local project properties.',
157 help='Read and list local project properties.'
158 )
159 # Add flag
160 parser_list.add_argument(
161 'select_list',
162 action='store_true',
163 help=argparse.SUPPRESS # Don't show in --help
164 )
165# --- END CHANGE -------------------------------------------------------
166 return parser.parse_args(), rc
167 except SystemExit:
168 return None, 0 # -h, --help
169 except:
170 log.error(sys.exc_info())
171 return None, 1
172
173## Main routine of the application.
174#
175# @return RC = 0 or other values in case of error.
176def main() -> int:
177 # noinspection PyBroadException
178 try:
179 while True:
180 # Audit the operating system platform
181 rc = hlp.auditPlatform('Windows')
182 if rc != 0:
183 # Deviation: Reject unsupported platform
184 break
185 # Audit the global Python version number
187 if rc != 0:
188 # Deviation: Reject unsupported Python version
189 break
190 # Initialize the colored logging to console
192 # Audit the "pyenv" version number
193 rc = hlp.auditPyEnv('3')
194 if rc != 0:
195 # Deviation: Reject unsupported "pyenv" version
196 break
197 # Parse arguments
198 log.verbose('Parsing arguments ...')
199 args, rc = parseCliArguments()
200 if rc != 0:
201 break
202 if args is None: # -h, --help
203 break
204 log.debug(f'Arguments: {args}')
205 # Run this application
206 log.verbose('Running application ...')
207 rc = run(args)
208 if rc != 0:
209 break
210 # Go on
211 break
212 # End while
213 except Exception as exc:
215 log.error(sys.exc_info())
216 else:
217 print(
218 '\x1b[91mERROR: Unexpected error "%s".\x1b[0m'
219 %
220 str(exc)
221 )
222 rc = 1
223 return rc
224
225
226if __name__ == "__main__":
227 sys.exit(main())
228
229# --- END OF CODE ------------------------------------------------------
230
int auditPyEnv(str min_ver)
Check if "pyenv" version is greater or equal the given minimal version.
Definition hlp.py:213
int auditGlobalPythonVersion(str min_ver)
Check if Python version is greater or equal the given minimal version.
Definition hlp.py:148
int auditPlatform(str name)
Check if the program in running on the required platform.
Definition hlp.py:85
int unsetProjectProperties()
Set project property files.
Definition hlp.py:831
int setProjectProperties(str ver, str env)
Set/override project property files.
Definition hlp.py:611
int listProjectProperties()
Display the table, which shows a list about project properties.
Definition hlp.py:866
debug((str, tuple) msg)
Log debug message colored to console only.
Definition log.py:215
verbose((str, tuple) msg)
Log verbose message colored to console only.
Definition log.py:209
initLogging()
Initialize the logging.
Definition log.py:71
bool isInitialized()
Definition log.py:90
error((str, tuple) msg)
Log error message colored to console only.
Definition log.py:179
tuple[(argparse.Namespace, None), int] parseCliArguments()
Parse CLI arguments for this application.
int run(argparse.Namespace args)
Sub routine to run the application.
int main()
Main routine of the application.