示例#1
0
 def test_force_list(self):
     self.assertEqual(force_list("a"), ["a"])
     self.assertEqual(force_list(["a"]), ["a"])
     self.assertEqual(force_list(["a", "b"]), ["a", "b"])
     self.assertItemsEqual(force_list({
         'a': True,
         'b': True
     }.keys()), ["a", "b"])
     self.assertItemsEqual(force_list(set(["a", "b"])), ["a", "b"])
示例#2
0
文件: pkgset.py 项目: kdudka/kobo
    def compute_checksums(self, checksum_types):
        """Compute and cache checksums of given types."""

        result = {}
        missing = []
        checksum_types = force_list(checksum_types)

        for checksum_type in checksum_types:
            if checksum_type in self._checksums:
                result[checksum_type] = self._checksums[checksum_type]
            else:
                missing.append(checksum_type)

        if missing:
            result.update(compute_file_checksums(self.file_path, missing))
            self._checksums.update(result)

        return result
示例#3
0
    def compute_checksums(self, checksum_types):
        """Compute and cache checksums of given types."""

        result = {}
        missing = []
        checksum_types = force_list(checksum_types)

        for checksum_type in checksum_types:
            if checksum_type in self._checksums:
                result[checksum_type] = self._checksums[checksum_type]
            else:
                missing.append(checksum_type)

        if missing:
            result.update(compute_file_checksums(self.file_path, missing))
            self._checksums.update(result)

        return result
示例#4
0
文件: task.py 项目: tkopecek/kobo
    def wait(self, subtasks=None):
        """Wait until subtasks finish.

        subtasks = None - wait for all subtasks
        subtasks = [task_id list] - wait for selected subtasks
        """

        if self.foreground:
            # wait would call signal.pause() in the *main* worker thread and lock program forever
            raise RuntimeError("Foreground tasks can't wait on subtasks.")

        if subtasks is not None:
            subtasks = force_list(subtasks)

        self.hub.worker.wait(self.task_id, subtasks)

        finished = []
        while True:
            (finished, unfinished) = self.hub.worker.check_wait(self.task_id)

            if len(unfinished) == 0:
                # all done
                break

            # sleep
            signal.pause()
            # wake up on signal to check the status

        # remove finished subtasks from the list, check results
        fail = False
        for i in finished:
            state = self.hub.worker.get_task(i)
            if state['state'] != TASK_STATES['CLOSED']:
                fail = True
            self._subtask_list.remove(i)

        if fail:
            print(
                "Failing because of at least one subtask hasn't closed properly."
            )
            self.fail()

        return finished
示例#5
0
    def wait(self, subtasks=None):
        """Wait until subtasks finish.

        subtasks = None - wait for all subtasks
        subtasks = [task_id list] - wait for selected subtasks
        """

        if self.foreground:
            # wait would call signal.pause() in the *main* worker thread and lock program forever
            raise RuntimeError("Foreground tasks can't wait on subtasks.")

        if subtasks is not None:
            subtasks = force_list(subtasks)

        self.hub.worker.wait(self.task_id, subtasks)

        finished = []
        while True:
            (finished, unfinished) = self.hub.worker.check_wait(self.task_id)

            if not unfinished:
                # all done
                break

            # sleep
            signal.pause()
            # wake up on signal to check the status

        # remove finished subtasks from the list, check results
        fail = False
        for i in finished:
            state = self.hub.worker.get_task(i)
            if state['state'] != TASK_STATES['CLOSED']:
                fail = True
            self._subtask_list.remove(i)

        if fail:
            print("Failing because of at least one subtask hasn't closed properly.")
            self.fail()

        return finished
示例#6
0
文件: pkgset.py 项目: kdudka/kobo
 def remove_by_filenames(self, file_names):
     file_names = [ os.path.basename(i) for i in force_list(file_names) ]
     for i in dict(self.file_cache):
         if os.path.basename(i) in file_names:
             self.remove(i)
示例#7
0
    def run(self, *args, **kwargs):
        prog = self.parser.get_prog_name()
        print(".. -*- coding: utf-8 -*-\n")
        print("=" * len(prog))
        print(prog)
        print("=" * len(prog), "\n")

        # add subtitle (command description)
        description = getattr(self.parser.container, "_description", None)
        if description:
            print(":Subtitle: %s\n" % description)

        # add copyright
        copyright = getattr(self.parser.container, "_copyright", None)
        if copyright:
            print(":Copyright: %s" % copyright)

        # add date
        print(":Date: $Date: %s $\n" % datetime.datetime.strftime(
            datetime.datetime.utcnow(), format="%F %X"))

        print("--------")
        print("COMMANDS")
        print("--------")

        for command_name, CommandClass in sorted(
                self.parser.container.plugins.items()):
            parser = optparse.OptionParser(usage=self.parser.usage)
            cmd = CommandClass(parser)
            cmd.normalized_name = command_name
            cmd.options()
            cmd.container = self.parser.container
            cmd_opts, cmd_args = parser.parse_args()

            print(command_name)
            print("-" * len(command_name))

            if cmd.admin:
                print("[ADMIN ONLY]", end=' ')

            print(cmd.__doc__.strip(), end="\n\n")
            usage = parser.get_usage().strip().replace("Usage: ",
                                                       "**Usage:** ", 1)
            if usage:
                print(usage, end="\n\n")

            for opt in sorted(parser.option_list,
                              lambda x, y: cmp(str(x), str(y))):
                if "-h/--help" in str(opt):
                    continue
                if opt.nargs:
                    metavar = opt.metavar or opt.dest.upper()
                opt_list = []
                for opt_str in opt._short_opts + opt._long_opts:
                    if opt.nargs is not None:
                        opt_list.append("%s=%s" % (opt_str, metavar))
                    else:
                        opt_list.append(opt_str)
                print("/".join(opt_list))
                print("  %s" % opt.help)
                if opt.action == "append":
                    print("\n  This option can be specified multiple times")
                print()
            print()

        # handle :Contact: and :Author: ourselves
        authors = force_list(getattr(self.parser.container, "_authors", []))
        contact = getattr(self.parser.container, "_contact", None)
        if authors or contact:
            print("-------")
            print("AUTHORS")
            print("-------")

            for author in sorted(authors):
                print("- %s\n" % author)

            if contact:
                print("**Contact:** %s\n" % contact)
示例#8
0
 def test_force_list(self):
     self.assertEqual(force_list("a"), ["a"])
     self.assertEqual(force_list(["a"]), ["a"])
     self.assertEqual(force_list(["a", "b"]), ["a", "b"])
     self.assertItemsEqual(force_list(set(["a", "b"])), ["a", "b"])
示例#9
0
文件: cli.py 项目: atodorov/kobo
    def run(self, *args, **kwargs):
        prog = self.parser.get_prog_name()
        print ".. -*- coding: utf-8 -*-"
        print
        print "=" * len(prog)
        print prog
        print "=" * len(prog)
        print

        # add subtitle (command description)
        description = getattr(self.parser.container, "_description", None)
        if description:
            print ":Subtitle: %s" % description
            print

        # add copyright
        copyright = getattr(self.parser.container, "_copyright", None)
        if copyright:
            print ":Copyright: %s" % copyright

        # add date
        print ":Date: $Date: %s $" % datetime.datetime.strftime(datetime.datetime.utcnow(), format="%F %X")
        print

        print "--------"
        print "COMMANDS"
        print "--------"

        for command_name, CommandClass in sorted(self.parser.container.plugins.items()):
            parser = optparse.OptionParser(usage=self.parser.usage)
            cmd = CommandClass(parser)
            cmd.normalized_name = command_name
            cmd.options()
            cmd.container = self.parser.container
            cmd_opts, cmd_args = parser.parse_args()

            print command_name
            print "-" * len(command_name)

            if cmd.admin:
                print "[ADMIN ONLY]",

            print cmd.__doc__.strip()
            print
            usage = parser.get_usage().strip().replace("Usage: ", "**Usage:** ", 1)
            if usage:
                print usage
                print 

            for opt in sorted(parser.option_list, lambda x, y: cmp(str(x), str(y))):
                if "-h/--help" in str(opt):
                    continue
                if opt.nargs:
                    metavar = opt.metavar or opt.dest.upper()
                opt_list = []
                for opt_str in opt._short_opts + opt._long_opts:
                    if opt.nargs is not None:
                        opt_list.append("%s=%s" % (opt_str, metavar))
                    else:
                        opt_list.append(opt_str)
                print "/".join(opt_list)
                print "  %s" % opt.help
                if opt.action == "append":
                    print
                    print "  This option can be specified multiple times"
                print
            print

        # handle :Contact: and :Author: ourselves
        authors = force_list(getattr(self.parser.container, "_authors", []))
        contact = getattr(self.parser.container, "_contact", None)
        if authors or contact:
            print "-------"
            print "AUTHORS"
            print "-------"

            for author in sorted(authors):
                print "- %s" % author
            print

            if contact:
                print "**Contact:** %s" % contact
                print
示例#10
0
 def remove_by_filenames(self, file_names):
     file_names = [os.path.basename(i) for i in force_list(file_names)]
     for i in dict(self.file_cache):
         if os.path.basename(i) in file_names:
             self.remove(i)
示例#11
0
 def test_force_list(self):
     self.assertEqual(force_list("a"), ["a"])
     self.assertEqual(force_list(["a"]), ["a"])
     self.assertEqual(force_list(["a", "b"]), ["a", "b"])
     self.assertItemsEqual(force_list(set(["a", "b"])), ["a", "b"])