1*eb8dc403SDave Cobbleyimport argparse
2*eb8dc403SDave Cobbley
3*eb8dc403SDave Cobbleyalready_loaded = False
4*eb8dc403SDave Cobbleykept_context = None
5*eb8dc403SDave Cobbley
6*eb8dc403SDave Cobbleydef plugin_name(filename):
7*eb8dc403SDave Cobbley    return os.path.splitext(os.path.basename(filename))[0]
8*eb8dc403SDave Cobbley
9*eb8dc403SDave Cobbleydef plugin_init(plugins):
10*eb8dc403SDave Cobbley    global already_loaded
11*eb8dc403SDave Cobbley    already_loaded = plugin_name(__file__) in (plugin_name(p.__name__) for p in plugins)
12*eb8dc403SDave Cobbley
13*eb8dc403SDave Cobbleydef print_name(args, config, basepath, workspace):
14*eb8dc403SDave Cobbley    print (__file__)
15*eb8dc403SDave Cobbley
16*eb8dc403SDave Cobbleydef print_bbdir(args, config, basepath, workspace):
17*eb8dc403SDave Cobbley    print (__file__.replace('/lib/devtool/bbpath.py',''))
18*eb8dc403SDave Cobbley
19*eb8dc403SDave Cobbleydef print_registered(args, config, basepath, workspace):
20*eb8dc403SDave Cobbley    global kept_context
21*eb8dc403SDave Cobbley    print(kept_context.loaded)
22*eb8dc403SDave Cobbley
23*eb8dc403SDave Cobbleydef multiloaded(args, config, basepath, workspace):
24*eb8dc403SDave Cobbley    global already_loaded
25*eb8dc403SDave Cobbley    print("yes" if already_loaded else "no")
26*eb8dc403SDave Cobbley
27*eb8dc403SDave Cobbleydef register_commands(subparsers, context):
28*eb8dc403SDave Cobbley    global kept_context
29*eb8dc403SDave Cobbley    kept_context = context
30*eb8dc403SDave Cobbley    if 'loaded' in context.__dict__:
31*eb8dc403SDave Cobbley        context.loaded += 1
32*eb8dc403SDave Cobbley    else:
33*eb8dc403SDave Cobbley        context.loaded = 1
34*eb8dc403SDave Cobbley
35*eb8dc403SDave Cobbley    def addparser(name, helptxt, func):
36*eb8dc403SDave Cobbley        parser = subparsers.add_parser(name, help=helptxt,
37*eb8dc403SDave Cobbley                                       formatter_class=argparse.ArgumentDefaultsHelpFormatter)
38*eb8dc403SDave Cobbley        parser.set_defaults(func=func)
39*eb8dc403SDave Cobbley        return parser
40*eb8dc403SDave Cobbley
41*eb8dc403SDave Cobbley    addparser('pluginfile', 'Print the filename of this plugin', print_name)
42*eb8dc403SDave Cobbley    addparser('bbdir', 'Print the BBPATH directory of this plugin', print_bbdir)
43*eb8dc403SDave Cobbley    addparser('count', 'How many times have this plugin been registered.', print_registered)
44*eb8dc403SDave Cobbley    addparser('multiloaded', 'How many times have this plugin been initialized', multiloaded)
45