macOS Sierra - how to enable "Allow Remote Automation" using command line - python

I am working on an automation stuff in macOS Sierra (10.12.2). By using python's atomac support I can launch the safari browser and make the settings enabled via Safari -> Preferences -> Advanced -> check "enable Develop Menu" and then select "Develop -> Allow Remote Automation". Looks like this is not so consistent for automation perspective. I would like to know if there is any shell command to make this possible.

Hmm ok, so as per my comment:
You could do defaults write com.apple.Safari IncludeDevelopMenu YES to activate the development menu via terminal. You could get python to execute this command for you using the subprocess module.
According to this this question it seems that one used to be able to simply do defaults write com.apple.Safari AllowRemoteAutomation 1. However, since Safari 10 this option has apparently been subjected to a higher security protocol or something (maybe because of the System Integrity Protection that was introduced). I've been looking for the plistfile that now holds the setting, but I have not been able to find it. Maybe it's not even there in an actual plistfile anymore.
I guess this leaves you with 2 options:
Use apple's osascript to simulate the needed mouseclicks.
Repost your question on AskDifferent or Apple's own developers forum, since you are more likely to find someone with in-depth knowledge of macOS.

I found the answer from: https://developer.apple.com/documentation/webkit/testing_with_webdriver_in_safari
safaridriver --enable
It will prompt for an admin password, so some people setup passwordless sudo (dangerous) or do another workaround like calling from an Applescript
I tested that this works with Mojave and Safari 12.0.3 and verified that it works regardless of if the Develop menu is enabled, though you probably want to save yourself the clicks and just enable that too:
defaults write com.apple.Safari IncludeDevelopMenu 1

Related

Check if windows 10 system needs a restart after automatic update using python

I have a bunch of python script that run on a scheduled basis in a windows 10 based system, sometimes after windows 10 automatic update, the OS will ask for a restart to finish the update and after some time it would restart automatically if not done manually which might mess with automated python script runs.
I am looking for a pythonic solution where I would query the OS if it needs a restart and upon getting the appropriate response I would trigger the appropriate solution
import necessary_libraries
isRestartRequired = check_if_restart_is_needed() // returns true or false
if isRestartRequired == True:
notifyUser()
Is this programmatically possible with python?
The following seems to work for me:
Install the PowerShell script Reboot pending:
Install-Module -Name PendingReboot
Check it works in Powershell:
Test-Pending Reboot
Call it from python using:
import subprocess
def restart_needed()->bool:
"""Uses Windows Powershell tool to figure out if a windows
reboot is pending. True indicates one is due."""
cmd:list[str]=["powershell","-Command","Test-PendingReboot"]
res:bytes=subprocess.run(cmd,capture_output=True)
output_text:str=res.stdout.decode("utf-8").strip()
restart:bool=output_text.endswith("True")
return restart
I personally don't think there is a dedicated library for that. I also don't think you should check that for updates individually, as that will likely require searching for the information about each and every update online (https://learn.microsoft.com/en-us/answers/questions/308547/how-to-know-if-a-windows-update-will-require-a-reb.html).
So you'll likely have to figure out a way how to determine whether Windows is going to reboot or not, and then manually perform that check (by running a system command or utility) in Python. Now your question sounds more like "Which mechanisms Windows use to reboot the machine during the update, and how to check whether any of them is invoked". Please note that there might be a bunch of such mechanisms, and they are most probably undocumented and may change in future. You can learn the basics on MS website, but they don't provide good amount of details there (https://learn.microsoft.com/en-us/windows/deployment/update/how-windows-update-works).
You can check the list of possible reboot status locations in How can I check for a pending reboot? (it looks quite promising), and implement these checks in Python afterwards using some function that allows you to check the output (Running shell command and capturing the output).
If you decide to use that approach, please capture a PC that requires a reboot and verify that one of the 4 sources mentioned in the answer indeed contains the reboot flag. I did it for my laptop yesterday - it was pending reboot, and some entries indeed contained that marker. Make sure you check that for your infrastructure as well.
P.S. If you have full control upon your entire infrastructure, you might look into PowerShell Gallery PendingReboot module mentioned in the referenced SO post (if it's installed on the machine, you can use just one command instead of four), or make yourself familiar with brilliant (and completely useless probably) opinion of MSDN guy https://devblogs.microsoft.com/oldnewthing/20151203-00/?p=92261 : In this specific case, the idea would be to change the design from “Install the update, and then postpone the reboot until a convenient time” to “Wait for a convenient time, then install the update and reboot immediately.”

Runaway Jedi Language task in VSCode

Every time I close VSCode (after running some Python script) I have a Python task that lingers in my terminal running 90-100% CPU on my M1 MacBook Air, and I have to manually kill it every time. I ran the following on the PID:
ps aux | grep <PID>
and it returns the same thing every time...
Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/Resources/Python.app/Contents/MacOS/Python /Users/<user>/.vscode/extensions/ms-python.python-2021.2.625869727/pythonFiles/runJediLanguageServer.py
I checked my VSCode extensions, and I don't have any "Jedi" ones installed, but maybe it's a lingering task from some other extension.
Any ideas how to fix this?
The solution as provided in the link Valy provided above is to:
Open the command palette (View > Command Palette...)
Run the "Preferences: Open Settings (JSON)" command. This will open the user settings in VS code
Paste the following line in the settings file:
"python.experiments.optOutFrom": ["pythonJediLSP"]
Reload the window (either by closing VS Code and opening it again or running the "Developer: Reload Window" command from the command palette)
reference: Rogue Python Processes with High CPU Consumption issue #15586
I had the same issue as apparently VSC is experimenting with a new feature. Another user helped me find the solution, which can be found here: https://github.com/microsoft/vscode-python/issues/15586#issuecomment-792360066
When using python in VS Code, it requires us to install the "python" extension, and it will automatically load the corresponding language service to better identify and analyze the code.
"Visual Studio Code provides smart editing features for different programming languages through Language Extensions. VS Code doesn't provide built-in language support but offers a set of APIs that enable rich language features."
We can use different python language services: Jedi, Microsoft, Pylance, and so on, and VS Code uses Jedi by default:
Reference:Language Server Extension Guide and Language Extensions Overview.

IDLE's subprocess didn't make a connection. Either IDLE can't start or personal firewall software is blocking connection

I have tried uninstalling it and have searched other answers. None of them have worked; IDLE opens, but I can't run anything I write.
In Windows 10
1. Type in "Controlled folder Access"
2. Select "Allow an app through Controlled folder access" Select yes to "UAC"
3. Click on "+ Add an allowed app"
4. Select "recently blocked apps"
5. Find the executable for the C:\Python27
6. Click the + to add it.
7. Select Close
Then try running the Python Shell again. This worked for me 100%
Also, add exception through Windows Firewall Python27 select Private and Public.
This question has been asked before. Based on previous answers, I compiled a list of possible causes and solution on CPython tracker issue 25514, post of 2016-01-18.
SO question 1 Causes people have found or think they have found.
Antivirus/firewall (4 people, linux iptables, Windows firewall, antivisus , so not a myth ;-)
Shadow files, as described [in the issue], and in the python directory (about as common; example user file names: tkinter,py, subprocess.py).
Registry problem (some people can fix, but most should re-install after complete removal).
Zombie pythonw process (kill with Windows Task manager).
Permissions (run as admin, though should not be needed with proper install)
?Multiple installed versions clash due to misconfiguration?
SO question 2 Add:
Running after stopping run with ^C (temporary, I believe Restart Shell will fix) Call this a timing issue. I have experienced temporary connection failures myself.
Modem reboot (rather obsolete answer for most of us).
?example program that reads and writes (test)
Someone somewhere reported that his problem was with network misconfiguration that affected the operation of the socket module that IDLE uses.
Last resort: start IDLE with -n command line option, as described in IDLE doc.
If you at the network environment then check on the secure Group (SG), to see if the user is listed under that group.
else as other had been suggested you have to have the (right click on the program the login as Admin right to enable the IDLE to run.
IDLE's subprocess didn't make a connection. Either IDLE can't start or a personal firewall software is blocking the connection.
Having had this problem myself I did an uninstall and created a new directory in the C drive and reinstalled in that folder, which worked for me.
First uninstall the application.Then reinstall it BUT at the time of reinstallation try -n at the end of location adress. It worked for me, you can copy the below text and paste it at the location while installing it.
“C:\Program Files\Python32\pythonw.exe” lib\idlelib\idle.py -n

Loading code from editor into PyCharm terminal

I've just started using PyCharm, and am consistently pleasantly surprised by its tools and configurability. I know that in PyCharm, you can load code from the editor into a Python console (see https://www.jetbrains.com/pycharm/webhelp/loading-code-from-editor-into-console.html). However, the remote console I'm using in PyCharm is very slow.
If I access python on the remote machine directly via ssh, it has no performance issues. As such, I'd like to just open the remote version of python via PyCharm's Terminal, and execute code in the terminal from my editor.
However, I haven't found any key bindings (or options) that let me automatically load code -- it's a manual copy and past process for now. When I try to create a keyboard shortcut to do this, the option isn't available. Is there a method to create a keyboard shortcut to load code from the editor into the terminal?
The only way to create such a shortcut by yourself is by writing a plugin to PyCharm. PyCharm plugins are written in Java, so I don't know whether this sounds like an appealing option for you. Alternatively, you can file a feature request at http://youtrack.jetbrains.com/ asking for this feature to be added.
Note that you can set up a run configuration that will run your code for you on a remote interpreter without going through a console. Have you tried this? Is it also slow?

Python terminal emulation

I'd like to have an xterm-compatible virtual terminal running inside a Python app.
I'll need to run ncurses-based applications inside it, feed it with user's input and read its output.
So far I've found python-vte, but it only provides a GTK+ widget (libvte has the required VtePty class, but Python bindings don't) and has an unacceptable libgtk dependency.
Is there any other way to perform a teminal emulation in Python?
After all, I've found the pyte, which is exactly what I wanted.
Have you looked at this VT100 terminal emulator, also described in blog form here? It's not an xterm emulator, but perhaps it can be leveraged for your purposes.
I have been running a Rails Tutorial site & have deployed Gateone terminal emulator for users to execute commands. Later we devloped our own Terminal emulation. Check http://github.com/pocha/terminal-codelearn .
If you just need to execute command as a user, then probably you just need http://docs.python.org/2/library/pty.html

Categories