How to use the kas.context.get_context function in kas

To help you get started, we’ve selected a few kas examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github siemens / kas / kas / libkas.py View on Github external
sys.exit(1)

    get_bb_env_file = tempfile.mktemp()
    with open(get_bb_env_file, 'w') as fds:
        script = """#!/bin/bash
        set -e
        source %s $1 > /dev/null
        env
        """ % init_script
        fds.write(script)
    os.chmod(get_bb_env_file, 0o775)

    env = {}
    env['PATH'] = '/usr/sbin:/usr/bin:/sbin:/bin'

    (_, output) = run_cmd([get_bb_env_file, get_context().build_dir],
                          cwd=init_repo.path, env=env, liveupdate=False)

    os.remove(get_bb_env_file)

    env = {}
    for line in output.splitlines():
        try:
            (key, val) = line.split('=', 1)
            env[key] = val
        except ValueError:
            pass

    conf_env = get_context().config.get_environment()

    env_vars = ['SSTATE_DIR', 'DL_DIR', 'TMPDIR']
    env_vars.extend(conf_env)
github siemens / kas / kas / libkas.py View on Github external
def ssh_cleanup_agent():
    """
        Removes the identities and stops the ssh-agent instance
    """
    env = get_context().environ
    # remove the identities
    process = Popen(['ssh-add', '-D'], env=env)
    process.wait()
    if process.returncode != 0:
        logging.error('failed to delete SSH identities')

    # stop the ssh-agent
    process = Popen(['ssh-agent', '-k'], env=env)
    process.wait()
    if process.returncode != 0:
        logging.error('failed to stop SSH agent')
github siemens / kas / kas / repos.py View on Github external
async def fetch_async(self):
        """
            Starts asynchronous repository fetch.
        """
        if self.operations_disabled:
            return 0

        if not os.path.exists(self.path):
            os.makedirs(os.path.dirname(self.path), exist_ok=True)
            sdir = os.path.join(get_context().kas_repo_ref_dir or '',
                                self.qualified_name)
            logging.debug('Looking for repo ref dir in %s', sdir)

            (retc, _) = await run_cmd_async(
                self.clone_cmd(sdir),
                cwd=get_context().kas_work_dir)
            if retc == 0:
                logging.info('Repository %s cloned', self.name)
            return retc

        # Make sure the remote origin is set to the value
        # in the kas file to avoid suprises
        try:
            (retc, output) = await run_cmd_async(
                self.set_remote_url_cmd(),
                cwd=self.path,
                fail=False,
                liveupdate=False)
            if retc != 0:
                logging.info('Changing remote URL to %s failed with error: %s',
                             self.effective_url, output.strip())
                return retc
github siemens / kas / kas / repos.py View on Github external
async def apply_patches_async(self):
        """
            Applies patches to a repository asynchronously.
        """
        if self.operations_disabled or not self._patches:
            return 0

        (retc, _) = await run_cmd_async(self.prepare_patches_cmd(),
                                        cwd=self.path)
        if retc:
            return retc

        my_patches = []

        for patch in self._patches:
            other_repo = get_context().config.repo_dict.get(patch['repo'],
                                                            None)

            if not other_repo:
                logging.error('Could not find referenced repo. '
                              '(missing repo: %s, repo: %s, '
                              'patch entry: %s)',
                              patch['repo'],
                              self.name,
                              patch['id'])
                return 1

            path = os.path.join(other_repo.path, patch['path'])
            cmd = []

            if os.path.isfile(path):
                my_patches.append((path, patch['id']))
github siemens / kas / kas / repos.py View on Github external
if url is None:
            # No version control operation on repository
            if path is None:
                path = Repo.get_root_path(repo_fallback_path)
                logging.info('Using %s as root for repository %s', path,
                             name)

            url = path
            disable_operations = True
        else:
            if path is None:
                path = os.path.join(get_context().kas_work_dir, name)
            else:
                if not os.path.isabs(path):
                    # Relative pathes are assumed to start from work_dir
                    path = os.path.join(get_context().kas_work_dir, path)

        if typ == 'git':
            return GitRepo(url, path, refspec, layers, patches,
                           disable_operations)
        if typ == 'hg':
            return MercurialRepo(url, path, refspec, layers, patches,
                                 disable_operations)
        raise NotImplementedError('Repo type "%s" not supported.' % typ)
github siemens / kas / kas / repos.py View on Github external
def clone_cmd(self, gitsrcdir):
        cmd = ['git', 'clone', '-q', self.effective_url, self.path]
        if get_context().kas_repo_ref_dir and os.path.exists(gitsrcdir):
            cmd.extend(['--reference', gitsrcdir])
        return cmd
github siemens / kas / kas / repos.py View on Github external
def checkout(self):
        """
            Checks out the correct revision of the repo.
        """
        if self.operations_disabled or self.refspec is None:
            return

        if not get_context().force_checkout:
            # Check if repos is dirty
            (_, output) = run_cmd(self.is_dirty_cmd(),
                                  cwd=self.path,
                                  fail=False)
            if output:
                logging.warning('Repo %s is dirty - no checkout', self.name)
                return

        (_, output) = run_cmd(self.resolve_branch_cmd(),
                              cwd=self.path, fail=False)
        if output:
            desired_ref = output.strip()
            branch = True
        else:
            desired_ref = self.refspec
            branch = False
github siemens / kas / kas / repos.py View on Github external
cwd=self.path,
                fail=False,
                liveupdate=False)
            if retc != 0:
                logging.info('Changing remote URL to %s failed with error: %s',
                             self.effective_url, output.strip())
                return retc
        except NotImplementedError:
            logging.warning('Repo implementation does not support changing '
                            'the remote url.')

        # take what came out of clone and stick to that forever
        if self.refspec is None:
            return 0

        if not get_context().update:
            # Does refspec exist in the current repository?
            (retc, output) = await run_cmd_async(self.contains_refspec_cmd(),
                                                 cwd=self.path,
                                                 fail=False,
                                                 liveupdate=False)
            if retc == 0:
                logging.info('Repository %s already contains %s as %s',
                             self.name, self.refspec, output.strip())
                return retc

        # Try to fetch if refspec is missing or if --update argument was passed
        (retc, output) = await run_cmd_async(self.fetch_cmd(),
                                             cwd=self.path,
                                             fail=False)
        if retc:
            logging.warning('Could not update repository %s: %s',
github siemens / kas / kas / libkas.py View on Github external
async def run_cmd_async(cmd, cwd, env=None, fail=True, liveupdate=True):
    """
        Run a command asynchronously.
    """

    env = env or get_context().environ
    cmdstr = ' '.join(cmd)
    logging.info('%s$ %s', cwd, cmdstr)

    logo = LogOutput(liveupdate)

    try:
        process = await asyncio.create_subprocess_exec(
            *cmd,
            cwd=cwd,
            env=env,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE)
    except FileNotFoundError as ex:
        if fail:
            raise ex
        return (errno.ENOENT, str(ex))
github siemens / kas / kas / repos.py View on Github external
def checkout_cmd(self, desired_ref, branch):
        cmd = ['hg', 'checkout', desired_ref]
        if get_context().force_checkout:
            cmd.append('--clean')
        return cmd