def handle(self, *args, **options):
     # Check latest commit/update of the repo and if it differs from
     # the existing last_repo_update, save the new update date.
     model_refs = ModelReference.objects.all()
     for model_ref in model_refs:
         last_update = get_last_update_date(model_ref)
         if not last_update == model_ref.last_repo_update:
             model_ref.last_repo_update = last_update
             model_ref.save()
             sys.stdout.write("Updated last_repo_date field of model "
                              "reference %s.\n" % model_ref)
Beispiel #2
0
    def process(self):
        if self.is_processed:
            logger.error("Already processed %s." % self)
            return
        try:
            z = zipfile.ZipFile(self.file_path)
        except zipfile.BadZipfile:
            logger.exception("File is not a zip file: %s" % self.file_path)
            return
        except IOError:
            logger.exception("Zip file path probably not found.")
            return

        extract_to = os.path.join(
            settings.MODEL_DATABANK_ZIP_EXTRACT_PATH, str(self.id))
        z.extractall(path=extract_to)

        # create repo dir if not exist
        if not self.model_reference:
            # convert to hg repo
            os.chdir(extract_to)
            subprocess.call([settings.HG_CMD, 'init'])
            # add the .hgignore file
            add_hgignore_file(extract_to)
            # loop through files and add files from largefiles extensions
            largefiles_file_paths = get_largefiles_file_paths(extract_to)
            for fp in largefiles_file_paths:
                # this is needed even when a largefiles patterns entry
                # is added to ~/.hgrc
                subprocess.call([settings.HG_CMD, 'add', '--large', fp])
                logger.info("Added %s as large file to repository\n" % fp)

            subprocess.call([settings.HG_CMD, 'add'])
            subprocess.call([settings.HG_CMD, 'commit', '-m',
                             'Initial commit.', '--user', 'Model Databank'])
            # make it a bare repository (i.e. without a working copy)
            subprocess.call([settings.HG_CMD, 'update', 'null'])
            # if we got here, create a ModelReference with uuid for the
            # repo dir naming
            model_reference = ModelReference(
                type=self.model_type,
                owner=self.uploaded_by,
                identifier=self.identifier,
                description=self.description,
                organisation=self.organisation)
            model_reference.save()

            shutil.move(extract_to, model_reference.repository)

            # create symlink to the repository
            try:
                os.symlink(model_reference.repository,
                           model_reference.symlink)
            except OSError:
                logger.exception("Failed to create symlink for repository. "
                                 "Could it be a Windows directory?")
                # TODO: rollback?
                # Call model_reference.delete(), which should be overriden.
                # Override model_reference.delete() by setting `deleted`
                # boolean field to True. This is done for restoring purposes.
                # But perhaps in this case it should be deleted totally.
                # Perhaps by creating a model
                return
            else:
                self.model_reference = model_reference
                self.is_processed = True
                self.save()

                # set the last repo update date
                last_repo_update = get_last_update_date(model_reference)
                model_reference.last_repo_update = last_repo_update
                model_reference.save()

                return self