This repository was archived by the owner on Feb 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
211 lines (188 loc) · 7.12 KB
/
setup.py
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
#!/usr/bin/env python
from distutils.core import setup
import distutils.command.build
import distutils.command.clean
from distutils import log
from distutils.spawn import find_executable
import subprocess
import shutil
import shlex
import os.path
import glob
from urllib import urlopen
import zipfile
try:
import cStringIO as StringIO
except ImportError:
import StringIO
CMAKE_NOT_INSTALLED = "cmake not found in PATH!"
FAILURE_IN_COMPILING_PHANTOMPY = "failed to compile phantompy!"
UNABLE_TO_GET_PHANTOMPY_SOURCE = "Unable to download phantompy source code!"
ERROR_IN_GIT_CLONE = "Failed to clone git repository!\n{0}"
GIT_PULL_FAILED = "Failed to update local git copy. Using " + \
"old/potentially empty repo!"
SUBPROCESS_ERROR = "subprocess.Popen threw an exception! " + \
"Location {2}, Command: {1}, Exception message: {0}"
PHANTOMPY_REPO = "git://github.com/niwibe/phantompy.git"
PHANTOMPY_REPO_ZIP = "https://github.com/niwibe/phantompy/archive/master.zip"
CLONE_REPO = "{git} clone {repo} {path}"
PHANTOMPY_SRC_DIR = os.path.join("src", "phantompy")
UPDATE_LOCAL_COPY = "{git} pull origin master"
SUBPROCESS_KWARGS = {
'stdout': subprocess.PIPE,
'stderr': subprocess.PIPE,
'cwd': os.path.dirname(os.path.abspath(__file__))
}
def download_git(opts):
if opts['git']:
try:
stdout, stderr = subprocess.Popen([opts['git']], **SUBPROCESS_KWARGS).communicate()
except OSError:
log.debug("{0} not a valid command!".format(opts['git']))
return None
else:
git_src_dir = os.path.join(
SUBPROCESS_KWARGS['cwd'], PHANTOMPY_SRC_DIR)
if not os.path.exists(PHANTOMPY_SRC_DIR):
command = CLONE_REPO.format(
repo=PHANTOMPY_REPO,
path=git_src_dir,
git=opts['git'])
try:
stdout, stderr = subprocess.Popen(
shlex.split(command),
**SUBPROCESS_KWARGS).communicate()
except OSError as e:
log.debug(SUBPROCESS_ERROR.format(
str(e), command, SUBPROCESS_KWARGS['cwd']))
else:
if stderr:
log.debug(ERROR_IN_GIT_CLONE.format(stderr))
return None
else:
command = UPDATE_LOCAL_COPY.format(opts['git'])
try:
stdout, stderr = subprocess.Popen(
shlex.split(command),
dict(SUBPROCESS_KWARGS,
**{'cwd': git_src_dir}))
except OSError as e:
log.debug(SUBPROCESS_ERROR.format(
str(e), command, git_src_dir))
if stderr:
log.debug(GIT_PULL_FAILED)
return git_src_dir
return None
def download_url(opts):
src_dir = os.path.join(SUBPROCESS_KWARGS['cwd'], 'src')
dest_dir = os.path.join(src_dir, 'phantompy')
zipfilename = os.path.join(src_dir, 'phantompy-master.zip')
for downloader in ('wget', 'fetch', 'curl',):
if find_executable(downloader):
break
else:
downloader = None
buf = StringIO.StringIO()
try:
data = urlopen(PHANTOMPY_REPO_ZIP)
except:
raise Exception(UNABLE_TO_GET_PHANTOMPY_SOURCE)
else:
buf.write(data.read())
data.close()
buf.seek(0)
if downloader:
try:
subprocess.call(
shlex.split("{0} {1}".format(
downloader, PHANTOMPY_REPO_ZIP)),
cwd=src_dir)
except OSError:
log.debug("Failed Command:\n{0} {1}".format(
downloader, PHANTOMPY_REPO_ZIP))
return None
archive = zipfile.ZipFile(
buf or zipfilename)
archive.extractall(
path=os.path.join(SUBPROCESS_KWARGS['cwd'], 'src'))
os.rename(
os.path.join(
SUBPROCESS_KWARGS['cwd'], 'src', 'phantompy-master'),
dest_dir)
if not buf:
os.remove(zipfilename)
return dest_dir
return None
def download_phantompy_source(opts):
for method in (download_git, download_url,):
result = method(opts)
if result is not None:
break
else:
raise Exception("Failed to download phantompy source code!")
return result
def get_cmake_path():
path = find_executable('cmake')
if not path:
raise CMAKE_NOT_INSTALLED
return path
def compile_phantompy(opts):
if not os.path.exists(opts['phantompy_src']):
opts['phantompy_src'] = download_phantompy_source(opts)
retval = subprocess.call(shlex.split('cmake ..'),
cwd=os.path.join(opts['phantompy_src'], 'build'))
if retval:
raise Exception("CMake Error!")
retval = subprocess.call(shlex.split('make'),
cwd=os.path.join(opts['phantompy_src'], 'build'))
if retval:
raise Exception("Make error!")
def get_paths():
return {'git': find_executable('git'), 'cmake': get_cmake_path(),
'new_module_dir': os.path.join(
SUBPROCESS_KWARGS['cwd'], 'build', 'lib',
'phantomffipy'),
'phantompy_src': os.path.join(
SUBPROCESS_KWARGS['cwd'], 'src', 'phantompy')}
class build(distutils.command.build.build):
def run(self):
result = distutils.command.build.build.run(self)
print("building _phantompy.so")
opts = get_paths()
compile_phantompy(opts)
for fileish in glob.glob(os.path.join(
opts['phantompy_src'], 'build', '_phantompy*.so*')):
if os.path.isfile(fileish) and not os.path.islink(fileish):
print("installing {0} -> {1}".format(
fileish, os.path.join(opts['new_module_dir'],
'_phantompy.so')))
shutil.move(fileish, os.path.join(
opts['new_module_dir'], '_phantompy.so'))
break
else:
raise Exception("Library not found!")
headers = (os.path.join(opts['phantompy_src'], 'lib', 'phantompy.hpp'),
os.path.join(opts['new_module_dir'], 'phantompy.hpp'),)
print("installing {0} -> {1}".format(*headers))
shutil.copy(*headers)
return result
class clean(distutils.command.clean.clean):
def run(self):
phantompy_dir = os.path.join(
SUBPROCESS_KWARGS['cwd'], 'src', 'phantompy')
if os.path.exists(phantompy_dir):
shutil.rmtree(phantompy_dir)
return distutils.command.clean.clean.run(self)
setup(name='PhantomFFIPy',
version='0.0',
description='FFI interface for phantompy',
author='Autumn Jolitz',
requires=['cffi'],
author_email='[email protected]',
url='https://github.com/autumnjolitz/phantomffipy',
cmdclass={
'build': build,
'clean': clean
},
packages=['phantomffipy'],
)