This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
165 lines (142 loc) · 5.7 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
# Copyright (c) 2012, Sven Thiele <sthiele78@gmail.com>
#
# This file is part of pyasp.
#
# pyasp is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyasp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyasp. If not, see <http://www.gnu.org/licenses/>.
# -*- coding: utf-8 -*-
#from distutils.core import setup
from setuptools import setup
from setuptools.command.install import install as _install
import os
import sys
import site
import logging
import platform
import sysconfig
import subprocess
# version specific imports
if sys.version_info >= (3,):
from urllib import request
urlretrieve = request.urlretrieve
else: # python 2 compatibility
# TODO: using requests module (http://docs.python-requests.org/en/latest/index.html)
# is maybe a good idea
from urllib import urlretrieve
# import pyasp, get access to binary files
from pyasp import constant
from pyasp import info
BINARIES_BASE_URL = 'https://github.com/sthiele/pyasp/releases/download/v1.4.4/{}-{}'
BINARIES_NAME = {
# binary remote name: binary local name
'clasp-3.1.1': 'clasp',
'gringo-3.0.5': 'gringo3',
'gringo-5.3.0': 'gringo4',
}
BASE_URL_PLATFORM_SPECIFIC_SUBPATHS = {
# (platform, architecture) : path to binaries from BINARIES_BASE_URL
('linux', '64'): 'linux-64', # linux 32 bits is not supported
('darwin', '64'): 'macos', # darwin 32 bits is not supported
# windows platform is not supported
}
def system_info():
"""Return (platform name, architecture), or (None, None) if not found"""
# get architecture and platform information
arch = platform.architecture()[0][:-3]
platform_name = None
for platform_name, _ in BASE_URL_PLATFORM_SPECIFIC_SUBPATHS:
if sys.platform.startswith(platform_name):
return platform_name, arch
return None, None
def binaries_urls(platform_name, arch):
"""Return tuple of binaries name and URL, based on given sys informations.
If detected system is not supported, an empty iterable is returned.
Architecture and platforms supported depends of distant binary repository.
"""
try:
subpath = BASE_URL_PLATFORM_SPECIFIC_SUBPATHS[platform_name, arch]
except KeyError:
logging.getLogger().error(
'clasp/gringo3/gringo4 binaries are not available for'
' platform ' + platform_name + ' under architecture '
+ arch + 'bits.')
return tuple() # empty iterable
# no error: build the tuple of binaries paths
return tuple((local_name, BINARIES_BASE_URL.format(subpath, remote_name))
for remote_name, local_name in BINARIES_NAME.items())
def binaries_directory():
"""Return the installation directory, or None"""
if '--user' in sys.argv:
paths = (site.getusersitepackages(),)
else:
py_version = '%s.%s' % (sys.version_info[0], sys.version_info[1])
paths = (s % (py_version) for s in (
sys.prefix + '/lib/python%s/dist-packages/',
sys.prefix + '/lib/python%s/site-packages/',
sys.prefix + '/local/lib/python%s/dist-packages/',
sys.prefix + '/local/lib/python%s/site-packages/',
'/Library/Python/%s/site-packages/',
))
# yield the first valid path
for path in paths:
# add the package and bin subdir and, if exists, return it
path = os.path.join(path, '%s/%s' % (info.__pkg_name__, constant.REL_DIR_BIN))
if os.path.exists(path):
return path
logging.getLogger().error(
'pyasp binaries path not found. You need to download and'
' put in place the binaries for gringo3, gringo4 and clasp'
' in order to start using pyasp.'
' You can find binaries from ' + BINARIES_BASE_URL +
' or https://sourceforge.net/projects/potassco/files/,'
' or compile them yourself.'
)
exit()
return None
def post_install():
"""Get the binaries online, and give them the execution permission"""
bin_dir = binaries_directory()
for binary_name, binary_url in binaries_urls(*system_info()):
logging.getLogger().info('RETRIEVE: ' + os.path.join(bin_dir, binary_name)
+ ' from ' + binary_url)
bin_path = os.path.join(bin_dir, binary_name)
urlretrieve(binary_url, bin_path)
logging.getLogger().info('CHMOD COMMAND: '
+ ' '.join(['chmod', '+x', bin_path]))
subprocess.call(['chmod', '+x', bin_path])
class install(_install):
def run(self):
"""Call superclass run method, then downloads the binaries"""
_install.run(self)
self.execute(post_install, args=[], msg=post_install.__doc__)
setup(
cmdclass={'install': install},
name = info.__pkg_name__,
version = info.__pkg_version__,
url='http://pypi.python.org/pypi/pyasp/',
license='GPLv3+',
description='A convenience wrapper for the ASP tools gringo, gringo4 and clasp.',
long_description=open('README.md').read(),
long_description_content_type="text/markdown",
author='Sven Thiele',
author_email='sthiele78@gmail.com',
zip_safe=False, # not zippable because of the binary retrieving
package_dir = { 'pyasp' : 'pyasp'},
package_data = {
'pyasp' : ['bin/*.txt']
},
packages = [
'pyasp',
'pyasp.ply'
],
)