OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
make-m2-proxy.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 '''
4 Generate ~/.m2/settings.xml specifying proxies
5 if such environment variables are set.
6 '''
7 
8 import os
9 import sys
10 import re
11 import errno
12 
13 def simple_xml(name, sections):
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)
20 
21 _made_ids = set()
22 
23 def gen_proxy(var_name):
24  value = os.environ.get(var_name, '')
25  if not value:
26  return None
27  try:
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))
35  return None
36 
37  if not parsed['host']:
38  return None
39  id_name = var_name.lower()
40  if id_name in _made_ids:
41  num = 0
42  while ('%s.%s' % (id_name, num)) in _made_ids:
43  num +=1
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]))
50  return simple_xml('proxy', sections)
51 
52 def make_settings(*var_names):
53  sections = []
54  for name in var_names:
55  value = gen_proxy(name)
56  if value:
57  sections.append(value)
58  if not sections:
59  return None
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">
64  <proxies>
65 %s
66  </proxies>
67 </settings>'''
68  return template % '\n'.join(sections)
69 
70 def main():
71  settings = make_settings('http_proxy', 'HTTP_PROXY', 'https_proxy', 'HTTPS_PROXY')
72  target = os.path.expanduser('~/.m2/settings.xml')
73  if not settings:
74  try:
75  os.remove(target)
76  except OSError as ex:
77  if ex.errno != errno.ENOENT:
78  raise
79  return
80  try:
81  os.makedirs(os.path.dirname(target))
82  except OSError as ex:
83  if ex.errno != errno.EEXIST:
84  raise
85  with open(target, 'w') as out:
86  out.write(settings)
87 
88 if __name__ == '__main__':
89  main()
std::string join(T const &container, std::string const &delim)
int open(const char *path, int flags, int mode)
Definition: heavyai_fs.cpp:66