def __init__(self, config_updates=None): self.config = Config() self.config.update(MODULE_DEFAULT_CONFIG) if config_updates: self.config.hard_update(config_updates) self.arg_parser = ArgumentParserWrapper( name = MODULE_INFO[1], desc = MODULE_INFO[2], indent = self.config.indent, indent_c = self.config.indent_c, ) self.set_args(self.arg_parser)
class Configure(object): """ Configuration 'wizard'. """ def set_args(self, parser): """Adds module arguments to parser.""" parser.add_argument( '-h', '--help' , action='store_true' , dest='help_dummy' , only_in_help=True , help="Display this help message" ) def debug_print(self, verbosity_level, *args, **kwargs): if self.config.verbose >= verbosity_level: print(*args, **kwargs) def __init__(self, config_updates=None): self.config = Config() self.config.update(MODULE_DEFAULT_CONFIG) if config_updates: self.config.hard_update(config_updates) self.arg_parser = ArgumentParserWrapper( name = MODULE_INFO[1], desc = MODULE_INFO[2], indent = self.config.indent, indent_c = self.config.indent_c, ) self.set_args(self.arg_parser) def run(self, args): """ Runs the module. @param args input arguments to be parsed. @type args list """ self.config.hard_update(vars(self.arg_parser.parse_args(args))) self.debug_print(2, "* Checking if the settings directory exists") if not os.path.isdir(self.config.settings_dir): try: # NOTE: Creation of '/etc/' is not handled here. # To do that, see 'os.makedirs'. os.mkdir(self.config.settings_dir) self.debug_print(1, "* Created settings directory '%s'") except OSError as e: error_msg = \ "* Error: unable to create the config directory: " + e print(gentoolkit.pprinter.error(error_msg), file=sys.stderr) return 1 self.configure_auth() self.configure_payload() self.debug_print(1, "* Configuration complete!") return 0 def configure_auth(self): file_s = os.path.join(self.config.settings_dir, self.config.auth_file) # Make sure to create the auth file if it doesn't already exist: if not touch_file(file_s): return cp = ConfigParser.RawConfigParser() # If a config already exists, read it: cp.read(file_s) if not cp.has_section('AUTH'): cp.add_section('AUTH') self.debug_print(2, "* Created 'AUTH' section in config") if cp.has_option('AUTH', 'UUID'): self.debug_print(2, "* UUID already exists, skipping") else: the_uuid = str(uuid.uuid4()) cp.set('AUTH', 'UUID', the_uuid) self.debug_print(2, "* Generated UUID '%s'" % (the_uuid)) if cp.has_option('AUTH', 'PASSWD'): self.debug_print(2, "* PASSWD already exists, skipping") else: # passwd = str(uuid.uuid4()).replace('-', '') # passwd = base64.urlsafe_b64encode(os.urandom(16))[:16] passwd = os.urandom(16).encode("base64")[:16] cp.set('AUTH', 'PASSWD', passwd) self.debug_print(2, "* Generated PASSWD") save_cp(cp, file_s) return def configure_payload(self): file_s = os.path.join(self.config.settings_dir, self.config.payload_file) # Make sure to create the payload file if it doesn't already exist: if not touch_file(file_s): return cp = ConfigParser.RawConfigParser() # If a config already exists, read it and update it: cp.read(file_s) if not cp.has_section('ENV'): cp.add_section('ENV') self.debug_print(2, "* Created 'ENV' section in config") while True: answer = userquery("Would you like to report any make.conf options?", ["Yes", "Help", "No"]) if answer == 'Help': print(" You'll be asked exactly what to report. If in doubt, answer 'Yes'.") else: break if answer == "Yes": for setting, desc in MAKE_CONF_SETTINGS: while True: answer = userquery(" Report %s?" % setting, ["Yes", "Help", "No"]) if answer == 'Help': print(" Short description of %s: %s" % (setting, desc)) else: cp.set('ENV', setting, answer.lower()) break else: for setting, _ in MAKE_CONF_SETTINGS: cp.set('ENV', setting, 'no') if not cp.has_section('PACKAGES'): cp.add_section('PACKAGES') self.debug_print(2, "* Created 'PACKAGES' section in config") while True: answer = userquery("Would you like to report statistics about your packages?", ["Yes", "Help", "No"]) if answer == 'Help': print(" You'll be asked exactly what to report. If in doubt, answer 'Yes'.") else: break if answer == "Yes": for setting, desc in PACKAGE_SETTINGS: while True: answer = userquery(" Report %s?" % setting, ["Yes", "Help", "No"]) if answer == 'Help': print(" Short description of %s: %s" % (setting, desc)) else: cp.set('PACKAGES', setting, answer.lower()) break else: for setting, _ in PACKAGE_SETTINGS: cp.set('PACKAGES', setting, 'no') save_cp(cp, file_s) return
class Submit(object): """ Module class. """ def set_args(self, parser): """Adds module arguments to parser.""" parser.add_argument( '-h', '--help' , action='store_true' , dest='help_dummy' , only_in_help=True , help="Display this help message" ) parser.add_argument( '-s', '--server' , metavar="ADDR:PORT" , default=self.config.server , help="Server to upload stats to\n(default: %s)" % (self.config.server) ) parser.add_argument( '-u', '--url' , metavar="URL" , default=self.config.url , help="URL to use for the upload (e.g. /upload/)" ) parser.add_argument( '-p', '--pretend' , action='store_true' , default=self.config.pretend , help="Generate, but don't upload stats" ) parser.add_argument( '-a', '--auth' , metavar="FILE" , default=self.config.auth , help="Authentication config file\n(default: %s)" % (self.config.auth) ) parser.add_argument( '-P', '--payload' , metavar="FILE" , default=self.config.payload , help="Payload config file\n(default: %s)" % (self.config.payload) ) parser.add_argument( '--ssl' , type=FlexibleBool , metavar="CHOICE" , default=self.config.ssl , help="Use SSL when uploading stats (default: %s)" \ % ('yes' if self.config.ssl else 'no') ) def __init__(self, config_updates=None): self.config = Config() self.config.update(MODULE_DEFAULT_CONFIG) if config_updates: self.config.update(config_updates) self.arg_parser = ArgumentParserWrapper( name = MODULE_INFO[1], desc = MODULE_INFO[2], indent = self.config.indent, indent_c = self.config.indent_c, ) self.set_args(self.arg_parser) def run(self, args): """runs the module @param args Input arguments to be parsed. @type args list """ self.config.update(vars(self.arg_parser.parse_args(args))) if self.config.ssl == False and \ self.config.server == MODULE_DEFAULT_CONFIG['server']: print("Note: Have you forgotten to change the port number?") print("You may want to try '%s'." % self.config.server_nossl) self.config.url = self.config.url.rstrip('/') + '/' full_upload_url = self.config.server + self.config.url if self.config.verbose: print("Using URL: '%s'" % (full_upload_url)) print("Using SSL: %s" % (self.config.ssl)) if self.config.pretend: print("Dry run: %s" % (self.config.pretend)) print("\nGenerating payload... ", end='') sys.stdout.flush() payload = Payload( payload_file=self.config.payload, auth_file=self.config.auth ) post_data = payload.get() if self.config.verbose: print("done") print("Serialising payload... ", end='') sys.stdout.flush() request_body = serialize(post_data) request_headers = {'Content-type': 'application/json'} if self.config.verbose: print("done") ### if self.config.verbose >= 2: print("Serialised payload:") payload.dump(human=True) ### if self.config.pretend: print("Dry run, exiting...") return 0 ### if self.config.ssl: conn_class = httplib.HTTPSConnection else: conn_class = httplib.HTTPConnection conn = conn_class(self.config.server) try: if self.config.verbose: print("Sending report... ") sys.stdout.flush() conn.request( 'POST' , url = self.config.url , headers = request_headers , body = request_body ) response = conn.getresponse() print('Server response: %s (%s)' % \ (response.status, response.reason)) print(response.read()) except httplib.HTTPException: sys.stderr.write('Something went wrong') return 1 finally: if conn: conn.close()