def test_createHTML(): """ confirm that createHTML creates the right folders and files """ delete('arbitrary') delete('arbitrary.py') global B with open('arbitrary.py', 'w') as f: f.write('box(color=color.purple)') B = BrowserFromService(headless=False) importsJS = make_importsJS() ret = createHTML('arbitrary.py', B, importsJS=importsJS) print(ret.__dict__) assert 'arbitrary.py' in ls().split() assert ret.folderName == 'arbitrary' assert 'arbitrary' in ls().split() assert 'index.html' in ls('arbitrary').split() assert 'imports.js' in ls('arbitrary').split() assert 'supportScripts' in ls('arbitrary').split() assert 'box(color=color.purple)' in open('arbitrary.py').read() # THIS CURRENTLY DOES NOT ENSURE FUNCTIONALITY OF arbitrary.py BECAUSE # we haven't put it into the workspace, # so the javscript in index.html is does not contain our program B.close() delete('arbitrary') delete('arbitrary.py')
def test_cwd(self): from plumbum.cmd import ls self.assertEqual(local.cwd, os.getcwd()) self.assertTrue("__init__.py" not in ls().splitlines()) with local.cwd("../plumbum"): self.assertTrue("__init__.py" in ls().splitlines()) self.assertTrue("__init__.py" not in ls().splitlines()) self.assertRaises(OSError, local.cwd.chdir, "../non_exist1N9")
def ufwPlainOutput(): plainFile = "blockedIPs/BlockedIPs_%s.txt" %(datetime.now().strftime("%I:%M_%p_%B-%d-%Y")) print ls() print "cwd is ",local.cwd print "filename is ", plainFile print "running command "+str(ufwOutput) #addrs = ufwOutput() (ufwOutput > plainFile)()
def test_cwd(self): from plumbum.cmd import ls assert local.cwd == os.getcwd() assert "__init__.py" not in ls().splitlines() with local.cwd("../plumbum"): assert "__init__.py" in ls().splitlines() assert "__init__.py" not in ls().splitlines() with pytest.raises(OSError): local.cwd.chdir("../non_exist1N9")
def test_quoting(self): ssh = local["ssh"] pwd = local["pwd"] cmd = ssh["localhost", "cd", "/usr", "&&", ssh["localhost", "cd", "/", "&&", ssh["localhost", "cd", "/bin", "&&", pwd]]] assert "\"'&&'\"" in " ".join(cmd.formulate(0)) ls = local['ls'] with pytest.raises(ProcessExecutionError) as execinfo: ls('-a', '') # check that empty strings are rendered correctly assert execinfo.value.argv[-2:] == ['-a', '']
def test_create(builddir, runner): # prepare needed files with local.cwd(builddir): (sh.echo["a.c"] > "a.c")() (sh.echo["b.c"] > "b.c")() builder = BuildFile(build_dir=builddir, runner=runner) builder.main(command_line=['-c', '-D', 'build']) expected_json = { 'tar czvf foo.tar.gz a.c b.c': { 'b.c': 'input-', 'foo.tar.gz': 'output-', 'a.c': 'input-' }, '.deps_version': 2 } # assertions with local.cwd(builddir): assert_same_json('.deps', expected_json) assert os.path.isfile('foo.tar.gz') assert sh.tar("tf", 'foo.tar.gz') == "a.c\nb.c\n" print(sh.ls("-al")) assert '"a.c": "input-' in sh.cat(".deps") sys.exit.assert_called_once_with(0) # Modify a.c to force rebuilding (sh.echo["newline"] > "a.c")() builder.main(command_line=['-D', 'build']) with local.cwd(builddir): sh.tar("df", "foo.tar.gz") # ensure tar diff return no difference
def test_quoting(self): ssh = local["ssh"] pwd = local["pwd"] cmd = ssh["localhost", "cd", "/usr", "&&", ssh["localhost", "cd", "/", "&&", ssh["localhost", "cd", "/bin", "&&", pwd]]] self.assertTrue("\"'&&'\"" in " ".join(cmd.formulate(0))) ls = local['ls'] try: ls('-a', '') # check that empty strings are rendered correctly except ProcessExecutionError: ex = sys.exc_info()[1] self.assertEqual(ex.argv[-2:], ['-a', '']) else: self.fail("Expected `ls` to fail")
def test_cwd(self): from plumbum.cmd import ls assert local.cwd == os.getcwd() assert "machines" not in ls().splitlines() with local.cwd("../plumbum"): assert "machines" in ls().splitlines() assert "machines" not in ls().splitlines() assert "machines" in ls.with_cwd("../plumbum")().splitlines() path = local.cmd.pwd.with_cwd("../plumbum")().strip() with local.cwd("/"): assert "machines" not in ls().splitlines() assert "machines" in ls.with_cwd(path)().splitlines() with pytest.raises(OSError): local.cwd.chdir("../non_exist1N9")
def test_putInHTMLdir(): open('box2.py','w').write('box(color=color.green)') putInHTMLdir('box2.py') assert( 'box2.py' in ls('box2').split() ) delete('box2.py') delete('box2') #prepareHTMLdir(dirName='xyz') #test_makeHTMLdir()
def test_imports(self): from plumbum.cmd import ls assert "test_local.py" in local["ls"]().splitlines() assert "test_local.py" in ls().splitlines() with pytest.raises(CommandNotFound): local["non_exist1N9"]() with pytest.raises(ImportError): from plumbum.cmd import non_exist1N9 #@UnresolvedImport @UnusedImport
def test_imports(self): from plumbum.cmd import ls self.assertTrue("test_local.py" in local["ls"]().splitlines()) self.assertTrue("test_local.py" in ls().splitlines()) self.assertRaises(CommandNotFound, lambda: local["non_exist1N9"]) try: from plumbum.cmd import non_exist1N9 #@UnresolvedImport @UnusedImport except (ImportError, CommandNotFound): pass else: self.fail("from plumbum.cmd import non_exist1N9")
def test_makeHTMLdir(): HTMLdirName = '123' delete( HTMLdirName ) fakeSSname = 'fakeSupportScripts' delete(fakeSSname) mkdir(fakeSSname) scriptNames=['xyz.test', 'xyz2.test'] for scriptName in scriptNames: touch(f'{fakeSSname}/{scriptName}') makeHTMLdir( HTMLdirName , stacheDir = fakeSSname, GLOWPATH='.', scriptNames= scriptNames) assert('supportScripts' in ls( HTMLdirName ).split() ) assert( ls('123/supportScripts').split() == scriptNames ) delete( HTMLdirName ) delete(fakeSSname)
def test_create(builddir, runner, end_fabricate): # prepare needed files with local.cwd(builddir): (sh.echo["a.c"] > "a.c")() (sh.echo["b.c"] > "b.c")() # build.py content >>>>>>>>>>>>>>>>>>>>> def fabricate_file(): def build(): run('tar', 'czvf', 'foo.tar.gz', 'a.c', 'b.c') def clean(): autoclean() return copy(locals()) main(globals_dict=fabricate_file(), build_dir=builddir, runner=runner, command_line=['-c', '-D', 'build']) end_fabricate() expected_json = {'tar czvf foo.tar.gz a.c b.c': {'b.c': 'input-', 'foo.tar.gz': 'output-', 'a.c': 'input-'}, u'.deps_version': 2} # assertions with local.cwd(builddir): assert_same_json('.deps', expected_json) assert os.path.isfile('foo.tar.gz') assert sh.tar("tf", 'foo.tar.gz') == "a.c\nb.c\n" print(sh.ls("-al")) assert '"a.c": "input-' in sh.cat(".deps") sys.exit.assert_called_once_with(0) # Modify a.c to force rebuilding (sh.echo["newline"] > "a.c")() main(globals_dict=fabricate_file(), build_dir=builddir, runner=runner, command_line=['-D', 'build']) end_fabricate() with local.cwd(builddir): sh.tar("df", "foo.tar.gz") # ensure tar diff return no difference
def main(self, srcDir, dstBucket, dstDir): # protect to prevent deleting of all backups if self.keepCount < 2: self.keepCount = 2 s3DirPath = "s3://" + dstBucket + "/" + dstDir if self.verbose: print("Sending backups from", srcDir, "to", s3DirPath, flush = True) # check if bucket exists and create if not lines = (s3["ls"] | grep[dstBucket])().splitlines() if not lines: if self.verbose: print("Bucket doesn't exist. Creating...") (s3["mb", "s3://" + dstBucket] > stdout)() # create dir for processed backup files (if not exists) processedDir = join(srcDir, "_processed") mkdir("-p", processedDir) # process new files for f in listdir(srcDir): fullPath = join(srcDir, f) if isfile(fullPath) and getsize(fullPath) > 0: datePrefix = time.strftime("%Y-%m-%d-", time.localtime(getctime(fullPath))) processedFileName = datePrefix + f mv(fullPath, join(processedDir, processedFileName)) # remove old backups, keep only requested count (--keep) for f in ls("-c", processedDir).splitlines()[self.keepCount:]: if self.verbose: print("Removing old backup", f, flush = True) rm(join(processedDir, f)) # sync to s3 (s3["sync", processedDir, s3DirPath, "--storage-class", "STANDARD_IA", "--delete"] > stdout)()
def test_arg_expansion(self): from plumbum.cmd import ls args = ["-l", "-F"] ls(*args) ls[args]
def path_exists(cls, path): try: return ls(path).strip() except plumbum.ProcessExecutionError: return False
from plumbum import local, FG,BG from plumbum.cmd import grep,wc,cat,uniq,which,dnshistory,ls,logtool from plumbum.path.utils import copy from datetime import datetime import os dns=dnshistory["-d blockedIPs", "-f ufw.log", "-w 10", "-l 2"] startcwd = local.cwd print "dns cmd ",dns with local.cwd("/var/log/"): firewall = local.path("ufw.log") if not firewall.exists(): print "no log, am in ",local.getcwd print ls() print ("resolving IPs in background..") print "in ",local.cwd colourMessages = cat["messages"] | uniq & FG #local["logtool"] print "running command "+str(colourMessages) print colourMessages() dns() & BG ufwOutput = cat["ufw.log"] | uniq | grep["-v", "SRC=192"] | grep["BLOCK"] def ufwPlainOutput(): plainFile = "blockedIPs/BlockedIPs_%s.txt" %(datetime.now().strftime("%I:%M_%p_%B-%d-%Y"))
def test_arg_expansion(self): from plumbum.cmd import ls args = [ '-l', '-F' ] ls(*args) ls[args]
def test_prepareHTMLdir(): delete('xyz') prepareHTMLdir('xyz') assert('xyz' in ls().strip()) delete('xyz')
def _is_empty(self): # we don't care with dot files return len(ls()) == 0
def test_makeSupportScriptStache(): delete('xyz') assert makeSupportScriptStache(stacheDir='xyz').endswith('xyz') assert ls('xyz').split()==['RSrun.2.7.min.js', 'glow.2.7.min.js', 'ide.css', 'jquery-ui.custom.css', 'jquery-ui.custom.min.js', 'jquery.min.js'] delete('xyz')
def test_arg_expansion(self): from plumbum.cmd import ls args = ['-l', '-F'] ls(*args) ls[args]
def index(): return ls()
from plumbum import local, FG, BG, cli, SshMachine, colors from plumbum.cmd import grep, awk, wc, head, cat, ls, tail, sudo, ifconfig if __name__ == "__main__": ls = local["ls"] print(ls()) # 环境在linux # 管道符 pipe command = ls["-a"] | awk['{if($2="100") print $2}'] | wc["-l"] print(command()) # 重定向 command = cat['test.file'] | head["-n", 5] print(command()) # 后台运行和当前终端运行 command = (cat['test.file'] | grep["-v", "test"] | (tail["-n", 5] > "out.file")) & FG print(command()) command = (awk['-F', '\t', '{print $1, $2}', 'test.file'] | (head['-n', 5] >> 'out.file')) & BG print(command()) # 嵌套命令 command = sudo[ifconfig['-a']] command1 = (sudo[ifconfig["-a"]] | grep["-i", "loop"]) & FG print(command()) print(command1())