예제 #1
0
def create_distribution_bundle_file():
    '''Creates a distribution bundle file as zip archiv.'''
    if not SCOPE['scripts'].get('build:export', SCOPE['scripts'].get(
        'build', False
    )) or Platform.run('/usr/bin/env yarn %s' % (
        'build:export' if SCOPE['scripts'].get('build:export') else 'build'
    ), error=False, log=True)['return_code'] == 0:
        __logger__.info('Pack to a zip archive.')
        distribution_bundle_file = FileHandler(
            location=make_secure_temporary_file()[1])
        current_directory_path = FileHandler()._path
        file_path_list = SCOPE.get('files', [])
        if 'main' in SCOPE:
            file_path_list.append(SCOPE['main'])
        if len(file_path_list) == 0:
            return None
        with zipfile.ZipFile(
            distribution_bundle_file.path, 'w'
        ) as zip_file:
            for file_path in file_path_list:
                file = FileHandler(location=file_path)
                __logger__.debug(
                    'Add "%s" to distribution bundle.', file.path)
                zip_file.write(file._path, file.name)
                if file.is_directory() and not is_file_ignored(file):
                    def add(sub_file):
                        if is_file_ignored(sub_file):
                            return None
                        __logger__.debug(
                            'Add "%s" to distribution bundle.', sub_file.path)
                        zip_file.write(sub_file._path, sub_file._path[len(
                            current_directory_path):])
                        return True
                    file.iterate_directory(function=add, recursive=True)
        return distribution_bundle_file
예제 #2
0
    def _run_command(self, command_name, command):
# #
        '''
            Runs the given command by printing out what is running by \
            presenting there results.

            Examples:

            >>> __test_buffer__.clear() # doctest: +ELLIPSIS
            '...'
            >>> FileHandler('temp_run_command_main.py').content = ''
            >>> run = Run()

            >>> run._run_command('list', 'ls') # doctest: +SKIP
            Object of "Run" with detected path "...".
            >>> __test_buffer__.clear() # doctest: +SKIP
            'List with "ls". output [...codeRunner...]'

            >>> run._run_command(
            ...     'do nothing', 'not_existing'
            ... ) # doctest: +IGNORE_EXCEPTION_DETAIL
            Traceback (most recent call last):
            ...
            SystemExit: 127
        '''
        return_code = self._log_command_run(
            command_name, command,
            result=Platform.run(
                command=command.strip(), shell=True, error=False))
        if return_code != 0:
            sys.exit(return_code)
        return self
def main():
    '''Generates a python api documentation website.'''
    if FileHandler('documentation').is_directory():
        current_working_directory = FileHandler()
        index_file = FileHandler('documentation/source/index.rst')
        modules_to_document = '\ninit'
        FileHandler(
            location='%sinit.rst' % index_file.directory.path
        ).content = (
            (79 * '=') + '\n{name}\n' + (79 * '=') + '\n\n.. automodule::' +
            ' {name}\n    :members:'
        ).format(name=current_working_directory.name)
        for file in FileHandler():
            if Module.is_package(file.path):
                modules_to_document += '\n    %s' % file.name
                FileHandler(location='%s%s.rst' % (
                    index_file.directory.path, file.name
                )).content = (
                    (79 * '=') + '\n{name}.{package}\n' +
                    (79 * '=') + '\n\n.. automodule:: {name}.{package}\n'
                    '    :members:'
                ).format(
                    name=current_working_directory.name, package=file.name)
                for module in __get_all_modules__(file.path):
                    modules_to_document += '\n    %s.%s' % (file.name, module)
                    FileHandler(location='%s%s.%s.rst' % (
                        index_file.directory.path, file.name, module
                    )).content = (
                        (79 * '=') + '\n{name}.{package}.{module}\n' +
                        (79 * '=') + '\n\n.. automodule:: {name}.{package}.'
                        '{module}\n    :members:'
                    ).format(
                        name=current_working_directory.name,
                        package=file.name, module=module)
        index_file.content = regularExpression.compile(
            '\n    ([a-z][a-zA-Z]+\n)+$', regularExpression.DOTALL
        ).sub(modules_to_document, index_file.content)
        Platform.run('/usr/bin/env git add --all', error=False, log=True)
        FileHandler('documentation').change_working_directory()
        makefile = FileHandler('Makefile')
# # python3.5         FileHandler('MakefilePython3').copy(makefile)
        FileHandler('MakefilePython2').copy(makefile)
        Platform.run(
            command='make html', native_shell=True, error=False, log=True)
        makefile.remove_file()
        FileHandler('build/html').path = '../apiDocumentation'
        FileHandler('build').remove_deep()
예제 #4
0
def main():
    '''Entry point for this script.'''
    global API_DOCUMENTATION_PATH_SUFFIX, CONTENT, SCOPE
    if markdown is None:
        __logger__.critical(
            "You haven't install a suitable markdown version. Documentation "
            "couldn't be updated.")
        return None
    CommandLine.argument_parser(module_name=__name__)
    if '* master' in Platform.run('/usr/bin/env git branch')[
        'standard_output'
    ] and 'gh-pages' in Platform.run('/usr/bin/env git branch --all')[
        'standard_output'
    ]:
        package_file = FileHandler('package.json')
        if package_file.is_file():
            SCOPE = json.loads(package_file.content)
        API_DOCUMENTATION_PATH_SUFFIX = API_DOCUMENTATION_PATH_SUFFIX.format(
            **SCOPE)
        temporary_documentation_folder = FileHandler(
            location=DOCUMENTATION_REPOSITORY[DOCUMENTATION_REPOSITORY.find(
                '/'
            ) + 1:-1])
        if temporary_documentation_folder:
            temporary_documentation_folder.remove_deep()
        __logger__.info('Compile all readme markdown files to html5.')
        FileHandler().iterate_directory(function=add_readme, recursive=True)
        CONTENT = markdown.markdown(
            CONTENT, output='html5',
            extensions=builtins.list(MARKDOWN_EXTENSIONS))
        distribution_bundle_file = create_distribution_bundle_file()
        if distribution_bundle_file is not None:
            data_location = FileHandler(location=DATA_PATH)
            data_location.make_directories()
            distribution_bundle_file.directory = data_location
        has_api_documentation = SCOPE['scripts'].get('document', False)
        if has_api_documentation:
            has_api_documentation = Platform.run(
                '/usr/bin/env yarn document', error=False, log=True
            )['return_code'] == 0
        if Platform.run(
            ('/usr/bin/env git checkout gh-pages', '/usr/bin/env git pull'),
            error=False, log=True
        )['return_code'][0] == 0:
            existing_api_documentation_directory = FileHandler(location='.%s' %
                API_DOCUMENTATION_PATH[1])
            if existing_api_documentation_directory.is_directory():
                existing_api_documentation_directory.remove_deep()
            FileHandler(location=API_DOCUMENTATION_PATH[0]).path = \
                existing_api_documentation_directory
            local_documentation_website_location = FileHandler(
                location='../%s' % temporary_documentation_folder.name)
            if local_documentation_website_location.is_directory():
                temporary_documentation_folder.make_directories()
                local_documentation_website_location.iterate_directory(
                    function=copy_repository_file, recursive=True,
                    source=local_documentation_website_location,
                    target=temporary_documentation_folder)
                node_modules_directory = FileHandler(location='%s%s' % (
                    local_documentation_website_location.path, 'node_modules'))
                if node_modules_directory.is_directory():
                    temporary_documentation_node_modules_directory = \
                        FileHandler('%snode_modules' %
                            temporary_documentation_folder.path)
                    '''
                        NOTE: Symlinking doesn't work since some node modules
                        need the right absolute location to work.

                        node_modules_directory.make_symbolic_link(
                            target='%s%s' % (
                                temporary_documentation_folder, 'node_modules')
                        )
                        return_code = 0

                        NOTE: Coping complete "node_modules" folder takes to
                        long.

                        node_modules_directory.copy(target='%s%s' % (
                            temporary_documentation_folder, 'node_modules'))
                        return_code = 0

                        NOTE: Mounting "node_modules" folder needs root
                        privileges.

                        temporary_documentation_node_modules_directory\
                            .make_directory(right=777)
                        return_code = Platform.run(
                            "/usr/bin/env sudo mount --bind --options ro '%s' "
                            "'%s'" % (
                                node_modules_directory.path,
                                temporary_documentation_node_modules_directory.path
                            ), native_shell=True, error=False, log=True
                        )['return_code']
                    '''
                    return_code = Platform.run(
                        "/usr/bin/env cp --dereference --recursive --reflink=auto '%s' '%s'" % (
                            node_modules_directory.path,
                            temporary_documentation_node_modules_directory.path
                        ),
                        native_shell=True,
                        error=False,
                        log=True
                    )['return_code']
                else:
                    return_code = Platform.run(
                        '/usr/bin/env yarn --production=false',
                        native_shell=True,
                        error=False,
                        log=True
                    )['return_code']
                if return_code == 0:
                    current_working_directory_backup = FileHandler()
                    temporary_documentation_folder.change_working_directory()
                    return_code = Platform.run(
                        '/usr/bin/env yarn clear', native_shell=True,
                        error=False, log=True
                    )['return_code']
                    current_working_directory_backup.change_working_directory()
            else:
                return_code = Platform.run((
                    'unset GIT_WORK_TREE; /usr/bin/env git clone %s;'
                    'yarn --production=false'
                ) % DOCUMENTATION_REPOSITORY, native_shell=True, error=False,
                log=True)['return_code']
            if return_code == 0:
                generate_and_push_new_documentation_page(
                    temporary_documentation_folder,
                    distribution_bundle_file,
                    has_api_documentation,
                    temporary_documentation_node_modules_directory)
            if existing_api_documentation_directory.is_directory():
                existing_api_documentation_directory.remove_deep()
예제 #5
0
def generate_and_push_new_documentation_page(
    temporary_documentation_folder,
    distribution_bundle_file,
    has_api_documentation,
    temporary_documentation_node_modules_directory
):
# #
    '''
        Renders a new index.html file and copies new assets to generate a new \
        documentation homepage.
    '''
    global BUILD_DOCUMENTATION_PAGE_COMMAND
    __logger__.info('Update documentation design.')
    if distribution_bundle_file:
        new_distribution_bundle_file = FileHandler(location='%s%s%s' % (
            temporary_documentation_folder.path, DOCUMENTATION_BUILD_PATH,
            DISTRIBUTION_BUNDLE_FILE_PATH))
        new_distribution_bundle_file.directory.make_directories()
        distribution_bundle_file.path = new_distribution_bundle_file
        new_distribution_bundle_directory = FileHandler(location='%s%s%s' % (
            temporary_documentation_folder.path, DOCUMENTATION_BUILD_PATH,
            DISTRIBUTION_BUNDLE_DIRECTORY_PATH))
        new_distribution_bundle_directory.make_directories()
        zipfile.ZipFile(distribution_bundle_file.path).extractall(
            new_distribution_bundle_directory.path)
    favicon = FileHandler(location='favicon.png')
    if favicon:
        favicon.copy(target='%s/source/image/favicon.ico' %
            temporary_documentation_folder.path)
    parameter = builtins.dict(builtins.map(lambda item: (
        String(item[0]).camel_case_to_delimited.content.upper(), item[1]
    ), SCOPE.get('documentationWebsite', {}).items()))
    if 'TAGLINE' not in parameter and 'description' in SCOPE:
        parameter['TAGLINE'] = SCOPE['description']
    if 'NAME' not in parameter and 'name' in SCOPE:
        parameter['NAME'] = SCOPE['name']
    __logger__.debug('Found parameter "%s".', json.dumps(parameter))
    api_documentation_path = None
    if has_api_documentation:
        api_documentation_path = '%s%s' % (
            API_DOCUMENTATION_PATH[1], API_DOCUMENTATION_PATH_SUFFIX)
        if not FileHandler(location='%s%s' % (
            FileHandler().path, api_documentation_path
        )).is_directory():
            api_documentation_path = API_DOCUMENTATION_PATH[1]
    parameter.update({
        'CONTENT': CONTENT,
        'CONTENT_FILE_PATH': None,
        'RENDER_CONTENT': False,
        'API_DOCUMENTATION_PATH': api_documentation_path,
        'DISTRIBUTION_BUNDLE_FILE_PATH': DISTRIBUTION_BUNDLE_FILE_PATH if (
            distribution_bundle_file and
            distribution_bundle_file.is_file()
        ) else None
    })
# # python3.5
# #     parameter = Dictionary(parameter).convert(
# #         value_wrapper=lambda key, value: value.replace(
# #             '!', '#%%%#'
# #         ) if builtins.isinstance(value, builtins.str) else value
# #     ).content
    parameter = Dictionary(parameter).convert(
        value_wrapper=lambda key, value: value.replace(
            '!', '#%%%#'
        ) if builtins.isinstance(value, builtins.unicode) else value
    ).content
# #
    if __logger__.isEnabledFor(logging.DEBUG):
        BUILD_DOCUMENTATION_PAGE_COMMAND = \
            BUILD_DOCUMENTATION_PAGE_COMMAND[:-1] + [
                '-debug'
            ] + BUILD_DOCUMENTATION_PAGE_COMMAND[-1:]
    serialized_parameter = json.dumps(parameter)
    parameter_file = FileHandler(location=make_secure_temporary_file('.json')[
        1])
    parameter_file.content = \
        BUILD_DOCUMENTATION_PAGE_PARAMETER_TEMPLATE.format(
            serializedParameter=serialized_parameter, **SCOPE)
    for index, command in builtins.enumerate(BUILD_DOCUMENTATION_PAGE_COMMAND):
        BUILD_DOCUMENTATION_PAGE_COMMAND[index] = \
            BUILD_DOCUMENTATION_PAGE_COMMAND[index].format(
                serializedParameter=serialized_parameter,
                parameterFilePath=parameter_file._path,
                **SCOPE)
    __logger__.debug('Use parameter "%s".', serialized_parameter)
    __logger__.info('Run "%s".', ' '.join(BUILD_DOCUMENTATION_PAGE_COMMAND))
    current_working_directory_backup = FileHandler()
    temporary_documentation_folder.change_working_directory()
    Platform.run(
        command=BUILD_DOCUMENTATION_PAGE_COMMAND[0],
        command_arguments=BUILD_DOCUMENTATION_PAGE_COMMAND[1:], error=False,
        log=True)
    current_working_directory_backup.change_working_directory()
    parameter_file.remove_file()
    for file in FileHandler():
        if not (file in (temporary_documentation_folder, FileHandler(
            location='.%s' % API_DOCUMENTATION_PATH[1]
        )) or is_file_ignored(file)):
            file.remove_deep()
    documentation_build_folder = FileHandler(location='%s%s' % (
        temporary_documentation_folder.path, DOCUMENTATION_BUILD_PATH
    ), must_exist=True)
    documentation_build_folder.iterate_directory(
        function=copy_repository_file, recursive=True,
        source=documentation_build_folder, target=FileHandler())
    if (Platform.run(
        "/usr/bin/env sudo umount '%s'" %
            temporary_documentation_node_modules_directory.path,
        native_shell=True, error=False, log=True
    )['return_code'] == 0):
        temporary_documentation_folder.remove_deep()
    Platform.run(
        (
            '/usr/bin/env git add --all',
            '/usr/bin/env git commit --message "%s" --all' %
                PROJECT_PAGE_COMMIT_MESSAGE,
            '/usr/bin/env git push',
            '/usr/bin/env git checkout master'
        ),
        native_shell=True,
        error=False,
        log=True
    )