Esempio n. 1
0
    def execute(self, name):
        assert name is not None

        cp_node = self.node['cp']

        for copy in cp_node:
            cp_command = ['cp', '-a', '-v']
            if isinstance(copy['source'], str):
                source = sanitize_input_variable(copy['source'])
                cp_command += [source]
            else:
                source = map(sanitize_input_variable, copy['source'])
                cp_command += source

            dest = sanitize_input_variable(copy['dest'])
            if not os.path.exists(dest):
                os.makedirs(dest)
            cp_command += [dest]

            logging.debug('Running copy command: %s', cp_command)

            if execute_sanitized('cp', cp_command, self.root) is None:
                return None

        return 0
Esempio n. 2
0
def dir_setup(opt):
    opt['Config']['code'] = promt(
        'Code direcotry:', sanitize_input_variable(opt['Config']['code']),
    ).strip()
    opt['Config']['usr'] = promt(
        'Usr direcotry:', sanitize_input_variable(opt['Config']['usr']),
    ).strip()

    with contextlib.suppress(FileExistsError):
        os.makedirs(opt['Config']['code'])
    with contextlib.suppress(FileExistsError):
        os.makedirs(opt['Config']['usr'])
Esempio n. 3
0
def venv_setup(opt):

    python_ver = promt('Python executable', 'python3')
    env_root = promt(
        'Virtual environment location',
        sanitize_input_variable(opt['Config']['venv']),
    )
    opt['Config']['venv'] = env_root

    with contextlib.suppress(FileExistsError):
        os.makedirs(os.path.abspath(os.path.join(env_root, os.pardir)))

    logging.info('Python version: %s', python_ver)
    logging.info('VEnv root:%s', env_root)

    venv_command = [
        'virtualenv',
        '--system-site-packages',
        '-p', python_ver,
        env_root,
    ]

    logging.info('Creating a virtual environment for code_manager')
    logging.debug(' '.join(venv_command))
    subprocess.Popen(venv_command).wait()

    with open(os.path.join(opt['Config']['code'], 'setenv.sh'), 'w') as file_handle:
        file_handle.write(get_default_setenv(opt))

    logging.info('The environment is now ready. You should source the\
\'setenv.sh\' file to use code_manager')
Esempio n. 4
0
    def append_manditory(self, attr, command):
        assert attr is not None
        assert isinstance(command, list)

        command.apend(sanitize_input_variable(self.node[attr]))

        return command
Esempio n. 5
0
    def append_optional(self, attr, command):
        assert attr is not None
        assert isinstance(command, list)

        self.get_optional(
            attr,
            lambda arg: command.append(sanitize_input_variable(arg)),
        )
        return command
Esempio n. 6
0
    def execute(self, name):
        assert name is not None

        ln_node = self.node['ln']

        for copy in ln_node:
            ln_command = ['sudo', 'ln', '-s', '-f']
            if isinstance(copy['source'], str):
                source = sanitize_input_variable(copy['source'])
                ln_command += [os.path.join(self.root, source)]

            dest = sanitize_input_variable(copy['dest'])
            ln_command += [dest]

            logging.debug('Running copy command: [%s]', ' '.join(ln_command))
            if execute_sanitized('ln', ln_command, self.root) is None:
                return None

        return 0
Esempio n. 7
0
    def execute(self, name):
        assert name is not None

        bash_lines = self.node['bash_lines']
        in_bashrc = self.node.get('in_bashrc', False)

        if isinstance(bash_lines, str):
            bash_lines = [sanitize_input_variable(bash_lines)]
        else:
            bash_lines = map(sanitize_input_variable, bash_lines)

        target_file = '~/.bashrc' if in_bashrc else os.path.join(
            self.code_dir,
            'setenv.sh',
        )
        target_file = sanitize_input_variable(target_file)

        with open(target_file) as target:
            target_file_lines = target.readlines()

        with open(target_file, 'a') as target:
            for line in bash_lines:

                if not BashrcInstaller._check_line(line):
                    debug_red(
                        'Skipping bash line.\
 The line is invalid: %s',
                        line,
                    )
                    continue

                if line in target_file_lines:
                    debug_red(
                        'Skipping bash line.\
 The line is already in the file: %s',
                        line,
                    )
                    continue
                logging.debug('Writing bashrc line in %s', target_file)
                target.write(line)

        return 0
Esempio n. 8
0
    def execute(self, name):
        assert name is not None

        paths = self.node['path']
        in_bashrc = self.node.get('in_bashrc', False)

        if isinstance(paths, str):
            paths = [paths]

        paths = map(
            lambda p: 'export LD_PATH=${{LD_PATH}}:{}'.format(
                sanitize_input_variable(p), ),
            paths,
        )

        target_file = '~/.bashrc' if in_bashrc else os.path.join(
            self.code_dir,
            'setenv.sh',
        )
        target_file = sanitize_input_variable(target_file)

        with open(target_file) as target:
            target_file_lines = target.read().splitlines()

        with open(target_file, 'a') as target:
            for line in list(paths):
                if line in target_file_lines:
                    debug_red(
                        'Skipping library path export line.\
The line is already in the file: %s',
                        line,
                    )
                    continue

                logging.debug('Adding library export in %s', target_file)
                target.write(line)
                target.write('\n')

        return 0
Esempio n. 9
0
def venv_check(opt):
    if is_venv():
        env = venv()
        cm_env = sanitize_input_variable(opt['Config']['venv'])
        if env != cm_env:
            logging.info("The activated virtual environment is not the one of code manageger.\
You have to source the 'setenv.sh' fist.")
            raise SystemExit
    else:
        logging.info(
            "No virtual environment is active. You have to source the 'setenv.sh' fist.",
        )
        raise SystemExit
Esempio n. 10
0
def setup_logging(args, opt):
    assert args is not None
    assert opt is not None

    logging.getLogger().handlers = []

    logger = logging.getLogger()

    if args.debug:
        logger.setLevel(logging.DEBUG)
    elif args.verbose:
        logger.setLevel(logging.VERBOSE)
    else:
        logger.setLevel(logging.INFO)

    if 'Logging' in opt.keys() and 'direcory' in opt['Logging']:

        path_to_log_directory = sanitize_input_variable(opt['Logging']['Direcory'])
        if not os.path.exists(path_to_log_directory):
            os.makedirs(path_to_log_directory)

        # Rotating file logging
        formatter = logging.Formatter(
            '%(asctime)s [%(levelname)s] %(message)s',
        )
        log_filename = datetime.datetime.now().strftime('%Y-%m-%d') + '.log'
        log_filename = os.path.join(path_to_log_directory, log_filename)

        rot = logging.handlers.RotatingFileHandler(
            log_filename, maxBytes=100 * 1024 * 1024, backupCount=10,
        )
        rot.setLevel(logging.VERBOSE)
        rot.setFormatter(formatter)
        logger.addHandler(rot)

    # Info Logging
    formatter = logging.Formatter(
        '{1}%(asctime)s{2} [{0}%(levelname)s{2}] %(message)s'.format(
            CYAN, GREEN, RESET,
        ),
    )
    sh_info = logging.StreamHandler(sys.stdout)
    sh_info.setFormatter(formatter)
    logger.addHandler(sh_info)

    # Exit on CRITICAL
    logger.addHandler(ShutdownHandler(level=50))
Esempio n. 11
0
    def _load_pack(pack, config):
        ConfigurationAware.sources.append(sanitize_input_variable(pack))

        con = None
        if is_link(pack):
            logging.debug(
                'Loading extra packages.json from link: %s',
                pack,
            )
            con = ConfigurationAware._load_pack_from_link(pack)
        elif os.path.exists(pack) and os.path.isfile(pack):
            logging.debug(
                'Loading extra packages file: %s',
                os.path.abspath(pack, ),
            )
            con = ConfigurationAware._load_pack_from_file(pack)

        if con is not None:
            config = ConfigurationAware._load_extra_pack(config, con)
Esempio n. 12
0
    def set_configuration(opt, args):

        ConfigurationAware.opt = opt
        ConfigurationAware.args = args

        ConfigurationAware.config_dir = sanitize_input_variable(
            code_manager.CONFDIR)

        try:
            ConfigurationAware.usr_dir = sanitize_input_variable(
                opt['Config']['usr'])
            ConfigurationAware.code_dir = sanitize_input_variable(
                opt['Config']['code'])
        except KeyError:
            logging.fatal(
                '\'usr\' and \'code\' are manditory fields in the configuration'
            )

        code_dir_env = os.environ['CODE_DIR']
        if code_dir_env:
            ConfigurationAware.code_dir = sanitize_input_variable(code_dir_env)

        usr_dir_env = os.environ['USR_DIR']
        if usr_dir_env:
            ConfigurationAware.usr_dir = sanitize_input_variable(usr_dir_env)

        if args.code_dir:
            ConfigurationAware.code_dir = sanitize_input_variable(
                args.code_dir)

        if args.usr_dir:
            ConfigurationAware.code_dir = sanitize_input_variable(
                args.code_dir)

        ConfigurationAware.resolver = CofigurationResolver()

        config = {}
        config['vars'] = {}
        config['packages_list'] = {}
        config['debian_packages'] = {}
        config['packages_config'] = {}
        config['packages'] = {}

        ConfigurationAware.sources = []
        package_sources = sanitize_input_variable(opt['Config'].get(
            'sources', code_manager.SOURCEFILE))
        if os.path.isfile(package_sources):
            with open(package_sources) as source_fd:
                for pack in source_fd.read().splitlines():
                    ConfigurationAware._load_pack(pack.strip(), config)

        if hasattr(args, 'packs'):
            for pack in args.packs:
                ConfigurationAware._load_pack(pack, config)

        ConfigurationAware.config = ConfigurationAware.resolver.configuration_dict(
            config, )

        ConfigurationAware.packages_list = ConfigurationAware.config[
            'packages_list']
        ConfigurationAware.packages = ConfigurationAware.config['packages']

        ConfigurationAware.packages_config = ConfigurationAware.config.get(
            'packages_config',
            {},
        )

        ConfigurationAware.variables = ConfigurationAware.resolver.variables

        ConfigurationAware.install_scripts_dir = sanitize_input_variable(
            opt['Config'].get(
                'install_scripts',
                code_manager.SCRIPTSDIR,
            ), )

        ConfigurationAware.cache_file = sanitize_input_variable(
            opt['Config'].get('cache', code_manager.CACHEFILE))

        ConfigurationAware.debug = ('debug' in opt['Config'].keys()
                                    and opt['Config']['debug'] == 'true')
        ConfigurationAware.git_ssh = ('git_ssh' in opt['Download'].keys()
                                      and opt['Download']['git_ssh'] == 'true')
Esempio n. 13
0
 def test_sanitize_input_variable(self):
     self.assertEqual(os.environ['HOME'], sanitize_input_variable('~'))
     self.assertEqual(os.environ['PATH'], sanitize_input_variable('$PATH'))