Skip to content

Commit

Permalink
ant plugin: honour proxy configuration (#1256)
Browse files Browse the repository at this point in the history
LP: #1682534
  • Loading branch information
cjwatson authored and Leo Arias committed Apr 27, 2017
1 parent 7b60407 commit 21fe3e1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
22 changes: 21 additions & 1 deletion snapcraft/plugins/ant.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import glob
import logging
import os
from urllib.parse import urlsplit

import snapcraft
import snapcraft.common
Expand Down Expand Up @@ -96,12 +97,31 @@ def build(self):
base = os.path.basename(f)
os.link(f, os.path.join(jardir, base))

def get_proxy_options(self, scheme):
proxy = os.environ.get('{}_proxy'.format(scheme))
if proxy:
parsed = urlsplit(proxy)
if parsed.hostname is not None:
yield '-D{}.proxyHost={}'.format(scheme, parsed.hostname)
if parsed.port is not None:
yield '-D{}.proxyPort={}'.format(scheme, parsed.port)

def env(self, root):
env = super().env(root)
jars = glob.glob(os.path.join(self.installdir, 'jar', '*.jar'))
if jars:
jars = [os.path.join(root, 'jar',
os.path.basename(x)) for x in jars]
os.path.basename(x)) for x in sorted(jars)]
env.extend(
['CLASSPATH={}:$CLASSPATH'.format(':'.join(jars))])
# Getting ant to use a proxy requires a little work; the JRE doesn't
# help as much as it should. (java.net.useSystemProxies=true ought
# to do the trick, but it relies on desktop configuration rather
# than using the standard environment variables.)
ant_opts = []
ant_opts.extend(self.get_proxy_options('http'))
ant_opts.extend(self.get_proxy_options('https'))
if ant_opts:
env.append("ANT_OPTS='{}'".format(
' '.join(opt.replace("'", "'\\''") for opt in ant_opts)))
return env
25 changes: 24 additions & 1 deletion snapcraft/tests/plugins/test_ant.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import os
import copy
from unittest import mock

import fixtures
from testtools.matchers import HasLength

import snapcraft
Expand Down Expand Up @@ -113,4 +115,25 @@ def test_env(self):
'jar', 'lib1.jar'), 'w').close()
open(os.path.join(plugin.installdir,
'jar', 'lib2.jar'), 'w').close()
plugin.env(plugin.partdir)
env = plugin.env(plugin.partdir)
self.assertIn(
'CLASSPATH={}/jar/lib1.jar:{}/jar/lib2.jar:$CLASSPATH'.format(
plugin.partdir, plugin.partdir),
env)

def test_env_proxies(self):
env_vars = (
('http_proxy', 'http://localhost:3132'),
('https_proxy', 'http://localhost2:3133'),
)
for v in env_vars:
self.useFixture(fixtures.EnvironmentVariable(v[0], v[1]))
plugin = ant.AntPlugin('test-part', self.options,
self.project_options)

env = plugin.env(plugin.partdir)
self.assertIn(
"ANT_OPTS='"
"-Dhttp.proxyHost=localhost -Dhttp.proxyPort=3132 "
"-Dhttps.proxyHost=localhost2 -Dhttps.proxyPort=3133'",
env)

0 comments on commit 21fe3e1

Please sign in to comment.