Skip to content

Commit 2203183

Browse files
committed
Improve package fetching duration by using the 'pip inspect' command.
1 parent 065f387 commit 2203183

4 files changed

Lines changed: 31 additions & 23 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ venv/
22
*.pyc
33
*/__pycache__
44
*.egg-info
5-
dist/
5+
dist/
6+
build/

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ classifiers = [
2727
"Operating System :: OS Independent",
2828
]
2929
dependencies = [
30-
"setuptools>=61.0"
30+
"setuptools>=61.0",
31+
"pip>=22.2"
3132
]
3233

3334
[tool.setuptools_scm]
3435

3536
[project.urls]
36-
"Homepage" = "http://github.com/mitch0s/pipslim"
37+
"GitHub" = "http://github.com/mitch0s/pipslim"
3738
"Bug Tracker" = "https://github.com/mitch0s/pipslim/issues"

src/pipslim/__init__.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
1+
#
2+
# pipslim - created by Mitch Naake (mitch0s)
3+
#
14
from .util import *
2-
import threading
35

46
def run():
57
"""
6-
Program entry point
8+
Print list of user-installed packages in pip freeze format.
79
"""
8-
packages = []
9-
def _threaded_list_package_deps(package:dict=None):
10-
if package == None : return
11-
if len(list_package_dependencies(package['name'])) == 0:
12-
packages.append(f"{package['name']}=={package['version']}")
13-
14-
threads = []
15-
for package in list_packages():
16-
thread = threading.Thread(target=lambda: _threaded_list_package_deps(package), daemon=True)
17-
threads.append(thread)
18-
thread.start()
19-
20-
for thread in threads: thread.join()
21-
22-
print('\n'.join(sorted(packages)))
10+
print('\n'.join(util.list_user_installed_packages()))
2311

2412
if __name__ == '__main__':
2513
run()

src/pipslim/util.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

22
import sys
3+
import os
34
import subprocess
45
import json
56

7+
os.environ['PYTHONUTF8'] = '1'
8+
69
def in_venv():
710
# Check for sys.real_prefix for older virtualenv compatibility
811
if hasattr(sys, 'real_prefix'):
@@ -18,9 +21,9 @@ def list_packages() -> list[dict]:
1821
return json.loads(result.stdout)
1922

2023

21-
def list_package_dependencies(package:str=None) -> list[dict]:
24+
def list_package_dependents(package:str=None) -> list[dict]:
2225
"""
23-
returns a list of higher-order packages that depend on this package.
26+
List what packages depend on the target package.
2427
"""
2528
result = subprocess.run(['pip', 'show', package], capture_output=True, text=True).stdout
2629
for line in result.split('\n'):
@@ -29,4 +32,19 @@ def list_package_dependencies(package:str=None) -> list[dict]:
2932
line = line.replace('Required-by:', '')
3033
deps = list(filter(lambda dep: dep != '', line.split(',')))
3134
return deps
32-
return []
35+
return []
36+
37+
38+
def list_user_installed_packages() -> list[str]:
39+
_packages = []
40+
41+
result = subprocess.run(['pip', 'inspect', '--no-color'], capture_output=True, text=True, errors='ignore')
42+
pinfo = json.loads(result.stdout)
43+
44+
for package in pinfo['installed']:
45+
if package['requested'] == True:
46+
package_name = package['metadata']['name']
47+
package_version = package['metadata']['version']
48+
_packages.append(f'{package_name}=={package_version}')
49+
50+
return sorted(_packages, key=str.lower)

0 commit comments

Comments
 (0)