def test_can_download_complete_project_from_project_url(self):
     for project_url, project_id in common_testing.TEST_PROJECT_URL_TO_ID_MAP.iteritems():
         self._set_testresult_folder_subdir(project_id)
         result_folder_path = self._testresult_folder_path
         scratchwebapi.download_project(project_url, result_folder_path)
         # TODO: replace with verifying function
         assert scratch.Project(result_folder_path)
 def test_fail_on_wrong_url(self):
     for wrong_url in ['http://www.tugraz.at', 'http://www.ist.tugraz.at/', 'http://scratch.mit.edu/', 'http://scratch.mit.edu/projects']:
         with self.assertRaises(common.ScratchtobatError):
             scratchwebapi.download_project(wrong_url, None)
Exemple #3
0
def run_converter(scratch_project_file_or_url, output_dir, extract_resulting_catrobat=False, temp_rm=True, show_version_only=False, show_info_only=False):
    def check_base_environment():
        if "java" not in sys.platform:
            raise EnvironmentError("Must be called with Jython interpreter.")
        if System.getProperty(helpers.JYTHON_RESPECT_JAVA_ACCESSIBILITY_PROPERTY) != 'false':
            raise EnvironmentError("Jython registry property '%s' must be set to 'false'." % helpers.JYTHON_RESPECT_JAVA_ACCESSIBILITY_PROPERTY)

    def check_converter_environment():
        # TODO: refactor to combined class with explicit environment check method
        tools.svgtopng._checked_batik_jar_path()
        tools.wavconverter._checked_sox_path()

    try:
        from java.io import IOError
        from java.lang import System
    except ImportError:
        log.error("Must be called with Jython interpreter.")
        return helpers.ExitCode.FAILURE

    # nested import to be able to check for Jython interpreter first
    from scratchtocatrobat import catrobat, common, converter, scratch, scratchwebapi, tools

    try:
        check_base_environment()
        check_converter_environment()

        tag_name = helpers.tag_name_of_used_catroid_hierarchy()
        latest_release_data = helpers.latest_catroid_repository_release_data()
        if show_version_only or show_info_only:
            helpers.print_info_or_version_screen(show_version_only, catrobat.CATROBAT_LANGUAGE_VERSION)
            return helpers.ExitCode.SUCCESS
        elif latest_release_data and tag_name != latest_release_data["tag_name"]:
            print("Latest Catroid release: %s (%s)" % (latest_release_data["tag_name"], latest_release_data["published_at"]))
            print("%sA NEW CATROID RELEASE IS AVAILABLE!\nPLEASE UPDATE THE CLASS HIERARCHY OF THE CONVERTER FROM CATROID VERSION %s TO VERSION %s%s" % (helpers.cli_colors.FAIL, tag_name, latest_release_data["tag_name"], helpers.cli_colors.ENDC))

        log.info("calling converter")
        if not os.path.isdir(output_dir):
            raise EnvironmentError("Output folder must be a directory, but is %s" % output_dir)

        with common.TemporaryDirectory(remove_on_exit=temp_rm) as scratch_project_dir:
            if scratch_project_file_or_url.startswith("http://"):
                log.info("Downloading project from URL: '{}' to temp dir {} ...".format(scratch_project_file_or_url, scratch_project_dir))
                scratchwebapi.download_project(scratch_project_file_or_url, scratch_project_dir)
            elif os.path.isfile(scratch_project_file_or_url):
                log.info("Extracting project from path: '{}' ...".format(scratch_project_file_or_url))
                common.extract(scratch_project_file_or_url, scratch_project_dir)
            else:
                log.info("Loading project from path: '{}' ...".format(scratch_project_file_or_url))
                scratch_project_dir = scratch_project_file_or_url

            project = scratch.Project(scratch_project_dir)
            log.info("Converting scratch project '%s' into output folder: %s", project.name, output_dir)
            converted_project = converter.converted(project)
            catrobat_program_path = converted_project.save_as_catrobat_package_to(output_dir)
            if extract_resulting_catrobat:
                extraction_path = os.path.join(output_dir, os.path.splitext(os.path.basename(catrobat_program_path))[0])
                common.rm_dir(extraction_path)
                common.makedirs(extraction_path)
                scratch_output_path = os.path.join(extraction_path, "scratch")
                common.copy_dir(scratch_project_dir, scratch_output_path, overwrite=True)
                common.extract(catrobat_program_path, extraction_path)

    except (common.ScratchtobatError, EnvironmentError, IOError) as e:
        log.error(e)
        return helpers.ExitCode.FAILURE
    except Exception as e:
        log.exception(e)
        return helpers.ExitCode.FAILURE
    return helpers.ExitCode.SUCCESS
Exemple #4
0
def run_converter(scratch_project_file_or_url, output_dir, extract_resulting_catrobat=False, temp_rm=True, show_version_only=False):
    # import pudb; pu.db

    def check_base_environment():
        if "java" not in sys.platform:
            raise EnvironmentError("Must be called with Jython interpreter.")
        if System.getProperty(_JYTHON_RESPECT_JAVA_ACCESSIBILITY_PROPERTY) != 'false':
            raise EnvironmentError("Jython registry property '%s' must be set to 'false'." % _JYTHON_RESPECT_JAVA_ACCESSIBILITY_PROPERTY)

    def check_converter_environment():
        # TODO: refactor to combined class with explicit environment check method
        tools.svgtopng._checked_batik_jar_path()
        tools.wavconverter._checked_sox_path()

    try:
        from java.io import IOError
        from java.lang import System
    except ImportError:
        log.error("Must be called with Jython interpreter.")
        return EXIT_FAILURE

    # nested import to be able to check for Jython interpreter first
    from scratchtocatrobat import catrobat
    from scratchtocatrobat import common
    from scratchtocatrobat import converter
    from scratchtocatrobat import scratch
    from scratchtocatrobat import scratchwebapi
    from scratchtocatrobat import tools

    try:
        check_base_environment()
        check_converter_environment()

        import org.catrobat.catroid.common as catcommon
        if show_version_only:
            # TODO: should return last modfication date or source control tag of Catrobat classes
            print("Catrobat language version:", catrobat.CATROBAT_LANGUAGE_VERSION)
        else:
            log.info("calling converter")
            if not os.path.isdir(output_dir):
                raise EnvironmentError("Output folder must be a directory, but is %s" % output_dir)
            with common.TemporaryDirectory(remove_on_exit=temp_rm) as scratch_project_dir:
                if scratch_project_file_or_url.startswith("http://"):
                    log.info("Downloading project from URL: '{}' to temp dir {} ...".format(scratch_project_file_or_url, scratch_project_dir))
                    scratchwebapi.download_project(scratch_project_file_or_url, scratch_project_dir)
                elif os.path.isfile(scratch_project_file_or_url):
                    log.info("Extracting project from path: '{}' ...".format(scratch_project_file_or_url))
                    common.extract(scratch_project_file_or_url, scratch_project_dir)
                else:
                    log.info("Loading project from path: '{}' ...".format(scratch_project_file_or_url))
                    scratch_project_dir = scratch_project_file_or_url
                project = scratch.Project(scratch_project_dir)
                log.info("Converting scratch project '%s' into output folder: %s", project.name, output_dir)
                converted_project = converter.converted(project)
                catrobat_program_path = converted_project.save_as_catrobat_package_to(output_dir)
                if extract_resulting_catrobat:
                    extraction_path = os.path.join(output_dir, os.path.splitext(os.path.basename(catrobat_program_path))[0])
                    if os.path.exists(extraction_path):
                        shutil.rmtree(extraction_path)
                    common.makedirs(extraction_path)
                    common.extract(catrobat_program_path, extraction_path)
    except (common.ScratchtobatError, EnvironmentError, IOError) as e:
        log.error(e)
        return EXIT_FAILURE
    except Exception as e:
        log.exception(e)
        return EXIT_FAILURE
    return EXIT_SUCCESS