def upload_bpcs(upload_dir,upload_file,del_file): access_token="access"#从百度云获取 disk=BaiduPan(access_token) bpcs_dir='/apps/bpcs_uploader/' if not disk.meta(bpcs_dir+upload_dir): disk.mkdir(bpcs_dir+upload_dir) #查看使用情况 #print disk.quota() disk.upload(upload_file, path=bpcs_dir+upload_dir+upload_file,ondup='overwrite') #上传文件,如果存在,直接覆盖 if disk.meta(bpcs_dir+upload_dir+del_file): #删除历史文件 disk.rm(bpcs_dir+upload_dir+del_file)
def upload_bpcs(upload_dir, upload_file, del_file): access_token = "access" # 从百度云获取 disk = BaiduPan(access_token) bpcs_dir = '/apps/bpcs_uploader/' if not disk.meta(bpcs_dir + upload_dir): disk.mkdir(bpcs_dir + upload_dir) # 查看使用情况 # print disk.quota() disk.upload(upload_file, path=bpcs_dir + upload_dir + upload_file, ondup='overwrite') # 上传文件,如果存在,直接覆盖 if disk.meta(bpcs_dir + upload_dir + del_file): # 删除历史文件 disk.rm(bpcs_dir + upload_dir + del_file)
class DefaultTestCase(unittest.TestCase): def setUp(self): self.baidupan = BaiduPan() self.request_patcher = patch("baidupan.baidupan.BaiduPan._request") self.mock_request = self.request_patcher.start() def tearDown(self): self.request_patcher.stop() def test_version(self): self.assertIsNotNone(baidupan.__version__, '0.0.1') @patch("__builtin__.open") def test_upload(self, mock_open): mock_open.return_value.read.return_value = "hulahoop" self.baidupan.upload({"keyword": "hulahoop"}) self.assertEqual(self.mock_request.call_args, call(file='hulahoop', filename={'keyword': 'hulahoop'})) def test_merge(self): self.baidupan.merge('path', 'param', keyword="hulahoop") self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', param='param', path='path')) def test_similar_path_functions(self): for method in ('download', 'mkdir', 'meta'): getattr(self.baidupan, method)('path', keyword='hulahoop') self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', path='path')) def test_similar_param_functions(self): for method in ('mmv', 'mmeta'): getattr(self.baidupan, method)('param', keyword='hulahoop') self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', param='param')) def test_mv_cp(self): for method in ('mv', 'cp'): getattr(self.baidupan, method)('from_path', 'to_path', keyword='hulahoop') self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', from_path='from_path', to='to_path')) @patch("requests.post") @patch("requests.get") def test_private_request(self, mock_get, mock_post): # Disable the earlier patch on _request to properly test it. self.request_patcher.stop() baidupan = BaiduPan() baidupan.base_url = 'base_url' baidupan.urlpath = 'urlpath' baidupan.method = 'method' baidupan.access_token = 'access_token' baidupan._method = "GET" mock_get.return_value.content = 'hulahoop' result = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertTrue(mock_get.call_args, call('base_urlurlpath', headers='headers', params={'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method'})) self.assertEqual(result, 'hulahoop') mock_post.return_value.content = 'hulahoop' baidupan._method = "POST" result_post = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertEqual(mock_post.call_args, call('base_urlurlpath', files={'files': ('file1', 'file1')}, headers='headers', params={'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method'})) self.assertEqual(result_post, 'hulahoop') baidupan.payload = 'payload' baidupan.files = None mock_post.reset_mock() result_payload = baidupan._request(headers='headers') self.assertEqual(mock_post.call_args, call('base_urlurlpath', data='payload', headers='headers', params={'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method'})) self.assertEqual(result_payload, 'hulahoop') baidupan._method = 'not allowed' with self.assertRaises(Exception): baidupan._request(headers="headers") self.request_patcher.start()
########NEW FILE######## __FILENAME__ = example #!/usr/bin/env python #coding=utf-8 import json from baidupan.baidupan import BaiduPan if __name__ == '__main__': access_token = '' disk = BaiduPan(access_token) #quota print disk.quota() #upload print disk.upload('hello', path='/apps/appname/hello.txt') #merge ''' def merge(self, path, param, **kw): self.urlpath = 'file' self.method = 'createsuperfile' self._method = 'POST' return self._request(path=path, param=param, **kw) ''' param = '' print disk.merge('/apps/appname/hello.txt', param=param) #download print disk.download(path='/apps/appname/hello.txt') print disk.download(path='/apps/appname/hello.txt', headers={"Range": "Range: bytes:1-100"}) #mkdir
class BaiFuse(LoggingMixIn, Operations): def __init__(self, root, token): self.root = root self.token = token self.api = BaiduPan(self.token) def get_path(self, path): return os.path.join(BAIDUPATH,path) # def chmod(self, path, mode): # return True # def chown(self, path, uid, gid): # return True # def create(self, path, mode): # f = self.sftp.open(path, 'w') # f.chmod(mode) # f.close() # return 0 # def destroy(self, path): # self.sftp.close() # self.client.close() def getattr(self, path, fh=None): resp = json.loads(self.api.meta(self.get_path(path))) if 'list' not in resp: return {} return { 'st_ino': 0, 'st_dev': 0, 'st_atime': 0, 'st_mtime': resp['list'][0]['mtime'], 'st_ctime': resp['list'][0]['ctime'], 'st_gid': os.getgid(), 'st_uid': os.getuid(), 'st_mode': ((stat.S_IFDIR | 0755) if resp['list'][0]['isdir'] else (stat.S_IFREG | 0755)), 'st_size': resp['list'][0]['size'], 'st_nlink': (2 if resp['list'][0]['isdir'] else 1), } def mkdir(self, path, mode): self.api.mkdir(self.get_path(path)) #return? def read(self, path, size, offset, fh): return self.api.download(self.get_path(path), headers={'Range':"Range: bytes=%s-%s"%(offset,offset+size)}) def readdir(self, path, fh): resp = json.loads(self.api.ls(self.get_path(path))) return ['.', '..'] + [name['path'].encode('utf-8') for name in resp['list']] # def readlink(self, path): # return self.sftp.readlink(path) # def rename(self, old, new): # return self.sftp.rename(old, self.root + new) def rmdir(self, path): self.api.rm(self.get_path(path)) # return ? # def symlink(self, target, source): # return self.sftp.symlink(source, target) # def truncate(self, path, length, fh=None): # return self.sftp.truncate(path, length) # def unlink(self, path): # return self.sftp.unlink(path) # def utimens(self, path, times=None): # return self.sftp.utime(path, times) def write(self, path, data, offset, fh): # can't use the api -> need file dissociation + merge (super file) return self.api.upload(path)
#!/usr/bin/env python #coding=utf-8 import json from baidupan.baidupan import BaiduPan if __name__ == '__main__': access_token = '' disk = BaiduPan(access_token) #quota print disk.quota() #upload print disk.upload('hello', path='/apps/appname/hello.txt') #merge ''' def merge(self, path, param, **kw): self.urlpath = 'file' self.method = 'createsuperfile' self._method = 'POST' return self._request(path=path, param=param, **kw) ''' param = '' print disk.merge('/apps/appname/hello.txt', param=param) #download print disk.download(path='/apps/appname/hello.txt') #mkdir print disk.mkdir('/apps/appname/dirname') #meta print disk.meta('/apps/appname/filename') #mmeta print disk.mmeta(json.dumps({"list": [{"path": "/apps/appname/"}]}))
class DefaultTestCase(unittest.TestCase): def setUp(self): self.baidupan = BaiduPan() self.request_patcher = patch("baidupan.baidupan.BaiduPan._request") self.mock_request = self.request_patcher.start() def tearDown(self): self.request_patcher.stop() def test_version(self): self.assertIsNotNone(baidupan.__version__, '0.0.1') @patch("__builtin__.open") def test_upload(self, mock_open): mock_open.return_value.read.return_value = "hulahoop" self.baidupan.upload({"keyword": "hulahoop"}) self.assertEqual( self.mock_request.call_args, call(file='hulahoop', filename={'keyword': 'hulahoop'})) def test_merge(self): self.baidupan.merge('path', 'param', keyword="hulahoop") self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', param='param', path='path')) def test_similar_path_functions(self): for method in ('download', 'mkdir', 'meta'): getattr(self.baidupan, method)('path', keyword='hulahoop') self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', path='path')) def test_similar_param_functions(self): for method in ('mmv', 'mmeta'): getattr(self.baidupan, method)('param', keyword='hulahoop') self.assertEqual(self.mock_request.call_args, call(keyword='hulahoop', param='param')) def test_mv_cp(self): for method in ('mv', 'cp'): getattr(self.baidupan, method)('from_path', 'to_path', keyword='hulahoop') self.assertEqual( self.mock_request.call_args, call(keyword='hulahoop', from_path='from_path', to='to_path')) @patch("requests.post") @patch("requests.get") def test_private_request(self, mock_get, mock_post): # Disable the earlier patch on _request to properly test it. self.request_patcher.stop() baidupan = BaiduPan() baidupan.base_url = 'base_url' baidupan.urlpath = 'urlpath' baidupan.method = 'method' baidupan.access_token = 'access_token' baidupan._method = "GET" mock_get.return_value.content = 'hulahoop' result = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertTrue( mock_get.call_args, call('base_urlurlpath', headers='headers', params={ 'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method' })) self.assertEqual(result, 'hulahoop') mock_post.return_value.content = 'hulahoop' baidupan._method = "POST" result_post = baidupan._request(headers='headers', file='file1', filename='file1', from_path='from_path', content_length='content_length', content_md5='content_md5', slice_md5='slice_md5', content_crc32='content_crc32') self.assertEqual( mock_post.call_args, call('base_urlurlpath', files={'files': ('file1', 'file1')}, headers='headers', params={ 'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method' })) self.assertEqual(result_post, 'hulahoop') baidupan.payload = 'payload' baidupan.files = None mock_post.reset_mock() result_payload = baidupan._request(headers='headers') self.assertEqual( mock_post.call_args, call('base_urlurlpath', data='payload', headers='headers', params={ 'slice-md5': 'slice_md5', 'content-length': 'content_length', 'from': 'from_path', 'access_token': 'access_token', 'filename': 'file1', 'content-crc32': 'content_crc32', 'content-md5': 'content_md5', 'method': 'method' })) self.assertEqual(result_payload, 'hulahoop') baidupan._method = 'not allowed' with self.assertRaises(Exception): baidupan._request(headers="headers") self.request_patcher.start()