def provide_download(dirname): project = project_no_dedicated_env(dirname) prepare_without_interaction( project, environ=minimal_environ(PROJECT_DIR=dirname)) assert ( "%s: 'downloads:' section should be a dictionary, found ['http://localhost/data.csv']" % DEFAULT_PROJECT_FILENAME) in project.problems
def provide_download_of_zip_no_unzip(zipname, dirname): with codecs.open(os.path.join(dirname, DEFAULT_PROJECT_FILENAME), 'w', 'utf-8') as f: f.write( complete_project_file_content( ZIPPED_DATAFILE_CONTENT_NO_UNZIP)) @gen.coroutine def mock_downloader_run(self, loop): class Res: pass res = Res() res.code = 200 assert self._url.endswith(".zip") # we aren't going to unzip so we should be downloading straignt to # the specified filename 'data' without the .zip on it assert not self._filename.endswith(".zip") shutil.copyfile(zipname, self._filename) raise gen.Return(res) monkeypatch.setattr( "anaconda_project.internal.http_client.FileDownloader.run", mock_downloader_run) project = project_no_dedicated_env(dirname) result = prepare_without_interaction( project, environ=minimal_environ(PROJECT_DIR=dirname)) assert hasattr(result, 'environ') assert 'DATAFILE' in result.environ assert os.path.isfile(os.path.join(dirname, 'data')) with zipfile.ZipFile(os.path.join(dirname, 'data')) as zf: assert zf.namelist() == ['foo']
def prepare_system_environ(dirname): project = project_no_dedicated_env(dirname) os_environ_copy = deepcopy(os.environ) result = prepare_without_interaction(project) assert project.directory_path == strip_environ( result.environ)['PROJECT_DIR'] # os.environ wasn't modified assert os_environ_copy == os.environ # result.environ inherits everything in os.environ for key in os_environ_copy: if key == 'PATH' and platform.system( ) == 'Windows' and result.environ[key] != os.environ[key]: print( "prepare changed PATH on Windows and ideally it would not." ) else: if key == 'PATH' and result.environ[key] != os.environ[key]: original = os.environ[key].split(os.pathsep) updated = result.environ[key].split(os.pathsep) print("ORIGINAL PATH: " + repr(original)) print("UPDATED PATH: " + repr(updated)) assert original == updated assert result.errors == [] assert result assert result.environ.get(key) == os.environ.get(key)
def provide_download(dirname): @gen.coroutine def mock_downloader_run(self, loop): raise Exception('error') monkeypatch.setattr( "anaconda_project.internal.http_client.FileDownloader.run", mock_downloader_run) project = project_no_dedicated_env(dirname) result = prepare_without_interaction( project, environ=minimal_environ(PROJECT_DIR=dirname)) assert not result assert ( 'missing requirement to run this project: A downloaded file which is referenced by DATAFILE.' ) in result.errors project.frontend.reset() status = unprepare(project, result) filename = os.path.join(dirname, 'data.csv') assert project.frontend.logs == [ "No need to remove %s which wasn't downloaded." % filename, ("Current environment is not in %s, no need to delete it." % dirname) ] assert status.status_description == 'Success.'
def provide_download_of_zip(zipname, dirname): with codecs.open(os.path.join(dirname, DEFAULT_PROJECT_FILENAME), 'w', 'utf-8') as f: f.write(complete_project_file_content(ZIPPED_DATAFILE_CONTENT_CHECKSUM)) @gen.coroutine def mock_downloader_run(self, loop): class Res: pass res = Res() res.code = 200 assert self._url.endswith(".zip") assert self._filename.endswith(".zip") shutil.copyfile(zipname, self._filename) self._hash = '12345abcdef' raise gen.Return(res) monkeypatch.setattr("anaconda_project.internal.http_client.FileDownloader.run", mock_downloader_run) project = project_no_dedicated_env(dirname) result = prepare_without_interaction(project, environ=minimal_environ(PROJECT_DIR=dirname)) assert hasattr(result, 'environ') assert 'DATAFILE' in result.environ assert os.path.isdir(os.path.join(dirname, 'data')) assert os.path.isfile(os.path.join(dirname, 'data', 'foo')) assert codecs.open(os.path.join(dirname, 'data', 'foo')).read() == 'hello\n' project.frontend.reset() status = unprepare(project, result) filename = os.path.join(dirname, 'data') assert project.frontend.logs == ["Removed downloaded file %s." % filename, ("Current environment is not in %s, no need to delete it." % dirname)] assert status.status_description == "Success."
def provide_download(dirname): @gen.coroutine def mock_downloader_run(self, loop): class Res: pass res = Res() res.code = 200 with open(os.path.join(dirname, 'data.csv'), 'w') as out: out.write('data') self._hash = '12345abcdef' raise gen.Return(res) monkeypatch.setattr("anaconda_project.internal.http_client.FileDownloader.run", mock_downloader_run) project = project_no_dedicated_env(dirname) result = prepare_without_interaction(project, environ=minimal_environ(PROJECT_DIR=dirname)) assert hasattr(result, 'environ') assert 'DATAFILE' in result.environ filename = os.path.join(dirname, 'data.csv') assert os.path.exists(filename) def mock_remove(path): raise IOError("Not gonna remove this") monkeypatch.setattr("os.remove", mock_remove) project.frontend.reset() status = unprepare(project, result) assert project.frontend.logs == [] assert status.status_description == ('Failed to remove %s: Not gonna remove this.' % filename) assert status.errors == [] assert not status assert os.path.exists(filename) monkeypatch.undo() # so os.remove isn't broken during directory cleanup
def check(dirname): @gen.coroutine def mock_downloader_run(self, loop): class Res: pass res = Res() res.code = 200 with open(os.path.join(dirname, 'data.csv'), 'w') as out: out.write('data') self._hash = '12345abcdef' raise gen.Return(res) monkeypatch.setattr("anaconda_project.internal.http_client.FileDownloader.run", mock_downloader_run) project = project_no_dedicated_env(dirname) environ = minimal_environ(PROJECT_DIR=dirname) stage = prepare_in_stages(project, environ=environ) status = None while status is None and stage is not None: prepare_context = stage.configure() status = _download_status(prepare_context) if status is None: stage = stage.execute() assert status is not None req = status.requirement provider = status.provider # check initial config config = provider.read_config(req, prepare_context.environ, prepare_context.local_state_file, prepare_context.default_env_spec_name, prepare_context.overrides) assert dict(source='download') == config config['source'] = 'environ' config['value'] = 'abc.txt' provider.set_config_values_as_strings(req, prepare_context.environ, prepare_context.local_state_file, prepare_context.default_env_spec_name, prepare_context.overrides, config) config = provider.read_config(req, prepare_context.environ, prepare_context.local_state_file, prepare_context.default_env_spec_name, prepare_context.overrides) assert dict(source='download', value='abc.txt') == config config['source'] = 'variables' config['value'] = 'qrs.txt' provider.set_config_values_as_strings(req, prepare_context.environ, prepare_context.local_state_file, prepare_context.default_env_spec_name, prepare_context.overrides, config) config = provider.read_config(req, prepare_context.environ, prepare_context.local_state_file, prepare_context.default_env_spec_name, prepare_context.overrides) assert dict(source='variables', value='qrs.txt') == config
def check(dirname): project = project_no_dedicated_env(dirname) environ = minimal_environ() result = prepare_without_interaction(project, environ=environ, command_name='default') assert result.errors == [] assert result assert os.path.join(project.directory_path, 'foo.py') in result.command_exec_info.args
def provide_download(dirname): @gen.coroutine def mock_downloader_run(self, loop): raise Exception("should not have tried to download in check mode") monkeypatch.setattr("anaconda_project.internal.http_client.FileDownloader.run", mock_downloader_run) project = project_no_dedicated_env(dirname) result = prepare_without_interaction(project, environ=minimal_environ(PROJECT_DIR=dirname), mode=provide.PROVIDE_MODE_CHECK) assert not result