Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf44e097

Browse files
authored
Makes uninstall --purge also remove PATH entry. (#112)
Fixes#95
1 parenta2ccd85 commitf44e097

File tree

3 files changed

+107
-25
lines changed

3 files changed

+107
-25
lines changed

‎src/manage/uninstall_command.py‎

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,75 @@
33
from .installsimportget_matching_install_tags
44
from .install_commandimportSHORTCUT_HANDLERS,update_all_shortcuts
55
from .loggingimportLOGGER
6-
from .pathutilsimportPurePath
6+
from .pathutilsimportPath,PurePath
77
from .tagutilsimporttag_or_range
88

99

1010
def_iterdir(p,only_files=False):
1111
try:
1212
ifonly_files:
13-
return [fforfinp.iterdir()ifp.is_file()]
14-
returnlist(p.iterdir())
13+
return [fforfinPath(p).iterdir()iff.is_file()]
14+
returnlist(Path(p).iterdir())
1515
exceptFileNotFoundError:
1616
LOGGER.debug("Skipping %s because it does not exist",p)
1717
return []
1818

1919

20+
def_do_purge_global_dir(global_dir,warn_msg,*,hive=None,subkey="Environment"):
21+
importos
22+
importwinreg
23+
24+
ifhiveisNone:
25+
hive=winreg.HKEY_CURRENT_USER
26+
try:
27+
withwinreg.OpenKeyEx(hive,subkey)askey:
28+
path,kind=winreg.QueryValueEx(key,"Path")
29+
ifkindnotin (winreg.REG_SZ,winreg.REG_EXPAND_SZ):
30+
raiseValueError("Value kind is not a string")
31+
except (OSError,ValueError):
32+
LOGGER.debug("Not removing global commands directory from PATH",exc_info=True)
33+
else:
34+
LOGGER.debug("Current PATH contains %s",path)
35+
paths=path.split(";")
36+
newpaths= []
37+
forpinpaths:
38+
# We should expand entries here, but we only want to remove those
39+
# that we added ourselves (during firstrun), and we never use
40+
# environment variables. So even if the kind is REG_EXPAND_SZ, we
41+
# don't need to expand to find our own entry.
42+
#ep = os.path.expandvars(p) if kind == winreg.REG_EXPAND_SZ else p
43+
ep=p
44+
ifPurePath(ep).match(global_dir):
45+
LOGGER.debug("Removing from PATH: %s",p)
46+
else:
47+
newpaths.append(p)
48+
iflen(newpaths)<len(paths):
49+
newpath=";".join(newpaths)
50+
withwinreg.CreateKeyEx(hive,subkey,access=winreg.KEY_READ|winreg.KEY_WRITE)askey:
51+
path2,kind2=winreg.QueryValueEx(key,"Path")
52+
ifpath2==pathandkind2==kind:
53+
LOGGER.info("Removing global commands directory from PATH")
54+
LOGGER.debug("New PATH contains %s",newpath)
55+
winreg.SetValueEx(key,"Path",0,kind,newpath)
56+
else:
57+
LOGGER.debug("Not removing global commands directory from PATH "
58+
"because the registry changed while processing.")
59+
60+
try:
61+
from_nativeimportbroadcast_settings_change
62+
broadcast_settings_change()
63+
except (ImportError,OSError):
64+
LOGGER.debug("Did not broadcast settings change notification",
65+
exc_info=True)
66+
67+
ifnotglobal_dir.is_dir():
68+
return
69+
LOGGER.info("Purging global commands from %s",global_dir)
70+
forfin_iterdir(global_dir):
71+
LOGGER.debug("Purging %s",f)
72+
rmtree(f,after_5s_warning=warn_msg)
73+
74+
2075
defexecute(cmd):
2176
LOGGER.debug("BEGIN uninstall_command.execute: %r",cmd.args)
2277

@@ -31,28 +86,28 @@ def execute(cmd):
3186
cmd.tags= []
3287

3388
ifcmd.purge:
34-
ifcmd.ask_yn("Uninstall all runtimes?"):
35-
foriininstalled:
36-
LOGGER.info("Purging %s from %s",i["display-name"],i["prefix"])
37-
try:
38-
rmtree(
39-
i["prefix"],
40-
after_5s_warning=warn_msg.format(i["display-name"]),
41-
remove_ext_first=("exe","dll","json")
42-
)
43-
exceptFilesInUseError:
44-
LOGGER.warn("Unable to purge %s because it is still in use.",
45-
i["display-name"])
46-
continue
47-
LOGGER.info("Purging saved downloads from %s",cmd.download_dir)
48-
rmtree(cmd.download_dir,after_5s_warning=warn_msg.format("cached downloads"))
49-
LOGGER.info("Purgingglobal commands from %s",cmd.global_dir)
50-
forfin_iterdir(cmd.global_dir):
51-
LOGGER.debug("Purging %s",f)
52-
rmtree(f,after_5s_warning=warn_msg.format("global commands"))
53-
LOGGER.info("Purging all shortcuts")
54-
for_,cleanupinSHORTCUT_HANDLERS.values():
55-
cleanup(cmd, [])
89+
ifnotcmd.ask_yn("Uninstall all runtimes?"):
90+
LOGGER.debug("END uninstall_command.execute")
91+
return
92+
foriininstalled:
93+
LOGGER.info("Purging %s from %s",i["display-name"],i["prefix"])
94+
try:
95+
rmtree(
96+
i["prefix"],
97+
after_5s_warning=warn_msg.format(i["display-name"]),
98+
remove_ext_first=("exe","dll","json")
99+
)
100+
exceptFilesInUseError:
101+
LOGGER.warn("Unable to purge %s because it is still in use.",
102+
i["display-name"])
103+
continue
104+
LOGGER.info("Purgingsaved downloads from %s",cmd.download_dir)
105+
rmtree(cmd.download_dir,after_5s_warning=warn_msg.format("cached downloads"))
106+
# Purge global commands directory
107+
_do_purge_global_dir(cmd.global_dir,warn_msg.format("global commands"))
108+
LOGGER.info("Purging all shortcuts")
109+
for_,cleanupinSHORTCUT_HANDLERS.values():
110+
cleanup(cmd, [])
56111
LOGGER.debug("END uninstall_command.execute")
57112
return
58113

‎tests/conftest.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ def setup(self, _subkey=None, **keys):
205205
else:
206206
raiseTypeError("unsupported type in registry")
207207

208+
defgetvalue(self,subkey,valuename):
209+
withwinreg.OpenKeyEx(self.key,subkey)askey:
210+
returnwinreg.QueryValueEx(key,valuename)[0]
211+
212+
defgetvalueandkind(self,subkey,valuename):
213+
withwinreg.OpenKeyEx(self.key,subkey)askey:
214+
returnwinreg.QueryValueEx(key,valuename)
215+
208216

209217
@pytest.fixture(scope='function')
210218
defregistry():

‎tests/test_uninstall_command.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
importos
2+
importpytest
3+
importwinreg
4+
5+
frompathlibimportPath
6+
7+
frommanageimportuninstall_commandasUC
8+
9+
10+
deftest_purge_global_dir(monkeypatch,registry,tmp_path):
11+
registry.setup(Path=rf"C:\A;{tmp_path}\X;{tmp_path};C:\B;%PTH%;C:\%D%\E")
12+
(tmp_path/"test.txt").write_bytes(b"")
13+
(tmp_path/"test2.txt").write_bytes(b"")
14+
15+
monkeypatch.setitem(os.environ,"PTH",str(tmp_path))
16+
UC._do_purge_global_dir(tmp_path,"SLOW WARNING",hive=registry.hive,subkey=registry.root)
17+
assertregistry.getvalueandkind("","Path")== (
18+
rf"C:\A;{tmp_path}\X;C:\B;%PTH%;C:\%D%\E",winreg.REG_SZ)
19+
assertnotlist(tmp_path.iterdir())

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp