Example #1
0
    def test_compute_sha1_hash(self):
        """
        Test compute_sha1_hash's invariance.
        """

        first_string = "Jackdaws love my big sphinx of quartz."
        second_string = "Watch \"Jeopardy!\", Alex Trebek's fun TV quiz game."

        tempdir = tempfile.mkdtemp()
        with open(os.path.join(tempdir, "one.txt"), "w") as f:
            f.write(first_string)
        with open(os.path.join(tempdir, "two.txt"), "w") as f:
            f.write(second_string)
        first = compute_sha1_hash(tempdir)
        shutil.rmtree(tempdir)

        # Again, but in the other order.
        tempdir = tempfile.mkdtemp()
        with open(os.path.join(tempdir, "two.txt"), "w") as f:
            f.write(second_string)
        with open(os.path.join(tempdir, "one.txt"), "w") as f:
            f.write(first_string)
        second = compute_sha1_hash(tempdir)
        shutil.rmtree(tempdir)

        self.assertEqual(first, second)
Example #2
0
    def read_task_package(self, pkg_name):
        """
        Read a `TaskPackage` directory from the task directory and import it
        into the internal task directory.

        This method may emit TASK_ADDED or TASK_UPDATED depending on the task
        it processes.

        Any pending callbacks on the task will be executed after this method
        runs.

        This method chews CPU because it runs hashes.

        :Parameters:
            pkg_name
                Name of the package.

        :returns: A `TaskPackage`, or None if the task is not loaded.
        """

        # this method is slow in finding updates of tasks
        with self._lock:
            pkg_dir = self.get_package_location(pkg_name)
            signal = None
            sha1_hash = packaging.compute_sha1_hash(pkg_dir)
            internal_folder = os.path.join(self.tasks_dir_internal,
                    pkg_name, sha1_hash)

            pkg = self.registry.get((pkg_name, None), None)
            if not pkg or pkg.version != sha1_hash:
                # copy this folder to tasks_dir_internal
                try:
                    shutil.copytree(pkg_dir, internal_folder)
                except OSError:
                    # already in tree, just update the timestamp so it shows
                    # as the newest version
                    os.utime('%s' % (internal_folder), None)
                    logger.warn('Package %s v%s already exists' % (pkg_name,
                                sha1_hash))
            # find updates
            if (pkg_name, None) not in self.registry:
                signal = 'TASK_ADDED'
            elif sha1_hash != self.registry[pkg.name, None].version:
                signal = 'TASK_UPDATED'

        if signal:
            pkg = self.init_package(pkg_name, sha1_hash)
            self.emit(signal, pkg_name)
            return pkg
        return None
Example #3
0
    def read_task_package(self, pkg_name):
        """
        Reads the directory corresponding to a TaskPackage from TASKS_DIR and
        imports it into TASKS_DIR_INTERNAL.  Tasks have a hash computed that is
        used as the version.  Imported packages are also added to the registry
        and emit TASK_ADDED or TASK_UPDATED
        
        After updating or adding a new task, any callbacks pending for the
        tasks (ie. task waiting for sync) will be executed.
        
        This method is CPU intensive as all files within the package are SHA1
        hashed
        
        @param pkg_name - name of package, also the root directory.
        @returns TaskPackage if loaded, otherwise None
        """
        # this method is slow in finding updates of tasks
        with self._lock:
            pkg_dir = self.get_package_location(pkg_name)
            signal = None
            sha1_hash = packaging.compute_sha1_hash(pkg_dir)
            internal_folder = os.path.join(self.tasks_dir_internal,
                    pkg_name, sha1_hash)

            pkg = self.registry.get((pkg_name, None), None)
            if not pkg or pkg.version <> sha1_hash:
                # copy this folder to tasks_dir_internal
                try:
                    shutil.copytree(pkg_dir, internal_folder)
                except OSError:
                    # already in tree, just update the timestamp so it shows
                    # as the newest version
                    os.utime('%s' % (internal_folder), None)
                    logger.warn('Package %s v%s already exists' % (pkg_name,
                                sha1_hash))
            # find updates
            if (pkg_name, None) not in self.registry:
                signal = 'TASK_ADDED'
                updated = True
            elif sha1_hash <> self.registry[pkg.name, None].version:
                signal = 'TASK_UPDATED'

        if signal:
            pkg = self.init_package(pkg_name, sha1_hash)
            self.emit(signal, pkg_name)
            return pkg
        return None