aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/runtime_py3/sitecustomize.pyx
blob: 0273d059de232b08ea0b3e62f707c68967b7ad1f (plain) (blame)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import re 
import sys 
 
import __res 

from importlib.abc import ResourceReader 
from importlib.metadata import Distribution, DistributionFinder, PackageNotFoundError, Prepared 
 
ResourceReader.register(__res._ResfsResourceReader) 
 
METADATA_NAME = re.compile('^Name: (.*)$', re.MULTILINE) 
 
 
class ArcadiaDistribution(Distribution): 
 
    def __init__(self, prefix): 
        self.prefix = prefix 
 
    def read_text(self, filename): 
        data = __res.resfs_read(f'{self.prefix}{filename}') 
        if data: 
            return data.decode('utf-8') 
    read_text.__doc__ = Distribution.read_text.__doc__ 
 
    def locate_file(self, path): 
        return f'{self.prefix}{path}' 
 
 
class ArcadiaMetadataFinder(DistributionFinder): 
 
    prefixes = {} 
 
    @classmethod 
    def find_distributions(cls, context=DistributionFinder.Context()): 
        found = cls._search_prefixes(context.name) 
        return map(ArcadiaDistribution, found) 
 
    @classmethod 
    def _init_prefixes(cls): 
        cls.prefixes.clear() 
 
        for resource in __res.resfs_files(): 
            resource = resource.decode('utf-8') 
            if not resource.endswith('METADATA'): 
                continue 
            data = __res.resfs_read(resource).decode('utf-8') 
            metadata_name = METADATA_NAME.search(data) 
            if metadata_name: 
                metadata_name = Prepared(metadata_name.group(1)) 
                cls.prefixes[metadata_name.normalized] = resource[:-len('METADATA')] 
 
    @classmethod 
    def _search_prefixes(cls, name): 
        if not cls.prefixes: 
            cls._init_prefixes() 
 
        if name: 
            try: 
                yield cls.prefixes[Prepared(name).normalized] 
            except KeyError: 
                raise PackageNotFoundError(name) 
        else: 
            for prefix in sorted(cls.prefixes.values()): 
                yield prefix 
 
 
# monkeypatch standart library 
import importlib.metadata 
importlib.metadata.MetadataPathFinder = ArcadiaMetadataFinder