4 Generate ~/.m2/settings.xml specifying proxies
5 if such environment variables are set.
14 ''' very simple xml generator for one-level depth items '''
15 result = [
'<%s>' % name]
16 for sec_name, sec_value
in sections:
17 result.append(
' <{0}>{1}</{0}>'.format(sec_name, sec_value))
18 result.append(
'</%s>' % name)
19 return '\n'.
join(
' ' + line
for line
in result)
24 value = os.environ.get(var_name,
'')
28 parsed = re.search(
r'''((?P<protocol>[^:]+)://)? # protocol followed by ://, optional
29 ((?P<username>[^:]+)(:(?P<password>[^@]+))?@)? # user:password part, optional
30 (?P<host>[^@]+?) # hostname, which is basically everything but other known parts
31 (:(?P<port>\d+))? # port, optional
32 $''', value, re.VERBOSE).groupdict()
33 except AttributeError:
34 sys.stderr.write(
'WARNING: unexpected format, could not parse $%s=%s\n' % (var_name, value))
37 if not parsed[
'host']:
39 id_name = var_name.lower()
40 if id_name
in _made_ids:
42 while (
'%s.%s' % (id_name, num))
in _made_ids:
44 id_name =
'%s.%s' % (id_name, num)
45 _made_ids.add(id_name)
46 sections = [(
'id', id_name), (
'active',
'true')]
47 for param_name
in (
'protocol',
'host',
'port',
'username',
'password'):
48 if parsed[param_name]:
49 sections.append((param_name, parsed[param_name]))
54 for name
in var_names:
57 sections.append(value)
60 template =
'''<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
61 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
62 xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
63 https://maven.apache.org/xsd/settings-1.0.0.xsd">
68 return template %
'\n'.
join(sections)
71 settings =
make_settings(
'http_proxy',
'HTTP_PROXY',
'https_proxy',
'HTTPS_PROXY')
72 target = os.path.expanduser(
'~/.m2/settings.xml')
77 if ex.errno != errno.ENOENT:
81 os.makedirs(os.path.dirname(target))
83 if ex.errno != errno.EEXIST:
85 with
open(target,
'w')
as out:
88 if __name__ ==
'__main__':
int open(const char *path, int flags, int mode)