Skip to content

Commit 0b0e1ea

Browse files
committed
updated
1 parent 8e5c9d0 commit 0b0e1ea

File tree

5 files changed

+138
-128
lines changed

5 files changed

+138
-128
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
v1.2.0b3, 01-01-2020 -- Basic structural changes.
2+
v1.2.0b2, 21-12-2020 -- Fixed some bugs.
13
v1.2.0b1, 21-12-2020 -- Initial release.

cythonizer/__init__.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import argparse
2+
import logging
3+
import os
4+
import pathlib
5+
import sys
6+
7+
# Always prefer setuptools over distutils
8+
from setuptools import Extension, setup
9+
import Cython.Compiler.Options
10+
from Cython.Build import cythonize
11+
from Cython.Distutils import build_ext
12+
13+
logging.basicConfig(level=logging.DEBUG, format="%(levelname)s: %(message)s")
14+
15+
16+
class cythonizer:
17+
def __init__(self) -> None:
18+
self.__extension: list = []
19+
self.__args = self.__Arguments()
20+
self.__Checking()
21+
self.__Compiling()
22+
# self.__Cleanup()
23+
24+
# Argumebts Parser ↓
25+
def __Arguments(self):
26+
parser = argparse.ArgumentParser(
27+
description="A script that will attempt to automatically convert one or more .py and .pyx files into the corresponding compiled .pyd or .so binary modules files."
28+
)
29+
30+
# Taking Files ↓
31+
parser.add_argument(
32+
"filenames", type=pathlib.Path, nargs="+", help=".py and .pyx files only"
33+
)
34+
35+
# Cython Annotation ↓
36+
parser.add_argument(
37+
"--annotation",
38+
default=False,
39+
action="store_true",
40+
help="(default: False)",
41+
)
42+
43+
# Numpy Includes ↓
44+
parser.add_argument(
45+
"--numpy-includes",
46+
default=False,
47+
action="store_true",
48+
help="(default: False)",
49+
)
50+
51+
# Cython Debugmode ↓
52+
parser.add_argument(
53+
"--debugmode",
54+
default=False,
55+
action="store_true",
56+
help="(default: False)",
57+
)
58+
59+
return parser.parse_args().__dict__
60+
61+
# Received File Checking ↓
62+
def __Checking(self):
63+
for file in self.__args.get("filenames"):
64+
if not os.path.exists(file):
65+
logging.error(f'"{file}" doesn\'t exist.')
66+
sys.exit(-1)
67+
if not os.path.isfile(file):
68+
logging.error(f'"{file}" is not a file')
69+
sys.exit(-1)
70+
if os.path.splitext(file)[-1] not in (".py", ".pyx"):
71+
logging.error(f'"{file}" is not a valid file')
72+
sys.exit(-1)
73+
74+
# Includes Directory ↓
75+
def __Includes_Dir(self):
76+
# Extra include folders. Mainly for numpy.
77+
if self.__args.get("numpy_includes"):
78+
try:
79+
import numpy as np
80+
81+
except ModuleNotFoundError:
82+
logging.error("Numpy is required, but not found. Please install it")
83+
sys.exit(-1)
84+
return [np.get_include()]
85+
else:
86+
return []
87+
88+
# Cython Compilation ↓
89+
def __Compiling(self):
90+
Cython.Compiler.Options.annotate = self.__args.get("annotation")
91+
92+
ext_modules: list = []
93+
for _ in self.__args.get("filenames"):
94+
file: str = str(_)
95+
# The name must be plain, no path
96+
module_name = os.path.basename(file)
97+
module_name = os.path.splitext(module_name)[0]
98+
ext_modules.append(
99+
Extension(
100+
module_name, [file], extra_compile_args=["-O2", "-march=native"]
101+
)
102+
)
103+
104+
sys.argv = [sys.argv[0], "build_ext", "--inplace"]
105+
106+
setup(
107+
cmdclass={"build_ext": build_ext},
108+
include_dirs=self.__Includes_Dir(),
109+
ext_modules=cythonize(module_list=ext_modules, language_level="3"),
110+
)
111+
112+
def __Cleanup(self):
113+
# Delete intermediate C files.
114+
for _ in self.__args.get("filenames"):
115+
filename = str(_)
116+
filename = f"{filename}.c"
117+
if os.path.exists(filename):
118+
os.remove(filename)
119+
120+
121+
def main():
122+
try:
123+
cythonizer()
124+
except Exception as e:
125+
logging.error(e)
126+
127+
128+
if __name__ == "__main__":
129+
print("Hello World")

cythonizer/__main__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from . import main
2+
3+
4+
if __name__ == "__main__":
5+
main()

cythonizer/cythonizer.py

Lines changed: 0 additions & 126 deletions
This file was deleted.

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def main():
2020
# Versions should comply with PEP440. For a discussion on single-sourcing
2121
# the version across setup.py and the project code, see
2222
# http://packaging.python.org/en/latest/tutorial.html#version
23-
version="1.2.0b2",
23+
version="1.2.0b3",
2424
description="Convert .py and .pyx to (.pyd | .so) very easily.",
2525
long_description=long_description,
2626
long_description_content_type="text/x-rst",
@@ -78,7 +78,7 @@ def main():
7878
# pip to create the appropriate form of executable for the target platform.
7979
entry_points={
8080
"console_scripts": [
81-
"cythonizer=cythonizer.cythonizer:main",
81+
"cythonizer=cythonizer:main",
8282
],
8383
},
8484
)

0 commit comments

Comments
 (0)