-
Notifications
You must be signed in to change notification settings - Fork 25
/
setup.py
192 lines (165 loc) · 6.29 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
from setuptools import setup, find_packages
from pkg_resources import parse_version
import os
import subprocess
here = os.path.abspath(os.path.dirname(__file__))
# Check if we want to building a release package
is_release = False
try:
is_release = bool(os.environ['SECML_ISRELEASE'])
except KeyError:
pass
def read(*path_parts):
with open(os.path.join(here, *path_parts), 'r', encoding='ascii') as fp:
return fp.read().strip()
def parse_readme(*path_parts): # For README.md we accept utf-8 chars
with open(os.path.join(here, *path_parts), 'r', encoding='utf-8') as fp:
return fp.read().strip()
# Return the git revision as a string
# Thanks to Numpy GitHub: https://github.com/numpy/numpy
def git_version():
def _minimal_ext_cmd(cmd):
# construct minimal environment
env = {}
for k in ['SYSTEMROOT', 'PATH', 'HOME']:
v = os.environ.get(k)
if v is not None:
env[k] = v
# LANGUAGE is used on win32
env['LANGUAGE'] = 'C'
env['LANG'] = 'C'
env['LC_ALL'] = 'C'
# Execute in the current dir
res = subprocess.Popen(cmd, cwd=here, env=env,
stdout=subprocess.PIPE,
stderr=open(os.devnull, 'w')).communicate()[0]
return res
try:
out = _minimal_ext_cmd(['git', 'rev-parse', '--short', 'HEAD'])
GIT_REVISION = out.strip().decode('ascii')
if len(GIT_REVISION) == 0:
raise OSError
except OSError:
GIT_REVISION = 'Unknown'
return GIT_REVISION
def find_version(*path_parts):
"""This function read version number and revision number if available."""
try:
_v_f = read(*path_parts) # Read main version file
if not is_release: # Override for is_release checks
_v_git = git_version()
if _v_git == 'Unknown':
try: # Try to read rev from file. May not exists
_v_git = read('src', 'secml', 'VERSION_REV')
except:
pass # _v_git will stay "Unknown"
else:
write_rev(_v_git, 'src', 'secml', 'VERSION_REV')
# Append rev number only if available
_v = _v_f if _v_git == 'Unknown' else _v_f + '+' + _v_git
else:
_v = _v_f # release package
_v = parse_version(_v) # Integrity checks
# Display rev number (if available) for dev releases only
# alpha/beta/rc/final only display public
return str(_v) if _v._version.dev is not None else _v.public
except:
raise RuntimeError("Unable to find version string.")
def write_rev(v, *path_parts):
"""Write revision id to file."""
a = open(os.path.join(here, *path_parts), 'w')
try:
a.write(v)
finally:
a.close()
def install_deps():
"""Reads requirements.txt and preprocess it
to be feed into setuptools.
This is the only possible way (we found)
how requirements.txt can be reused in setup.py
using dependencies from private github repositories.
Links must be appended by `-{StringWithAtLeastOneNumber}`
or something like that, so e.g. `-9231` works as well as
`1.1.0`. This is ignored by the setuptools, but has to be there.
Warnings
--------
To make pip respect the links, you have to use
`--process-dependency-links` switch. So e.g.:
`pip install --process-dependency-links {git-url}`
Returns
-------
List of packages and dependency links.
Notes
-----
Thanks to knykda for this implementation:
https://github.com/pypa/pip/issues/3610#issuecomment-356687173
"""
default = open(os.path.join(here, 'requirements.txt'),
'r', encoding='ascii').readlines()
new_pkgs = []
links = []
for resource in default:
if 'git+ssh' in resource:
pkg = resource.split('#')[-1]
links.append(resource.strip() + '-9876543210')
new_pkgs.append(pkg.replace('egg=', '').rstrip())
else:
new_pkgs.append(resource.strip())
return new_pkgs, links
REQ_PKGS, DEP_LINKS = install_deps()
LONG_DESCRIPTION = parse_readme('README.md')
# List of classifiers: https://pypi.org/pypi?%3Aaction=list_classifiers
CLASSIFIERS = """\
Development Status :: 3 - Alpha
Intended Audience :: Science/Research
Intended Audience :: Developers
License :: OSI Approved
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: Implementation :: PyPy
Topic :: Software Development
Topic :: Scientific/Engineering
Operating System :: POSIX
Operating System :: Unix
Operating System :: MacOS
Operating System :: Microsoft :: Windows
"""
setup(
name='secml',
version=find_version("src", "secml", "VERSION"),
description='A library for Secure and Explainable Machine Learning',
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
license='Apache License 2.0',
classifiers=[_f for _f in CLASSIFIERS.split('\n') if _f],
platforms=["Linux", "Mac OS-X", "Unix", "Windows"],
url='https://secml.readthedocs.io',
download_url="https://pypi.python.org/pypi/secml#files",
project_urls={
"Bug Tracker": "https://github.com/pralab/secml/issues",
"Source Code": "https://github.com/pralab/secml",
},
maintainer='Maura Pintor, Luca Demetrio',
maintainer_email='[email protected], [email protected]',
packages=find_packages('src', exclude=[
"*.tests", "*.tests.*", "tests.*", "tests"]),
package_dir={'': 'src'},
include_package_data=True,
python_requires='>=3.5, <3.11',
install_requires=REQ_PKGS,
extras_require={
'pytorch': ["torch>=1.4,!=1.5.*", "torchvision>=0.5,!=0.6.*"],
'cleverhans': ["tensorflow>=1.14,<2", "cleverhans<3.1"],
'tf-gpu': ["tensorflow-gpu>=1.14,<2"],
'foolbox': ["foolbox>=3.3.0", "torch>=1.4,!=1.5.*", "torchvision>=0.5,!=0.6.*"],
'unittests': ['pytest>=5',
'pytest-cov>=2.9', 'coverage',
'jupyter', 'nbval', 'requests-mock']
},
zip_safe=False
)