예제 #1
0
def mucua_list(request, repository=None):
    """
    List all mucuas
    """
    
    logger.debug(_(u"Acessing mucuas in repository: %s") % repository)
   
    if repository:
        try:
            repository = Repository.objects.get(name=repository)
        except Repository.DoesNotExist:
            return Response("Repository not found")

    mucuas = get_available_mucuas(None, repository)   # retorna tupla de mucuas
    mucuas_list = []

    if mucuas is None:
        return Response(None)

    for mucua_obj in mucuas:
        if mucua_obj[1] != 'web':
            mucua_description = mucua_obj[1]
            mucua_uuid = mucua_obj[0]
            try:
                mucua = Mucua.objects.get(uuid=mucua_uuid)
            except Mucua.DoesNotExist:
                return Response(_("Mucua not found"))

            if mucua:
                mucuas_list.append(mucua)

    serializer = MucuaSerializer(mucuas_list, many=True)

    return Response(serializer.data)
예제 #2
0
def mucua_list(request, repository=None):
    """
    List all mucuas
    """

    logger.debug(_(u"Acessing mucuas in repository: %s") % repository)

    if repository:
        try:
            repository = Repository.objects.get(name=repository)
        except Repository.DoesNotExist:
            return Response("Repository not found")

    mucuas = get_available_mucuas(None, repository)  # retorna tupla de mucuas
    mucuas_list = []

    if mucuas is None:
        return Response(None)

    for mucua_obj in mucuas:
        if mucua_obj[1] != 'web':
            mucua_description = mucua_obj[1]
            try:
                mucua = Mucua.objects.get(description=mucua_description)
            except Mucua.DoesNotExist:
                return Response("Mucua not found")

            if mucua:
                mucuas_list.append(mucua)

    serializer = MucuaSerializer(mucuas_list, many=True)

    return Response(serializer.data)
예제 #3
0
    def authenticate(self, username=None, password=None):
        match = re.findall("(.*)@(.*)\.(.*)\.(.*)$", username)
        if match:
            (current_mocambola, current_mucua, current_repository, term) = match[0]
            # verifica se mucua e repositorio sao validos
            try:
                current_mucua = Mucua.objects.get(description=current_mucua)
            except Mucua.DoesNotExist:
                return None
            try:
                current_repository = Repository.objects.get(name=current_repository)
            except Repository.DoesNotExist:
                return None
        else:
            print "invalid address"
            return None
        # Get file from MOCAMBOLA_DIR
        mocambola_path = os.path.join(str(REPOSITORY_DIR), str(current_repository), str(current_mucua), MOCAMBOLA_DIR)
        print "Mocambola Path: " + mocambola_path

        for jmocambola in os.listdir(mocambola_path):

            if jmocambola == username + ".json":
                # Deserialize the customized User object
                mocambola_json_file = open(os.path.join(mocambola_path, jmocambola))
                data = JSONParser().parse(mocambola_json_file)
                u = User()
                serializer = UserSerializer(u, data=data)
                if serializer.errors:
                    logger.debug(u"%s %s" % (_("Error deserialing"), serializer.errors))
                serializer.is_valid()

                current_user = serializer.object
                login_valid = username == current_user.username
                pwd_valid = check_password(password, current_user.password)
                if login_valid and pwd_valid:
                    logger.info(u"%s %s %s" % (_("User"), current_mocambola, _("logged in")))
                    try:
                        user = User.objects.get(username=username)
                    except User.DoesNotExist:
                        logger.debug(u"%s" % (_("Exception caught, UserDoesNotExist")))
                        # Create a new user. Note that we can set password
                        # to anything, because it won't be checked; the
                        # password from settings.py will.
                        user = User(
                            username=username,
                            password=current_user.password,
                            is_staff=current_user.is_staff,
                            is_superuser=current_user.is_superuser,
                        )
                        user.save()
                    return user
                else:
                    logger.info(u"%s %s %s" % (_("User"), current_mocambola, _("doesn't exist or password is wrong!")))
                    return None
                return True
            # fim do if
        # fim do for
        return None
예제 #4
0
def git_annex_group_add(repository_path, mucua, group):
    u"""Adiciona a Mucua no grupo."""
    cmd = "git annex group " + mucua + " " + group
    pipe = subprocess.Popen(cmd, shell=True, cwd=repository_path, stdout=subprocess.PIPE)
    output, error = pipe.communicate()
    logger.debug(error)
    logger.debug(output)
    return output
예제 #5
0
def git_get_SHA(repository_path):
    u"""Resgata o codigo identificativo (SHA) da ultima revisao do repositório,
    retorna o codigo."""
    logger.info('git rev-parse HEAD')
    cmd = 'git rev-parse HEAD'
    pipe = subprocess.Popen(cmd, shell=True, cwd=repository_path)
    output, error = pipe.communicate()
    logger.debug('>>> Revision is: ' + output)
    return output
예제 #6
0
def git_get_SHA(repository_path):
    u"""Resgata o codigo identificativo (SHA) da ultima revisao do repositório,
    retorna o codigo."""
    logger.info("git rev-parse HEAD")
    cmd = "git rev-parse HEAD"
    pipe = subprocess.Popen(cmd, shell=True, cwd=repository_path)
    output, error = pipe.communicate()
    logger.debug(">>> Revision is: " + output)
    return output
예제 #7
0
def git_annex_group_add(repository_path, mucua_uuid, group):
    u"""Adiciona a Mucua no grupo."""
    cmd = 'git annex group ' + mucua_uuid + ' ' + group
    pipe = subprocess.Popen(cmd, shell=True, cwd=repository_path,
                            stdout=subprocess.PIPE)
    output, error = pipe.communicate()
    logger.debug("E > %s", error)
    logger.debug("O > %s", output)
    return output
예제 #8
0
def git_annex_drop(media):
    u"""Mostra quais mucuas tem copia do media."""
    cmd = "git annex drop --force " + os.path.basename(media.media_file.name)
    logger.debug("Dropping filepath: " + get_file_path(media) + media.get_file_name())
    pipe = subprocess.Popen(cmd, shell=True, cwd=get_file_path(media), stdout=subprocess.PIPE)
    output, error = pipe.communicate()
    logger.debug(error)
    logger.info(output)
    return output
예제 #9
0
def mucua_get_territory(request, uuid=None, repository=None):
    try:     
        logger.debug(_(u"MUCUA UUID: %s") % uuid)
        mucua = Mucua.objects.get(uuid=uuid)
        logger.debug(_(u"MUCUA name: %s") % mucua.description)
    except:
        return Response(_("Mucua not found"))
    
    io = mucua.get_territory(repository)
    return Response(io)
예제 #10
0
def git_annex_group_add(repository_path, mucua, group):
    u"""Adiciona a Mucua no grupo."""
    cmd = 'git annex group ' + mucua + ' ' + group
    pipe = subprocess.Popen(cmd,
                            shell=True,
                            cwd=repository_path,
                            stdout=subprocess.PIPE)
    output, error = pipe.communicate()
    logger.debug(error)
    logger.debug(output)
    return output
예제 #11
0
def get_media_type_by_filename(file_path):
    try:
        mime = magic.from_file(file_path, mime=True)
    except IOError:
        logger.info('Error while reading uploaded file.')

    if mime in VALID_MIMETYPES:
        return VALID_MIMETYPES[mime]
    else:
        logger.debug('Mime: ' + mime)
        logger.info('Mime type not accepted.')
        return False
예제 #12
0
def get_media_type_by_filename(file_path):
    try:
        mime = magic.from_file(file_path, mime=True)
    except IOError:
        logger.info('Error while reading uploaded file.')

    if mime in VALID_MIMETYPES:
        return VALID_MIMETYPES[mime]
    else:
        logger.debug('Mime: '+ mime)
        logger.info('Mime type not accepted.')
        return False
예제 #13
0
def git_annex_drop(media):
    u"""Mostra quais mucuas tem copia do media."""
    cmd = 'git annex drop --force ' + os.path.basename(media.media_file.name)
    logger.debug('Dropping filepath: ' + get_file_path(media) +
                 media.get_file_name())
    pipe = subprocess.Popen(cmd,
                            shell=True,
                            cwd=get_file_path(media),
                            stdout=subprocess.PIPE)
    output, error = pipe.communicate()
    logger.debug(error)
    logger.info(output)
    return output
예제 #14
0
def git_annex_list_tags(media):
    u"""Enumerar as etiquetas do media."""
    metadata = git_annex_metadata(media.get_file_name(), get_file_path(media))
    try:
        metadata = json.loads(metadata)
    except ValueError:
        # Sometimes, the JSON metadata output on non-present media files is
        # malformed. Ignore these cases, but log.
        logger.debug(u"Malformed JSON found on media: {0}".format(media))
        pass
    tags = []
    for item in metadata:
        if item.endswith("-tag"):
            for tag in metadata[item]:
                tags.append((item, tag))
    return tags
예제 #15
0
def git_annex_list_tags(media):
    u"""Enumerar as etiquetas do media."""
    metadata = git_annex_metadata(media.get_file_name(), get_file_path(media))
    try:
        metadata = json.loads(metadata)
    except ValueError:
        # Sometimes, the JSON metadata output on non-present media files is
        # malformed. Ignore these cases, but log.
        logger.debug(u'Malformed JSON found on media: {0}'.format(media))
        pass
    tags = []
    for item in metadata:
        if item.endswith('-tag'):
            for tag in metadata[item]:
                tags.append((item, tag))
    return tags
예제 #16
0
def login(request):
    
    if request.method == 'POST':
        username = request.DATA['username'] + '@' + request.DATA['mucua'] + '.' + request.DATA['repository'] + '.net'
        password = request.DATA['password']
        fileBackend = FileBackend()
        authenticate = fileBackend.authenticate(username, password)
        
        # TODO: get this data from logger or bbx/auth.py,
        # so the next section won't be needed anymore
        if (authenticate):
            try:
                user = User.objects.get(username=username)
            except User.DoesNotExist:
                logger.debug(u"%s" % (
                    _('Exception caught, UserDoesNotExist')
                ))
            
            if user:
                # gera token
                
                jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
                jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
                payload = jwt_payload_handler(user)
                payload['orig_iat'] = timegm(
                    datetime.utcnow().utctimetuple()
                )
                payload['exp'] = datetime.utcnow() + timedelta(minutes=3600)

                response_data = {
                    'username': username,
                    'token': jwt_encode_handler(payload)
                }
                
                return HttpResponse(json.dumps(response_data))
            else:
                response_data = {
                    'errorMessage': _('User don\'t exists: ')
                }
                return HttpResponse(json.dumps(response_data), mimetype=u'application/json')
        else:
            response_data = {
                'error': True,
                'errorMessage': _('Invalid user or password')
            }
            
            return HttpResponse(json.dumps(response_data), mimetype=u'application/json')
예제 #17
0
    def get_territory(self, repository):
        u"""Retorna o territorio da mucua"""
        if not repository:
            try:
                repository = get_default_repository()
            except DatabaseError:
                return []

        groups = git_annex_group_list(repository.get_path(), self.uuid)
        territory = ''
        logger.debug('Grupos:')
        for group in groups:
            logger.debug('> %s', group)
            if group.startswith('t:'):
                territory = group

        return territory[2:]
예제 #18
0
    def handle(self, *args, **options):
        if len(args) == 0:
            medias = Media.objects.filter(is_local=False)
            logger.info("Atualizando todos as copias locais dos medias")
        else:
            since = int(args[0])
            some_day_ago = timezone.now().date() - timedelta(days=since)
            logger.info("Atualizando os medias desde o dia: " +
                        str(some_day_ago))
            medias = Media.objects.filter(date__gte=some_day_ago)

        for media in medias:
            try:

                # Synchronize/update tags.
                #
                # 1) Add all tags found in the git-annex metadata and not
                # already present on the media.
                # 2) If tags from other mucuas have been deleted (are missing in
                # the git_annex metadata tags), remove them from this media.
                tags_on_media = set(git_annex_list_tags(media))
                existing_tags = set(
                    (t.namespace, t.name) for t in media.tags.all())
                # Add new tags to media
                for t in tags_on_media - existing_tags:
                    # Add tag - search for existing, if none found create new tag.
                    namespace, name = t
                    try:
                        tag = Tag.objects.get(name=unicode(name),
                                              namespace=unicode(namespace))
                    except Tag.DoesNotExist:
                        tag = Tag(name=name, namespace=namespace)
                        tag.save()
                    media.tags.add(tag)

                # Remove tags that were removed on remote media
                for t in existing_tags - tags_on_media:
                    namespace, name = t
                    tag = Tag.objects.get(name=name, namespace=namespace)
                    media.tags.remove(tag)

                media.save(is_syncing=True)

            except OSError, e:
                logger.debug('Requested media not found: ' + media.name)
예제 #19
0
파일: models.py 프로젝트: tdruiva/baobaxia
 def where_is(self):
     from repository.models import git_annex_where_is
     
     data = git_annex_where_is(self)
     try:
         whereis = json.loads(data)
     except ValueError:
         logger.debug("Data error " + data)
         whereis = { 'whereis': [] }
     
     index = 0
     for item in whereis['whereis']:
         # strip [ or ]  (prevent errors of getting mucuas address like [dpadua])
         mucua_name = re.sub("[\[\]]", "", item['description']).split(' ')[0]
         whereis['whereis'][index]['description'] = mucua_name
         index += 1
     
     return whereis
예제 #20
0
    def get_territory(self, repository):
        u"""Retorna o territorio da mucua"""
        if not repository:
            try:
                repository = get_default_repository()
            except DatabaseError:
                return []

        groups = git_annex_group_list(repository.get_path(),
                                      self.uuid)
        territory = ''
        logger.debug('Grupos:')
        for group in groups:
            logger.debug('> %s', group)
            if group.startswith('t:'):
                territory = group

        return territory[2:]
예제 #21
0
    def handle(self, *args, **options):
        if len(args) == 0:
            medias = Media.objects.filter(is_local=False)
            logger.info("Atualizando todos as copias locais dos medias")
        else:
            since = int(args[0])
            some_day_ago = timezone.now().date() - timedelta(days=since)
            logger.info("Atualizando os medias desde o dia: " + str(some_day_ago))
            medias = Media.objects.filter(date__gte=some_day_ago)
        
        for media in medias:
            try:

                # Synchronize/update tags.  
                #
                # 1) Add all tags found in the git-annex metadata and not
                # already present on the media.
                # 2) If tags from other mucuas have been deleted (are missing in
                # the git_annex metadata tags), remove them from this media.
                tags_on_media = set(git_annex_list_tags(media))
                existing_tags = set((t.namespace, t.name) for t in media.tags.all())
                # Add new tags to media
                for t in tags_on_media - existing_tags:
                    # Add tag - search for existing, if none found create new tag.
                    namespace, name = t
                    try: 
                        tag = Tag.objects.get(name=unicode(name),
                                              namespace=unicode(namespace))
                    except Tag.DoesNotExist:
                        tag = Tag(name=name, namespace=namespace)
                        tag.save()
                    media.tags.add(tag)

                # Remove tags that were removed on remote media
                for t in existing_tags - tags_on_media:
                    namespace, name = t 
                    tag = Tag.objects.get(name=name, namespace=namespace)
                    media.tags.remove(tag) 

                media.save(is_syncing=True)

            except OSError, e:
                logger.debug('Requested media not found: ' + media.name)        
예제 #22
0
    def set_territory(self, territory, repository):
        u"""Define o territorio da mucua"""
        if not repository:
            try:
                repository = get_default_repository()
            except DatabaseError:
                return []

        actual_territory = self.get_territory(repository)
        if actual_territory != '':
            if territory.startswith('t:'):
                git_annex_group_add(repository.get_path(),
                                    self.get_description(),
                                    territory)
                return _("Mucua enraizada em " + territory)
            else:
                logger.debug('Not a territory.. should start with t:')
                return _("O territorio precisa ser indicado como 't:Nome_do_territorio'")
        else: 
            return _("A mucua esta já enraizada em " + actual_territory)
예제 #23
0
    def set_territory(self, territory, repository):
        u"""Define o territorio da mucua"""
        if not repository:
            try:
                repository = get_default_repository()
            except DatabaseError:
                return []

        actual_territory = self.get_territory(repository)
        if actual_territory != '':
            if territory.startswith('t:'):
                git_annex_group_add(repository.get_path(),
                                    self.get_description(), territory)
                return _("Mucua enraizada em " + territory)
            else:
                logger.debug('Not a territory.. should start with t:')
                return _(
                    "O territorio precisa ser indicado como 't:Nome_do_territorio'"
                )
        else:
            return _("A mucua esta já enraizada em " + actual_territory)
예제 #24
0
    def check_and_set_remotes(self):
        if get_default_mucua() == self.mucua:
            r_mucua = self.mucuia
        else:
            r_mucua = self.mucua
            
        remotes = discover()
        logger.debug("Mucuas" + str(remotes))
        access_URI = remotes.get(r_mucua.description, "")
        
        if access_URI != "":
            remote = access_URI
        elif r_mucua.uri_backend != "":
            remote = r_mucua.uri_backend
        else:
            remote = ""
        
        logger.debug("Remote" + str(remotes))

        try:
            repository = get_default_repository()
        except DatabaseError:
            pass

        git_remote_remove(r_mucua.description, repository.get_path())
        if remote != "" and self.is_active: 
            logger.debug("Adicionando " + str(remote) + " em " + str(repository.get_path()))
            git_remote_add(r_mucua.description, remote, repository.get_path())
예제 #25
0
    def check_and_set_remotes(self):
        if get_default_mucua() == self.mucua:
            r_mucua = self.mucuia
        else:
            r_mucua = self.mucua

        remotes = discover()
        logger.debug("Mucuas" + str(remotes))
        access_URI = remotes.get(r_mucua.description, "")

        if access_URI != "":
            remote = access_URI
        elif r_mucua.uri_backend != "":
            remote = r_mucua.uri_backend
        else:
            remote = ""

        logger.debug("Remote" + str(remotes))

        try:
            repository = get_default_repository()
        except DatabaseError:
            pass

        git_remote_remove(r_mucua.description, repository.get_path())
        if remote != "" and self.is_active:
            logger.debug("Adicionando " + str(remote) + " em " +
                         str(repository.get_path()))
            git_remote_add(r_mucua.description, remote, repository.get_path())
예제 #26
0
def git_annex_group_del(repository_path, mucua, group):
    u"""Remove a Mucua do grupo."""
    cmd = "git annex ungroup " + mucua + " " + group
    logger.debug("Command: " + cmd)
    pipe = subprocess.Popen(cmd, shell=True, cwd=repository_path, stdout=subprocess.PIPE)
    output, error = pipe.communicate()
    logger.debug(error)
    logger.debug(output)
    return output
예제 #27
0
def git_annex_where_is(media):
    u"""Mostra quais mucuas tem copia do media."""
    cmd = 'git annex whereis ' + media.get_file_name() + ' --json'
    logger.debug('Whereis filepath: ' + get_file_path(media) + media.get_file_name())
    pipe = subprocess.Popen(cmd, shell=True, cwd=get_file_path(media), stdout=subprocess.PIPE)
    output, error = pipe.communicate()
    logger.debug(error)
    logger.debug(output)
    return output
예제 #28
0
    def request_copy(self, save=True):
        u"""
        Gera um pedido de copia local do media

        Os pedidos tem um codigo uuid e são gravados em 
        /repository/mucua/requests/uuid

        O arquivo atualmente contem somente o caminho para o media no
        repositorio.

        """
        self.set_is_local()
        if not self.is_local:
            self.is_requested = True
            if save:
                self.save()
            try:
                requests_path = os.path.join(REPOSITORY_DIR,
                                             self.get_repository(),
                                             DEFAULT_MUCUA, 'requests')
                if not os.path.exists(requests_path):
                    os.makedirs(requests_path)

                request_filename = os.path.join(requests_path, self.uuid)
                logger.info("REQUESTING: " + request_filename)
                request_file = open(request_filename, 'a')
                request_file.write(self.media_file.path)
                request_file.close
                # TODO: Need to git add
                logger.debug("ADDING REQUEST: " +
                             os.path.basename(request_filename))
                logger.debug("ADDED ON: " + os.path.dirname(request_filename))
                from repository.models import git_add
                git_add(os.path.basename(request_filename),
                        os.path.dirname(request_filename))

            except IOError:
                logger.info(u'Alo! I can\'t write request file!')

                logger.debug("get_file_path: " + get_file_path(self))
                logger.debug("media_file.name: " +
                             os.path.basename(self.media_file.name))

            async_result = git_annex_get.delay(
                get_file_path(self), os.path.basename(self.media_file.name))
예제 #29
0
def git_annex_group_del(repository_path, mucua, group):
    u"""Remove a Mucua do grupo."""
    cmd = 'git annex ungroup ' + mucua + ' ' + group
    logger.debug("Command: " + cmd)
    pipe = subprocess.Popen(cmd,
                            shell=True,
                            cwd=repository_path,
                            stdout=subprocess.PIPE)
    output, error = pipe.communicate()
    logger.debug(error)
    logger.debug(output)
    return output
예제 #30
0
def git_annex_where_is(media):
    u"""Mostra quais mucuas tem copia do media."""
    cmd = 'git annex whereis ' + media.get_file_name() + ' --json'
    logger.debug('Whereis filepath: ' + get_file_path(media) +
                 media.get_file_name())
    pipe = subprocess.Popen(cmd,
                            shell=True,
                            cwd=get_file_path(media),
                            stdout=subprocess.PIPE)
    output, error = pipe.communicate()
    logger.debug(error)
    logger.debug(output)
    return output
예제 #31
0
    def request_copy(self, save=True):
        u"""
        Gera um pedido de copia local do media

        Os pedidos tem um codigo uuid e são gravados em 
        /repository/mucua/requests/uuid

        O arquivo atualmente contem somente o caminho para o media no
        repositorio.

        """
        self.set_is_local()
        if not self.is_local:
            self.is_requested = True
            if save:
                self.save()
            try:
                requests_path = os.path.join(REPOSITORY_DIR, self.get_repository(), 
                                                DEFAULT_MUCUA,
                                                'requests')
                if not os.path.exists(requests_path):
                    os.makedirs(requests_path)
                
                request_filename = os.path.join(requests_path, self.uuid)
                logger.info("REQUESTING: " + request_filename)
                request_file = open(request_filename, 'a')
                request_file.write(self.media_file.path)
                request_file.close
                # TODO: Need to git add
                logger.debug("ADDING REQUEST: " + os.path.basename(request_filename))
                logger.debug("ADDED ON: " + os.path.dirname(request_filename))
                from repository.models import git_add
                git_add(os.path.basename(request_filename), os.path.dirname(request_filename))
                
            except IOError:
                logger.info(u'Alo! I can\'t write request file!')
            
                logger.debug("get_file_path: " + get_file_path(self))
                logger.debug("media_file.name: " + os.path.basename(self.media_file.name))
        
            async_result = git_annex_get.delay(get_file_path(self), os.path.basename(self.media_file.name))
예제 #32
0
def git_annex_group_list(repository_path, group, mucua=None):
    u"""Lista todos os grupos ou de uma dada mucua"""
    if mucua == None:
        from mucua.models import Mucua

        mucuas = Mucua.objects.all()
        group_set = set()
        for mucua in mucuas:
            cmd = "git annex group " + mucua.get_description()
            logger.debug("Command: " + cmd)
            pipe = subprocess.Popen(cmd, shell=True, cwd=repository_path, stdout=subprocess.PIPE)
            output, error = pipe.communicate()
            logger.debug(error)
            logger.debug(output)
            if output != "":
                for group in output.split():
                    group_set.add(group)
        return list(group_set)
    else:
        cmd = "git annex group " + mucua.get_description()
        logger.debug("Command: " + cmd)
        pipe = subprocess.Popen(cmd, shell=True, cwd=repository_path, stdout=subprocess.PIPE)
        output, error = pipe.communicate()
        logger.debug(error)
        logger.debug(output)
        if output != "":
            return output.split()
        else:
            return []
예제 #33
0
    def authenticate(self, username=None, password=None):
        match = re.findall("(.*)@(.*)\.(.*)\.(.*)$", username)
        if match:
            (current_mocambola, current_mucua, current_repository,
             term) = match[0]
            # verifica se mucua e repositorio sao validos
            try:
                current_mucua = Mucua.objects.get(description=current_mucua)
            except Mucua.DoesNotExist:
                return None
            try:
                current_repository = Repository.objects.get(
                    name=current_repository)
            except Repository.DoesNotExist:
                return None
        else:
            print "invalid address"
            return None
        # Get file from MOCAMBOLA_DIR
        mocambola_path = os.path.join(str(REPOSITORY_DIR),
                                      str(current_repository),
                                      str(current_mucua), MOCAMBOLA_DIR)
        print "Mocambola Path: " + mocambola_path

        for jmocambola in os.listdir(mocambola_path):

            if jmocambola == username + '.json':
                # Deserialize the customized User object
                mocambola_json_file = open(
                    os.path.join(mocambola_path, jmocambola))
                data = JSONParser().parse(mocambola_json_file)
                u = User()
                serializer = UserSerializer(u, data=data)
                if serializer.errors:
                    logger.debug(u"%s %s" %
                                 (_('Error deserialing'), serializer.errors))
                serializer.is_valid()

                current_user = serializer.object
                login_valid = (username == current_user.username)
                pwd_valid = check_password(password, current_user.password)
                if login_valid and pwd_valid:
                    logger.info(u"%s %s %s" %
                                (_('User'), current_mocambola, _('logged in')))
                    try:
                        user = User.objects.get(username=username)
                    except User.DoesNotExist:
                        logger.debug(u"%s" %
                                     (_('Exception caught, UserDoesNotExist')))
                        # Create a new user. Note that we can set password
                        # to anything, because it won't be checked; the
                        # password from settings.py will.
                        user = User(username=username,
                                    password=current_user.password,
                                    is_staff=current_user.is_staff,
                                    is_superuser=current_user.is_superuser)
                        user.save()
                    return user
                else:
                    logger.info(u"%s %s %s" %
                                (_('User'), current_mocambola,
                                 _('doesn\'t exist or password is wrong!')))
                    return None
                return True
            # fim do if
        # fim do for
        return None
예제 #34
0
    def handle(self, *args, **options):
        for repository in args:
            try:
                repository_instance = Repository.objects.get(name=repository)
            except Repository.DoesNotExist:
                return False

            logger.debug('Repository sync started ... [sync_repository]')
            repository_instance.sync_repository()
            logger.debug('Repository sync ......... [update_mucua_list]')
            update_mucuas_list(repository_instance)
            logger.debug('Repository sync .... [create_user_from_files]')
            create_user_from_files(repository_instance)
            logger.debug('Repository sync . [create_objects_from_files]')
            create_objects_from_files(repository_instance)
            logger.debug('Repository sync ...... [remove_deleted_media]')
            remove_deleted_media(repository_instance)
            # Atualiza o arquivo lastSyncMark                                                                                                                                  
            path = os.path.join(REPOSITORY_DIR, repository_instance.name)
            output = subprocess.check_output(
                ["git", "log", "--pretty=format:'%H'", "-n 1"],
                cwd=path)
            logger.debug(u"%s: %s" % (_('Revision is'), output))
            logger.info('<<<')
            last_sync_mark = open(os.path.join(path, 'lastSync.txt'), 'w+')
            last_sync_mark.write(output)
            last_sync_mark.close()
    def handle(self, *args, **options):
        for repository in args:
            try:
                repository_instance = Repository.objects.get(name=repository)
            except Repository.DoesNotExist:
                return False

            logger.debug('Repository sync started ... [sync_repository]')
            repository_instance.sync_repository()
            logger.debug('Repository sync ......... [update_mucua_list]')
            update_mucuas_list(repository_instance)
            logger.debug('Repository sync .... [create_user_from_files]')
            create_user_from_files(repository_instance)
            logger.debug('Repository sync . [create_objects_from_files]')
            create_objects_from_files(repository_instance)
            logger.debug('Repository sync ...... [remove_deleted_media]')
            remove_deleted_media(repository_instance)
            # Atualiza o arquivo lastSyncMark
            path = os.path.join(REPOSITORY_DIR, repository_instance.name)
            output = subprocess.check_output(
                ["git", "log", "--pretty=format:'%H'", "-n 1"], cwd=path)
            logger.debug(u"%s: %s" % (_('Revision is'), output))
            logger.info('<<<')
            last_sync_mark = open(os.path.join(path, 'lastSync.txt'), 'w+')
            last_sync_mark.write(output)
            last_sync_mark.close()
예제 #36
0
    def handle(self, *args, **options):
        repository = args[0]
        mucua = args[1]
        # Here we copy mucuas requests from all "linked mucuas"
        linked_mucuas = git_remote_get_list(repository)
        requests_path = os.path.join(REPOSITORY_DIR, repository, mucua,
                                     "requests")
        if not os.path.exists(requests_path):
            os.makedirs(requests_path)

        for lmucua in linked_mucuas:
            lmucua_dir = os.path.join(REPOSITORY_DIR, repository, lmucua,
                                      "requests")
            if not os.path.isdir(lmucua_dir):
                os.mkdir(lmucua_dir)

            src_files = os.listdir(lmucua_dir)
            for file_name in src_files:
                full_file_name = os.path.join(lmucua_dir, file_name)
                if (os.path.isfile(full_file_name)):
                    shutil.copy(full_file_name, requests_path)
                    # TODO: Need to git add
                    logger.debug('ADDING REQUEST: ' +
                                 os.path.basename(full_file_name))
                    logger.debug('ADDED ON: ' + requests_path)
                    git_add(os.path.basename(full_file_name), requests_path)

        request_list = [uuid for uuid in os.listdir(requests_path)]

        for request_uuid in request_list:
            if os.path.isfile(os.path.join(requests_path, request_uuid)):
                try:
                    media = Media.objects.get(uuid=request_uuid)
                    media.set_is_local()
                    if media.is_local:
                        request_list = [
                            uuid for uuid in request_list
                            if uuid != request_uuid
                        ]
                        logger.debug('REMOVING REQUEST: ' + request_uuid)
                        git_rm(request_uuid, requests_path)
                        # Here we also git annex drop the file because
                        # it's only a "transport copy", couse of
                        # media.is_requested = False.
                        if not media.is_requested:
                            requests = []
                            # Check if we can drop, couse linked mucuas doesn't have pending request
                            for lmucua in linked_mucuas:
                                requests_path = os.path.join(
                                    REPOSITORY_DIR, repository, lmucua,
                                    "requests")
                                for name in os.listdir(requests_path):
                                    if os.path.isfile(
                                            os.path.join(requests_path, name)):
                                        requests.append(name)
                                        if request_uuid not in requests:
                                            # Check to see if origin mucua is not the current mucua
                                            if media.origin.description != mucua:
                                                # Doesn't work
                                                #git_annex_drop(media)
                                                pass
                        else:
                            media.is_requested = False
                        media.save()
                    else:
                        repository_path = os.path.join(REPOSITORY_DIR,
                                                       media.get_repository())
                        async_result = git_annex_get.delay(
                            get_file_path(media),
                            os.path.basename(media.media_file.name))
                        logger.debug(async_result.info)
                        media.save()
                except Media.DoesNotExist:
                    logger.debug(
                        'Requested media not found: {0}'.format(request_uuid))
예제 #37
0
    def handle(self, *args, **options):
        repository = args[0]
        mucua = args[1]
        # Here we copy mucuas requests from all "linked mucuas" 
        linked_mucuas = git_remote_get_list(repository)
        requests_path = os.path.join(REPOSITORY_DIR, repository, mucua, "requests")
        if not os.path.exists(requests_path):
            os.makedirs(requests_path)

        for lmucua in linked_mucuas:
            lmucua_dir = os.path.join(REPOSITORY_DIR, repository, lmucua, "requests")
            if not os.path.isdir(lmucua_dir):
                os.mkdir(lmucua_dir)
            
            src_files = os.listdir(lmucua_dir)
            for file_name in src_files:
                full_file_name = os.path.join(lmucua_dir, file_name)
                if (os.path.isfile(full_file_name)):
                    shutil.copy(full_file_name, requests_path)
                    # TODO: Need to git add
                    logger.debug('ADDING REQUEST: ' + os.path.basename(full_file_name))
                    logger.debug('ADDED ON: ' + requests_path)
                    git_add(os.path.basename(full_file_name), requests_path)

        request_list = [uuid for uuid in os.listdir(requests_path)]

        for request_uuid in request_list:
            if os.path.isfile(os.path.join(requests_path, request_uuid)):
                try:
                    media = Media.objects.get(uuid=request_uuid)
                    media.set_is_local()
                    if media.is_local:
                        request_list = [uuid for uuid in request_list if uuid != request_uuid]
                        logger.debug('REMOVING REQUEST: ' + request_uuid)
                        git_rm(request_uuid, requests_path)
                        # Here we also git annex drop the file because
                        # it's only a "transport copy", couse of
                        # media.is_requested = False.
                        if not media.is_requested:
                            requests = []
                            # Check if we can drop, couse linked mucuas doesn't have pending request
                            for lmucua in linked_mucuas:
                                requests_path = os.path.join(REPOSITORY_DIR, 
                                                             repository, lmucua, "requests")
                                for name in os.listdir(requests_path):
                                    if os.path.isfile(os.path.join(requests_path, name)):
                                        requests.append(name)                        
                                        if request_uuid not in requests:
                                            # Check to see if origin mucua is not the current mucua
                                            if media.origin.description != mucua:
                                                # Doesn't work 
                                                #git_annex_drop(media)
                                                pass
                        else: 
                            media.is_requested = False                    
                        media.save()
                    else:
                        repository_path = os.path.join(REPOSITORY_DIR, media.get_repository())
                        async_result = git_annex_get.delay(get_file_path(media), 
                                                           os.path.basename(media.media_file.name))
                        logger.debug(async_result.info)
                        media.save()
                except Media.DoesNotExist:
                    logger.debug('Requested media not found: {0}'.format(request_uuid))
예제 #38
0
def git_annex_group_list(repository_path, group, mucua=None):
    u"""Lista todos os grupos ou de uma dada mucua"""
    if mucua == None:
        from mucua.models import Mucua
        mucuas = Mucua.objects.all()
        group_set = set()
        for mucua in mucuas:
            cmd = 'git annex group ' + mucua.get_description()
            logger.debug("Command: " + cmd)
            pipe = subprocess.Popen(cmd,
                                    shell=True,
                                    cwd=repository_path,
                                    stdout=subprocess.PIPE)
            output, error = pipe.communicate()
            logger.debug(error)
            logger.debug(output)
            if output != '':
                for group in output.split():
                    group_set.add(group)
        return list(group_set)
    else:
        cmd = 'git annex group ' + mucua.get_description()
        logger.debug("Command: " + cmd)
        pipe = subprocess.Popen(cmd,
                                shell=True,
                                cwd=repository_path,
                                stdout=subprocess.PIPE)
        output, error = pipe.communicate()
        logger.debug(error)
        logger.debug(output)
        if output != '':
            return output.split()
        else:
            return []
예제 #39
0
def media_file_name(instance, file_name):
    """Retorna o caminho, completo de nome, do media"""
    logger.debug(
        os.path.join(get_file_path(instance) + instance.get_file_name()))
    return os.path.join(get_file_path(instance), instance.get_file_name())
예제 #40
0
def media_file_name(instance, file_name):
    """Retorna o caminho, completo de nome, do media"""
    logger.debug(os.path.join(get_file_path(instance) + instance.get_file_name()))
    return os.path.join(get_file_path(instance), instance.get_file_name())