Ejemplo n.º 1
0
 def _test_project(self, project_name):
     if os.path.splitext(project_name)[1]:
         tempdir = common.TemporaryDirectory()
         scratch_project_dir = tempdir.name
         self.addCleanup(tempdir.cleanup)
         common.extract(common_testing.get_test_project_packed_file(project_name), scratch_project_dir)
     else:
         scratch_project_dir = common_testing.get_test_project_path(project_name)
     scratch_project = scratch.Project(scratch_project_dir, name=project_name, id_=common_testing.PROJECT_DUMMY_ID)
     converted_project = converter.converted(scratch_project)
     catrobat_zip_file_name = converted_project.save_as_catrobat_package_to(self._testresult_folder_path)
     self.assertValidCatrobatProgramPackageAndUnpackIf(catrobat_zip_file_name, project_name, unused_scratch_resources=scratch_project.unused_resource_names)
     return converted_project.catrobat_program
Ejemplo n.º 2
0
 def test_can_extract_project(self):
     for project_file in common_testing.TEST_PROJECT_FILENAME_TO_ID_MAP:
         common.extract(common_testing.get_test_project_packed_file(project_file), self.temp_dir)
         assert scratch.Project(self.temp_dir)
Ejemplo n.º 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
Ejemplo n.º 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