Commit 5ee4b8e5 authored by Gao, Shang's avatar Gao, Shang
Browse files

added download_resource method to crossbowMount

parent c67c38bb
Loading
Loading
Loading
Loading
+28 −5
Original line number Diff line number Diff line
from ckanapi import RemoteCKAN
from shutil import copyfile
from clint.textui import progress
import os

@@ -176,7 +177,8 @@ class crossbowMount(crossbowBase):
      - get_package_details(package)
        prints and returns details for a specific package

      - create_package(package,title,description,author,author_email,maintainer,maintainer_email,version,tags)
      - create_package(package,owner_org,title=None,description=None,author=None,author_email=None,
            maintainer=None,maintainer_email=None,version=None,tags=[])
        creates a new package (dataset)

      - edit_package(package,*args,**kwargs)
@@ -188,7 +190,7 @@ class crossbowMount(crossbowBase):
      - get_resource_details(package,resource)
        prints and returns details for a specific resource

      - add_resource(package,resource,resource_path,description)
      - add_resource(package,resource,resource_path,description=None)
        add a resource located on local NFS mount to an existing package

      - edit_resource(package,resource,*args,**kwargs)
@@ -197,7 +199,10 @@ class crossbowMount(crossbowBase):
      - get_resource_path(package,resource)
        return the path to a resource on the local NFS mount

      - delete_resource(package,resource,delete_from_nfs)
      - download_resource(package,resource,destination="./")
        copy the resource to a local destination

      - delete_resource(package,resource,delete_from_nfs=False)
        delete a resource from a package (Note: your api key must belong to a sysadmin 
        or the owner of the resource to delete a resource)
    '''
@@ -207,8 +212,6 @@ class crossbowMount(crossbowBase):
        #make sure NFS_path exists
        if not os.path.isdir(NFS_path) or not os.path.ismount(NFS_path):
            raise Exception("Invald NFS path")
        if NFS_path[0] != "/":
            raise Exception("Please use full path to NFS mount")
        if NFS_path[-1] != '/':
            self.NFS_path = NFS_path + '/'
        else:
@@ -307,6 +310,26 @@ class crossbowMount(crossbowBase):
        resource_path = resource_path.replace("file://CROSSBOW_NFS/", self.NFS_path)
        return resource_path

    def download_resource(self,package,resource,destination="./"):
        '''
        copy the resource to a local destination
        
        parameters:
          - package: string
            name of package
          - resource: string
            name of resource
          - destination: string (default: "./")
            directory to copy resource into
        '''
        pkg_metadata = self.ckan.action.package_show(id=package)
        resource_path = [metadata['url'] if metadata['name']==resource else None for metadata in pkg_metadata['resources']][0]
        resource_path = resource_path.replace("file://CROSSBOW_NFS/", self.NFS_path)
        dest_path = destination + os.path.basename(resource_path)
        
        print "copying %s to %s" % (resource_path, dest_path)
        copyfile(resource_path, dest_path)

    def delete_resource(self,package,resource,delete_from_nfs=False):
        '''
        delete a resource from a package (Note: your api key must belong to a sysadmin 
+13 −7
Original line number Diff line number Diff line
@@ -20,8 +20,8 @@ def setup_module():
    ckan.action.resource_create(package_id="nosetests1",url="file://CROSSBOW_NFS/testfile.csv",name="myresource1",description="myresource1")
    
    #create temporary file for testing
    tempfile = open('/data/testfile.csv', 'w+')
    tempfile.close()
    #tempfile = open('/data/testfile.csv', 'w+')
    #tempfile.close()
    
def test_list_packages():
    assert "nosetests1" in cbowMount.list_packages()
@@ -62,12 +62,16 @@ def test_edit_resource():
def test_get_resource_path():
    assert cbowMount.get_resource_path("nosetests1","myresource1") == "/data/testfile.csv"

def test_download_resource():
    cbowMount.download_resource("nosetests1","myresource1")
    assert os.path.isfile('testfile.csv')

def test_delete_resource():
    cbowMount.delete_resource("nosetests1","myresource1",delete_from_nfs=True)
    cbowMount.delete_resource("nosetests1","myresource1",delete_from_nfs=False)
    metadata = ckan.action.package_show(id="nosetests1")
    resources = [resource['name'] for resource in metadata['resources']]
    assert "myresource1" not in resources
    assert not os.path.isfile('/data/testfile.csv')
    #assert not os.path.isfile('/data/testfile.csv')

def teardown_module():
    #purge all packages used for testing
@@ -75,7 +79,9 @@ def teardown_module():
    if "nosetests2" in ckan.action.package_list():
        ckan.action.dataset_purge(id="nosetests2")
    
    #delete temporary file if it exists
    if os.path.isfile('/data/testfile.csv'):
       os.remove('/data/testfile.csv')
    #delete temporary files if they exist
    #if os.path.isfile('/data/testfile.csv'):
       #os.remove('/data/testfile.csv')
    if os.path.isfile('testfile.csv'):
       os.remove('testfile.csv')