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 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 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(self): from stubo.model.importer import UrlFetch return UrlFetch()