コード例 #1
0
    def create_token(self):
        import getpass
        import github3
        # Asks user for username/password
        user = input("Please enter your GitHub username: "******"Please enter your GitHub password: "******"/!\\ WARNING /!\\ Bad GitHub credentials.")
            print(
                "Cannot access disease packages. Please contact %s for assistance."
                .format(self.SUPPORT_EMAIL))
            sys.stdout.flush()
            raise self.AuthorizationError()

        # Write the info to disk
        # Update the (local) mysql db with the token
        from simtools.DataAccess.DataStore import DataStore
        DataStore.save_setting(
            DataStore.create_setting(key=self.auth_token_field,
                                     value=auth.token))

        return auth.token
コード例 #2
0
    def check_overseer():
        """
        Ensure that the overseer thread is running.
        The thread pid is retrieved from the settings and then we test if it corresponds to a python thread.
        If not, just start it.
        """
        logger.debug("Checking Overseer state")
        setting = DataStore.get_setting('overseer_pid')
        overseer_pid = int(setting.value) if setting else None

        # Launch the Overseer if needed
        if is_running(overseer_pid, name_part='python'):
            logger.debug("A valid Overseer was detected, pid: %d" %
                         overseer_pid)
        else:
            logger.debug(
                "A valid Overseer was not detected for stored pid %s." %
                overseer_pid)
            current_dir = os.path.dirname(os.path.realpath(__file__))
            runner_path = os.path.abspath(
                os.path.join(current_dir, '..', 'Overseer.py'))
            if LocalOS.name == LocalOS.WINDOWS:
                p = subprocess.Popen([sys.executable, runner_path],
                                     shell=False,
                                     creationflags=512)
            else:
                p = subprocess.Popen([sys.executable, runner_path],
                                     shell=False)

            # Save the pid in the settings
            DataStore.save_setting(
                DataStore.create_setting(key='overseer_pid', value=str(p.pid)))
コード例 #3
0
def LogCleaner():
    # Get the last time a cleanup happened
    last_cleanup = DataStore.get_setting('last_log_cleanup')
    if not last_cleanup or (datetime.today() - datetime.strptime(last_cleanup.value.split(' ')[0],'%Y-%m-%d')).days > 1:
        # Do the cleanup
        from simtools.DataAccess.LoggingDataStore import LoggingDataStore
        LoggingDataStore.log_cleanup()
        DataStore.save_setting(DataStore.create_setting(key='last_log_cleanup', value=datetime.today()))
コード例 #4
0
ファイル: commands.py プロジェクト: m-v-nikolov/dtk-tools-py3
def get_package(args, unknownArgs):
    import pip
    import tempfile
    import zipfile
    is_test = args.is_test if hasattr(
        args, 'is_test') else None  # test == no pip install

    # overwrite any existing package by the same name (any version) with the specified version
    release_dir = None
    try:
        package_name = args.package_name
        github = DTKGitHub(disease=package_name)

        if args.package_version.upper() == GitHub.HEAD:
            version = GitHub.HEAD
        elif args.package_version.lower() == 'latest':
            version = github.get_latest_version(
            )  # if no versions exist, returns None
        elif github.version_exists(version=args.package_version):
            version = args.package_version
        else:
            version = None

        if version is None:
            print(
                "Requested version: %s for package: %s does not exist. No changes made."
                % (args.package_version, package_name))
            return

        tempdir = tempfile.mkdtemp()
        zip_filename = github.get_zip(version=version, destination=tempdir)

        # Unzip the obtained zip
        zip_ref = zipfile.ZipFile(zip_filename, 'r')
        zip_ref.extractall(tempdir)
        zip_ref.close()
        os.remove(zip_filename)

        # Identify the unziped source directory
        source_dir_candidates = [
            f for f in os.listdir(tempdir)
            if 'InstituteforDiseaseModeling-%s' %
            DTKGitHub.PACKAGE_LIST[package_name] in f
        ]
        if len(source_dir_candidates) != 1:
            raise Exception('Failure to identify package to install')
        else:
            source_dir = source_dir_candidates[0]
        release_dir = os.path.join(os.path.dirname(zip_filename), source_dir)

        # edit source dir setup file for the correct version
        setup_file = os.path.join(release_dir, 'setup.py')
        with open(setup_file, 'r') as file:
            text = file.read()
        text = text.replace('$VERSION$', str(version))
        with open(setup_file, 'w') as file:
            file.write(text)

        # install
        if not is_test:
            pip.main([
                'install', '--no-dependencies', '--ignore-installed',
                release_dir
            ])

        # update the local DB with the version
        db_key = github.disease_package_db_key
        DataStore.save_setting(
            DataStore.create_setting(key=db_key, value=str(version)))
        shutil.rmtree(tempdir)
    except GitHub.AuthorizationError:
        pass

    return release_dir