def upload(source, resource_id=None, package_id=None, **kwargs): """Updates the filestore of an existing resource or creates a new one""" verbose = not kwargs['quiet'] resource_id = resource_id or p.splitext(p.basename(source))[0] ckan_kwargs = {k: v for k, v in kwargs.items() if k in api.CKAN_KEYS} if package_id and verbose: print( 'Creating filestore resource %s in dataset %s...' % (source, package_id)) elif verbose: print( 'Uploading %s to filestore resource %s...' % (source, resource_id)) ckan = CKAN(**ckan_kwargs) resource_kwargs = { 'url' if 'http' in source else 'filepath': source, 'name': kwargs.get('name') } if package_id: resource = ckan.create_resource(package_id, **resource_kwargs) else: resource = ckan.update_filestore(resource_id, **resource_kwargs) if package_id and resource and verbose: infix = '%s ' % resource['id'] if resource.get('id') else '' print('Success! Resource %screated.' % infix) elif resource and verbose: print('Success! Resource %s updated.' % resource_id) elif not resource: sys.exit('Error uploading file!')
def migrate(resource_id, **kwargs): """Copies a filestore resource from one ckan instance to another""" src_remote, dest_remote = kwargs['src_remote'], kwargs['dest_remote'] if src_remote == dest_remote: msg = ( 'ERROR: `dest-remote` of %s is the same as `src-remote` of %s.\n' 'The dest and src remotes must be different.\n' % (src_remote, dest_remote)) sys.exit(msg) verbose = not kwargs['quiet'] chunksize = kwargs['chunksize_bytes'] ckan_kwargs = {k: v for k, v in kwargs.items() if k in api.CKAN_KEYS} src_ckan = CKAN(remote=src_remote, **ckan_kwargs) dest_ckan = CKAN(remote=dest_remote, **ckan_kwargs) try: r = src_ckan.fetch_resource(resource_id) filepath = NamedTemporaryFile(delete=False).name except api.NotAuthorized as err: sys.exit('ERROR: %s\n' % str(err)) except Exception as err: sys.exit('ERROR: %s\n' % str(err)) else: tio.write(filepath, r.raw.read(), chunksize=chunksize) resource = dest_ckan.update_filestore(resource_id, filepath=filepath) if resource and verbose: print('Success! Resource %s updated.' % resource_id) elif not resource: sys.exit('Error uploading file!') finally: if verbose: print('Removing tempfile...') unlink(filepath)