File: setup.py

package info (click to toggle)
python-ua-parser 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 184 kB
  • sloc: python: 727; makefile: 22
file content (213 lines) | stat: -rw-r--r-- 8,248 bytes parent folder | download
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
import os
from distutils import log
from distutils.core import Command
from distutils.command.build import build as _build
from setuptools import setup
from setuptools.command.develop import develop as _develop
from setuptools.command.sdist import sdist as _sdist
from setuptools.command.install import install as _install


def check_output(*args, **kwargs):
    from subprocess import Popen
    proc = Popen(*args, **kwargs)
    output, _ = proc.communicate()
    rv = proc.poll()
    assert rv == 0, output


class build_regexes(Command):
    description = 'build supporting regular expressions from uap-core'
    user_options = [
        ('work-path=', 'w',
         "The working directory for source files. Defaults to ."),
        ('build-lib=', 'b',
         "directory for script runtime modules"),
        ('inplace', 'i',
         "ignore build-lib and put compiled javascript files into the source " +
         "directory alongside your pure Python modules"),
        ('force', 'f',
         "Force rebuilding of static content. Defaults to rebuilding on version "
         "change detection."),
    ]
    boolean_options = ['force']

    def initialize_options(self):
        self.build_lib = None
        self.force = None
        self.work_path = None
        self.inplace = None

    def finalize_options(self):
        install = self.distribution.get_command_obj('install')
        sdist = self.distribution.get_command_obj('sdist')
        build_ext = self.distribution.get_command_obj('build_ext')

        if self.inplace is None:
            self.inplace = (build_ext.inplace or install.finalized
                            or sdist.finalized) and 1 or 0

        if self.inplace:
            self.build_lib = '.'
        else:
            self.set_undefined_options('build',
                                       ('build_lib', 'build_lib'))
        if self.work_path is None:
            self.work_path = os.path.realpath(os.path.join(os.path.dirname(__file__)))

    def run(self):
        return # No rebuild of regexes.yaml during Debian builds.

        work_path = self.work_path
        if not os.path.exists(os.path.join(work_path, '.git')):
            return

        log.info('initializing git submodules')
        check_output(['git', 'submodule', 'init'], cwd=work_path)
        check_output(['git', 'submodule', 'update'], cwd=work_path)

        yaml_src = os.path.join(work_path, 'uap-core', 'regexes.yaml')
        if not os.path.exists(yaml_src):
            raise RuntimeError(
                'Unable to find regexes.yaml, should be at %r' % yaml_src)

        def force_bytes(text):
            if text is None:
                return text
            return text.encode('utf8')

        import yaml
        py_dest = os.path.join(self.build_lib, 'ua_parser', '_regexes.py')

        log.info('compiling regexes.yaml -> _regexes.py')
        with open(yaml_src, 'rb') as fp:
            regexes = yaml.safe_load(fp)
        with open(py_dest, 'wb') as fp:
            fp.write(b'# -*- coding: utf-8 -*-\n')
            fp.write(b'############################################\n')
            fp.write(b'# NOTICE: This file is autogenerated from  #\n')
            fp.write(b'# regexes.yaml. Do not edit by hand,       #\n')
            fp.write(b'# instead, re-run `setup.py build_regexes` #\n')
            fp.write(b'############################################\n')
            fp.write(b'\n')
            fp.write(b'from __future__ import absolute_import, unicode_literals\n')
            fp.write(b'from .user_agent_parser import (\n')
            fp.write(b'    UserAgentParser, DeviceParser, OSParser,\n')
            fp.write(b')\n')
            fp.write(b'\n')
            fp.write(b'__all__ = (\n')
            fp.write(b'    \'USER_AGENT_PARSERS\', \'DEVICE_PARSERS\', \'OS_PARSERS\',\n')
            fp.write(b')\n')
            fp.write(b'\n')
            fp.write(b'USER_AGENT_PARSERS = [\n')
            for device_parser in regexes['user_agent_parsers']:
                fp.write(b'    UserAgentParser(\n')
                fp.write(force_bytes('        %r,\n' % device_parser['regex']))
                fp.write(force_bytes('        %r,\n' % device_parser.get('family_replacement')))
                fp.write(force_bytes('        %r,\n' % device_parser.get('v1_replacement')))
                fp.write(force_bytes('        %r,\n' % device_parser.get('v2_replacement')))
                fp.write(b'    ),\n')
            fp.write(b']\n')
            fp.write(b'\n')
            fp.write(b'DEVICE_PARSERS = [\n')
            for device_parser in regexes['device_parsers']:
                fp.write(b'    DeviceParser(\n')
                fp.write(force_bytes('        %r,\n' % device_parser['regex']))
                fp.write(force_bytes('        %r,\n' % device_parser.get('regex_flag')))
                fp.write(force_bytes('        %r,\n' % device_parser.get('device_replacement')))
                fp.write(force_bytes('        %r,\n' % device_parser.get('brand_replacement')))
                fp.write(force_bytes('        %r,\n' % device_parser.get('model_replacement')))
                fp.write(b'    ),\n')
            fp.write(b']\n')
            fp.write(b'\n')
            fp.write(b'OS_PARSERS = [\n')
            for device_parser in regexes['os_parsers']:
                fp.write(b'    OSParser(\n')
                fp.write(force_bytes('        %r,\n' % device_parser['regex']))
                fp.write(force_bytes('        %r,\n' % device_parser.get('os_replacement')))
                fp.write(force_bytes('        %r,\n' % device_parser.get('os_v1_replacement')))
                fp.write(force_bytes('        %r,\n' % device_parser.get('os_v2_replacement')))
                fp.write(force_bytes('        %r,\n' % device_parser.get('os_v3_replacement')))
                fp.write(force_bytes('        %r,\n' % device_parser.get('os_v4_replacement')))
                fp.write(b'    ),\n')
            fp.write(b']\n')

        self.update_manifest()

    def update_manifest(self):
        sdist = self.distribution.get_command_obj('sdist')
        if not sdist.finalized:
            return

        sdist.filelist.files.append('ua_parser/_regexes.py')


class develop(_develop):
    def run(self):
        self.run_command('build_regexes')
        _develop.run(self)


class install(_install):
    def run(self):
        self.run_command('build_regexes')
        _install.run(self)


class build(_build):
    def run(self):
        self.run_command('build_regexes')
        _build.run(self)


class sdist(_sdist):
    sub_commands = _sdist.sub_commands + [('build_regexes', None)]


cmdclass = {
    'sdist': sdist,
    'develop': develop,
    'build': build,
    'install': install,
    'build_regexes': build_regexes,
}


setup(
    name='ua-parser',
    version='0.8.0',
    description="Python port of Browserscope's user agent parser",
    author='PBS',
    author_email='no-reply@pbs.org',
    packages=['ua_parser'],
    package_dir={'': '.'},
    license='LICENSE.txt',
    zip_safe=False,
    url='https://github.com/ua-parser/uap-python',
    include_package_data=True,
    setup_requires=['pyyaml'],
    install_requires=[],
    cmdclass=cmdclass,
    classifiers=[
        'Development Status :: 4 - Beta',
        'Environment :: Web Environment',
        'Intended Audience :: Developers',
        'Operating System :: OS Independent',
        'License :: OSI Approved :: Apache Software License',
        'Programming Language :: Python',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Software Development :: Libraries :: Python Modules',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: Implementation :: CPython',
        'Programming Language :: Python :: Implementation :: PyPy',
    ],
)