def register_or_get_nailpack(app, api, nailpack_name): if api: if hasattr(api, '_registered_nailpacks'): if _.includes(_.keys(api._registered_nailpacks), nailpack_name): return api._registered_nailpacks[nailpack_name] nailpack = importlib.import_module(nailpack_name).Nailpack() api._registered_nailpacks[nailpack_name] = nailpack else: nailpack = importlib.import_module(nailpack_name).Nailpack() registered_nailpacks = {} registered_nailpacks[nailpack_name] = nailpack setattr(api, '_registered_nailpacks', registered_nailpacks) setattr(nailpack, 'api', api) else: nailpack = importlib.import_module(nailpack_name).Nailpack() if hasattr(app, '_registered_nailpacks'): if _.includes(_.keys(app._registered_nailpacks), nailpack_name): return app._registered_nailpacks[nailpack_name] app._registered_nailpacks.append(nailpack.name) else: registered_nailpacks = {} registered_nailpacks[nailpack_name] = nailpack setattr(app, '_registered_nailpacks', registered_nailpacks) for key in helpers.pubkeys(nailpack): if app: regsiter_event(app, key, getattr(nailpack, key)) if api: regsiter_event(api, key, getattr(nailpack, key)) if app: setattr(nailpack, 'app', app) setattr(nailpack, 'name', nailpack_name) setattr(nailpack, 'level', get_level(nailpack)) return nailpack
def is_cached(self, cached): s = self.app.services spinner = self.app.spinner if hasattr(self, 'root') and self.root and not self.app.finished: return False if not hasattr(self, 'checksum_paths') and not hasattr( self, 'has_paths'): return cached if not cached and (not hasattr(self, 'root') or not self.root) and \ (not hasattr(self, 'agnostic') or not self.agnostic): return cached cached = True checksum_paths = self.checksum_paths if hasattr( self, 'checksum_paths') else [] has_paths = self.has_paths if hasattr(self, 'has_paths') else [] for checksum_path in checksum_paths: checksum = s.cache.checksum(checksum_path) cached_checksums = s.cache.get_checksums(self.name) if not _.includes(cached_checksums, checksum): cached = False break for has_path in has_paths: if not path.exists(has_path): cached = False break return cached
def get_keys(self, include_ubuntu_keys=False, trying_again=False): s = self.app.services keys = [] stdout = s.util.subproc('gpg --keyid-format LONG --list-keys') matches = re.split(r'-{4}\n', stdout) stdout_keys = [] if len(matches) > 1: stdout_keys = list(filter(None, matches[1].split('\n\n'))) for stdout_key in stdout_keys: key = Munch() lines = stdout_key.split('\n') if (len(lines) >= 2): match = next( re.finditer(r'(pub\s+)(\w+)\/(\w+)\s+([\w-]+)', lines[0]), None) if match: key_long = match.groups()[2].strip() pub_key_short = key_long[8:] key.pub = munchify({ 'cipher': match.groups()[1].strip(), 'key': { 'long': key_long, 'short': pub_key_short, }, 'date': match.groups()[3].strip() }) match = next( re.finditer(r'(uid\s+\[.+]\s+)(.+)(<[^<>]+>)', lines[1]), None) if match: email = match.groups()[2].strip() key.name = match.groups()[1].strip() key.email = email[1:len(email) - 1] if (len(lines) >= 3): match = next( re.finditer(r'(sub\s+)(\w+)\/(\w+)\s+([\w-]+)', lines[2]), None) if match: key_long = match.groups()[2].strip() key.sub = Munch({ 'cipher': match.groups()[1].strip(), 'key': { 'long': key_long, 'short': key_long[8:], }, 'date': match.groups()[3].strip() }) if not ('pub' in key and 'name' in key and 'email' in key): continue if not include_ubuntu_keys: if _.includes(self.ubuntu_keys, key.pub.key.short): continue keys.append(key) if len(keys) <= 0: if trying_again: self.app.spinner.fail('failed to load gpg keys') exit(1) self.gen_key() return self.get_keys(trying_again=True) return keys
def get_random_numbers(count, length): random_numbers = list() for i in range(count): random_number = int(random() * length) if _.includes(random_numbers, random_number): random_number = get_next_unique_number(length, random_numbers, random_number) random_numbers.append(random_number) return random_numbers
def handle_error(app, err): debug = False if app.config.get('log.logging', 'level') == 'DEBUG': debug = True if _.includes(app.config.keys(NAME), 'debug') and app.config.get( NAME, 'debug'): debug = True if debug: traceback.print_exc() return app.log.error(err.message)
def load_conf(conf): if _.includes(sys.argv, '-h') or _.includes(sys.argv, '--help'): return conf cwd_path = os.getcwd() flag = None if _.includes(sys.argv, '--source'): flag = '--source' elif _.includes(sys.argv, '--src'): flag = '--src' elif _.includes(sys.argv, '-s'): flag = '-s' if flag: flag_index = _.index_of(sys.argv, flag) if len(sys.argv) > flag_index + 1: cwd_path = path.abspath(sys.argv[flag_index + 1]) config_path = path.join(cwd_path, 'config.yml') if not path.exists(config_path): Halo(text='config not found: ' + config_path).fail() exit(1) with open(config_path, 'r') as f: try: conf = munchify(_.merge({}, conf, yaml.load(f))) except yaml.YAMLError as err: print(err) exit(1) conf.paths.cwd = cwd_path if 'version' in conf: conf.version = str(conf.version) conf.paths.install = path.join(conf.paths.mount, 'casper') if not path.exists(path.join(conf.paths.install, 'filesystem.squashfs')): conf.paths.install = path.join(conf.paths.mount, 'install') output_path = conf.paths.output conf = munchify( _.merge({}, conf, { 'paths': _.zip_object( _.keys(conf.paths), _.map(conf.paths, lambda x: path.abspath(path.join(conf.paths.cwd, x)))) })) conf.paths.output = path.abspath(output_path) return conf
def get_words(path, category): words_by_category = get_words_by_category(path) if category: if not category in words_by_category: raise DefaultException('\'' + category + '\' category does not exist') return words_by_category[category] words = list() for (key, item) in viewitems(words_by_category): for word in words_by_category[key]: if not _.includes(words, item): words.append(word) return words
def run_required(self): global steps_ran steps = self.app.steps cached = True if not hasattr(self, 'requires'): return False if len(self.requires) <= 0: cached = False for required in self.requires: maybe_cached = True if _.includes(_.keys(steps_ran), required): maybe_cached = steps_ran[required] else: maybe_cached = getattr(steps, required).start() if not maybe_cached: cached = maybe_cached return cached
def setup_logger(config_path='logging.yml', name=None, level=logging.INFO, key='LOG_CFG'): config_path = path.abspath(os.environ[key] if key in os.environ else config_path) config = None if path.exists(config_path): with open(config_path, 'r') as f: config = yaml.safe_load(f.read()) logging.config.dictConfig(config) else: logging.basicConfig(level=level) logger = logging.getLogger() if name: logger = logging.getLogger(name) if _.includes(sys.argv, '--debug'): logger.setLevel(logging.DEBUG) return logger
def get_level(nailpack): if hasattr(nailpack, 'level_override'): if nailpack.level_override == 'api': if hasattr(nailpack, 'api'): if _.includes(nailpack.api.config.main.nailpacks, nailpack.name): return 'api' elif _.includes(nailpack.app.config.main.nailpacks, nailpack.name): return 'app' elif nailpack.level_override == 'app_as_api': if hasattr(nailpack, 'api'): if _.includes(nailpack.api.config.main.nailpacks, nailpack.name): return 'api' elif _.includes(nailpack.app.config.main.nailpacks, nailpack.name): nailpack.api.config.main.nailpacks.append(nailpack.name) return 'api' elif _.includes(nailpack.app.config.main.nailpacks, nailpack.name): return 'app' if not hasattr(nailpack, 'api'): if _.includes(nailpack.app.config.main.nailpacks, nailpack.name): return 'app' elif not _.includes(nailpack.app.config.main.nailpacks, nailpack.name): if _.includes(nailpack.api.config.main.nailpacks, nailpack.name): return 'api' return 'invalid'
def get_next_unique_number(length, numbers, number): number = (number + 1) % length if _.includes(numbers, number): number = get_next_unique_number(length, numbers, number) return number
def get_data_type(self, container): data_type = container['Config']['Image'][:container['Config']['Image']. index(':')] if not _.includes(_.keys(self.handlers), data_type): data_type = 'raw' return data_type
lambda x: path.abspath(path.join(conf.paths.cwd, x)))) })) conf.paths.output = path.abspath(output_path) return conf App = create_app(name='forkbuntu', controllers=controllers, services=services, conf=load_conf({ 'apt': { 'restricted': True, 'universe': True, 'multiarch': True }, 'debug': _.includes(sys.argv, '--debug'), 'filesystem': { 'compress': False }, 'packages': [], 'paths': { 'apt_ftparchive': '.tmp/apt-ftparchive', 'cwd': os.getcwd(), 'cwt': '.tmp', 'filesystem': '.tmp/filesystem', 'indices': '.tmp/indices', 'iso': 'ubuntu-18.04-server-amd64.iso', 'keyring': '.tmp/keyring', 'initrd': '.tmp/initrd', 'mount': '.tmp/iso', 'output': 'forkbuntu.iso',