Esempio n. 1
0
    def save_as_catrobat_package_to(self, output_dir):

        def iter_dir(path):
            for root, _, files in os.walk(path):
                for file_ in files:
                    yield os.path.join(root, file_)
        log.info("convert Scratch project to '%s'", output_dir)
        with common.TemporaryDirectory() as catrobat_program_dir:
            self.save_as_catrobat_directory_structure_to(catrobat_program_dir)
            common.makedirs(output_dir)
            catrobat_zip_file_path = self._converted_output_path(output_dir, self.name)
            log.info("  save packaged Scratch project to '%s'", catrobat_zip_file_path)
            if os.path.exists(catrobat_zip_file_path):
                os.remove(catrobat_zip_file_path)
            with zipfile.ZipFile(catrobat_zip_file_path, 'w') as zip_fp:
                for file_path in iter_dir(unicode(catrobat_program_dir)):
                    assert isinstance(file_path, unicode)
                    path_inside_zip = file_path.replace(catrobat_program_dir, u"")
                    zip_fp.write(file_path, path_inside_zip)
            assert os.path.exists(catrobat_zip_file_path), "Catrobat package not written: %s" % catrobat_zip_file_path
        return catrobat_zip_file_path
Esempio n. 2
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
Esempio n. 3
0
 def _testresult_folder_path(self):
     folder_path = self.__testresult_base_path
     if self.__testresult_folder_subdir is not None:
         folder_path = os.path.join(folder_path, self.__testresult_folder_subdir)
     common.makedirs(folder_path)
     return folder_path
Esempio 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