Off topic: How does WingetUI parse winget CLI output #1725
-
Hi, I'm trying to parse the CLI output of I found the relevant line in the source code: p = subprocess.Popen([self.EXECUTABLE, "list", "--accept-source-agreements"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, cwd=os.getcwd(), env=os.environ.copy(), shell=True) However, calling this code in a Python script does not work. The output is still trimmed: import subprocess, os
p = subprocess.Popen(["winget.exe", "list", "--accept-source-agreements"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, cwd=os.getcwd(), env=os.environ.copy(), shell=True)
while p.poll() is None:
line: str = str(p.stdout.readline().strip(), "utf-8", errors="ignore")
print(line) > python .\test.py
Name Id Version Available Source
------------------------------------------------------------------------------------------------------------------------------
Visual Studio Build Tools 2022 Microsoft.VisualStudio.2022.BuildTo… 17.8.5 winget This might be off topic, but I wondered if someone could help me understand how this project avoids the trimmed columns. Any help would be greatly appreciated |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Yes, this is an unavoidable bug with Winget. WingetUI also has this issue. Certain winget packages are trimmed. You may want to take a look at Winget's PowerShell Modules |
Beta Was this translation helpful? Give feedback.
-
@MichaelBrunn3r MichaelBrunn3r
https://docs.python.org/3.8/library/subprocess.html#subprocess.Popen
So winget.exe is definitely a console-based executable (albeit a strange one -- I can't for the life of me launch it correctly from within a .bat or .cmd file in Windows 10). If we tell Python to just launch it directly (i.e. without asking Windows cmd.exe to do that), then Python should still be able to grab stdout and stderr, no? And cmd.exe executing in a (hidden) default window size of 120 x 30 characters seems to be the root cause of the Id column being truncated, no? In any case, it seems to be proper form to use shell=False for this call to subprocess.Popen, since winget.exe is indeed not built into the shell... (Yes I am just spitballing here, unfortunately I don't have Python setup in my development VM yet, since I was planning to use WingetUI to build out that VM...) |
Beta Was this translation helpful? Give feedback.
-
Would it perhaps be simpler to use the Powershell module |
Beta Was this translation helpful? Give feedback.
Yes, this is an unavoidable bug with Winget. WingetUI also has this issue. Certain winget packages are trimmed.
You may want to take a look at Winget's PowerShell Modules