def get_open_port(): """ Resolves an available port for socket messages Returns: available port number """ environment.getEnvironmentVars() try: # get the port number from the settings file port = int(os.environ['APP_CONSOLE_SOCKET']) except KeyError: # set the port to 0 if it is not found in the settings file. This will make the socket library find an open port. port = 0 try: s = socket(AF_INET, SOCK_DGRAM) # attempt to bind to the port s.bind(("localhost", port)) # get the port number from the socket port = s.getsockname()[1] # always close the socket. This allows the gui console to bind to it later (see caveat below) s.close() # save this port back to the environment vars environment.setEnvironmentVar('APP', 'CONSOLE_SOCKET', port) except: # an error is raised if the gui has bound to this address before the engine tries to. # ignore this error for now, however we should wait until sprint has resolved socket # ports before allowing the gui to bind to it. pass return port
def test_set_environment_vars(self): # get the current environment variables vars = os.environ self.assertTrue('LEGEND_LOCATIONBOTTOM' in vars) self.assertTrue('LEGEND_LOCATIONRIGHT' in vars) self.assertTrue('LOGGING_SHOWCRITICAL' in vars) self.assertTrue('LOGGING_SHOWDEBUG' in vars) self.assertTrue('LOGGING_SHOWERROR' in vars) self.assertTrue('LOGGING_SHOWINFO' in vars) self.assertTrue('LOGGING_SHOWWARNING' in vars) self.assertTrue('APP_IMAGES_PATH' in vars) self.assertTrue('APP_LOCAL_DB_PATH' in vars) self.assertTrue('APP_SETTINGS_PATH' in vars) self.assertTrue('APP_TOOLBOX_PATH' in vars) self.assertTrue('APP_USER_PATH' in vars) self.assertTrue('GDAL_DATA' in vars) # change some paths user1 = os.environ['APP_USER_PATH'] environment.setEnvironmentVar('APP','USER_PATH', 'some_other_path') self.assertTrue(user1 != os.environ['APP_USER_PATH']) # add a new section environment.setEnvironmentVar('MyNewSection','test', '1') self.assertTrue('MYNEWSECTION_TEST' in os.environ.keys()) environment.writeDefaultEnvironment()
def get_open_port(): """ Resolves an available port for socket messages Returns: available port number """ environment.getEnvironmentVars() try: # get the port number from the settings file port = int(os.environ['APP_CONSOLE_SOCKET']) except KeyError: # set the port to 0 if it is not found in the settings file. This will make the socket library find an open port. port = 0 try: s = socket(AF_INET, SOCK_DGRAM) # attempt to bind to the port s.bind(("localhost", port)) # get the port number from the socket port = s.getsockname()[1] # always close the socket. This allows the gui console to bind to it later (see caveat below) s.close() # save this port back to the environment vars environment.setEnvironmentVar('APP','CONSOLE_SOCKET',port) except: # an error is raised if the gui has bound to this address before the engine tries to. # ignore this error for now, however we should wait until sprint has resolved socket # ports before allowing the gui to bind to it. pass return port