示例#1
0
def process_readme_doc(target_dir: str, content_dir: str, prefix: str,
                       imgs_dir: str, relative_images_dir: str,
                       readme_file: str) -> DocInfo:
    try:
        base_dir = os.path.dirname(readme_file)
        if readme_file.endswith('_README.md'):
            ymlfile = readme_file[0:readme_file.index('_README.md')] + '.yml'
        else:
            ymlfiles = glob.glob(base_dir + '/*.yml')
            if not ymlfiles:
                raise ValueError('no yml file found')
            if len(ymlfiles) > 1:
                raise ValueError(f'mulitple yml files found: {ymlfiles}')
            ymlfile = ymlfiles[0]
        with open(ymlfile, 'r', encoding='utf-8') as f:
            yml_data = yaml.safe_load(f)
        id = yml_data.get('commonfields', {}).get('id') or yml_data['id']
        id = normalize_id(id)
        name = yml_data.get('display') or yml_data['name']
        desc = yml_data.get('description') or yml_data.get('comment')
        if desc:
            desc = handle_desc_field(desc)
        doc_info = DocInfo(id, name, desc, readme_file)
        with open(readme_file, 'r', encoding='utf-8') as f:
            content = f.read()
        if not content.strip():
            raise ValueError(EMPTY_FILE_MSG)
        if is_html_doc(content):
            print(f'{readme_file}: detect html file')
            content = gen_html_doc(content)
        else:
            content = fix_mdx(content)
            content = fix_relative_images(content, base_dir, f'{prefix}-{id}',
                                          imgs_dir, relative_images_dir)
        # check if we have a header
        lines = content.splitlines(True)
        has_header = len(lines) >= 2 and lines[0].startswith(
            '---') and lines[1].startswith('id:')
        if not has_header:
            readme_repo_path = readme_file
            if readme_repo_path.startswith(content_dir):
                readme_repo_path = readme_repo_path[len(content_dir):]
            edit_url = f'https://github.com/demisto/content/blob/{BRANCH}/{readme_repo_path}'
            header = f'---\nid: {id}\ntitle: {json.dumps(doc_info.name)}\ncustom_edit_url: {edit_url}\n---\n\n'
            content = add_content_info(content, yml_data, desc, readme_file)
            content = header + content
        verify_mdx_server(content)
        with open(f'{target_dir}/{id}.md', mode='w',
                  encoding='utf-8') as f:  # type: ignore
            f.write(content)
        return doc_info
    except Exception as ex:
        print(f'fail: {readme_file}. Exception: {traceback.format_exc()}')
        return DocInfo('', '', '', readme_file, str(ex).splitlines()[0])
    finally:
        sys.stdout.flush()
        sys.stderr.flush()
示例#2
0
def test_fix_relative_images(tmp_path):
    readme = f'{SAMPLE_CONTENT}/Packs/GoogleCalendar/Integrations/GoogleCalendar/README.md'
    with open(readme, 'r') as f:
        content = f.read()
    res = fix_relative_images(
        content,
        f'{SAMPLE_CONTENT}/Packs/GoogleCalendar/Integrations/GoogleCalendar',
        'google-calendar', str(tmp_path), 'relative-test')
    target_img_name = 'google-calendar-_-__-__-doc_files-add-scope-admin-3.png'
    assert f'relative-test/{target_img_name}' in res
    os.path.isfile(tmp_path / target_img_name)
    # test a readme that shouldn't change
    readme = f'{SAMPLE_CONTENT}/Integrations/Gmail/README.md'
    with open(readme, 'r') as f:
        content = f.read()
    res = fix_relative_images(content, f'{SAMPLE_CONTENT}/Integrations/Gmail',
                              'google-calendar', str(tmp_path),
                              'relative-test')
    assert res == content
示例#3
0
def test_fix_relative_images_html_img(tmp_path):
    readme = f'{SAMPLE_CONTENT}/Packs/ProofpointServerProtection/Integrations/ProofpointProtectionServerV2/README.md'
    with open(readme, 'r') as f:
        content = f.read()
    res = fix_relative_images(
        content,
        f'{SAMPLE_CONTENT}/Packs/ProofpointServerProtection/Integrations/ProofpointProtectionServerV2',
        'proofpoint-test', str(tmp_path), 'relative-test')
    target_img_name = 'proofpoint-test-_-__-__-doc_imgs-api_role.png'
    assert f'relative-test/{target_img_name}' in res
    os.path.isfile(tmp_path / target_img_name)