Skip to content

Commit 8a1d9d4

Browse files
author
burivuh
authored
Merge pull request #31 from vicpopov/master
Pypi build and python3 support
2 parents e2ebc5e + a0a8415 commit 8a1d9d4

File tree

8 files changed

+169
-15
lines changed

8 files changed

+169
-15
lines changed

snippets/c_api/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.PHONY: all indent clean
22

33
ALOHALYTICS_ROOT=../../Alohalytics
4-
BUILD_DIR=build
4+
BUILD_DIR=../pyaloha
55

66
CPP=g++
77
CFLAGS=-Wall -Werror -O3 -fpic

snippets/pyaloha/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__="0.0.1"

snippets/pyaloha/ccode.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
import datetime
33
import itertools
44
import os
5+
import sys
56

67
from pyaloha.protocol import SerializableDatetime
78

8-
9-
ctypes.set_conversion_mode('utf8', 'strict')
9+
try:
10+
from ctypes import set_conversion_mode
11+
except ImportError:
12+
pass
13+
else:
14+
set_conversion_mode('utf8', 'strict')
1015

1116

1217
def c_unicode(c_data):
@@ -151,10 +156,7 @@ def stripped(self):
151156
def iterate_events(stream_processor, events_limit):
152157
base_path = os.path.dirname(os.path.abspath(__file__))
153158
c_module = ctypes.cdll.LoadLibrary(
154-
os.path.join(
155-
base_path, '..',
156-
'c_api', 'build', 'iterate_events.so'
157-
)
159+
os.path.join(base_path, 'iterate_events.so')
158160
)
159161
use_keys = tuple(itertools.chain.from_iterable(
160162
e.keys for e in stream_processor.__events__

snippets/pyaloha/examples/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from pyaloha.base import DataAggregator as BaseDataAggregator
2+
from pyaloha.base import DataStreamWorker as BaseDataStreamWorker
3+
from pyaloha.base import StatsProcessor as BaseStatsProcessor
4+
5+
6+
class DataStreamWorker(BaseDataStreamWorker):
7+
def __init__(self):
8+
super(DataStreamWorker, self).__init__()
9+
10+
self.events = []
11+
12+
def process_unspecified(self, event):
13+
self.events.append(event)
14+
15+
16+
class DataAggregator(BaseDataAggregator):
17+
def aggregate(self, results):
18+
for event in results.events:
19+
print(event)
20+
21+
22+
class StatsProcessor(BaseStatsProcessor):
23+
def gen_stats(self, *args):
24+
return []

snippets/pyaloha/examples/run.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
#!/bin/env python
2+
3+
# This helper cmd run script is to be used like:
4+
# run.py <pyscript_name> <params>
5+
# It will take care of PYTHONPATH and plugin_dir
6+
# and start pyaloha with specified script and params
17
import os
28
import sys
39

410
base_path = os.path.dirname(os.path.abspath(__file__))
511

6-
pyaloha_path = os.path.join(
7-
base_path, '..', '..'
8-
)
9-
10-
sys.path.append(pyaloha_path)
11-
1212
PYSCRIPT_PATH = base_path
1313

14-
pyaloha = __import__('pyaloha.main')
15-
pyaloha.main.cmd_run(plugin_dir=PYSCRIPT_PATH)
14+
ALOHA_DATA = os.environ['ALOHA_DATA_DIR']
15+
16+
if __name__ == '__main__':
17+
pyaloha = __import__('pyaloha.main')
18+
pyaloha.main.cmd_run(plugin_dir=PYSCRIPT_PATH, data_dir=ALOHA_DATA)

snippets/pyaloha/protocol.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ class CustomEncoder(json.JSONEncoder):
145145
def default(self, obj):
146146
if hasattr(obj, '__dumpdict__'):
147147
return obj.__dumpdict__()
148+
if isinstance(obj, bytes):
149+
return obj.decode()
148150
# Let the base class default method raise the TypeError
149151
return super(CustomEncoder, self).default(obj)
150152

snippets/setup.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import io
4+
import os
5+
import sys
6+
import re
7+
from setuptools import setup, find_packages
8+
from distutils.command.build_clib import build_clib
9+
from distutils.command.install_lib import install_lib
10+
from distutils import log
11+
12+
NAME = "pyaloha"
13+
DESCRIPTION = "Alohalytics statistics engine python parser"
14+
URL = "https://github.com/mapsme/Alohalytics"
15+
EMAIL = "me@alex.bio"
16+
AUTHOR = "Alexander Zolotarev"
17+
18+
here = os.path.abspath(os.path.dirname(__file__))
19+
about = {}
20+
project_slug = NAME.lower().replace("-", "_").replace(" ", "_")
21+
with open(os.path.join(here, project_slug, '__init__.py')) as f:
22+
exec(f.read(), about)
23+
24+
class build_clib_shared(build_clib, object):
25+
def build_libraries(self, libraries):
26+
for (lib_name, build_info) in libraries:
27+
sources = build_info.get('sources')
28+
if sources is None or not isinstance(sources, (list, tuple)):
29+
raise DistutilsSetupError(
30+
"in 'libraries' option (library '%s'), "
31+
"'sources' must be present and must be "
32+
"a list of source filenames" % lib_name)
33+
sources = list(sources)
34+
35+
log.info("building '%s' library", lib_name)
36+
37+
# First, compile the source code to object files in the library
38+
# directory. (This should probably change to putting object
39+
# files in a temporary build directory.)
40+
macros = build_info.get('macros')
41+
include_dirs = build_info.get('include_dirs')
42+
extra_preargs = build_info.get('extra_preargs')
43+
extra_postargs = build_info.get('extra_postargs')
44+
target_lang = build_info.get('target_lang')
45+
objects = self.compiler.compile(sources,
46+
output_dir=self.build_temp,
47+
macros=macros,
48+
include_dirs=include_dirs,
49+
extra_preargs=extra_preargs,
50+
extra_postargs=extra_postargs,
51+
debug=self.debug)
52+
53+
# Now "link" the object files together into a static library.
54+
# (On Unix at least, this isn't really linking -- it just
55+
# builds an archive. Whatever.)
56+
self.compiler.link_shared_object(objects, lib_name,
57+
output_dir=self.build_clib,
58+
target_lang=target_lang,
59+
debug=self.debug)
60+
61+
62+
class install_lib_custom(install_lib, object):
63+
def install(self):
64+
outfiles = super(install_lib_custom, self).install()
65+
66+
log.info("copying additional libraries")
67+
build_clib = self.get_finalized_command('build_clib')
68+
for lib in build_clib.libraries:
69+
libname = lib[0]
70+
ext = build_clib.compiler.shared_lib_extension
71+
target_libname = os.path.join(
72+
self.install_dir, *libname.split(".")
73+
) + ext
74+
75+
self.copy_file(
76+
os.path.join(build_clib.build_clib, libname),
77+
target_libname
78+
)
79+
if outfiles is None:
80+
outfiles = []
81+
outfiles.append(target_libname)
82+
return outfiles
83+
84+
85+
c_api = (
86+
'pyaloha.iterate_events',
87+
{
88+
'sources': ['c_api/iterate_events.cc'],
89+
'include_dirs': ['../Alohalytics/src'],
90+
'extra_preargs': ['-std=c++11'],
91+
'target_lang': 'c++',
92+
}
93+
)
94+
95+
setup(
96+
name=NAME,
97+
version=about['__version__'],
98+
description=DESCRIPTION,
99+
author=AUTHOR,
100+
author_email=EMAIL,
101+
url=URL,
102+
license='MIT',
103+
libraries=[c_api],
104+
packages=find_packages(),
105+
cmdclass={
106+
'build_clib': build_clib_shared,
107+
'install_lib': install_lib_custom,
108+
},
109+
classifiers=[
110+
# Trove classifiers
111+
# Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
112+
'License :: OSI Approved :: MIT License',
113+
'Programming Language :: Python',
114+
'Programming Language :: Python :: 2',
115+
'Programming Language :: Python :: 2.7',
116+
'Programming Language :: Python :: 3',
117+
'Programming Language :: Python :: 3.5',
118+
'Programming Language :: Python :: 3.6',
119+
'Programming Language :: Python :: Implementation :: CPython',
120+
],
121+
)
122+

0 commit comments

Comments
 (0)