# Path: formatters/log.py
# class Log:
# 
#     DEBUG = False
# 
#     @staticmethod
#     def d(tag, message):
#         if Log.DEBUG: print "D/%s: %s" %(tag, message)
# 
#     @staticmethod
#     def i(tag, message):
#         print "I/%s: %s" %(tag, message)
# 
#     @staticmethod
#     def w(tag, message):
#         print "W/%s: %s" %(tag, message)
# 
#     @staticmethod
#     def e(tag, message):
#         print "E/%s: %s" %(tag, message)
# 
# class Paint:
# 
#     @staticmethod
#     def bold(s):
#         return "%s[01m%s%s[0m" % (chr(27), s.rstrip(), chr(27))
# 
#     @staticmethod
#     def red(s):
#         return "%s[33;31m%s%s[0m" % (chr(27), s.rstrip(), chr(27))
# 
#     @staticmethod
#     def green(s):
#         return "%s[31;32m%s%s[0m" % (chr(27), s.rstrip(), chr(27))
# 
#     @staticmethod
#     def blue(s):
#         return "%s[34;01m%s%s[0m" % (chr(27), s.rstrip(), chr(27))
# 
#     @staticmethod
#     def yellow(s):
#         return "%s[31;33m%s%s[0m" % (chr(27), s.rstrip(), chr(27))


import shutil
import subprocess
import os, sys
import tempfile
import commands
import getopt
from config import Config
from changelist import ChangeList
from formatters.log import Log, Paint


    def upgrade(self):
        """ Prepare precondition of upgrade
        """

        # Remove last_bosp and bosp
        Utils.run(["rm", "-rf", OPTIONS.olderRoot], stdout=subprocess.PIPE).communicate()
        Utils.run(["rm", "-rf", OPTIONS.newerRoot], stdout=subprocess.PIPE).communicate()

        lastBaiduZip  = os.path.join(Config.PRJ_ROOT, "baidu/last_baidu.zip")
        baiduZip      = os.path.join(Config.PRJ_ROOT, "baidu/baidu.zip")

        if os.path.exists(lastBaiduZip) and os.path.exists(baiduZip):
            # Phase 1: prepare LAST_BOSP from last_baidu.zip
            Utils.decode(lastBaiduZip, OPTIONS.olderRoot)

            # Phase 2: prepare BOSP from baidu.zip
            Utils.decode(baiduZip, OPTIONS.newerRoot)

            # Phase 3: prepare patch XML
            ChangeList(OPTIONS.olderRoot, OPTIONS.newerRoot, OPTIONS.patchXml).make(force=True)

        else:
            if OPTIONS.commit1 != None:
                self.baseDevice.setLastHead(OPTIONS.commit1)

            # Phase 1: get last and origin head from base device
            (lastHead, origHead) = self.baseDevice.getLastAndOrigHead()
            if lastHead == origHead:

# Path: formatters/log.py
# class Log:
# 
#     DEBUG = False
# 
#     @staticmethod
#     def d(tag, message):
#         if Log.DEBUG: print "D/%s: %s" %(tag, message)
# 
#     @staticmethod
#     def i(tag, message):
#         print "I/%s: %s" %(tag, message)
# 
#     @staticmethod
#     def w(tag, message):
#         print "W/%s: %s" %(tag, message)
# 
#     @staticmethod
#     def e(tag, message):
#         print "E/%s: %s" %(tag, message)
# 
# class Paint:
# 
#     @staticmethod
#     def bold(s):
#         return "%s[01m%s%s[0m" % (chr(27), s.rstrip(), chr(27))
# 
#     @staticmethod
#     def red(s):
#         return "%s[33;31m%s%s[0m" % (chr(27), s.rstrip(), chr(27))
# 
#     @staticmethod
#     def green(s):
#         return "%s[31;32m%s%s[0m" % (chr(27), s.rstrip(), chr(27))
# 
#     @staticmethod
#     def blue(s):
#         return "%s[34;01m%s%s[0m" % (chr(27), s.rstrip(), chr(27))
# 
#     @staticmethod
#     def yellow(s):
#         return "%s[31;33m%s%s[0m" % (chr(27), s.rstrip(), chr(27))


# Path: autopatch/precondition.py
import shutil
import subprocess
import os, sys
import tempfile
import commands
import getopt
from config import Config
from changelist import ChangeList
from formatters.log import Log, Paint



    def upgrade(self):
        """ Prepare precondition of upgrade
        """

        # Remove last_bosp and bosp
        Utils.run(["rm", "-rf", OPTIONS.olderRoot], stdout=subprocess.PIPE).communicate()
        Utils.run(["rm", "-rf", OPTIONS.newerRoot], stdout=subprocess.PIPE).communicate()

        lastBaiduZip  = os.path.join(Config.PRJ_ROOT, "baidu/last_baidu.zip")
        baiduZip      = os.path.join(Config.PRJ_ROOT, "baidu/baidu.zip")

        if os.path.exists(lastBaiduZip) and os.path.exists(baiduZip):
            # Phase 1: prepare LAST_BOSP from last_baidu.zip
            Utils.decode(lastBaiduZip, OPTIONS.olderRoot)

            # Phase 2: prepare BOSP from baidu.zip
            Utils.decode(baiduZip, OPTIONS.newerRoot)

            # Phase 3: prepare patch XML
            ChangeList(OPTIONS.olderRoot, OPTIONS.newerRoot, OPTIONS.patchXml).make(force=True)

        else:
            if OPTIONS.commit1 != None:
                self.baseDevice.setLastHead(OPTIONS.commit1)

            # Phase 1: get last and origin head from base device
            (lastHead, origHead) = self.baseDevice.getLastAndOrigHead()
            if lastHead == origHead:

                Log.w(TAG, Paint.red("Nothing to upgrade. Did you forget to sync the %s ?" %OPTIONS.baseName))