Example #1
0
def git_media_post_save(instance, **kwargs):
    u"""Intercepta o sinal de *post_save* de objetos multimedia (*media*) e
    adiciona o objeto ao repositório."""
    if not instance.is_syncing:
        from media.serializers import MediaFileSerializer

        git_annex_add(instance.get_file_name(), get_file_path(instance))
        serializer = MediaFileSerializer(instance)
        mediapath = get_file_path(instance) + "/"
        mediadata = os.path.splitext(instance.get_file_name())[0] + ".json"
        fout = open(mediapath + mediadata, "w")
        fout.write(str(serializer.getJSON()))
        fout.close()

        git_add(mediadata, mediapath)

        from mucua.models import get_default_mucua
        from media.views import add_and_synchronize_tags

        tags = [t.name for t in instance.tags.all()]
        add_and_synchronize_tags(instance, tags, get_default_mucua())

        git_commit(
            instance.get_file_name(),
            instance.author.username,
            instance.author.email,
            get_file_path(instance),
            os.path.join(mediapath, mediadata),
        )
Example #2
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
Example #3
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
Example #4
0
def git_media_post_delete(instance, **kwargs):
    u"""Intercepta o sinal de *pre_delete* de objetos multimedia (*media*) e
    remove o objeto do banco e do repositório."""
    mediapath = get_file_path(instance) + '/'
    mediadata = os.path.splitext(instance.get_file_name())[0] + '.json'
    if os.path.isfile(os.path.join(mediapath, mediadata)):
        git_annex_drop(instance)
        git_rm(mediadata, mediapath)
    if os.path.isfile(os.path.join(mediapath, instance.get_file_name())):
        git_rm(instance.get_file_name(), mediapath)
        git_commit(instance.get_file_name(), instance.author.username,
                   instance.author.email, get_file_path(instance),
                   instance.get_repository())
Example #5
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
Example #6
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
Example #7
0
def git_media_post_save(instance, **kwargs):
    u"""Intercepta o sinal de *post_save* de objetos multimedia (*media*) e
    adiciona o objeto ao repositório."""
    from media.serializers import MediaFileSerializer
    git_annex_add(instance.get_file_name(), get_file_path(instance))
    serializer = MediaFileSerializer(instance)
    mediapath = get_file_path(instance) + '/'
    mediadata = os.path.splitext(instance.get_file_name())[0] + '.json'
    fout = open(mediapath + mediadata, 'w')
    fout.write(str(serializer.getJSON()))
    fout.close()
    git_add(mediadata, mediapath)
    git_commit(instance.get_file_name(), instance.author.username,
               instance.author.email, get_file_path(instance))
Example #8
0
def git_media_post_delete(instance, **kwargs):
    u"""Intercepta o sinal de *pre_delete* de objetos multimedia (*media*) e
    remove o objeto do banco e do repositório."""
    mediapath = get_file_path(instance)+'/'
    mediadata = os.path.splitext(instance.get_file_name())[0] + '.json'
    if os.path.isfile(os.path.join(mediapath, mediadata)): 
        git_annex_drop(instance)    
        git_rm(mediadata, mediapath)
    if os.path.isfile(os.path.join(mediapath, instance.get_file_name())):
        git_rm(instance.get_file_name(), mediapath)
        git_commit(instance.get_file_name(),
                   instance.author.username,
                   instance.author.email,
                   get_file_path(instance),
                   instance.get_repository())
Example #9
0
def git_media_post_save(instance, **kwargs):
    u"""Intercepta o sinal de *post_save* de objetos multimedia (*media*) e
    adiciona o objeto ao repositório."""
    from media.serializers import MediaFileSerializer
    git_annex_add(instance.get_file_name(), get_file_path(instance))
    serializer = MediaFileSerializer(instance)
    mediapath = get_file_path(instance)+'/'
    mediadata = os.path.splitext(instance.get_file_name())[0] + '.json'
    fout = open(mediapath + mediadata, 'w')
    fout.write(str(serializer.getJSON()))
    fout.close()
    git_add(mediadata, mediapath)
    git_commit(instance.get_file_name(),
              instance.author.username,
              instance.author.email,
              get_file_path(instance))
Example #10
0
def git_media_post_save(instance, **kwargs):
    u"""Intercepta o sinal de *post_save* de objetos multimedia (*media*) e
    adiciona o objeto ao repositório."""
    if not instance.is_syncing:
        from media.serializers import MediaFileSerializer
        git_annex_add(instance.get_file_name(), get_file_path(instance))
        serializer = MediaFileSerializer(instance)
        mediapath = get_file_path(instance) + '/'
        mediadata = os.path.splitext(instance.get_file_name())[0] + '.json'
        fout = open(mediapath + mediadata, 'w')
        fout.write(str(serializer.getJSON()))
        fout.close()

        git_add(mediadata, mediapath)

        from mucua.models import get_default_mucua
        from media.views import add_and_synchronize_tags
        tags = [t.name for t in instance.tags.all()]
        add_and_synchronize_tags(instance, tags, get_default_mucua())

        git_commit(instance.get_file_name(), instance.author.username,
                   instance.author.email, get_file_path(instance),
                   os.path.join(mediapath, mediadata))
Example #11
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
Example #12
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
Example #13
0
def git_annex_remove_tag(media, namespace, tag):
    u"""Apagar uma etiqueta do media."""
    git_annex_metadata_del(media.get_file_name(), get_file_path(media), namespace, tag)
Example #14
0
def git_annex_add_tag(media, namespace, tag):
    u"""Adicionar uma etiqueta ao media."""
    if tag.isspace() or not tag:
        raise RuntimeError("Attempt to set empty tag!")
    git_annex_metadata_add(media.get_file_name(), get_file_path(media), namespace, tag)
Example #15
0
def git_annex_add_tag(media, namespace, tag):
    u"""Adicionar uma etiqueta ao media."""
    if tag.isspace() or not tag:
        raise RuntimeError("Attempt to set empty tag!")
    git_annex_metadata_add(media.get_file_name(), get_file_path(media),
                           namespace, tag)
Example #16
0
def git_annex_where_is(media):
    u"""Mostra quais mucuas tem copia do media."""
    cmd = "git annex whereis " + media.get_file_name() + " --json"
    pipe = subprocess.Popen(cmd, shell=True, cwd=get_file_path(media), stdout=subprocess.PIPE)
    output, error = pipe.communicate()
    return output
Example #17
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))
Example #18
0
def git_annex_remove_tag(media, namespace, tag):
    u"""Apagar uma etiqueta do media."""
    git_annex_metadata_del(media.get_file_name(), get_file_path(media),
                           namespace, tag)
Example #19
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))