Skip to content

Instantly share code, notes, and snippets.

@JustinVenus
Created January 31, 2019 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinVenus/2fece24acfb376553a629c805ae6eace to your computer and use it in GitHub Desktop.
Save JustinVenus/2fece24acfb376553a629c805ae6eace to your computer and use it in GitHub Desktop.
[pants-1.7.0:pex-1.3.2] work around PEP 503 and Sonatype Nexus PypiServer issues
diff --git a/3rdparty/python/pants/main.py b/3rdparty/python/pants/main.py
new file mode 100644
index 00000000..9cb08c35
--- /dev/null
+++ b/3rdparty/python/pants/main.py
@@ -0,0 +1,116 @@
+from collections import Iterable
+import re
+
+from pex.version import __version__
+
+
+def normalized_name(name):
+ """Return a PEP-503 normalized version of the given name.
+ See: https://www.python.org/dev/peps/pep-0503/#normalized-names
+ :param str name: The distribution name to normalize.
+ :rtype: str
+ """
+ return re.sub(r"[-_.]+", "-", name).lower()
+
+
+# Modified from: `pex.package.Package.satisfies`
+def satisfies(self, requirement, allow_prereleases=None):
+ """Determine whether this package matches the requirement.
+ :param requirement: The requirement to compare this Package against
+ :type requirement: string or :class:`pkg_resources.Requirement`
+ :param Optional[bool] allow_prereleases: Whether to allow prereleases to satisfy
+ the `requirement`.
+ :returns: True if the package matches the requirement, otherwise False
+ """
+ from pex.base import maybe_requirement
+ requirement = maybe_requirement(requirement)
+ link_name = normalized_name(self.name)
+ if link_name != normalized_name(requirement.key):
+ return False
+
+ # NB: If we upgrade to setuptools>=34 the SpecifierSet used here (requirement.specifier) will
+ # come from a non-vendored `packaging` package and pex's bootstrap code in `PEXBuilder` will
+ # need an update.
+ return requirement.specifier.contains(self.raw_version, prereleases=allow_prereleases)
+
+
+# Modified from: `pex.link.Link.wrap_iterable`
+def wrap_iterable(cls, url_or_urls):
+ """Given a string or :class:`Link` or iterable, return an iterable of :class:`Link` objects.
+
+ :param url_or_urls: A string or :class:`Link` object, or iterable of string or :class:`Link`
+ objects.
+ :returns: A list of :class:`Link` objects.
+ """
+ try:
+ result = [cls.wrap(url_or_urls)]
+ if result[-1].remote and result[-1].url.endswith('/'):
+ name = result[-1].url.split('/')[-2]
+ link_name = normalized_name(name)
+ if name != link_name:
+ result.append(cls.wrap(result[-1].url.replace('/' + name + '/', '/' + link_name + '/')))
+ return result
+ except ValueError:
+ pass
+ if isinstance(url_or_urls, Iterable):
+ result = []
+ for url in url_or_urls:
+ result.append(cls.wrap(url))
+ if result[-1].remote and result[-1].url.endswith('/'):
+ name = result[-1].url.split('/')[-2]
+ link_name = normalized_name(name)
+ if name != link_name:
+ result.append(cls.wrap(result[-1].url.replace('/' + name + '/', '/' + link_name + '/')))
+ return result
+ raise ValueError('url_or_urls must be string/Link or iterable of strings/Links')
+
+
+# work around `PEP 503` issues
+# https://github.com/pantsbuild/pex/pull/643
+# backporting fixes to pants-1.7.0
+if __version__ in ('1.3.2',):
+ # import order matters
+ import pex.resolver_options # noqa
+
+ def allow_external(self, key):
+ self._allow_external.add(normalized_name(key))
+ return self
+ pex.resolver_options.ResolverOptionsBuilder.allow_external = allow_external
+
+
+ def allow_unverified(self, key):
+ self._allow_external.add(normalized_name(key))
+ return self
+ pex.resolver_options.ResolverOptionsBuilder.allow_unverified = allow_unverified
+
+
+ import pex.resolvable # noqa
+ pex.resolvable.ResolvableRequirement.name = property(
+ lambda self: normalized_name(self.requirement.key))
+
+
+ import pex.resolver # noqa
+ pex.resolver._ResolvableSet.normalize = classmethod(lambda _, name: normalized_name(name))
+
+
+ import pex.link
+ pex.link.Link.wrap_iterable = classmethod(wrap_iterable)
+
+
+ import pex.package # noqa
+ pex.package.Package.name = property(lambda self: normalized_name(self._name))
+ pex.package.Package.satisfies = satisfies
+
+ pex.package.SourcePackage.name = property(lambda self: normalized_name(self._name))
+ pex.package.SourcePackage.satisfies = satisfies
+
+ pex.package.WheelPackage.name = property(lambda self: normalized_name(self._name))
+ pex.package.WheelPackage.satisfies = satisfies
+
+ pex.package.EggPackage.name = property(lambda self: normalized_name(self._name))
+ pex.package.EggPackage.satisfies = satisfies
+
+
+if __name__ == '__main__':
+ from pants.bin.pants_loader import main as proxy_main
+ proxy_main()
diff --git a/pants b/pants
index 0d9ef461..042eb249 100755
--- a/pants
+++ b/pants
@@ -99,4 +99,4 @@ function bootstrap_pants {
echo "${PANTS_BOOTSTRAP}/${pants_version}"
}
pants_dir=$(bootstrap_pants) && \
-exec "${pants_dir}/bin/python" "${pants_dir}/bin/pants" "$@"
+exec "${pants_dir}/bin/python" "3rdparty/python/pants/main.py" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment