def test_server_certs_mount(self): """Test server enables cert authentication through file mounts.""" with local.tempdir() as tdir: with local.cwd(tdir): self._generate_certs() cert_vols = [ "-v{0}/{1}:/etc/postgres/{1}".format(local.cwd, cert) for cert in [ "client.ca.cert.pem", "server.cert.pem", "server.key.pem", ] ] self.postgres_container = docker( "container", "run", "-d", "--network", "lan", "-e", "POSTGRES_DB=test_db", "-e", "POSTGRES_PASSWORD=test_password", "-e", "POSTGRES_USER=test_user", *cert_vols, self.image, ).strip() self._check_local_connection() self._check_password_auth() self._connect_wan_network() self._check_cert_auth()
def make(hash=DEFAULT_HASH): if hash != 'master': if checkExists(get_path(hash)): return with local.tempdir() as tmpdir, local.cwd(tmpdir): repo = downloadGithubRepo('reckbo/nrrdchecker', hash) sha, date = getCommitInfo(repo) with local.cwd(repo): if on_partners_cluster(): import os os.system('module load stack') from plumbum.cmd import stack stack['setup'] & FG stack['build'] & FG binary = stack('exec', 'which', 'nrrdchecker')[:-1] get_path(sha).dirname.mkdir() local.path(binary).move(get_path(sha)) symlink = get_path(date).dirname print("Make symlink: {} -> {}".format(symlink, get_path(sha).dirname)) symlink.unlink() get_path(sha).dirname.symlink(symlink) logger.info("Made '{}'".format(get_path(sha))) logger.info("Made '{}'".format(symlink))
def gtmsegs_are_equal(gtmseg1, gtmseg2): with local.tempdir() as tmpdir: diff_mgz = tmpdir / 'diff.mgz' runv('fscalc', gtmseg1, 'sub', gtmseg2, '--o', diff_mgz) _, _, stderr = runv('mris_calc', diff_mgz, 'max') diff = stderr.split()[2] return diff == '0.000000'
def test_server_certs_var(self): """Test server enables cert authentication through env vars.""" with local.tempdir() as tdir: with local.cwd(tdir): self._generate_certs() certs_var = {name: cat(name) for name in self.cert_files} self.postgres_container = docker( "container", "run", "-d", "--network", "lan", "-e", "CERTS=" + json.dumps(certs_var), "-e", "POSTGRES_DB=test_db", "-e", "POSTGRES_PASSWORD=test_password", "-e", "POSTGRES_USER=test_user", self.image, ).strip() self._check_local_connection() self._check_password_auth() self._connect_wan_network() self._check_cert_auth()
def make(commit=DEFAULT_HASH): """Downloads a lean version of tract_querier. Output is '$soft/tract_querier-<commit>'.""" if commit != 'master': out = get_path(commit) if checkExists(out): return with local.tempdir() as tmpdir, local.cwd(tmpdir): repo = downloadGithubRepo(REPO, commit) sha, date = getCommitInfo(repo) out = get_path(sha) if checkExists(out): return # save space (repo / 'doc').delete() (repo / '.git').delete() log.info("Make '{out}'".format(**locals())) repo.move(out) with open(out / 'env.sh', 'w') as f: f.write("export PATH={}:$PATH\n".format(out / 'scripts')) f.write("export PYTHONPATH={}:$PYTHONPATH\n".format(out)) date_symlink = get_path(date) date_symlink.unlink() out.symlink(date_symlink)
def install_sd(sd_version="0.7.6"): with local.tempdir() as d, local.cwd(d): cmd.wget( f"https://github.com/chmln/sd/releases/download/v{sd_version}/sd-v{sd_version}-x86_64-unknown-linux-musl", "-O", "sd") cmd.chmod("+x", "sd") cmd.sudo[cmd.mv["sd", "/usr/local/bin"]]()
def test_server_certs_var(self): """Prueba que el servidor habilita autenticación de cert a través de env vars.""" with local.tempdir() as tdir: with local.cwd(tdir): self._generate_certs() certs_var = {name: cat(name) for name in self.cert_files} self.postgres_container = docker( "container", "run", "-d", "--network", "lan", "-e", "CERTS=" + json.dumps(certs_var), "-e", "POSTGRES_DB=test_db", "-e", "POSTGRES_PASSWORD=test_password", "-e", "POSTGRES_USER=test_user", CONF_EXTRA, self.image, ).strip() self._check_local_connection() self._check_password_auth() self._connect_wan_network() self._check_cert_auth()
def test_server_certs_mount(self): """El test server habilita la autenticación cert a través de montajes de archivos.""" with local.tempdir() as tdir: with local.cwd(tdir): self._generate_certs() cert_vols = [ "-v{0}/{1}:/etc/postgres/{1}".format(local.cwd, cert) for cert in [ "client.ca.cert.pem", "server.cert.pem", "server.key.pem", ] ] self.postgres_container = docker( "container", "run", "-d", "--network", "lan", "-e", "POSTGRES_DB=test_db", "-e", "POSTGRES_PASSWORD=test_password", "-e", "POSTGRES_USER=test_user", CONF_EXTRA, *cert_vols, self.image, ).strip() self._check_local_connection() self._check_password_auth() self._connect_wan_network() self._check_cert_auth()
def static_build(self): fssubjdir = self.fs with local.tempdir() as tmpdir, BRAINSTools.env( self.BRAINSTools_hash): tmpoutdir = tmpdir / 'fsindwi' dwi = tmpdir / 'dwi.nrrd' dwimask = tmpdir / 'dwimask.nrrd' fs = tmpdir / 'fs' t2 = tmpdir / 't2.nrrd' t1 = tmpdir / 't1.nrrd' t1mask = tmpdir / 't1mask.nrrd' t2mask = tmpdir / 't2mask.nrrd' fssubjdir.copy(fs) dwiconvert_py('-i', self.dwi, '-o', dwi) convertImage(self.dwimask, dwimask, self.BRAINSTools_hash) convertImage(self.t2, t2, self.BRAINSTools_hash) convertImage(self.t1, t1, self.BRAINSTools_hash) convertImage(self.t2mask, t2mask, self.BRAINSTools_hash) convertImage(self.t1mask, t1mask, self.BRAINSTools_hash) script = local['pnlscripts/old/fs2dwi_T2.sh'] script['--fsdir', fs, '--dwi', dwi, '--dwimask', dwimask, '--t2', t2, '--t2mask', t2mask, '--t1', t1, '--t1mask', t1mask, '-o', tmpoutdir] & FG convertImage(tmpoutdir / 'wmparc-in-bse.nrrd', self.output(), self.BRAINSTools_hash)
def mk_theme_previews(self, message: Message): if not self.admin_chat_id or str( message.chat.id) != self.admin_chat_id: self.log.msg("naughty preview attempt", user_id=message.from_user.id, user_first_name=message.from_user.first_name, chat_id=message.chat.id, admin_chat_id=self.admin_chat_id) return sample_code = dedent(""" # palinDay :: Int -> [ISO Date] def palinDay(y): '''A possibly empty list containing the palindromic date for the given year, if such a date exists. ''' s = str(y) r = s[::-1] iso = '-'.join([s, r[0:2], r[2:]]) try: datetime.strptime(iso, '%Y-%m-%d') return [iso] except ValueError: return [] """) for button in chain.from_iterable(self.kb['theme'].keyboard): theme = button.text html = mk_html(f"# {theme}{sample_code}", 'py', theme) with local.tempdir() as folder: png_path = mk_png(html, folder=folder) send_image(bot=self.bot, chat_id=message.chat.id, png_path=png_path, reply_msg_id=message.message_id)
def test_read_write(self): with local.tempdir() as dir: f = dir / "test.txt" text = six.b('hello world\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d').decode("utf8") f.write(text, "utf8") text2 = f.read("utf8") assert text == text2
def test_mkdir_mode(self): # (identical to test_remote.TestRemotePath.test_mkdir_mode) with local.tempdir() as tmp: # just verify that mode argument works the same way it does for # Python's own os.mkdir, which takes into account the umask # (different from shell mkdir mode argument!); umask on my # system is 022 by default, so 033 is ok for testing this try: (tmp / "pb_333").mkdir(exist_ok=False, parents=False, mode=0o333) local.python( "-c", "import os; os.mkdir({}, 0o333)".format( repr(str(tmp / "py_333"))), ) pb_final_mode = oct((tmp / "pb_333").stat().st_mode) py_final_mode = oct((tmp / "py_333").stat().st_mode) assert pb_final_mode == py_final_mode finally: # we have to revert this so the tempdir deletion works if (tmp / "pb_333").exists(): (tmp / "pb_333").chmod(0o777) if (tmp / "py_333").exists(): (tmp / "py_333").chmod(0o777) assert not tmp.exists()
def test_unlink(self): with local.tempdir() as tmp: one = tmp / 'one' one.touch() assert one.exists() one.unlink() assert not one.exists()
def main(self): represent_dict_order = lambda self, data: self.represent_mapping('tag:yaml.org,2002:map', data.items()) yaml.add_representer(OrderedDict, represent_dict_order) make_extra() with open_db() as (url, machine, dbpath), local.tempdir() as tmpdir: # expand caselist as it's usually a text file yml = read_project_yml() pipelines = yml['pipelines'] for pipeline in yml['pipelines']: pipeline['paths']['caselist'] = readCaselist(pipeline['paths'][ 'caselist']) yml['pipelines'] = pipelines tmpyml = tmpdir / 'pnldash.yml' with open(tmpyml, 'w') as f: yaml.dump(yml, f, default_flow_style=False) if not (tmpyml).exists(): raise Exception('Error: could not write out temporary yaml file: {}'.format(tmpyml)) destdir = dbpath / (local.cwd.replace('/', '---')[3:]) log.info('Copy files to central database...') copy(PNLDASH_FILES, destdir) copy(tmpyml, destdir) print("Copied") print('\n'.join(PNLDASH_FILES)) print(tmpyml) print('to {}'.format(url))
def test_read_write(self): with local.tempdir() as dir: f = dir / "test.txt" text = six.b('hello world\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d').decode("utf8") f.write(text, "utf8") text2 = f.read("utf8") self.assertEqual(text, text2)
def main(): user_home = local.path('~') print( f"Hello! We're going to clone {user_home.name}'s .zshrc files for the root user, and maybe make some changes along the way." ) abortable_ask(OD([('Enter', "begin")]), ['']) for zshrc_file in [f.name for f in user_home // '.*zshrc']: user_zshrc = (user_home / zshrc_file).read('utf8') root_zshrc = '' for line in user_zshrc.split('\n'): root_line = line # print(blue | root_line) root_line = make_suggestion(root_line, 'sudo ', {'sudo ': ''}) root_line = make_suggestion(root_line, '~', {'~': user_home}) root_zshrc += root_line + '\n' with local.tempdir() as tmp: file = tmp / zshrc_file file.write(root_zshrc, 'utf8') # file.write(root_zshrc.encode()) print(magenta | f'{zshrc_file} for root written to', file) print(green | 'Opening for your review and edits . . .') xdg_open(file) abortable_ask(OD([('Enter', f'overwrite /root/{zshrc_file}')]), ['']) sudo[cp[file, f'/root/{zshrc_file}']]()
def fuseAvg(labels, out): from plumbum.cmd import AverageImages with local.tempdir() as tmpdir: nii = local.path(tmpdir) / 'avg.nii.gz' AverageImages('3', nii, '0', *labels) ConvertBetweenFileFormats(nii, out) (unu['2op', 'gt', out, '0.5'] | \ unu['save', '-e', 'gzip', '-f', 'nrrd', '-o', out]) & FG
def test_convert_ecat_to_nifti_script(): script = local['ecat_to_nifti.py'] with local.tempdir() as tmpdir: test_input = NEUROLIB_TEST_DATA / 'fdg.v' expected_output = NEUROLIB_TEST_DATA / 'fdg.nii.gz' test_output = tmpdir / 'ecat.nii.gz' script('-i', test_input, '-o', test_output) filecmp.cmp(test_output, expected_output)
def test_chown(self): with local.tempdir() as dir: p = dir / "foo.txt" p.write(six.b("hello")) self.assertEqual(p.uid, os.getuid()) self.assertEqual(p.gid, os.getgid()) p.chown(p.uid.name) self.assertEqual(p.uid, os.getuid())
def test_chown(self): with local.tempdir() as dir: p = dir / "foo.txt" p.write(b"hello") assert p.uid == os.getuid() assert p.gid == os.getgid() p.chown(p.uid.name) assert p.uid == os.getuid()
def test_fscalc_mean(): expected_output = TEST_DIR / 'mean.nii.gz' with local.tempdir() as tmpdir: test_output = tmpdir / 'test_mean.nii.gz' retcode, _, _ = freesurfer.fscalc_mean(TEST_DIR // '*.nii.gz', test_output) assert retcode == 0 filecmp.cmp(expected_output, test_output)
def test_touch(self): with local.tempdir() as tmp: one = tmp / 'one' assert not one.is_file() one.touch() assert one.is_file() one.delete() assert not one.is_file()
def test_ecat2nifti(): ecat = NEUROLIB_TEST_DATA / 'fdg.v' expected_ecat = NEUROLIB_TEST_DATA / 'fdg.nii.gz' with local.tempdir() as tmpdir: SPM.ecat2nifti(ecat, tmpdir) niis = tmpdir // '*.nii' assert len(niis) == 1 filecmp.cmp(niis[0], expected_ecat)
def computeWarp(image, target, out): with local.tempdir() as tmpdir: tmpdir = local.path(tmpdir) pre = tmpdir / 'ants' warp = pre + '1Warp.nii.gz' affine = pre + '0GenericAffine.mat' antsRegistrationSyN_sh['-m', image, '-f', target, '-o', pre, '-n', 32] & FG ComposeMultiTransform('3', out, '-R', target, warp, affine)
def test_pipeline_creates_missing(): with local.tempdir() as tmpdir: pipeline = make_pipeline(tmpdir) source_txt = tmpdir / 'source.txt' hello_txt = tmpdir / 'out/hello.txt' pipeline.run() source_txt.delete() pipeline.run() assert source_txt.read() == 'world' assert hello_txt.read() == 'hello world'
def test_pipeline_first_run(): with local.tempdir() as tmpdir: pipeline = make_pipeline(tmpdir) source_txt = tmpdir / 'source.txt' hello_txt = tmpdir / 'out/hello.txt' assert not source_txt.exists() assert not hello_txt.exists() pipeline.run() assert source_txt.read() == 'world' assert hello_txt.read() == 'hello world'
def test_tempdir(self): from plumbum.cmd import cat with local.tempdir() as dir: self.assertTrue(dir.isdir()) with open(str(dir / "test.txt"), "w") as f: f.write("hello world") with open(str(dir / "test.txt"), "r") as f: self.assertEqual(f.read(), "hello world") self.assertFalse(dir.exists())
def test_direct_open_tmpdir(self): with local.tempdir() as dir: assert dir.is_dir() data = b"hello world" with open(dir / "test.txt", "wb") as f: f.write(data) with open(dir / "test.txt", "rb") as f: assert f.read() == data assert not dir.exists()
def test_chown(self): if not hasattr(os, "chown"): self.skip("os.chown not supported") with local.tempdir() as dir: p = dir / "foo.txt" p.write("hello") self.assertEqual(p.uid, os.getuid()) self.assertEqual(p.gid, os.getgid()) p.chown(p.uid.name) self.assertEqual(p.uid, os.getuid())
def test_copy_nonexistant_dir(self): with local.tempdir() as tmp: one = tmp / 'one' one.write(b'lala') two = tmp / 'two' / 'one' three = tmp / 'three' / 'two' / 'one' one.copy(two) assert one.read() == two.read() one.copy(three) assert one.read() == three.read()
def test_links(self): with local.tempdir() as tmp: src = tmp / "foo.txt" dst1 = tmp / "bar.txt" dst2 = tmp / "spam.txt" data = "hello world" src.write(data) src.link(dst1) assert data == dst1.read() src.symlink(dst2) assert data == dst2.read()
def test_links(self): with local.tempdir() as tmp: src = tmp / "foo.txt" dst1 = tmp / "bar.txt" dst2 = tmp / "spam.txt" data = six.b("hello world") src.write(data) src.link(dst1) self.assertEqual(data, dst1.read()) src.symlink(dst2) self.assertEqual(data, dst2.read())
def test_direct_open_tmpdir(self): from plumbum.cmd import cat with local.tempdir() as dir: assert dir.is_dir() data = six.b("hello world") with open(dir / "test.txt", "wb") as f: f.write(data) with open(dir / "test.txt", "rb") as f: assert f.read() == data assert not dir.exists()
def test_copy_override(self): """Edit this when override behavior is added""" with local.tempdir() as tmp: one = tmp / 'one' one.touch() two = tmp / 'two' assert one.is_file() assert not two.is_file() one.copy(two) assert one.is_file() assert two.is_file()
def test_direct_open_tmpdir(self): from plumbum.cmd import cat with local.tempdir() as dir: self.assertTrue(dir.is_dir()) data = six.b("hello world") with open(dir / "test.txt", "wb") as f: f.write(data) with open(dir / "test.txt", "rb") as f: self.assertEqual(f.read(), data) self.assertFalse(dir.exists())
def build_and_cmp(name, target): source = local.cwd + test + name with local.tempdir() as tmp: with local.cwd(tmp): # local['cp'][target][name]() cp[source]['spec.yaml']() assert ak['build']['--fileonly'] & TF local['cat']['repo.yaml'] & FG ytarget = yaml.load(open(target).read()) ysource = yaml.load(open('repo.yaml').read()) assert ytarget == ysource
def test_shadowed_by_dir(self): real_ls = local['ls'] with local.tempdir() as tdir: with local.cwd(tdir): ls_dir = tdir / 'ls' ls_dir.mkdir() fake_ls = local['ls'] assert fake_ls.executable == real_ls.executable local.env.path.insert(0, tdir) fake_ls = local['ls'] del local.env.path[0] assert fake_ls.executable == real_ls.executable
def build_and_cmp(name, target): source = local.cwd + test + name with local.tempdir() as tmp: with local.cwd(tmp): cp[source]['spec.yaml']() produced = ak['build']['--links']() start = len("('Addons path for your config file: , ''") end = -len("')\n") paths = produced[start:end].split(',') paths.sort() open('output.txt', 'w').write(','.join(paths)) assert cat['output.txt']() == cat[target]()
def test_mkdir(self): # (identical to test_remote.TestRemotePath.test_mkdir) with local.tempdir() as tmp: (tmp / "a").mkdir(exist_ok=False, parents=False) assert (tmp / "a").exists() assert (tmp / "a").is_dir() (tmp / "a").mkdir(exist_ok=True, parents=False) (tmp / "a").mkdir(exist_ok=True, parents=True) with pytest.raises(OSError): (tmp / "a").mkdir(exist_ok=False, parents=False) with pytest.raises(OSError): (tmp / "a").mkdir(exist_ok=False, parents=True) (tmp / "b" / "bb").mkdir(exist_ok=False, parents=True) assert (tmp / "b" / "bb").exists() assert (tmp / "b" / "bb").is_dir() assert not tmp.exists()
def test_copy_move_delete(self): from plumbum.cmd import touch with local.tempdir() as dir: (dir / "orog").mkdir() (dir / "orog" / "rec").mkdir() for i in range(20): touch(dir / "orog" / ("f%d.txt" % (i,))) for i in range(20,40): touch(dir / "orog" / "rec" / ("f%d.txt" % (i,))) move(dir / "orog", dir / "orig") s1 = sorted(f.basename for f in (dir / "orig").walk()) copy(dir / "orig", dir / "dup") s2 = sorted(f.basename for f in (dir / "dup").walk()) self.assertEqual(s1, s2) with SshMachine("localhost") as rem: with rem.tempdir() as dir2: copy(dir / "orig", dir2) s3 = sorted(f.basename for f in (dir2 / "orig").walk()) self.assertEqual(s1, s3) copy(dir2 / "orig", dir2 / "dup") s4 = sorted(f.basename for f in (dir2 / "dup").walk()) self.assertEqual(s1, s4) copy(dir2 / "dup", dir / "dup2") s5 = sorted(f.basename for f in (dir / "dup2").walk()) self.assertEqual(s1, s5) with SshMachine("localhost") as rem2: with rem2.tempdir() as dir3: copy(dir2 / "dup", dir3) s6 = sorted(f.basename for f in (dir3 / "dup").walk()) self.assertEqual(s1, s6) move(dir3 / "dup", dir / "superdup") self.assertFalse((dir3 / "dup").exists()) s7 = sorted(f.basename for f in (dir / "superdup").walk()) self.assertEqual(s1, s7) # test rm delete(dir)
def test_copy_move_delete(self): from plumbum.cmd import touch with local.tempdir() as dir: (dir / "orog").mkdir() (dir / "orog" / "rec").mkdir() for i in range(20): touch(dir / "orog" / ("f%d.txt" % (i,))) for i in range(20,40): touch(dir / "orog" / "rec" / ("f%d.txt" % (i,))) move(dir / "orog", dir / "orig") s1 = sorted(f.name for f in (dir / "orig").walk()) copy(dir / "orig", dir / "dup") s2 = sorted(f.name for f in (dir / "dup").walk()) assert s1 == s2 with SshMachine("localhost") as rem: with rem.tempdir() as dir2: copy(dir / "orig", dir2) s3 = sorted(f.name for f in (dir2 / "orig").walk()) assert s1 == s3 copy(dir2 / "orig", dir2 / "dup") s4 = sorted(f.name for f in (dir2 / "dup").walk()) assert s1 == s4 copy(dir2 / "dup", dir / "dup2") s5 = sorted(f.name for f in (dir / "dup2").walk()) assert s1 == s5 with SshMachine("localhost") as rem2: with rem2.tempdir() as dir3: copy(dir2 / "dup", dir3) s6 = sorted(f.name for f in (dir3 / "dup").walk()) assert s1 == s6 move(dir3 / "dup", dir / "superdup") assert not (dir3 / "dup").exists() s7 = sorted(f.name for f in (dir / "superdup").walk()) assert s1 == s7 # test rm delete(dir)
def test_mkdir_mode(self): # (identical to test_remote.TestRemotePath.test_mkdir_mode) with local.tempdir() as tmp: # just verify that mode argument works the same way it does for # Python's own os.mkdir, which takes into account the umask # (different from shell mkdir mode argument!); umask on my # system is 022 by default, so 033 is ok for testing this try: (tmp / "pb_333").mkdir(exist_ok=False, parents=False, mode=0o333) local.python('-c', 'import os; os.mkdir({0}, 0o333)'.format( repr(str(tmp / "py_333")))) pb_final_mode = oct((tmp / "pb_333").stat().st_mode) py_final_mode = oct((tmp / "py_333").stat().st_mode) assert pb_final_mode == py_final_mode finally: # we have to revert this so the tempdir deletion works if (tmp / "pb_333").exists(): (tmp / "pb_333").chmod(0o777) if (tmp / "py_333").exists(): (tmp / "py_333").chmod(0o777) assert not tmp.exists()
def test_read_write_bin(self): with local.tempdir() as tmp: data = six.b("hello world") (tmp / "foo.txt").write(data) assert (tmp / "foo.txt").read(mode='rb') == data
def test_read_write(self): with local.tempdir() as tmp: data = six.b("hello world") (tmp / "foo.txt").write(data) self.assertEqual((tmp / "foo.txt").read(), data)
def test_read_write_str(self): with local.tempdir() as tmp: data = "hello world" (tmp / "foo.txt").write(data) assert (tmp / "foo.txt").read() == data
def test_read_write_unicode(self): with local.tempdir() as tmp: data = six.u("hello world") (tmp / "foo.txt").write(data) assert (tmp / "foo.txt").read() == data