def test02catTests(self): """ Test the cat command """ self.env = pyflagsh.environment(case=self.test_case) pyflagsh.shell_execv(env=self.env, command="load", argv=[ self.test_case, ]) self.fsfd = FileSystem.DBFS(self.test_case) fd = self.fsfd.open("/dscf1080.jpg") data1 = fd.read() fd = self.fsfd.open("/dscf1081.jpg") data2 = fd.read() fd = self.fsfd.open("/dscf1082.jpg") data3 = fd.read() result = '' for l in pyflagsh.shell_execv_iter(env=self.env, command="cat", argv=["/dscf1081.jpg"]): result += l self.assertEqual(result, data2) result = '' for l in pyflagsh.shell_execv_iter(env=self.env, command="cat", argv=["/dscf108*"]): result += l self.assertEqual(len(result), len(data1) + len(data2) + len(data3)) self.assert_(result == data1 + data2 + data3)
def test01ls(self): """ Test the ls command """ self.env = pyflagsh.environment(case=self.test_case) pyflagsh.shell_execv(env=self.env, command="load", argv=[self.test_case,]) ## Check we can list default directory lines = [ l for l in pyflagsh.shell_execv_iter(env=self.env, command="ls", argv=[])] self.assertEqual(len(lines),18) ## Check we can list directories lines = [ l for l in pyflagsh.shell_execv_iter(env=self.env, command="ls", argv=["docs"])] self.assert_(len(lines)>=3) ## Check that we can glob files: lines = [ l for l in pyflagsh.shell_execv_iter(env=self.env, command="ls", argv=["*.jpg"])] self.assertEqual(len(lines),5) ## Check that we can glob directories: lines = [ l for l in pyflagsh.shell_execv_iter(env=self.env, command="ls", argv=["do*"])] self.assert_(len(lines)>3)
def test02catTests(self): """ Test the cat command """ self.env = pyflagsh.environment(case=self.test_case) pyflagsh.shell_execv(env=self.env, command="load", argv=[self.test_case,]) self.fsfd = FileSystem.DBFS(self.test_case) fd = self.fsfd.open("/dscf1080.jpg") data1=fd.read() fd = self.fsfd.open("/dscf1081.jpg") data2=fd.read() fd = self.fsfd.open("/dscf1082.jpg") data3=fd.read() result = '' for l in pyflagsh.shell_execv_iter(env=self.env, command="cat", argv=["/dscf1081.jpg"]): result+=l self.assertEqual(result,data2) result = '' for l in pyflagsh.shell_execv_iter(env=self.env, command="cat", argv=["/dscf108*"]): result+=l self.assertEqual(len(result),len(data1)+len(data2)+len(data3)) self.assert_(result==data1+data2+data3)
#!/usr/bin/env python """ A program demonstrating the automation of flag using the Flag Shell and python. We extract all the files with type like image into the /tmp/ directory""" ## Provides access to the pyflag shell import pyflag.pyflagsh as pyflagsh ## First we load the filesystem in: pyflagsh.shell_execv('load','demo') #Do a big find over the filesystem to recover all the files for file in pyflagsh.shell_execv_iter('find_dict','/'): # Use file to check their magic t = pyflagsh.shell_execv('file',"%s%s" % (file['path'],file['name'])) try: if t and t['type'].index('image'): ## Create this file in the /tmp/ directory new_filename = "/tmp/results/%s" % file['name'] if not new_filename.endswith('.jpg'): new_filename+='.jpg' print "created file %s magic %s" % (new_filename,t['type']) fd = open(new_filename,'w') for data in pyflagsh.shell_execv_iter('cat',"%s%s" % (file['path'],file['name'])): fd.write(data) fd.close() except ValueError: pass