120 lines
3.1 KiB
Python
120 lines
3.1 KiB
Python
import os
|
|
import sys
|
|
import tomli
|
|
from config import Config
|
|
from command import Command, UserCommand
|
|
|
|
|
|
all_platforms = {}
|
|
|
|
|
|
class Platform:
|
|
def all_platforms():
|
|
return all_platforms
|
|
|
|
|
|
def current_platform():
|
|
name = Config.get('target.platform')
|
|
if not name or name not in all_platforms:
|
|
return None
|
|
|
|
return all_platforms[name]
|
|
|
|
|
|
def load_file(self, path):
|
|
self.__path = path
|
|
with open(path, mode='rb') as f:
|
|
self.__data = tomli.load(f)
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
if 'path' in kwargs:
|
|
self.__platform_file = os.path.abspath(kwargs['path'])
|
|
self.load_file(kwargs['path'])
|
|
|
|
|
|
def base_directory(self):
|
|
return os.path.dirname(self.__platform_file)
|
|
|
|
|
|
def name(self):
|
|
if 'platform' not in self.__data:
|
|
return None
|
|
|
|
platform = self.__data['platform']
|
|
if 'name' in platform:
|
|
return platform['name']
|
|
|
|
return None
|
|
|
|
|
|
def arch(self):
|
|
if 'platform' not in self.__data:
|
|
return None
|
|
|
|
platform = self.__data['platform']
|
|
if 'arch' in platform:
|
|
return platform['arch']
|
|
|
|
return None
|
|
|
|
|
|
def load_extensions(self):
|
|
from seed import Seed
|
|
if 'extension' not in self.__data:
|
|
return
|
|
|
|
|
|
all_seeds = Seed.all_seeds()
|
|
|
|
extension_dir = os.path.dirname(self.__path)
|
|
extension = self.__data['extension']
|
|
|
|
if 'seeds' in extension:
|
|
seeds = extension['seeds']
|
|
for seed_filename in seeds:
|
|
seed_path = os.path.join(extension_dir, seed_filename)
|
|
seed = None
|
|
try:
|
|
seed = Seed(path=seed_path)
|
|
except tomli.TOMLDecodeError as e:
|
|
print('Err: Failed to parse seed file {}\n {}'.format(seed_path, e))
|
|
return -1
|
|
|
|
all_seeds[seed.name()] = seed
|
|
|
|
if 'commands' in extension:
|
|
commands = extension['commands']
|
|
for cmd_filename in commands:
|
|
cmd_path = os.path.join(extension_dir, cmd_filename)
|
|
cmd = None
|
|
try:
|
|
cmd = UserCommand(path=cmd_path)
|
|
except tomli.TOMLDecodeError as e:
|
|
print('Err: Failed to parse command file {}\n {}'.format(seed_path, e))
|
|
return -1
|
|
|
|
Command.register(cmd)
|
|
|
|
|
|
def get_component(self, name):
|
|
if name in self.__data:
|
|
return self.__data[name]
|
|
return None
|
|
|
|
|
|
def scan_directory(root_path):
|
|
for root, dirs, files in os.walk(root_path):
|
|
for f in files:
|
|
if not f.endswith('.platform'):
|
|
continue
|
|
platform_path = os.path.join(root, f)
|
|
platform = None
|
|
try:
|
|
platform = Platform(path=platform_path)
|
|
except tomli.TOMLDecodeError as e:
|
|
print('Err: Failed to parse platform file {}\n {}'.format(platform_path, e))
|
|
continue
|
|
|
|
all_platforms[platform.name()] = platform
|