-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy path_bootstrap.py
More file actions
41 lines (34 loc) · 1.2 KB
/
_bootstrap.py
File metadata and controls
41 lines (34 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Copyright (c) 2009 Giampaolo Rodola. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Bootstrap utilities for loading psutil modules without psutil
being installed.
"""
import ast
import importlib.util
import os
import pathlib
ROOT_DIR = pathlib.Path(__file__).resolve().parent
def load_module(path):
"""Load a Python module by file path without importing it
as part of a package.
"""
name = os.path.splitext(os.path.basename(path))[0]
spec = importlib.util.spec_from_file_location(name, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
def get_version():
"""Extract __version__ from psutil/__init__.py using AST
(no imports needed).
"""
path = ROOT_DIR / "psutil" / "__init__.py"
with open(path, encoding="utf-8") as f:
mod = ast.parse(f.read())
for node in mod.body:
if isinstance(node, ast.Assign):
for target in node.targets:
if getattr(target, "id", None) == "__version__":
return ast.literal_eval(node.value)
msg = "could not find __version__"
raise RuntimeError(msg)