forked from danielgatis/darknetpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
135 lines (105 loc) · 3.65 KB
/
setup.py
File metadata and controls
135 lines (105 loc) · 3.65 KB
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
import os
import re
import shutil
import subprocess
import tempfile
import zipfile
try:
import urllib.request as retriver
except:
import urllib as retriver
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
def readme():
with open('README.rst') as f:
return f.read()
def sed(pattern, replace, source, dest=None, count=0):
fin = open(source, 'r')
num_replaced = count
if dest:
fout = open(dest, 'w')
else:
fd, name = tempfile.mkstemp()
fout = open(name, 'w')
for line in fin:
out = re.sub(pattern, replace, line)
fout.write(out)
if out != line:
num_replaced += 1
if count and num_replaced > count:
break
try:
fout.writelines(fin.readlines())
except Exception as E:
raise E
fin.close()
fout.close()
if not dest:
shutil.move(name, source)
class BuildExtCommand(build_ext):
def run(self):
tempdir = tempfile.gettempdir()
darknet_url = 'https://github.com/pjreddie/darknet/archive/1e729804f61c8627eb257fba8b83f74e04945db7.zip'
darknet_zip_file = os.path.join(tempdir, 'darknet.zip')
darknet_unziped = os.path.join(tempdir, 'darknet-1e729804f61c8627eb257fba8b83f74e04945db7')
darknet_root = os.path.join(tempdir, 'darknet')
makefile = os.path.join(darknet_root, 'Makefile')
try:
if not os.path.exists(darknet_zip_file):
retriver.urlretrieve(darknet_url, darknet_zip_file)
if not os.path.exists(darknet_unziped):
with zipfile.ZipFile(darknet_zip_file, 'r') as zip_ref:
zip_ref.extractall(tempdir)
shutil.move(darknet_unziped, darknet_root)
if (os.environ.get('GPU', None)):
sed('GPU=0', 'GPU=1', makefile, count=1)
if (os.environ.get('CUDNN', None)):
sed('CUDNN=0', 'CUDNN=1', makefile, count=1)
if (os.environ.get('OPENCV', None)):
sed('OPENCV=0', 'OPENCV=1', makefile, count=1)
if (os.environ.get('OPENMP', None)):
sed('OPENMP=0', 'OPENMP=1', makefile, count=1)
process = subprocess.Popen('make', cwd=darknet_root, shell=True)
process.wait()
os.remove(os.path.join(darknet_root, 'libdarknet.so'))
self.include_dirs.append(tempdir)
self.include_dirs.append(os.path.join(darknet_root, 'include'))
self.library_dirs.append(darknet_root)
self.libraries.append('darknet')
super().run()
finally:
try:
os.remove(darknet_zip_file)
shutil.rmtree(darknet_root, ignore_errors=True)
except OSError:
pass
modules = [
Extension(
name='darknetpy.detector',
sources=['extension/detector.c'],
extra_compile_args=['-fPIC', '-std=c99']
)
]
setup(
name='darknetpy',
version='2.1',
long_description=readme(),
author='Daniel Gatis Carrazzoni',
author_email='danielgatis@gmail.com',
url='https://github.com/danielgatis/darknetpy',
license='BSD License',
platforms=['Linux'],
classifiers=[
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: POSIX',
'Programming Language :: Python',
'Programming Language :: Python :: 3'
],
python_requires='>=3',
packages=find_packages(exclude=['tests.py']),
ext_modules=modules,
cmdclass={'build_ext': BuildExtCommand},
include_package_data=True,
zip_safe=False
)