plugin_upload.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. """This script uploads a plugin package to the plugin repository.
  4. Authors: A. Pasotti, V. Picavet
  5. git sha : $TemplateVCSFormat
  6. """
  7. import sys
  8. import getpass
  9. import xmlrpc.client
  10. from optparse import OptionParser
  11. standard_library.install_aliases()
  12. # Configuration
  13. PROTOCOL = 'https'
  14. SERVER = 'plugins.qgis.org'
  15. PORT = '443'
  16. ENDPOINT = '/plugins/RPC2/'
  17. VERBOSE = False
  18. def main(parameters, arguments):
  19. """Main entry point.
  20. :param parameters: Command line parameters.
  21. :param arguments: Command line arguments.
  22. """
  23. address = "{protocol}://{username}:{password}@{server}:{port}{endpoint}".format(
  24. protocol=PROTOCOL,
  25. username=parameters.username,
  26. password=parameters.password,
  27. server=parameters.server,
  28. port=parameters.port,
  29. endpoint=ENDPOINT)
  30. print("Connecting to: %s" % hide_password(address))
  31. server = xmlrpc.client.ServerProxy(address, verbose=VERBOSE)
  32. try:
  33. with open(arguments[0], 'rb') as handle:
  34. plugin_id, version_id = server.plugin.upload(
  35. xmlrpc.client.Binary(handle.read()))
  36. print("Plugin ID: %s" % plugin_id)
  37. print("Version ID: %s" % version_id)
  38. except xmlrpc.client.ProtocolError as err:
  39. print("A protocol error occurred")
  40. print("URL: %s" % hide_password(err.url, 0))
  41. print("HTTP/HTTPS headers: %s" % err.headers)
  42. print("Error code: %d" % err.errcode)
  43. print("Error message: %s" % err.errmsg)
  44. except xmlrpc.client.Fault as err:
  45. print("A fault occurred")
  46. print("Fault code: %d" % err.faultCode)
  47. print("Fault string: %s" % err.faultString)
  48. def hide_password(url, start=6):
  49. """Returns the http url with password part replaced with '*'.
  50. :param url: URL to upload the plugin to.
  51. :type url: str
  52. :param start: Position of start of password.
  53. :type start: int
  54. """
  55. start_position = url.find(':', start) + 1
  56. end_position = url.find('@')
  57. return "%s%s%s" % (
  58. url[:start_position],
  59. '*' * (end_position - start_position),
  60. url[end_position:])
  61. if __name__ == "__main__":
  62. parser = OptionParser(usage="%prog [options] plugin.zip")
  63. parser.add_option(
  64. "-w", "--password", dest="password",
  65. help="Password for plugin site", metavar="******")
  66. parser.add_option(
  67. "-u", "--username", dest="username",
  68. help="Username of plugin site", metavar="user")
  69. parser.add_option(
  70. "-p", "--port", dest="port",
  71. help="Server port to connect to", metavar="80")
  72. parser.add_option(
  73. "-s", "--server", dest="server",
  74. help="Specify server name", metavar="plugins.qgis.org")
  75. options, args = parser.parse_args()
  76. if len(args) != 1:
  77. print("Please specify zip file.\n")
  78. parser.print_help()
  79. sys.exit(1)
  80. if not options.server:
  81. options.server = SERVER
  82. if not options.port:
  83. options.port = PORT
  84. if not options.username:
  85. # interactive mode
  86. username = getpass.getuser()
  87. print("Please enter user name [%s] :" % username, end=' ')
  88. res = input()
  89. if res != "":
  90. options.username = res
  91. else:
  92. options.username = username
  93. if not options.password:
  94. # interactive mode
  95. options.password = getpass.getpass()
  96. main(options, args)