示例#1
0
    def _project_from_path(path='.', name_hint=None):
        path = os.path.abspath(path)
        project_config = None
        project_config_file = os.path.join(path, "builder.json")
        if os.path.exists(project_config_file):
            import json
            with open(project_config_file, 'r') as config_fp:
                try:
                    project_config = json.load(config_fp)
                except Exception as e:
                    print("Failed to parse config file", project_config_file,
                          e)
                    sys.exit(1)

                if name_hint == None or project_config.get('name',
                                                           None) == name_hint:
                    print('    Found project: {} at {}'.format(
                        project_config['name'], path))
                    project = Project._create_project(**project_config,
                                                      path=path)
                    return Project._cache_project(project)

        # load any builder scripts and check them
        Scripts.load()
        # only construct a new instance of the class if there isn't one already in the cache
        if name_hint and name_hint.lower() not in Project._projects:
            project_cls = Project._find_project_class(name_hint)
            if project_cls:
                project = project_cls(
                    name=name_hint,
                    path=path if os.path.basename(path) == name_hint else None)
                return Project._cache_project(project)

        return None
示例#2
0
def run_action(action, env):
    config = env.config
    # Set build environment from config
    env.shell.pushenv()
    for var, value in getattr(env, 'env', {}).items():
        env.shell.setenv(var, value)

    if isinstance(action, str):
        action_cls = Scripts.find_action(action)
        if not action_cls:
            print('Action {} not found'.format(action))
            sys.exit(13)
        action = action_cls()

    if action.is_main():
        Scripts.run_action(action, env)
    else:
        Scripts.run_action(
            Script([
                InstallCompiler(),
                InstallPackages(),
                DownloadDependencies(),
                action,
            ],
                   name='main'), env)

    env.shell.popenv()
示例#3
0
    def find_import(name, hints=[]):
        imp = Project._projects.get(name.lower(), None)
        if imp and imp.resolved():
            return imp

        for h in hints:
            Scripts.load(h)
        imp_cls = Project._find_import_class(name)
        if imp_cls:
            return Project._cache_project(imp_cls())
        return Import(name=name, resolved=False)
示例#4
0
    def run(self, env):
        sh = env.shell

        def _expand_vars(cmd):
            cmd_type = type(cmd)
            if cmd_type == str:
                cmd = replace_variables(cmd, env.config['variables'])
            elif cmd_type == list:
                cmd = [
                    replace_variables(sub, env.config['variables'])
                    for sub in cmd
                ]
            return cmd

        # Interpolate any variables
        self.commands = [_expand_vars(cmd) for cmd in self.commands]

        # Run each of the commands
        children = []
        for cmd in self.commands:
            cmd_type = type(cmd)
            # See if the string is actually an action
            if cmd_type == str:
                action_cls = Scripts.find_action(cmd)
                if action_cls:
                    cmd = action_cls()
                    cmd_type = type(cmd)

            if cmd_type == str:
                result = sh.exec(*cmd.split(' '))
                if result.returncode != 0:
                    print('Command failed, exiting')
                    sys.exit(12)
            elif cmd_type == list:
                result = sh.exec(*cmd)
                if result.returncode != 0:
                    print('Command failed, exiting')
                    sys.exit(12)
            elif isinstance(cmd, Action):
                Scripts.run_action(cmd, env)
            elif callable(cmd):
                children += to_list(cmd(env))
            else:
                print('Unknown script sub command: {}: {}', cmd_type, cmd)
                sys.exit(4)
        return children
示例#5
0
    def analyze(self, path):
        # use the Scripts class to get the scripts
        scripts_analyzer = Scripts()
        scripts_results = scripts_analyzer.analyze(path)
        counter = scripts_results['counter']
        files = scripts_results['scripts']

        # make a dictionary of sprites that have changed since the last snapshot
        # changes[filename] = set of sprites who have changed since the last snapshot
        changes = {}

        # store the last file so we can compare
        last = False

        # go through the files
        for file, scriptdict in files.items():
            # this means we're on the first one, so last is false
            if not last:
                last = scriptdict

            # make a set of sprite for this file
            changes[file] = set()

            # go through the scripts for this file
            for sprite, scripts in scriptdict.items():

                # check if the sprite was in the last snapshot
                if sprite in last.keys():

                    # get the diff of this sprite's scripts and
                    # the scripts it had in the last snapshot
                    diff = last[sprite] ^ scripts

                    # if there were any differences, add it to changes
                    if len(diff) != 0:
                        changes[file].add(sprite)
                else:
                    # the sprite wasn't in the last file, so it's changed
                    changes[file].add(sprite)

            # now this will be the alst file
            last = scriptdict

        return {'counter': counter, 'scripts': files,
                'changes': changes}
示例#6
0
    def analyze(self, path):
        # use the Scripts class to get the scripts
        scripts_analyzer = Scripts()
        scripts_results = scripts_analyzer.analyze(path)
        counter = scripts_results['counter']
        files = scripts_results['scripts']

        # make a dictionary of sprites that have changed since the last snapshot
        # changes[filename] = set of sprites who have changed since the last snapshot
        changes = {}

        # store the last file so we can compare
        last = False

        # go through the files
        for file, scriptdict in files.items():
            # this means we're on the first one, so last is false
            if not last:
                last = scriptdict

            # make a set of sprite for this file
            changes[file] = set()

            # go through the scripts for this file
            for sprite, scripts in scriptdict.items():

                # check if the sprite was in the last snapshot
                if sprite in last.keys():

                    # get the diff of this sprite's scripts and
                    # the scripts it had in the last snapshot
                    diff = last[sprite] ^ scripts

                    # if there were any differences, add it to changes
                    if len(diff) != 0:
                        changes[file].add(sprite)
                else:
                    # the sprite wasn't in the last file, so it's changed
                    changes[file].add(sprite)

            # now this will be the alst file
            last = scriptdict

        return {'counter': counter, 'scripts': files, 'changes': changes}
def main(setting_file='settings.json'):
    global scripts
    global users
    global dict_settings
    global database

    try:
        cprint("Load setting file from: \"%s\"" % setting_file, "green")
        with open(setting_file) as f:
            settings_json = json.load(f)
    except (FileNotFoundError):
        cprint("[ERROR] %s is not defined!" % setting_file, "red")
        sys.exit(1)

    for key in dict_settings:
        result, value, message = utils.safe_load_settings(key, settings_json)
        if result is False:
            cprint("[ERROR] {}".format(message), "red")
            if key not in ["telegram_token", "instapy_folder", "allowed_id"]:
                cprint(
                    "[WARNING] Load default value of: {} : {}".format(
                        key, dict_settings[key]), "yellow")
            else:
                sys.exit()
        else:
            dict_settings[key] = value
            cprint("[SUCCESS] {}".format(message), "green")

    if dict_settings['project_path'] != "./":
        sys.path.insert(0, dict_settings['project_path'])

    try:
        from scripts import Scripts
    except (ModuleNotFoundError):
        cprint("[ERROR] Require \"scripts.py\" file!", "red")
        sys.exit(1)

    scripts = Scripts().scripts

    try:
        users = pickle.load(
            open(dict_settings['project_path'] + dict_settings['users_path'],
                 'rb'))
        cprint(
            "[SUCCESS] Load users list from: {}".format(
                dict_settings['users_path']), "green")
    except (FileNotFoundError, IOError):
        pickle.dump(
            users,
            open(dict_settings['project_path'] + dict_settings['users_path'],
                 'wb'))
        cprint(
            "[WARNING] Init user list in: {}".format(
                dict_settings['users_path']), "yellow")

    database = TinyDB('./db.json')

    updater = Updater(dict_settings['telegram_token'],
                      request_kwargs={
                          'read_timeout': 20,
                          'connect_timeout': 20
                      },
                      use_context=True)

    dp = updater.dispatcher

    dp.add_handler(CommandHandler("start", help))
    dp.add_handler(CommandHandler("help", help))

    dp.add_handler(CommandHandler("status", status_process, pass_args=True))
    dp.add_handler(CommandHandler("logs", logs, pass_args=True))
    dp.add_handler(CommandHandler("activity", activity, pass_args=True))

    dp.add_handler(
        CommandHandler("set",
                       set_job,
                       pass_args=True,
                       pass_job_queue=True,
                       pass_chat_data=True))
    dp.add_handler(CommandHandler("now", now, pass_args=True))
    dp.add_handler(CommandHandler("stop", stop, pass_args=True))

    dp.add_handler(
        CommandHandler("unset", unset, pass_args=True, pass_chat_data=True))
    dp.add_handler(CommandHandler("jobs", list_jobs, pass_chat_data=True))
    dp.add_handler(
        CommandHandler("reload",
                       reload_jobs,
                       pass_job_queue=True,
                       pass_chat_data=True))

    dp.add_handler(CommandHandler("add_user", add_user, pass_args=True))
    dp.add_handler(CommandHandler("delete_user", delete_user, pass_args=True))
    dp.add_handler(CommandHandler("users", print_users))

    dp.add_handler(CommandHandler("scripts", list_scripts))

    dp.add_handler(CommandHandler("time", timenow))

    dp.add_handler(
        CallbackQueryHandler(day_choose,
                             pass_job_queue=True,
                             pass_chat_data=True))

    dp.add_error_handler(error)

    updater.start_polling(timeout=25)

    cprint("TELEGRAM-INSTAPY-SCHEDULING IS READY!", "green")

    updater.idle()

    sys.exit(0)
示例#8
0
 def runScript(self, scriptname, arguments=None, **kwargs):
     """Runs a script on the instance"""
     return Scripts.run(scriptname, self.get('path'), arguments=arguments, cmdkwargs=kwargs)
示例#9
0
def main(settings_file='settings.json'):
    global scripts
    global dict_settings

    try:
        cprint("Load setting file from: \"%s\"" % settings_file, "green")
        with open(settings_file) as f:
            settings_json = json.load(f)
    except (FileNotFoundError):
        cprint("[ERROR] %s is not defined!" % settings_file, "red")
        sys.exit(1)

    for key in dict_settings:
        result, value, message = utils.safe_load_settings(key, settings_json)
        if result is False:
            cprint("[ERROR] {}".format(message), "red")
            if key not in ["telegram_token", "instapy_folder", "allowed_ids"]:
                cprint(
                    "[WARNING] Load default value of: {} : {}".format(
                        key, dict_settings[key]), "yellow")
            else:
                sys.exit()
        else:
            dict_settings[key] = value
            cprint("[SUCCESS] {}".format(message), "green")

    if dict_settings['project_path'] != "./":
        sys.path.insert(0, dict_settings['project_path'])

    try:
        from scripts import Scripts
    except (ModuleNotFoundError):
        cprint("[ERROR] Require \"scripts.py\" file!", "red")
        sys.exit(1)

    scripts = Scripts().scripts

    updater = Updater(token=dict_settings['telegram_token'],
                      request_kwargs={
                          'read_timeout': 20,
                          'connect_timeout': 20
                      })

    dispatcher = updater.dispatcher

    dispatcher.add_handler(CommandHandler('start', start))
    dispatcher.add_handler(CommandHandler('help', help))

    dispatcher.add_handler(CommandHandler("add_user", add_user,
                                          pass_args=True))
    dispatcher.add_handler(
        CommandHandler("delete_user", delete_user, pass_args=True))
    dispatcher.add_handler(CommandHandler("users", print_users))

    dispatcher.add_handler(CommandHandler("actions", list_actions))
    dispatcher.add_handler(CommandHandler("run", run, pass_args=True))
    dispatcher.add_handler(CommandHandler("stop", stop, pass_args=True))

    dispatcher.add_error_handler(error)

    updater.start_polling(timeout=25)

    cprint("INSTAPY TELEGRAM BOT IS READY!", "green")

    updater.idle()

    sys.exit(0)
示例#10
0
    def _cache_project(project):
        Project._projects[project.name.lower()] = project
        if getattr(project, 'path', None):
            Scripts.load(project.path)

        return project
示例#11
0
 def _find_import_class(name):
     return Scripts.find_import(name)
示例#12
0
 def _find_project_class(name):
     return Scripts.find_project(name)
示例#13
0
from pathlib import Path


def get_loop():
    if sys.platform == "win32":
        loop = asyncio.ProactorEventLoop()
        asyncio.set_event_loop(loop)
        return loop
    else:
        return asyncio.get_event_loop()


if __name__ == '__main__':
    # Project path.
    path = sys.path[0]
    start_time = time.time()
    paths = Scripts.run(str(Path(path).parent))
    gathering_time = time.time()

    loop = get_loop()
    loop.run_until_complete(
        asyncio.gather(*[Pep8._on_file_async(file) for file in paths]))
    loop.close()
    end_time = time.time()
    print(f'Total Files: {len(paths)}\n'
          f'Start: {start_time}\n'
          f'End: {end_time}\n'
          f'Gathering: {gathering_time - start_time}\n'
          f'Processing: {end_time - gathering_time}\n'
          f'Duration: {end_time - start_time}')
示例#14
0
    if spec.target == current_os() and spec.arch == current_arch():
        inspect_host(spec)
    if args.command == 'inspect':
        sys.exit(0)

    # set up environment
    env = Env({
        'dryrun': args.dry_run,
        'args': args,
        'project': args.project,
        'branch': args.branch,
        'spec': spec,
    })

    Scripts.load()

    if not env.project and args.command != 'mirror':
        print('No project specified and no project found in current directory')
        sys.exit(1)

    print('Using Spec:')
    print('  Host: {} {}'.format(spec.host, current_arch()))
    print('  Target: {} {}'.format(spec.target, spec.arch))
    print('  Compiler: {} {}'.format(spec.compiler, spec.compiler_version))

    if not env.config.get('enabled', True):
        raise Exception("The project is disabled in this configuration")

    if env.config.get('needs_compiler', True):
        env.toolchain = Toolchain(spec=env.spec)
示例#15
0
文件: moodle.py 项目: jacano1969/mdk
 def runScript(self, scriptname, **kwargs):
     """Runs a script on the instance"""
     return Scripts.run(scriptname, self.get("path"), cmdkwargs=kwargs)
示例#16
0
import os
from qtflow import get_flowcontrol
from qtlab.source.instruments import get_instruments
from lib import config as _config
from data import Data
# from plot import Plot, plot, plot3, replot_all
from scripts import Scripts, Script

config = _config.get_config()

data = Data.get_named_list()
instruments = get_instruments()
frontpanels = {}
sliders = {}
scripts = Scripts()

flow = get_flowcontrol()
msleep = flow.measurement_idle
mstart = flow.measurement_start
mend = flow.measurement_end

# from plot import Plot2D, Plot3D
# try:
#     from plot import plot_file
# except:
#     pass

# plots = Plot.get_named_list()