Automating Running PyCharm's Inspection tool from Windows Command Line

Hello, and thank you for taking the time to look into helping me with this issue.

I'm trying to automate PyCharm's inspection tool to run in the background while people are coding as a way to collect information about the person's syntax errors and such as they work. In order to do this, I have two separate PyCharm installations on the computer to be used: PyCharm Edu 2018.1.2 and PyCharm Community Edition 2018.3.3. 

The computers I would like to run this program on are computers at a public University. Needless to say, there are multiple shared accounts and such that are accessible from each computer and do not save any changes when logged out. I want to run this program while students are using the PCs to gain information about their coding habits (as part of my Thesis, and yes, I have gone through all the steps necessary to do human research, IRB, CITI Training, etc.).

I am using a module called subprocess which can be used to execute commands, such as the inspect.bat command, from within Python. Here is the way I am currently setting up the command via Python (this is after substituting in the values of the variables I am using):

subprocess.Popen(['runas', '/user:Administrator', 'C:\\"Program Files (x86)"\\JetBrains\\"PyCharm Edu 2018.1.2"\\bin\\inspect.bat', 
'C:/Users/pcse_user.PCS/PycharmProjects/tester_proj', '-e',
'C:/Users/pcse_user.PCS/L323-18_inspections/05_14_2019-04_20_28PM/tester_proj'], stdin=subprocess.PIPE)

What this code is supposed to do is run the inspection using the default inspection profile and output the XML files to a folder I generated in the code above this line. This code will run fine on my personal computer/account (which is the admin account) without the need of the "runas /user:Administrator". I will say that my other computer has older versions of PyCharm (but only like CE 2018.3.2). I do also stdin.write the password immediately after this and communicate it. It does not write anything to the console, and it does not produce any XML files in the folder specified.

If I do not include the "runas /user:Administrator" phrase in the call, I get the following error:

File "D:/InspectCode/src/Inspections.py", line 186, in do_inspects
output_directory + "/" + project_name], stdin=subprocess.PIPE)
File "C:\Program Files\Python37\lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\Program Files\Python37\lib\subprocess.py", line 1155, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied

Is anyone able to tell me what I'm doing wrong or how to troubleshoot this? I don't believe it has anything to do with the way the paths are written (forward/backward slashes, etc.) as I've fiddled with those a lot. I'm thinking it may be because of administrator privilege issues or because of PyCharm's new versions.

I'm open to all ideas and suggestions, even if they require taking a completely different route!

Thanks in advance!

1 comment
Comment actions Permalink

Hi Matthew, I believe a more suitable tool to do this is to use either pycodestyle, pylint or some similar programs. It is much easier to configure, extend and run them, especially on separate machines. E.g. it should be straightforward to write a pylint plugin on top of the existed code base to add some extra metrics. As a different approach one can implement a custom Java/Kotlin plugin for PyCharm itself to gather some code habit metrics, though it will require more work.

If you still want to stick with inspect.bat you should first tweak your calling script as it violates the existed API as far as I can see, see https://www.jetbrains.com/help/pycharm/command-line-code-inspector.html for details. The following example works for me:

import subprocess

process = subprocess.run(
[
# Path to inspect.bat
r'C:\Users\jetbrains\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-1\191.7141.48\bin\inspect.bat',
# Path to project to inspect
r'C:\Users\jetbrains\PycharmProjects\test_project',
# Inspection profile
r'C:\Users\jetbrains\PycharmProjects\test_project\.idea\inspectionProfiles\Inspections.xml',
# Where to put inspect.bat output
r'C:\Users\jetbrains\PycharmProjects\post_360003561559\output',
# Max verbosity level
r'-v2',
# Run inspections against a specific directory in the project
r'-d',
r'C:\Users\jetbrains\PycharmProjects\test_project\src'
]
)

Possible problems with running inspect.bat

  • running inspect.bat against a project which is currently used will most likely cause file access collision
  • inspect.bat should be run against the project which was previously opened with the given PyCharm version as IDE needs to do two things before inspect.bat can be used 1) generate skeletons for binary modules of the selected project interpreter (or there will be a bunch of weird inspection errors like a missing os module) and 2) configure jdk.table.xml file by adding a new project interpreter information there
  • the previous point makes it especially cumbersome to run inspect.bat on the remote machines, see the relevant discussion here https://youtrack.jetbrains.com/issue/PY-34864
  • PyCharm which is used by inspect.bat should be closed during the inspect.bat execution as there can not be two same major PyCharm versions running on the same machine simultaneously

Hope it helps.

1

Please sign in to leave a comment.