예제 #1
0
파일: component.py 프로젝트: ctldev/ctlweb
    def upload_to_web(self, url):
        """ Upload the component to the given url

        """
        Log.critical('upload_to_web() has been called. This function is deprecated')
        Log.debug("Uploading component to url %s" % url)
        import requests
        manifest_file = self._component_file
        files = {'manifest': open(manifest_file, "rb")}
        r = requests.post(url, files=files)
        if r.status_code != requests.codes.ok:
            Log.critical("Error %s occured while upload" % r.status_code)
예제 #2
0
파일: component.py 프로젝트: ctldev/ctlweb
    def upload_to_web(self, url):
        """ Upload the component to the given url

        """
        Log.critical(
            'upload_to_web() has been called. This function is deprecated')
        Log.debug("Uploading component to url %s" % url)
        import requests
        manifest_file = self._component_file
        files = {'manifest': open(manifest_file, "rb")}
        r = requests.post(url, files=files)
        if r.status_code != requests.codes.ok:
            Log.critical("Error %s occured while upload" % r.status_code)
예제 #3
0
파일: component.py 프로젝트: ctldev/ctlweb
    def add(cls, component):
        """ A given package will be added to the local database.

        A package has to be in the ctlweb-format which can be found in our
        docmuentation.

        returns newly created object
        """
        from os import path
        Log.debug("add(package): adding package to local database")
        # Create Database entry
        data = Component._unpack(component)
        comp = cls.create(data)
        comp._component_file = component
        try:
            comp.save()
        except NoSuchTable:
            comp.create_table().save()
        except ValueError as e:
            Log.critical('%s' % e.args[0])
            return
        except MergedWarning as e:
            Log.info('Merged components')
            return comp
        # Copy package to store
        comp._component_file = None  # reset the component to default.
        try:
            comp_log = ComponentLog.get_exactly(comp['c_exe_hash'])
            comp_log.remove()
        except (InstanceNotFoundError, NoSuchTable):
            pass
        import shutil
        try:
            target_name = comp._component_file
            shutil.copy(component, target_name)
        except IOError:
            Log.critical("Unable to save component to Manifest store in %s" %
                         store)
            exit(1)
        return comp
예제 #4
0
파일: database.py 프로젝트: ctldev/ctlweb
    def __init__(self, config_file=DEFAULT_CONFIG):
        import configparser
        reader = configparser.ConfigParser()
        reader.read(config_file)
        self.c_pk = -1
        if not Database.config:
            Database.config = config_file  # share config file with others

        if Database.store is None:
            try:
                Database.store = reader.get('Backend', 'Manifest_store')
            except configparser.Error:
                Log.critical("""Your Config-File seems to be malformated! Check
                your Config-File and try again!""")
                sys.exit(1)
        if Database.db_file is None:
            try:
                Database.db_file = reader.get('Backend', 'Database')
                import os
                if os.path.isdir(Database.db_file):
                    Log.critical("Database(): Database is directory!")
                    sys.exit(1)

            except configparser.Error:
                Log.critical("""Your Config-File seems to be malformated! Check
                your Config-File and try again!""")
                sys.exit(1)
        try:  # Check if connection is active
            Database.db_connection.execute("""SELECT name from sqlite_master
                                                 LIMIT 1""")
            Database.db_connection.fetchone()
        except (sqlite3.ProgrammingError, AttributeError):
            Database.db_connection = sqlite3.connect(Database.db_file)
예제 #5
0
파일: component.py 프로젝트: ctldev/ctlweb
    def add(cls, component):
        """ A given package will be added to the local database.

        A package has to be in the ctlweb-format which can be found in our
        docmuentation.

        returns newly created object
        """
        from os import path
        Log.debug("add(package): adding package to local database")
        # Create Database entry
        data = Component._unpack(component)
        comp = cls.create(data)
        comp._component_file = component
        try:
            comp.save()
        except NoSuchTable:
            comp.create_table().save()
        except ValueError as e:
            Log.critical('%s' % e.args[0])
            return
        except MergedWarning as e:
            Log.info('Merged components')
            return comp
        # Copy package to store
        comp._component_file = None  # reset the component to default.
        try:
            comp_log = ComponentLog.get_exactly(comp['c_exe_hash'])
            comp_log.remove()
        except (InstanceNotFoundError, NoSuchTable):
            pass
        import shutil
        try:
            target_name = comp._component_file
            shutil.copy(component, target_name)
        except IOError:
            Log.critical("Unable to save component to Manifest store in %s"
                         % store)
            exit(1)
        return comp
예제 #6
0
파일: component.py 프로젝트: ctldev/ctlweb
 def _read_control(control):
     """ Reads for the backend required keys of the control file. See
     Component.create() for more detail.
     """
     Log.debug("_read_control(): parsing control file %s")
     import configparser
     parser = configparser.ConfigParser()
     parser.read(control)
     try:
         name = parser['DEFAULT']['name']
     except KeyError:
         Log.critical("Found no component name")
         raise
     try:
         exe = parser['DEFAULT']['exe']
         exe_hash = parser['DEFAULT']['exe_hash']
     except KeyError:
         Log.critical("Found no corresponding exe in component")
         raise
     return {"c_id": name,
             "c_exe": exe,
             "c_exe_hash": exe_hash,
             }
예제 #7
0
파일: component.py 프로젝트: ctldev/ctlweb
 def _read_control(control):
     """ Reads for the backend required keys of the control file. See
     Component.create() for more detail.
     """
     Log.debug("_read_control(): parsing control file %s")
     import configparser
     parser = configparser.ConfigParser()
     parser.read(control)
     try:
         name = parser['DEFAULT']['name']
     except KeyError:
         Log.critical("Found no component name")
         raise
     try:
         exe = parser['DEFAULT']['exe']
         exe_hash = parser['DEFAULT']['exe_hash']
     except KeyError:
         Log.critical("Found no corresponding exe in component")
         raise
     return {
         "c_id": name,
         "c_exe": exe,
         "c_exe_hash": exe_hash,
     }
예제 #8
0
파일: database.py 프로젝트: ctldev/ctlweb
    def __init__(self, config_file=DEFAULT_CONFIG):
        import configparser

        reader = configparser.ConfigParser()
        reader.read(config_file)
        self.c_pk = -1
        if not Database.config:
            Database.config = config_file  # share config file with others

        if Database.store is None:
            try:
                Database.store = reader.get("Backend", "Manifest_store")
            except configparser.Error:
                Log.critical(
                    """Your Config-File seems to be malformated! Check
                your Config-File and try again!"""
                )
                sys.exit(1)
        if Database.db_file is None:
            try:
                Database.db_file = reader.get("Backend", "Database")
                import os

                if os.path.isdir(Database.db_file):
                    Log.critical("Database(): Database is directory!")
                    sys.exit(1)

            except configparser.Error:
                Log.critical(
                    """Your Config-File seems to be malformated! Check
                your Config-File and try again!"""
                )
                sys.exit(1)
        try:  # Check if connection is active
            Database.db_connection.execute(
                """SELECT name from sqlite_master
                                                 LIMIT 1"""
            )
            Database.db_connection.fetchone()
        except (sqlite3.ProgrammingError, AttributeError):
            Database.db_connection = sqlite3.connect(Database.db_file)
예제 #9
0
class AutoHelper(object):
    def __init__(self, config_name='sample_config', *args):
        try:
            # Get the current abspath
            self.path = getcwd()

            # Instantiate util.Log module
            self.log = Log()

            # Import config file
            self.config = importlib.import_module(config_name,
                                                  package='AutoHelper')
            self.log.info(f'Import config success: {config_name}.py.')
            if not self.__verify_config():
                self.log.warning(
                    'Config file is not compliant. Missing args will be blank.'
                )

            # Instantiate util.ADB_client to interact with emulator
            self.adb = ADBClient(self.config.ADB_root, self.config.ADB_host)

            # Instantiate util.Ocr to interact with Baidu-api
            self.ocr = Ocr(self.config.APP_ID, self.config.API_KEY,
                           self.config.SECRET_KEY)

            # Initialize
            with open('ingame.json', mode='r') as file:
                self.ingame = json.load(file)
            self.initialize()

        except ImportError as e:
            self.log.critical(f'Import config error:', e)
        except ADBError as e:
            self.log.critical(f'ADB Error:', e)
        except OCRError as e:
            self.log.critical(f'Connecting to Baidu-api error:', e)
        except Exception as e:
            self.log.warning('Unknown Error:', e)

    # Initializers
    def initialize(self):
        """
        Initialize in-game info (Includes window size, click positions etc.)
        And save into ingame.json
        :return: None
        """
        def homepage():
            self.log.info(
                "Please go to home page, when finished, input anything below:")
            self.log.input()
            self.log.info("Start recognizing...")
            with self.adb.screen_shot(screenshot_path):
                pic_content = self.ocr.scan(screenshot_path)
            if pic_content['flag']['is_homepage'] == 'TRUE':
                self.ingame['location']['homepage_strength'] = pic_content[
                    'location']['strength']
                self.log.info('Done')
            else:
                raise AutoHelperError('Incorrect page')

        def mission_page():
            self.log.info(
                "Please go to mission page (anyone with '开始行动' button), "
                "when finished, input anything below:")
            self.log.input()
            self.log.info("Start recognizing...")
            with self.adb.screen_shot(screenshot_path):
                pic_content = self.ocr.scan(screenshot_path)
            if pic_content['flag']['is_mission_page'] == 'TRUE':
                self.ingame['location']['mission_page_strength'] = pic_content[
                    'location']['strength']
                self.ingame['location']['start_mission'] = pic_content[
                    'location']['start_mission']
                self.log.info('Done')
            else:
                raise AutoHelperError('Incorrect page')

        def preparation():
            self.log.info(
                'The program will automatically go to preparation page(without actually start the mission)\n'
                "Please don't disturbance the program")
            self.adb.click(
                self.confuse(self.ingame['location']['start_mission']))
            with self.adb.screen_shot(screenshot_path):
                pic_content = self.ocr.scan(screenshot_path)
            if pic_content['flag']['is_preparation_page'] == 'TRUE':
                self.ingame['location']['prepare_start'] = pic_content[
                    'location']['prepare_start']
                self.log.info('Done')
            else:
                raise AutoHelperError('Incorrect page')

        try:
            # Detect if it's first time
            if self.ingame['FIRST_TIME'] == "TRUE":
                self.log.info(
                    'First time using AA-Helper, Initializing, please follow the instruction.'
                )  # TODO
                # Detect window size
                self.ingame['window_size'] = self.adb.get_window_size()

                # Set screenshot save path
                screenshot_path = path.join(self.path, 'pictures',
                                            'Screenshot.png')

                # Detect homepage
                self.retry(homepage)

                # Detect mission page
                self.retry(mission_page)

                # Detect start button in preparation page
                self.retry(preparation)

                # Change the first-time status into false
                # self.ingame['FIRST_TIME'] = "FALSE" TODO: Uncomment

                # Writing into file
                with open('ingame.json', mode='w') as file:
                    file.write(json.dumps(self.ingame))
        except FileNotFoundError as e:
            self.log.warning('Cannot found ingame.json, creating...', e)
            with open('ingame.json', mode='w'
                      ) as file:  # Create the file and set FIRST_TIME to TRUE
                self.ingame = dict()
                self.ingame['FIRST_TIME'] = "TRUE"
                file.write(json.dumps(self.ingame))
        except JSONDecodeError as e:
            self.log.warning('JSON decoder error:', e)
        except Exception as e:
            self.log.warning('Unknown error during initializing:', e)

    def retry(self, func, max_time=3):
        try:
            return func()
        except Exception as e:
            self.log.warning('Unknown error:', e)
            if max_time > 0:
                self.log.info(
                    f"Error while running '{func.__name__}', retrying ({max_time} time(s) left)."
                )
                return self.retry(func, max_time - 1)
            else:
                raise

    def __verify_config(self):  # TODO
        """
        To verify if the config file is compliant format.
        :return:(bool)
        """
        return True

    # Battle functions
    def battle(self):  # TODO
        """
        A overall battle module
        :return: None
        """
        try:
            self.__start_battle()
        except Exception as e:
            self.log.warning("Unknown Error:", e)

    def __start_battle(self, times=0):  # TODO
        """
        Simply click on '开始战斗' for times
        :param times: Times of clicking '开始战斗' button
        :return: None
        """
        try:
            self.adb.click(
                self.confuse(self.ingame['location']['start_mission']))
        except Exception as e:
            self.log.warning('Unknown error:', e)

    def confuse(self, loca):
        try:
            return loca['left'] + uniform(-0.49, 0.49) * loca['width'], \
                   loca['top'] + uniform(-0.49, 0.49) * loca['height'],
        except Exception as e:
            self.log.warning("Unknown error:", e)