-
Notifications
You must be signed in to change notification settings - Fork 147
/
setup.py
145 lines (117 loc) · 5.13 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
#
# Copyright 2016 LinkedIn Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import print_function
import os
import sys
import pkg_resources
import platform
from setuptools import setup, find_packages, Command
from setuptools.command.install_egg_info import install_egg_info as _install_egg_info
from setuptools.dist import Distribution
class EntryPoints(Command):
description = 'get entrypoints for a distribution'
user_options = [
('dist=', None, 'get entrypoints for specified distribution'),
]
def initialize_options(self):
self.dist = self.distribution.get_name()
def finalize_options(self):
"""Abstract method that is required to be overwritten"""
def run(self):
req_entry_points = pkg_resources.get_entry_map(self.dist)
if req_entry_points and 'console_scripts' in req_entry_points:
for entry in list(req_entry_points['console_scripts'].values()):
print(entry, file=sys.stdout)
class install_egg_info(_install_egg_info): # noqa
"""Override the setuptools namespace package templates.
Customizes the "nspkg.pth" files so that they're compatible with
"--editable" packages.
See this pip issue for details:
https://github.com/pypa/pip/issues/3
Modifications to the original implementation are marked with CHANGED
"""
_nspkg_tmpl = (
# CHANGED: Add the import of pkgutil needed on the last line.
"import sys, types, os, pkgutil",
"p = os.path.join(sys._getframe(1).f_locals['sitedir'], *%(pth)r)",
"ie = os.path.exists(os.path.join(p, '__init__.py'))",
"m = not ie and "
"sys.modules.setdefault(%(pkg)r, types.ModuleType(%(pkg)r))",
"mp = (m or []) and m.__dict__.setdefault('__path__', [])",
"(p not in mp) and mp.append(p)",
# CHANGED: Fix the resulting __path__ on the namespace packages to
# properly traverse "--editable" packages too.
"mp[:] = m and pkgutil.extend_path(mp, %(pkg)r) or mp",
)
"lines for the namespace installer"
_nspkg_tmpl_multi = (
# CHANGED: Use "__import__" to ensure the parent package has been
# loaded before attempting to read it from sys.modules.
# This avoids a possible issue with nested namespace packages where the
# parent could be skipped due to an existing __init__.py file.
'm and __import__(%(parent)r) and setattr(sys.modules[%(parent)r], %(child)r, m)',
)
"additional line(s) when a parent package is indicated"
class GradleDistribution(Distribution, object):
PINNED_TXT = 'pinned.txt'
excluded_platform_packages = {}
def __init__(self, attrs):
attrs['name'] = os.getenv('PYGRADLE_PROJECT_NAME')
attrs['version'] = os.getenv('PYGRADLE_PROJECT_VERSION')
attrs['install_requires'] = list(self.load_pinned_deps())
super(GradleDistribution, self).__init__(attrs)
def get_command_class(self, command):
"""Return a customized command class or the base one."""
if command == 'install_egg_info':
return install_egg_info
elif command == 'entrypoints':
return EntryPoints
return super(GradleDistribution, self).get_command_class(command)
@property
def excluded_packages(self):
platform_name = platform.system().lower()
if platform_name in self.excluded_platform_packages:
return set(pkg.lower() for pkg in self.excluded_platform_packages[platform_name])
return set()
def load_pinned_deps(self):
"""Load a pinned.txt file.
The pinned.txt file contains a list of dependencies that this Python
project depends on. Although the PyGradle build system will ignore this
file and never install dependencies declared via this method, it is
important to declare the dependencies using this method to maintain
backwards compatibility with non-PyGradle build systems.
"""
# calculate this only once
blacklisted = self.excluded_packages
try:
reqs = []
with open(self.PINNED_TXT) as fh:
reqs = fh.readlines()
# Don't include the version information so that we don't mistakenly
# introduce a version conflict issue.
for req in reqs:
if req:
name, version = req.split('==')
if name and name.lower() not in blacklisted:
yield name
except IOError:
raise StopIteration
setup(
distclass=GradleDistribution,
package_dir={'': 'src'},
packages=find_packages('src'),
include_package_data=True,
)