def find_and_replace(build, *files, **kwargs): '''replace one string with another in a set of files :param kwargs: must contain ``find`` and ``replace`` keys, representing the string to look for, and string to replace with, respectively. :param kwargs: can also contain the ``template`` boolean argument, which determines if we will run the ``replace`` argument through genshi templating first (defaults to True). :param files: array of glob patterns to select files :param kwargs: must contain ``find`` and ``replace`` keys ''' if "find" not in kwargs: raise ConfigurationError("Find not passed in to find_and_replace") if "replace" not in kwargs: raise ConfigurationError("Replace not passed in to find_and_replace") template = kwargs.get('template', True) find = kwargs["find"] replace = kwargs['replace'] if template: replace = utils.render_string(build.config, replace) replace_summary = replace[:60] + '...' if len(replace) > 60 else replace build.log.debug("replacing %s with %s" % (find, repr(replace_summary))) for glob_str in files: found_files = glob.glob(utils.render_string(build.config, glob_str)) if len(found_files) == 0: build.log.warning('No files were found to match pattern "%s"' % glob_str) for _file in found_files: _replace_in_file(build, _file, find, replace)
def check_local_config_schema(build): log.info("Verifying your configuration settings...") # leave this import here: might not be on sys.path in some situations import validictory local_conf_filename = build.tool_config.get('general.local_config') if local_conf_filename is not None: # explicit conf file defined if not path.isfile(local_conf_filename): raise ConfigurationError("{file} does not exist!".format(file=local_conf_filename)) else: local_conf_filename = 'local_config.json' if not path.isfile(local_conf_filename): log.warning("Local configuration file '{file}' does not exist!".format(file=local_conf_filename)) with open(local_conf_filename) as local_conf_file: local_conf = json.load(local_conf_file) from forge.remote import Remote from forge import build_config remote = Remote(build_config.load()) local_conf_schema = remote._api_get('platform/{platform_version}/local_config_schema'.format( platform_version=build.config['platform_version'])) try: validictory.validate(local_conf, local_conf_schema) except validictory.validator.UnexpectedPropertyError as e: log.warning('Unexpected setting: "{error}" in "{file}". This will be ignored.'.format( file=local_conf_filename, error=e) ) log.info("Configuration settings check complete")
def check_local_config_schema(build): log.info("Verifying your configuration settings...") # leave this import here: might not be on sys.path in some situations import validictory local_conf_filename = build.tool_config.get('general.local_config') if local_conf_filename is not None: # explicit conf file defined if not path.isfile(local_conf_filename): raise ConfigurationError( "{file} does not exist!".format(file=local_conf_filename)) else: local_conf_filename = 'local_config.json' if not path.isfile(local_conf_filename): log.warning( "Local configuration file '{file}' does not exist!".format( file=local_conf_filename)) with open(path.join(path_to_lib(), "local_config_schema.json")) as local_conf_schema_file: local_conf_schema = json.load(local_conf_schema_file) try: validictory.validate(build.tool_config.all_config(), local_conf_schema) except validictory.validator.UnexpectedPropertyError as e: log.warning( 'Unexpected setting: "{error}". This will be ignored'.format( file=local_conf_filename, error=e)) log.info("Configuration settings check complete")
def copy_files(build, **kw): if 'from' not in kw or 'to' not in kw: raise ConfigurationError( 'copy_files requires "from" and "to" keyword arguments') return _rename_or_copy_files(build, kw['from'], kw['to'], rename=False, ignore_patterns=kw.get('ignore_patterns'))
def extract_files(build, **kw): if 'from' not in kw or 'to' not in kw: raise ConfigurationError( 'extract_files requires "from" and "to" keyword arguments') build.log.debug('Extracting %s to %s' % (utils.render_string(build.config, kw['from']), utils.render_string(build.config, kw['to']))) zipf = zipfile.ZipFile(utils.render_string(build.config, kw['from'])) zipf.extractall(utils.render_string(build.config, kw['to'])) zipf.close()
def copy_jquery(build, **kw): if 'to' not in kw: raise ConfigurationError('copy_jquery needs "to" keyword arguments') _from = 'common-v2/libs/jquery/jquery-' + build.config.get( 'plugins')['jquery']['config']['version'] + '.min.js' _to = utils.render_string(build.config, kw['to']) dir = '' for next_dir in _to.split('/'): dir += next_dir + '/' if not os.path.isdir(dir): os.mkdir(dir) shutil.copy(_from, _to)
def concatenate_files(build, **kw): if 'in' not in kw or 'out' not in kw: raise ConfigurationError( 'concatentate_files requires "in" and "out" keyword arguments') with open(kw['out'], 'a') as out_file: for frm in kw['in']: if not path.isfile(frm): raise Exception("not a file: " + frm) build.log.debug('concatenating %s to %s' % (frm, kw['out'])) with open(frm) as in_file: out_file.write(in_file.read()) build.log.info('appended %s to %s' % ( frm, kw['out'], ))
def run_hook(build, **kw): for file in sorted(os.listdir(os.path.join('hooks', kw['hook']))): if os.path.isfile(os.path.join('hooks', kw['hook'], file)): cwd = os.getcwd() os.chdir(kw['dir']) target = iter(build.enabled_platforms).next() # Get the extension ext = os.path.splitext(file)[-1][1:] proc = None if ext == "py": build.log.info('Running (Python) hook: ' + file) proc = lib.PopenWithoutNewConsole([ "python", os.path.join(cwd, 'hooks', kw['hook'], file), target ]) elif ext == "js": build.log.info('Running (node) hook: ' + file) proc = lib.PopenWithoutNewConsole([ "node", os.path.join(cwd, 'hooks', kw['hook'], file), target ]) elif ext == "bat" and sys.platform.startswith('win'): build.log.info('Running (Windows Batch file) hook: ' + file) proc = lib.PopenWithoutNewConsole( [os.path.join(cwd, 'hooks', kw['hook'], file), target]) elif ext == "sh" and not sys.platform.startswith('win'): build.log.info('Running (shell) hook: ' + file) proc = lib.PopenWithoutNewConsole( [os.path.join(cwd, 'hooks', kw['hook'], file), target]) if proc != None: proc.wait() os.chdir(cwd) if proc != None and proc.returncode != 0: raise ConfigurationError( 'Hook script exited with a non-zero return code.')
def rename_files(build, **kw): if 'from' not in kw or 'to' not in kw: raise ConfigurationError( 'rename_files requires "from" and "to" keyword arguments') return _rename_or_copy_files(build, kw['from'], kw['to'], rename=True)