send-error-report 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. # Sends an error report (if the report-error class was enabled) to a remote server.
  3. #
  4. # Copyright (C) 2013 Intel Corporation
  5. # Author: Andreea Proca <andreea.b.proca@intel.com>
  6. import httplib, urllib, os, sys, json, base64
  7. from urllib2 import _parse_proxy as parseproxy
  8. def handle_connection(server, data):
  9. params = urllib.urlencode({'data': data})
  10. headers = {"Content-type": "application/json"}
  11. proxyrequired = False
  12. if os.environ.get("http_proxy") or os.environ.get("HTTP_PROXY"):
  13. proxyrequired = True
  14. # we need to check that the server isn't a local one, as in no_proxy
  15. try:
  16. temp = httplib.HTTPConnection(server, strict=True, timeout=5)
  17. temp.request("GET", "/")
  18. tempres = temp.getresponse()
  19. if tempres.status == 200:
  20. proxyrequired = False
  21. temp.close()
  22. except:
  23. pass
  24. if proxyrequired:
  25. proxy = parseproxy(os.environ.get("http_proxy") or os.environ.get("HTTP_PROXY"))
  26. if proxy[1] and proxy[2]:
  27. auth = base64.encodestring("%s:%s" % (proxy[1], proxy[2]))
  28. headers["Authorization"] = "Basic %s" % auth
  29. conn = httplib.HTTPConnection(proxy[3])
  30. conn.request("POST", "http://%s/ClientPost/" % server, params, headers)
  31. else:
  32. conn = httplib.HTTPConnection(server)
  33. conn.request("POST", "/ClientPost/", params, headers)
  34. return conn
  35. def sendData(json_file, server):
  36. if os.path.isfile(json_file):
  37. home = os.path.expanduser("~")
  38. userfile = os.path.join(home, ".oe-send-error")
  39. if os.path.isfile(userfile):
  40. with open(userfile) as g:
  41. username = g.readline()
  42. email = g.readline()
  43. else:
  44. print("Please enter your name and your email (optionally), they'll be saved in the file you send.")
  45. username = raw_input("Name: ")
  46. email = raw_input("E-mail (not required): ")
  47. if len(username) > 0 and len(username) < 50:
  48. with open(userfile, "w") as g:
  49. g.write(username + "\n")
  50. g.write(email + "\n")
  51. else:
  52. print("Invalid inputs, try again.")
  53. return
  54. with open(json_file) as f:
  55. data = f.read()
  56. try:
  57. jsondata = json.loads(data)
  58. jsondata['username'] = username.strip()
  59. jsondata['email'] = email.strip()
  60. data = json.dumps(jsondata, indent=4, sort_keys=True)
  61. except:
  62. print("Invalid json data")
  63. return
  64. try:
  65. conn = handle_connection(server, data)
  66. response = conn.getresponse()
  67. print response.status, response.reason
  68. res = response.read()
  69. if response.status == 200:
  70. print(res)
  71. else:
  72. print("There was a problem submiting your data, response written in %s.response.html" % json_file)
  73. with open("%s.response.html" % json_file, "w") as f:
  74. f.write(res)
  75. conn.close()
  76. except Exception as e:
  77. print("Server connection failed: %s" % e)
  78. else:
  79. print("No data file found.")
  80. if __name__ == '__main__':
  81. print ("\nSends an error report (if the report-error class was enabled) to a remote server.")
  82. print("\nThis scripts sends the contents of the error to a public upstream server.")
  83. print("\nPlease remove any identifying information before sending.")
  84. if len(sys.argv) < 2:
  85. print("\nUsage: send-error-report <error_fileName> [server]")
  86. print("\nIf this is the first when sending a report you'll be asked for your name and optionally your email address.")
  87. print("They will be associated with your report.\n")
  88. elif len(sys.argv) == 3:
  89. sendData(sys.argv[1], sys.argv[2])
  90. else:
  91. sendData(sys.argv[1], "errors.yoctoproject.org")