hdfs.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import json
  18. from django.http import HttpResponse
  19. from django.utils.translation import ugettext as _
  20. from desktop.lib.exceptions_renderable import PopupException
  21. from filebrowser.views import display, listdir_paged
  22. def _get_acl_name(acl):
  23. return ('default:' if acl['isDefault'] else '') + acl['type'] + ':' + acl['name'] + ':'
  24. def _get_acl(acl):
  25. return _get_acl_name(acl) + ('r' if acl['r'] else '-') + ('w' if acl['w'] else '-') + ('x' if acl['x'] else '-')
  26. def _diff_list_dir(user_listing, hdfs_listing):
  27. user_files = [f['stats']['path'] for f in user_listing['files']]
  28. hdfs_files = [f['stats']['path'] for f in hdfs_listing['files']]
  29. # Files visible by hdfs only
  30. hdfs_only = list(set(hdfs_files) - set(user_files))
  31. new_hdfs = filter(lambda f: f['stats']['path'] in hdfs_only, hdfs_listing['files'])
  32. for f in new_hdfs:
  33. f['striked'] = True
  34. listing = user_listing['files'] + new_hdfs
  35. return sorted(listing, key=lambda f: f['path'])
  36. def list_hdfs(request, path):
  37. try:
  38. stats = request.fs.stats(path)
  39. if stats.isDir:
  40. json_response = listdir_paged(request, path)
  41. else:
  42. json_response = display(request, path)
  43. except:
  44. json_response = HttpResponse(json.dumps({'files': [], 'page': {}}), mimetype="application/json") # AccessControlException: Permission denied: user=test, access=READ_EXECUTE, inode="/tmp/dir":romain:supergroup:drwxr-xr-x:group::r-x,group:bob:---,group:test:---,default:user::rwx,default:group::r--,default:mask::r--,default:other::rwx (error 403)
  45. if json.loads(request.GET.get('isDiffMode', 'false')):
  46. request.doas = 'hdfs'
  47. stats = request.fs.stats(path)
  48. if stats.isDir:
  49. hdfs_response = json.loads(listdir_paged(request, path).content)
  50. resp = json.loads(json_response.content)
  51. resp['page'] = hdfs_response['page']
  52. resp['files'] = _diff_list_dir(resp, hdfs_response)
  53. json_response.content = json.dumps(resp)
  54. return json_response
  55. def get_acls(request):
  56. path = request.GET.get('path')
  57. try:
  58. acls = request.fs.get_acl_status(path)
  59. except Exception, e:
  60. acls = None
  61. return HttpResponse(json.dumps(acls is not None and acls['AclStatus'] or None), mimetype="application/json")
  62. def update_acls(request):
  63. path = request.POST.get('path')
  64. acls = json.loads(request.POST.get('acls'))
  65. original_acls = json.loads(request.POST.get('originalAcls'))
  66. try:
  67. renamed_acls = set([_get_acl_name(acl) for acl in original_acls]) - set([_get_acl_name(acl) for acl in acls]) # We need to remove ACLs that have been renamed
  68. _remove_acl_names(request.fs, path, list(renamed_acls))
  69. _remove_acl_entries(request.fs, path, [acl for acl in acls if acl['status'] == 'deleted'])
  70. _modify_acl_entries(request.fs, path, [acl for acl in acls if acl['status'] in ('new', 'modified')])
  71. except Exception, e:
  72. raise PopupException(unicode(str(e.message), "utf8"))
  73. return HttpResponse(json.dumps({'status': 0}), mimetype="application/json")
  74. def _modify_acl_entries(fs, path, acls):
  75. aclspec = ','.join([_get_acl(acl) for acl in acls])
  76. return fs.modify_acl_entries(path, aclspec)
  77. def _remove_acl_entries(fs, path, acls):
  78. aclspec = ','.join([_get_acl_name(acl) for acl in acls])
  79. return fs.remove_acl_entries(path, aclspec)
  80. def _remove_acl_names(fs, path, acl_names):
  81. aclspec = ','.join(acl_names)
  82. return fs.remove_acl_entries(path, aclspec)