def put_module(handler, names): module = Module(handler.track.host) added = [] result = dict(version=version) for name in names: uri, module_name = UriLocation(handler.request)(name) log.info('uri={0}, module_name={1}'.format(uri, module_name)) response, _, code = UrlFetch().get(uri) module_name = module_name[:-3] last_version = module.latest_version(module_name) module_version_name = module.sys_module_name(module_name, last_version + 1) if last_version and response == module.get_source( module_name, last_version): msg = 'Module source has not changed for {0}'.format( module_version_name) result['data'] = dict(message=msg) try: code, mod = module.add_sys_module(module_version_name, response) log.debug('{0}, {1}'.format(mod, code)) except Exception, e: msg = 'error={0}'.format(e) raise exception_response( 400, title='Unable to compile {0}:{1}, {2}'.format( module.host(), module_version_name, msg)) module.add(module_name, response) added.append(module_version_name)
def run_command_file(cmd_file_url, request, static_path): def run(cmd_file_path): response = {'version': version} is_legacy_text_format = cmd_file_path.endswith('.commands') location = UriLocation(request) cmd_processor = TextCommandsImporter( location, cmd_file_path) if is_legacy_text_format else YAMLImporter( location, cmd_file_path) responses = cmd_processor.run() log.debug('responses: {0}'.format(responses)) response['data'] = responses return response file_type = os.path.basename( urlparse(cmd_file_url).path).rpartition('.')[-1] supported_types = ('zip', 'gz', 'tar', 'jar') if file_type in supported_types: # import compressed contents and run contained config file import_dir = os.path.join(static_path, 'imports') with make_temp_dir(dirname=import_dir) as temp_dir: temp_dir_name = os.path.basename(temp_dir) response, headers, status_code = UrlFetch().get( UriLocation(request)(cmd_file_url)[0]) content_type = headers["Content-Type"] log.debug('received {0} file.'.format(content_type)) config_types = ('.yaml', '.commands') def run_config(files): # find the config file in the extract and run it config_file = [x for x in files if x.endswith(config_types)] if not config_file: raise exception_response( 400, title='Config file not' ' found in archive: {0}'.format(cmd_file_url)) return run( os.path.join('static', 'imports', temp_dir_name, config_file[0])) if content_type == 'application/x-tar' or file_type == 'tar': with closing(tarfile.open(fileobj=StringIO(response))) as tar: tar.extractall(path=temp_dir) response = run_config(tar.getnames()) elif content_type in ('application/zip', 'application/java-archive') or file_type in \ ('zip', 'jar'): with zipfile.ZipFile(StringIO(response)) as zipf: zipf.extractall(path=temp_dir) response = run_config(zipf.namelist()) else: raise exception_response( 400, title='Expected Content-Type has' ' to be one of these: {0} not {1}'.format( supported_types, content_type)) else: response = run(cmd_file_url) return response
def run_commands(handler, cmds_text): response = {'version': version} host = get_hostname(handler.request) cmd_processor = TextCommandsImporter(UriLocation(handler.request)) cmds = cmd_processor.parse(cmds_text) if any(x for x in cmds if urlparse(x).path not in form_input_cmds): raise exception_response(400, title='command/s not supported, must be ' 'one of these: {0}'.format(form_input_cmds)) responses = cmd_processor.run_cmds(cmds) log.debug('responses: {0}'.format(responses)) response['data'] = { 'executed_commands': responses, 'number_of_requests': len(responses['commands']), 'number_of_errors': len([x for x in responses['commands'] if x[1] > 399]) } def get_links(cmd): cmd_uri = urlparse(cmd) scenario_name = cmd_uri.query.partition('=')[-1] scenario_name_key = '{0}:{1}'.format(host, scenario_name) files = [(scenario_name + '.zip', ), (scenario_name + '.tar.gz', ), (scenario_name + '.jar', )] links = get_export_links(handler, scenario_name_key, files) return links export_links = [(x, get_links(x)) for x in cmds if 'get/export' in x] if export_links: response['data']['export_links'] = export_links return response
def run(cmd_file_path): response = {'version': version} is_legacy_text_format = cmd_file_path.endswith('.commands') location = UriLocation(request) cmd_processor = TextCommandsImporter( location, cmd_file_path) if is_legacy_text_format else YAMLImporter( location, cmd_file_path) responses = cmd_processor.run() log.debug('responses: {0}'.format(responses)) response['data'] = responses return response
def import_bookmarks(handler, location): request = handler.request cache = Cache(get_hostname(request)) response = dict(version=version, data={}) uri, bookmarks_name = UriLocation(request)(location) log.info('uri={0}, bookmarks_name={1}'.format(uri, bookmarks_name)) payload, _, status_code = UrlFetch().get(uri) payload = json.loads(payload) # e.g payload #{"converse": {"first": {"8981c0dda19403f5cc054aea758689e65db2": "2"}}} imported = {} for scenario, bookmarks in payload.iteritems(): for bookmark_name, bookmark in bookmarks.iteritems(): is_new = cache.set_saved_request_index_data( scenario, bookmark_name, bookmark) scenario_book = '{0}:{1}'.format(scenario, bookmark_name) imported[scenario_book] = ('new' if is_new else 'updated', bookmark) response['data']['imported'] = imported return response
def make_one(self, handler, cmd_file_url=None): from stubo.model.cmds import TextCommandsImporter from stubo.model.importer import UriLocation return TextCommandsImporter(UriLocation(handler.request), cmd_file_url)
def make_one(self, handler, cmd_file_url=None): from stubo.model.importer import YAMLImporter, UriLocation return YAMLImporter(UriLocation(handler.request), cmd_file_url)
def _make(self, protocol='http', host='localhost:8001'): from stubo.model.importer import UriLocation return UriLocation(DummyModel(protocol=protocol, host=host))