1
1
#!/usr/bin/env python
2
+ import os
3
+ from distutils import log
4
+ from distutils .core import Command
2
5
from setuptools import setup
3
6
from setuptools .command .develop import develop as _develop
4
7
from setuptools .command .sdist import sdist as _sdist
5
8
6
9
7
- def install_regexes ():
8
- print ('Copying regexes.yaml to package directory...' )
9
- import os
10
- cwd = os .path .abspath (os .path .dirname (__file__ ))
11
- yaml_src = os .path .join (cwd , 'uap-core' , 'regexes.yaml' )
12
- if not os .path .exists (yaml_src ):
13
- raise RuntimeError (
14
- 'Unable to find regexes.yaml, should be at %r' % yaml_src )
10
+ def check_output (* args , ** kwargs ):
11
+ from subprocess import Popen
12
+ proc = Popen (* args , ** kwargs )
13
+ output , _ = proc .communicate ()
14
+ rv = proc .poll ()
15
+ assert rv == 0 , output
15
16
16
- print ('Converting regexes.yaml to regexes.json...' )
17
- import json
18
- import yaml
19
- json_dest = os .path .join (cwd , 'ua_parser' , 'regexes.json' )
20
- with open (yaml_src , 'rb' ) as fp :
21
- regexes = yaml .safe_load (fp )
22
- with open (json_dest , "w" ) as f :
23
- json .dump (regexes , f )
17
+
18
+ class build_regexes (Command ):
19
+ description = 'build supporting regular expressions from uap-core'
20
+ user_options = [
21
+ ('work-path=' , 'w' ,
22
+ 'The working directory for source files. Defaults to .' ),
23
+ ]
24
+
25
+ def initialize_options (self ):
26
+ self .work_path = None
27
+
28
+ def finalize_options (self ):
29
+ if self .work_path is None :
30
+ self .work_path = os .path .realpath (os .path .join (os .path .dirname (__file__ )))
31
+
32
+ def run (self ):
33
+ work_path = self .work_path
34
+ if os .path .exists (os .path .join (work_path , '.git' )):
35
+ log .info ('initializing git submodules' )
36
+ check_output (['git' , 'submodule' , 'init' ], cwd = work_path )
37
+ check_output (['git' , 'submodule' , 'update' ], cwd = work_path )
38
+
39
+ yaml_src = os .path .join (work_path , 'uap-core' , 'regexes.yaml' )
40
+ if not os .path .exists (yaml_src ):
41
+ raise RuntimeError (
42
+ 'Unable to find regexes.yaml, should be at %r' % yaml_src )
43
+
44
+ def force_bytes (text ):
45
+ if text is None :
46
+ return text
47
+ return text .encode ('utf8' )
48
+
49
+ import yaml
50
+ py_dest = os .path .join (work_path , 'ua_parser' , '_regexes.py' )
51
+
52
+ log .info ('Compiling regexes.yaml -> _regexes.py' )
53
+ with open (yaml_src , 'rb' ) as fp :
54
+ regexes = yaml .safe_load (fp )
55
+ with open (py_dest , 'wb' ) as fp :
56
+ fp .write (b'############################################\n ' )
57
+ fp .write (b'# NOTICE: This file is autogenerated from #\n ' )
58
+ fp .write (b'# regexes.yaml. Do not edit by hand, #\n ' )
59
+ fp .write (b'# instead, re-run `setup.py build_regexes` #\n ' )
60
+ fp .write (b'############################################\n ' )
61
+ fp .write (b'\n ' )
62
+ fp .write (b'from __future__ import absolute_import\n ' )
63
+ fp .write (b'from .user_agent_parser import (\n ' )
64
+ fp .write (b' UserAgentParser, DeviceParser, OSParser,\n ' )
65
+ fp .write (b')\n ' )
66
+ fp .write (b'\n ' )
67
+ fp .write (b'__all__ = (\n ' )
68
+ fp .write (b' \' USER_AGENT_PARSERS\' , \' DEVICE_PARSERS\' , \' OS_PARSERS\' ,\n ' )
69
+ fp .write (b')\n ' )
70
+ fp .write (b'\n ' )
71
+ fp .write (b'USER_AGENT_PARSERS = [\n ' )
72
+ for device_parser in regexes ['user_agent_parsers' ]:
73
+ fp .write (b' UserAgentParser(\n ' )
74
+ fp .write (force_bytes (' %r,\n ' % device_parser ['regex' ]))
75
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('family_replacement' )))
76
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('v1_replacement' )))
77
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('v2_replacement' )))
78
+ fp .write (b' ),\n ' )
79
+ fp .write (b']\n ' )
80
+ fp .write (b'\n ' )
81
+ fp .write (b'DEVICE_PARSERS = [\n ' )
82
+ for device_parser in regexes ['device_parsers' ]:
83
+ fp .write (b' DeviceParser(\n ' )
84
+ fp .write (force_bytes (' %r,\n ' % device_parser ['regex' ]))
85
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('regex_flag' )))
86
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('device_replacement' )))
87
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('brand_replacement' )))
88
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('model_replacement' )))
89
+ fp .write (b' ),\n ' )
90
+ fp .write (b']\n ' )
91
+ fp .write (b'\n ' )
92
+ fp .write (b'OS_PARSERS = [\n ' )
93
+ for device_parser in regexes ['os_parsers' ]:
94
+ fp .write (b' OSParser(\n ' )
95
+ fp .write (force_bytes (' %r,\n ' % device_parser ['regex' ]))
96
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('os_replacement' )))
97
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('os_v1_replacement' )))
98
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('os_v2_replacement' )))
99
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('os_v3_replacement' )))
100
+ fp .write (force_bytes (' %r,\n ' % device_parser .get ('os_v4_replacement' )))
101
+ fp .write (b' ),\n ' )
102
+ fp .write (b']\n ' )
103
+ fp .write (b'\n ' )
24
104
25
105
26
106
class develop (_develop ):
27
107
def run (self ):
28
- install_regexes ( )
108
+ self . run_command ( 'build_regexes' )
29
109
_develop .run (self )
30
110
31
111
32
112
class sdist (_sdist ):
33
- def run (self ):
34
- install_regexes ()
35
- _sdist .run (self )
113
+ sub_commands = _sdist .sub_commands + [('build_regexes' , None )]
114
+
115
+
116
+ cmdclass = {
117
+ 'develop' : develop ,
118
+ 'sdist' : sdist ,
119
+ 'build_regexes' : build_regexes ,
120
+ }
36
121
37
122
38
123
setup (
@@ -47,13 +132,9 @@ def run(self):
47
132
zip_safe = False ,
48
133
url = 'https://github.com/ua-parser/uap-python' ,
49
134
include_package_data = True ,
50
- package_data = {'ua_parser' : ['regexes.json' ]},
51
135
setup_requires = ['pyyaml' ],
52
136
install_requires = [],
53
- cmdclass = {
54
- 'develop' : develop ,
55
- 'sdist' : sdist ,
56
- },
137
+ cmdclass = cmdclass ,
57
138
classifiers = [
58
139
'Development Status :: 4 - Beta' ,
59
140
'Environment :: Web Environment' ,
0 commit comments