<commit_before># (C) 2015 - <NAME>.
#
# Mozilla Public License 2.0
#
# Python dbus service that faces SOTA interface 
# of Software Loading manager.


import gtk
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop



class SOTAClientService(dbus.service.Object):
    def __init__(self):
        self.bus = dbus.SessionBus()
        self.sota_bus_name = dbus.service.BusName('org.genivi.sota_client', bus=self.bus)
        dbus.service.Object.__init__(self, 
                                     self.sota_bus_name, 
                                     '/org/genivi/sota_client')

        self.slm_bus_name = dbus.service.BusName('org.genivi.software_loading_manager', bus=self.bus)
        slm_obj = self.bus.get_object(self.slm_bus_name.get_name(), 
                                      '/org/genivi/software_loading_manager')

        self.package_available_dbus = slm_obj.get_dbus_method('package_available', 
                                                              'org.genivi.software_loading_manager')

        self.download_complete_dbus = slm_obj.get_dbus_method('download_complete', 
                                                              'org.genivi.software_loading_manager')

        self.installation_report = slm_obj.get_dbus_method('installation_report', 
                                                           'org.genivi.software_loading_manager')

        self.get_installed_packages_dbus = slm_obj.get_dbus_method('get_installed_packages', 
                                                                   'org.genivi.software_loading_manager')

    def handle_reply(self, r):
        print "Got reply", r

    def handle_error(self, e):
        print "Got error", e

    def package_available(self, *args):
        self.package_available_dbus(*args,
                                    reply_handler = self.handle_reply,
                                    error_handler = self.handle_error)


    def download_complete(self, *args):
        self.download_complete_dbus(*args)


    @dbus.service.method('org.genivi.sota_client')
    def initiate_download(self, 
                          package_id,
                          major,
                          minor,
                          patch): 
        print "Got initiate_download"
        print "  ID:     {}".format(package_id)
        print "  ver:    {}.{}.{} ".format(major, minor, patch)
        print "---"
        self.download_complete("/nev/dull", 
                               package_id, 
                               major, 
                               minor, 
                               patch, 
                               "install", 
                               47114711, 
                               "Linux Kernel - Now with 6502 support",
                               "Linux Foundation",
                               "package_manager")
        return True
    
    @dbus.service.method('org.genivi.sota_client')
    def installation_report(self,
                            package_id, 
                            major,
                            minor,
                            patch,
                            result,
                            desc):
        print "Installation report"
        print "  ID:     {}".format(package_id)
        print "  ver:    {}.{}.{} ".format(major, minor, patch)
        print "  result: {}".format(result)
        print "  descr:  {}".format(desc)
        print "---"
        return True

    @dbus.service.method('org.genivi.sota_client')
    def get_installed_packages(self): 
        print "Got get_installed_packages()"
        return [ { 'package_id': 'bluez_driver', 
                   'major': 1,
                   'minor': 2,
                   'patch': 3 },
                 { 'package_id': 'bluez_apps', 
                   'major': 3,
                   'minor': 2,
                   'patch': 1 } ]
                 



DBusGMainLoop(set_as_default=True)
sota_svc = SOTAClientService()

# Get things started
sota_svc.package_available('linux_kernel', 
                         7,8,9, 
                         "install", 
                         47114711, 
                         "Linux Kernel - Now with 6502 support",
                         "Linux Foundation",
                           "package_manager")
<commit_msg>Changed sota_client to be command line parameter-driven<commit_after># (C) 2015 - <NAME>.
#
# Mozilla Public License 2.0
#
# Python dbus service that faces SOTA interface 
# of Software Loading manager.


import gtk
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
import getopt
import sys


class SOTAClientService(dbus.service.Object):
    def __init__(self):
        self.bus = dbus.SessionBus()
        self.sota_bus_name = dbus.service.BusName('org.genivi.sota_client', bus=self.bus)
        dbus.service.Object.__init__(self, 
                                     self.sota_bus_name, 
                                     '/org/genivi/sota_client')

        self.slm_bus_name = dbus.service.BusName('org.genivi.software_loading_manager', bus=self.bus)
        slm_obj = self.bus.get_object(self.slm_bus_name.get_name(), 
                                      '/org/genivi/software_loading_manager')

        self.package_available_dbus = slm_obj.get_dbus_method('package_available', 
                                                              'org.genivi.software_loading_manager')

        self.download_complete_dbus = slm_obj.get_dbus_method('download_complete', 
                                                              'org.genivi.software_loading_manager')

        self.installation_report = slm_obj.get_dbus_method('installation_report', 
                                                           'org.genivi.software_loading_manager')

        self.get_installed_packages_dbus = slm_obj.get_dbus_method('get_installed_packages', 
                                                                   'org.genivi.software_loading_manager')

    def handle_reply(self, r):
        print "Got reply", r

    def handle_error(self, e):
        print "Got error", e

    def package_available(self, *args):
        self.package_available_dbus(*args,
                                    reply_handler = self.handle_reply,
                                    error_handler = self.handle_error)


    def download_complete(self, *args):
        self.download_complete_dbus(*args)


    @dbus.service.method('org.genivi.sota_client')
    def initiate_download(self, 
                          package_id,
                          major,
                          minor,
                          patch): 
        print "Got initiate_download"
        print "  ID:     {}".format(package_id)
        print "  ver:    {}.{}.{} ".format(major, minor, patch)
        print "---"
        self.download_complete("/nev/dull", 
                               package_id, 
                               major, 
                               minor, 
                               patch, 
                               "install", 
                               47114711, 
                               "Linux Kernel - Now with 6502 support",
                               "Linux Foundation",
                               "package_manager")
        return True
    
    @dbus.service.method('org.genivi.sota_client')
    def installation_report(self,
                            package_id, 
                            major,
                            minor,
                            patch,
                            result,
                            desc):
        print "Installation report"
        print "  ID:     {}".format(package_id)
        print "  ver:    {}.{}.{} ".format(major, minor, patch)
        print "  result: {}".format(result)
        print "  descr:  {}".format(desc)
        print "---"
        return True

    @dbus.service.method('org.genivi.sota_client')
    def get_installed_packages(self): 
        print "Got get_installed_packages()"
        return [ { 'package_id': 'bluez_driver', 
                   'major': 1,
                   'minor': 2,
                   'patch': 3 },
                 { 'package_id': 'bluez_apps', 
                   'major': 3,
                   'minor': 2,
                   'patch': 1 } ]
                 


def usage():
    print "Usage:", sys.argv[0], "[-p package_id] [-v major.minor.patch] \\"
    print "                      [-t target] [-c command] [-s size] \\"
    print "                      [-d description] [-V vendor]"
    print
    print "  -p package_id        Pacakage id string. Default: 'bluez'"
    print "  -v major.minor.patch Version of package. Default: '1.2.3'"
    print "  -t target            Target installer. package | partition | module_loader"
    print "                       Default: 'package'"
    print "  -c command           install | upgrade | remove. Default: 'install'"
    print "  -s size              Package size in bytes. Default: '1000000'"
    print "  -d description       Package description. Default: 'Bluez stack'"
    print "  -V vendor            Package vendor. Default: 'Bluez project'"
    print
    print "Example:", sys.argv[0],"-n http://rvi1.nginfotpdx.net:8801 \\"
    print "                      jlr.com/vin/aaron/4711/test/ping \\"
    print "                      arg1=val1 arg2=val2"                    

    sys.exit(255)


opts, args= getopt.getopt(sys.argv[1:], "p:v:t:c:s:d:V:")
package_id='bluez'
major=1
minor=2
patch=3
target='package'
command='install'
size=1000000
description='bluez stack'
vendor='Bluez project'

for o, a in opts:
    if o == "-p":
        package_id = a
    elif o == "-v":
        [major,minor,patch] = a
        major = int(major)
        minor = int(minor)
        patch = int(patch)
    elif o == "-t":
        target = a
    elif o == "-c":
        command = a
    elif o == "-s":
        size = int(a)
    elif o == "-d":
        description = a
    elif o == "-V":
        vendor = a
    else:
        usage()

# Sanity check args
if target != "package" and target != "partition" and  target != "module_loader":
    print "Error: -t target must be 'package', 'partition' or 'module_loader'"
    print
    usage()

if command != "install" and command != "upgrade" and command != "remove":
    print "Error: -c command must be 'install', 'upgrade' or 'remove'"
    print
    usage()

    
print "Will simulate downloaded package:"
print "Package ID:  {} {}.{}.{}".format(package_id, major, minor, patch)
print "Description: {}".format(description)
print "Vendor:      {}".format(vendor)
print "Size:        {}".format(size)
print "Target:      {}".format(target)
print "Command:     {}".format(command)

DBusGMainLoop(set_as_default=True)
sota_svc = SOTAClientService()

# USE CASE
#
# This sota_client will send a package_available() call to the 
# software loading manager (SLM).
#
# SLM will pop an operation confirmation dialog on the HMI.
#
# If confirmed, SLM will make an initiate_download() callback to 
# this sota_client.
#
# The sota_client will, on simulated download completion, make a
# download_complete() call to the SLM to  indicate that the package is 
# ready to be processed.
#
# The SLM will look at the target parameter and forward the package to 
# module_loader, package_manager, or partition_manager.
# 
# Once the package has been processed by its target, a package operation
# report will be sent back to the SLM.
#
# The SLM will forward the package operation report to this sota client
# as an installation_report() call.


sota_svc.package_available(package_id, 
                           major,
                           minor, 
                           patch,
                           command,
                           size,
                           description,
                           vendor,
                           target)

{'fraction_non_alphanumeric': 0.09934717107465685, 'fraction_numerical': 0.008536993639102779, 'mean_word_length': 1.4455587392550144, 'pattern_counts': {'":': 9, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6366702', 'n_tokens_mistral': 3392, 'n_tokens_neox': 3155, 'n_words': 730}