File size: 3,155 Bytes
a65138c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
from __future__ import with_statement
import logging
import os
import os.path
import sys
from discover_python import expose_options
from discover_python import log_discovered


logger = logging.getLogger(__name__)


EXPOSE_NAMES = ('location', 'version')


FIND_SOURCE = '''
import os
import os.path
import sys
try:
    from pkg_resources import get_distribution
except ImportError:
    sys.stderr.write('pkg_resources is not found' + os.linesep)
    try:
        import lxml
    except ImportError:
        sys.stderr.write('lxml is not found' + os.linesep)
        raise SystemExit(1)
    else:
        print(os.path.dirname(lxml.__path__[0]))
        print('')
        raise SystemExit(0)

try:
    dist = get_distribution('lxml')
except Exception:
    e = sys.exc_info()[1]
    sys.stderr.write(repr(e))
    sys.stderr.write(os.linesep)
    raise SystemExit(1)
else:
    print(dist.location)
    print(dist.version)
'''


def discover_lxml(executable):
    import tempfile
    fd, path = tempfile.mkstemp()
    try:
        with os.fdopen(fd, 'w') as f:
            f.write(FIND_SOURCE)

        from subprocess import Popen
        from subprocess import PIPE
        args = [executable, path]
        env = dict(os.environ)
        for k in ('PYTHONPATH', 'PYTHONHOME'):
            if k in env:
                del env[k]
        try:
            p = Popen(args, stdout=PIPE, env=env)
        except Exception:
            e = sys.exc_info()[1]
            logger.exception(e)
            return
        else:
            try:
                lines = list(p.stdout)
            finally:
                p.wait()
    finally:
        os.unlink(path)

    if p.returncode == 0:
        location = lines[0].strip()
        version = lines[1].strip()
        yield dict(location=location,
                   version=version)


class DiscoverRecipe(object):
    ''' Discover lxml and provide its location.
    '''

    def __init__(self, buildout, name, options):
        self.__logger = logger = logging.getLogger(name)
        for k, v in options.items():
            logger.info('%s: %r', k, v)

        self.__recipe = options['recipe']

        not_found = options.get('not-found', 'not-found')
        executable = options.get('python', 'python').strip()
        #version = options.get('version', '').strip()

        founds = discover_lxml(executable=executable)
        founds = log_discovered('matching', founds, EXPOSE_NAMES,
                                log=logger.info)
        founds = list(founds)

        # location is not specified: try to discover a Python installation
        if founds:
            found = founds[0]
            logger.info('the first-found one will be used:')
            expose_options(options, EXPOSE_NAMES, found,
                           not_found=not_found, logger=logger)
            return

        # ensure executable publishes not-found marker
        expose_options(options, ['location'], dict(), not_found=not_found,
                       logger=logger)
        logger.warning('lxml not found')
        return

    def install(self):
        return []

    update = install