示例#1
0
def remove_unused_images(dirpath, extensions='*.png', cmd=''):
    """
    List/remove unused images

    :param dirpath: Directory where to search for unused images
    :param extensions: Extensions to include in search. Example: "*.png", "*.png,*.gif"
    :param cmd: Command to execute on unsed images. For example: "git rm", "rm"
                The file path is appended in the end of the command. By default, just prints the image path

    Example execution::

        fab devel.remove_unused_images:"themes/default/",cmd="git rm"

    """
    dirpath = os.path.abspath(dirpath or os.path.curdir)
    logger.info('Looking for unused images: %s' % dirpath)

    # Iterate images
    for imagepath in get_files(dirpath, extensions, recursive=True):
        logger.debug('Processing image: %s' % imagepath)
        image = os.path.basename(imagepath)

        with settings(hide('running', 'stderr'), warn_only=True):
            # Run inside the dirpath
            with lcd(dirpath):
                # Check if image can be found from source files
                findcmd = 'grin "%s"' % image
                search = local(findcmd, capture=True)

                # If output does not contain image, then it is unused
                if not search.stdout:
                    if cmd:
                        rmcmd = '%s %s' % (cmd, imagepath)
                        logger.info('Executing command: %s' % rmcmd)
                        local(rmcmd)
                    else:
                        logger.info('Unsed image: %s' % imagepath)
示例#2
0
def bundle(template_path='', resource_dir=''):
    """
    Finds out the resources files (css,js) from given template file (in example, ``resources.html``), bundles them into
    one package (stylepack_yymmdd.css, scriptpack_yymmdd.js) and generates bundle.html (in the same directory where
    template_path is), containing references to these files. This bundle.html file can be used in Genshi
    templates as follows::

         <xi:include href="bundle.html">
            <xi:fallback>
                <!-- Link resources normally -->
                <xi:include href="resources.html">
            <xi:fallback>
        </xi:include>

    Where ``resources.html`` contains::

        <html xmlns="http://www.w3.org/1999/xhtml"
              xmlns:py="http://genshi.edgewall.org/"
              py:strip="">
            <link rel="stylesheet" type="text/css" href="ui.css" />
            <script type="text/javascript" src="jquery-1.4.1.min.js" />
        </html>

    If bundle.html is found, only packaged resources are served to end users. Otherwise, the resources are served as-is.

    :param str template_path: Absolute path to HTML template
    :param str resource_dir: The parent directory where the resources are located in
    """
    assert template_path, 'Please provide template_path'
    assert resource_dir, 'Please provide resource_dir'

    now = datetime.utcnow()
    variable_regx = re.compile('\${(\w|-|_|\.)+}\/')
    hrp = HTMLResourceParser()

    logger.info('Parsing template file: %s' % template_path)

    # Operate with layout file
    with io.open(template_path, 'r') as layout_fd:
        # Read file into parser
        content = layout_fd.read()
        hrp.feed(content)

        # Bundle stylesheets
        css_bundle_path = os.path.join(resource_dir, 'css', now.strftime('stylepack_%y%m%d.css'))
        with io.open(css_bundle_path, 'w') as bundle_fd:
            # Iterate styles
            for style in hrp.styles:
                # Replace variable prefixed stylesheet with actual one
                style_path = os.path.join(resource_dir, variable_regx.sub('', style['href']))
                logger.debug('Bundle script: %s' % style_path)
                with io.open(style_path, 'r') as style_fd:
                    bundle_fd.write(style_fd.read())

        logger.info('Bundled %d stylesheets into %s' % (len(hrp.styles), css_bundle_path))

        # Bundle scripts
        js_bundle_path = os.path.join(resource_dir, 'js', now.strftime('scriptpack_%y%m%d.js'))
        with io.open(js_bundle_path, 'w') as bundle_fd:
            # Iterate scripts
            for script in hrp.scripts:
                # Replace variable prefixed script with actual one
                script_path = os.path.join(resource_dir, variable_regx.sub('', script['src']))
                logger.debug('Bundle script: %s' % script_path)
                with io.open(script_path, 'r') as script_fd:
                    bundle_fd.write(script_fd.read())

        logger.info('Bundled %d scripts into %s' % (len(hrp.scripts), js_bundle_path))

        # Generate build.html files, containing the resource links
        bundle_html_path = os.path.join(os.path.dirname(template_path), 'bundle.html')
        with open(bundle_html_path, 'w') as bundle_html_fd:
            bundle_html_fd.write('''
            <html xmlns="http://www.w3.org/1999/xhtml"
                  xmlns:py="http://genshi.edgewall.org/"
                  py:strip="">
                <link rel="stylesheet" type="text/css" href="${conf.theme_htdocs_location}/css/%s" />
                <script type="text/javascript" src="${conf.theme_htdocs_location}/js/%s" />
            </html>
            ''' % (os.path.basename(css_bundle_path), os.path.basename(js_bundle_path)))

        logger.info('Generated bundle HTML file: %s' % bundle_html_path)
示例#3
0
def bundle(template_path='', resource_dir=''):
    """
    Finds out the resources files (css,js) from given template file (in example, ``resources.html``), bundles them into
    one package (stylepack_yymmdd.css, scriptpack_yymmdd.js) and generates bundle.html (in the same directory where
    template_path is), containing references to these files. This bundle.html file can be used in Genshi
    templates as follows::

         <xi:include href="bundle.html">
            <xi:fallback>
                <!-- Link resources normally -->
                <xi:include href="resources.html">
            <xi:fallback>
        </xi:include>

    Where ``resources.html`` contains::

        <html xmlns="http://www.w3.org/1999/xhtml"
              xmlns:py="http://genshi.edgewall.org/"
              py:strip="">
            <link rel="stylesheet" type="text/css" href="ui.css" />
            <script type="text/javascript" src="jquery-1.4.1.min.js" />
        </html>

    If bundle.html is found, only packaged resources are served to end users. Otherwise, the resources are served as-is.

    :param str template_path: Absolute path to HTML template
    :param str resource_dir: The parent directory where the resources are located in
    """
    assert template_path, 'Please provide template_path'
    assert resource_dir, 'Please provide resource_dir'

    now = datetime.utcnow()
    variable_regx = re.compile('\${(\w|-|_|\.)+}\/')
    hrp = HTMLResourceParser()

    logger.info('Parsing template file: %s' % template_path)

    # Operate with layout file
    with io.open(template_path, 'r') as layout_fd:
        # Read file into parser
        content = layout_fd.read()
        hrp.feed(content)

        # Bundle stylesheets
        css_bundle_path = os.path.join(resource_dir, 'css',
                                       now.strftime('stylepack_%y%m%d.css'))
        with io.open(css_bundle_path, 'w') as bundle_fd:
            # Iterate styles
            for style in hrp.styles:
                # Replace variable prefixed stylesheet with actual one
                style_path = os.path.join(resource_dir,
                                          variable_regx.sub('', style['href']))
                logger.debug('Bundle script: %s' % style_path)
                with io.open(style_path, 'r') as style_fd:
                    bundle_fd.write(style_fd.read())

        logger.info('Bundled %d stylesheets into %s' %
                    (len(hrp.styles), css_bundle_path))

        # Bundle scripts
        js_bundle_path = os.path.join(resource_dir, 'js',
                                      now.strftime('scriptpack_%y%m%d.js'))
        with io.open(js_bundle_path, 'w') as bundle_fd:
            # Iterate scripts
            for script in hrp.scripts:
                # Replace variable prefixed script with actual one
                script_path = os.path.join(
                    resource_dir, variable_regx.sub('', script['src']))
                logger.debug('Bundle script: %s' % script_path)
                with io.open(script_path, 'r') as script_fd:
                    bundle_fd.write(script_fd.read())

        logger.info('Bundled %d scripts into %s' %
                    (len(hrp.scripts), js_bundle_path))

        # Generate build.html files, containing the resource links
        bundle_html_path = os.path.join(os.path.dirname(template_path),
                                        'bundle.html')
        with open(bundle_html_path, 'w') as bundle_html_fd:
            bundle_html_fd.write('''
            <html xmlns="http://www.w3.org/1999/xhtml"
                  xmlns:py="http://genshi.edgewall.org/"
                  py:strip="">
                <link rel="stylesheet" type="text/css" href="${conf.theme_htdocs_location}/css/%s" />
                <script type="text/javascript" src="${conf.theme_htdocs_location}/js/%s" />
            </html>
            ''' % (os.path.basename(css_bundle_path),
                   os.path.basename(js_bundle_path)))

        logger.info('Generated bundle HTML file: %s' % bundle_html_path)