Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow forcing to overwrite the registry key. #233

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ $RECYCLE.BIN/
*.msm
*.msp

# IDE specific
.idea/

# Windows shortcuts
*.lnk

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ quit by running `px --quit`. When running in the foreground, use `CTRL-C`.

Px can also be setup to automatically run on startup on Windows with the
`--install` flag. This is done by adding an entry into the Window registry which
can be removed with `--uninstall`.
can be removed with `--uninstall`. You can provide `--force` to the install command
in order to always write the key, even if an existing one is there.

NOTE: Command line parameters passed with `--install` are not saved for use on
startup. The `--save` flag or manual editing of `px.ini` is required to provide
Expand All @@ -344,8 +345,8 @@ Actions:
Values specified on CLI override any values in existing config file
Values not specified on CLI or config file are set to defaults

--install
Add Px to the Windows registry to run on startup
--install [--force]
Add Px to the Windows registry to run on startup. Use '--force' to overwrite a possible existing key.

--uninstall
Remove Px from the Windows registry
Expand Down
2 changes: 1 addition & 1 deletion px/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ def parse_config(self):

if sys.platform == "win32":
if "--install" in sys.argv:
windows.install(get_script_cmd())
windows.install(get_script_cmd(), '--force' in sys.argv)
elif "--uninstall" in sys.argv:
windows.uninstall()

Expand Down
4 changes: 2 additions & 2 deletions px/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
Values specified on CLI override any values in existing config file
Values not specified on CLI or config file are set to defaults

--install
Add Px to the Windows registry to run on startup
--install [--force]
Add Px to the Windows registry to run on startup. Use '--force' to overwrite a possible existing key.

--uninstall
Remove Px from the Windows registry
Expand Down
17 changes: 8 additions & 9 deletions px/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@
###
# Install Px to startup

def check_installed():
def is_installed():
"Check if Px is already installed in the Windows registry"
ret = True
runkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Run", 0, winreg.KEY_READ)
try:
winreg.QueryValueEx(runkey, "Px")
except FileNotFoundError:
ret = False
winreg.CloseKey(runkey)

return ret
return False
finally:
winreg.CloseKey(runkey)
return True

def install(script_cmd):
def install(script_cmd, force_overwrite):
"Install Px to Windows registry if not already"
if check_installed() is False:
if not is_installed() or force_overwrite:
runkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Run", 0,
winreg.KEY_WRITE)
Expand All @@ -47,7 +46,7 @@ def install(script_cmd):

def uninstall():
"Uninstall Px from Windows registry if installed"
if check_installed() is True:
if is_installed() is True:
runkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Run", 0,
winreg.KEY_WRITE)
Expand Down