Exemple #1
0
def get():
	try:
		path = '/'
		import sys
		if len(sys.argv[1:]) > 1:
			if sys.argv[2].startswith('/'):
				path = sys.argv[2]
				if path.endswith('/'):
					path = path[:-1]
		
		else:
			print 'arg err get failed'
			return
		filename = path.split("/")[-1]
		if not filename:
			print 'Invalid filename'
			return
		fd = sbpfs.open(path, 1, 0)
		if fd < 0: 
			print 'Make sure your request path exist and file-type must be file'
			return
		dict = sbpfs.stat(path)
		length = int(dict['size'])
		if os.path.exists(filename):
			print 'File : %s already exists' % filename
			sbpfs.close(fd)
			return
		f = file(filename, 'wb')
		bufsize = 1024*1024
		for i in range(0, length / bufsize):
			r = sbpfs.read(fd, bufsize)
			if r and len(r) == bufsize:
				f.write(r)
				print "received : %.2f%% of %d bytes" % (((float(i*bufsize)/length)*100) ,length)
			else:
				print 'Error occur'
				sbpfs.perror('')
				sbpfs.close(fd)
				return
		if length % bufsize:
			r = sbpfs.read(fd, length % bufsize)
			if r and len(r) == length % bufsize:
				f.write(r)
			print "received : %.2f%%" % float(100.0)
		f.close()
		sbpfs.close(fd)
				
					
	except:
		print 'Unknow error occur'
		import traceback
		import sys
		traceback.print_exc(file=sys.stdout)
Exemple #2
0
def put():
	try:
		path = '/'
		dst = '/'
		import sys
		if len(sys.argv[1:]) > 2:
			path = sys.argv[2]
			if path.endswith('/'):
				path = path[:-1]
	
			if sys.argv[3].startswith('/'):
				dst = sys.argv[3]
				if dst.endswith('/'):
					dst = dst[:-1]
		elif len(sys.argv[1:]) == 2:
			if sys.argv[2].startswith('/'):
				path = sys.argv[2]
				if path.endswith('/'):
					path = path[:-1]
			dst = '/' + path.split('/')[-1]

		else:
			print 'arg err get failed'
			return
		filename = path.split("/")[-1]
		if not filename:
			print 'Invalid filename'
			return
		
		
		if not os.path.exists(path):
			print 'File : %s not exists' % path
			return
		if not os.path.isfile(path):
			print '%s is not a file' % path
			return
		length = os.path.getsize(path)
		f = file(path, 'rb')
		fd = sbpfs.open(dst, 10, 14)
		if fd < 0: 
			print 'Make sure your request path exist and file-type must be file'
			return
		bufsize = 1024*1024
		for i in range(0, length / bufsize):
			r = f.read(bufsize)
			if r and len(r) == bufsize:
				rb = buffer(r)
				sbpfs.write(fd,rb,bufsize)
				print "send : %.2f%% of %d bytes" % (((float(i*bufsize)/length)*100) ,length)
			else:
				print 'Error occur'
				sbpfs.perror('')
				sbpfs.close(fd)
				return
		if length % bufsize:
			r = f.read(length % bufsize)
			
			rb=buffer(r)
			if r and len(r) == length % bufsize:
				sbpfs.write(fd,rb,length%bufsize)
			
			print "send : %.2f%%" % (100.0)
		f.close()
		sbpfs.close(fd)
				
					
	except:
		print 'Unknow error occur'
		import traceback
		import sys
		traceback.print_exc(file=sys.stdout)