Skip to content

Commit 2b0417f

Browse files
Updated info plugin to exclude guessed venv paths (#8286)
* Updated info plugin to hide usernames * Updated info plugin to show active venv directory * Updated info plugin to exclude possible venv paths * Added exception handling in info plugin
1 parent 2b42860 commit 2b0417f

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

material/plugins/info/plugin.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1919
# IN THE SOFTWARE.
2020

21+
import getpass
2122
import glob
2223
import json
2324
import logging
@@ -200,6 +201,19 @@ def on_config(self, config):
200201
if path.startswith(os.getcwd()):
201202
self.exclusion_patterns.append(_resolve_pattern(path))
202203

204+
# Guess other Virtual Environment paths in case we forget to activate
205+
# them or in case we have multiple. Making sure which venv is activated
206+
# is not necessary, as it is an optional step in the guidelines.
207+
for path in glob.iglob(
208+
pathname = "**/pyvenv.cfg",
209+
root_dir = os.getcwd(),
210+
recursive = True
211+
):
212+
path = os.path.join(os.getcwd(), os.path.dirname(path))
213+
if path not in site.PREFIXES:
214+
print(f"Possible inactive venv: {path}")
215+
self.exclusion_patterns.append(_resolve_pattern(path))
216+
203217
# Exclude site_dir for projects
204218
if projects_plugin:
205219
for path in glob.iglob(
@@ -266,6 +280,12 @@ def on_config(self, config):
266280
]))
267281
)
268282

283+
# Try to get login to replace it with USERNAME placeholder
284+
try:
285+
username = getpass.getuser()
286+
except Exception:
287+
username = ""
288+
269289
# Add information on platform
270290
f.writestr(
271291
os.path.join(example, "platform.json"),
@@ -280,12 +300,13 @@ def on_config(self, config):
280300
*sys.argv[1:]
281301
]),
282302
"env:$PYTHONPATH": os.getenv("PYTHONPATH", ""),
303+
"env:$VIRTUAL_ENV": os.getenv("VIRTUAL_ENV", ""),
283304
"sys.path": sys.path,
284305
"excluded_entries": self.excluded_entries
285306
},
286307
default = str,
287308
indent = 2
288-
)
309+
).replace(username, "USERNAME")
289310
)
290311

291312
# Retrieve list of processed files

src/plugins/info/plugin.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1919
# IN THE SOFTWARE.
2020

21+
import getpass
2122
import glob
2223
import json
2324
import logging
@@ -200,6 +201,19 @@ def on_config(self, config):
200201
if path.startswith(os.getcwd()):
201202
self.exclusion_patterns.append(_resolve_pattern(path))
202203

204+
# Guess other Virtual Environment paths in case we forget to activate
205+
# them or in case we have multiple. Making sure which venv is activated
206+
# is not necessary, as it is an optional step in the guidelines.
207+
for path in glob.iglob(
208+
pathname = "**/pyvenv.cfg",
209+
root_dir = os.getcwd(),
210+
recursive = True
211+
):
212+
path = os.path.join(os.getcwd(), os.path.dirname(path))
213+
if path not in site.PREFIXES:
214+
print(f"Possible inactive venv: {path}")
215+
self.exclusion_patterns.append(_resolve_pattern(path))
216+
203217
# Exclude site_dir for projects
204218
if projects_plugin:
205219
for path in glob.iglob(
@@ -266,6 +280,12 @@ def on_config(self, config):
266280
]))
267281
)
268282

283+
# Try to get login to replace it with USERNAME placeholder
284+
try:
285+
username = getpass.getuser()
286+
except Exception:
287+
username = ""
288+
269289
# Add information on platform
270290
f.writestr(
271291
os.path.join(example, "platform.json"),
@@ -280,12 +300,13 @@ def on_config(self, config):
280300
*sys.argv[1:]
281301
]),
282302
"env:$PYTHONPATH": os.getenv("PYTHONPATH", ""),
303+
"env:$VIRTUAL_ENV": os.getenv("VIRTUAL_ENV", ""),
283304
"sys.path": sys.path,
284305
"excluded_entries": self.excluded_entries
285306
},
286307
default = str,
287308
indent = 2
288-
)
309+
).replace(username, "USERNAME")
289310
)
290311

291312
# Retrieve list of processed files

0 commit comments

Comments
 (0)