-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from scbedd/readme-content-verification
Readme Content Verification
- Loading branch information
Showing
9 changed files
with
246 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from setuptools import setup, find_packages | ||
import setuptools | ||
|
||
|
@@ -25,7 +28,7 @@ | |
description=DESCRIPTION, | ||
long_description=long_description, | ||
long_description_content_type='text/markdown', | ||
url='https://github.com/Azure/azure-sdk-tools/packages/python-packages/', | ||
url='https://github.com/Azure/azure-sdk-tools/', | ||
author='Microsoft Corporation', | ||
author_email='[email protected]', | ||
|
||
|
@@ -45,7 +48,11 @@ | |
], | ||
packages=find_packages(), | ||
install_requires = [ | ||
'pyyaml', | ||
'pyyaml', # docsettings file parse | ||
'markdown2', # parsing markdown to html | ||
'docutils', # parsing rst to html | ||
'pygments', # docutils uses pygments for parsing rst to html | ||
'beautifulsoup4', # parsing of generated html | ||
'pathlib' | ||
], | ||
entry_points = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,21 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from .version import VERSION | ||
from .enforce_readme_presence import * | ||
from .WardenConfiguration import WardenConfiguration | ||
|
||
from .enforce_readme_presence import find_missing_readmes | ||
from .enforce_readme_content import verify_readme_content | ||
from .WardenConfiguration import WardenConfiguration | ||
from .warden_common import walk_directory_for_pattern, get_omitted_files | ||
from .cmd_entry import console_entry_point | ||
|
||
__all__ = ['WardenConfiguration', | ||
'DEFAULT_LOCATION', | ||
'return_true', | ||
'unrecognized_option', | ||
__all__ = [ | ||
'WardenConfiguration', | ||
'find_missing_readmes', | ||
'verify_readme_content', | ||
'console_entry_point', | ||
'scan_repo', | ||
'results', | ||
'check_package_readmes', | ||
'check_python_readmes', | ||
'check_js_readmes', | ||
'check_net_readmes', | ||
'is_net_csproj_package', | ||
'check_java_readmes', | ||
'is_java_pom_package_pom', | ||
'check_repo_root', | ||
'find_alongside_file', | ||
'get_file_sets', | ||
'get_omitted_files', | ||
'walk_directory_for_pattern', | ||
'check_match', | ||
'parse_pom'] | ||
'get_omitted_files', | ||
] | ||
|
||
__version__ = VERSION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from __future__ import print_function | ||
|
||
from .enforce_readme_presence import find_missing_readmes | ||
from .enforce_readme_content import verify_readme_content | ||
from .WardenConfiguration import WardenConfiguration | ||
|
||
# CONFIGURATION. ENTRY POINT. EXECUTION. | ||
def console_entry_point(): | ||
cfg = WardenConfiguration() | ||
print(cfg.dump()) | ||
|
||
command_selector = { | ||
'scan': scan, | ||
} | ||
|
||
if cfg.command in command_selector: | ||
command_selector.get(cfg.command)(cfg) | ||
else: | ||
print('Unrecognized command invocation {}.'.format(cfg.command)) | ||
exit(1) | ||
|
||
def scan(config): | ||
find_missing_readmes(config) | ||
verify_readme_content(config) |
102 changes: 102 additions & 0 deletions
102
packages/python-packages/doc-warden/warden/enforce_readme_content.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from __future__ import print_function | ||
|
||
import os | ||
import markdown2 | ||
import bs4 | ||
import re | ||
from .warden_common import check_match, walk_directory_for_pattern, get_omitted_files | ||
from docutils import core | ||
from docutils.writers.html4css1 import Writer,HTMLTranslator | ||
|
||
# fnmatch is case insensitive by default, just look for readme rst and md | ||
README_PATTERNS = ['*/readme.md', '*/readme.rst'] | ||
|
||
# entry point | ||
def verify_readme_content(config): | ||
all_readmes = walk_directory_for_pattern(config.target_directory, README_PATTERNS) | ||
omitted_readmes = get_omitted_files(config) | ||
targeted_readmes = [readme for readme in all_readmes if readme not in omitted_readmes] | ||
|
||
readme_results = [] | ||
|
||
for readme in targeted_readmes: | ||
ext = os.path.splitext(readme)[1] | ||
if ext == '.rst': | ||
readme_results.append(verify_rst_readme(readme, config)) | ||
else: | ||
readme_results.append(verify_md_readme(readme, config)) | ||
|
||
results([readme_tuple for readme_tuple in readme_results if readme_tuple[1]], config) | ||
|
||
# output results | ||
def results(readmes_with_issues, config): | ||
if len(readmes_with_issues): | ||
print('{} readmes have missing required sections.'.format(len(readmes_with_issues))) | ||
for readme_tuple in readmes_with_issues: | ||
print(readme_tuple[0].replace(os.path.normpath(config.target_directory), '') + ' is missing headers with pattern(s):') | ||
for missing_pattern in readme_tuple[1]: | ||
print(' * {0}'.format(missing_pattern)) | ||
exit(1) | ||
|
||
# parse rst to html, check for presence of appropriate sections | ||
def verify_rst_readme(readme, config): | ||
with open(readme, 'r') as f: | ||
readme_content = f.read() | ||
html_readme_content = rst_to_html(readme_content) | ||
html_soup = bs4.BeautifulSoup(html_readme_content, "html.parser") | ||
|
||
missed_patterns = find_missed_sections(html_soup, config.required_readme_sections) | ||
|
||
return (readme, missed_patterns) | ||
|
||
# parse md to html, check for presence of appropriate sections | ||
def verify_md_readme(readme, config): | ||
with open(readme, 'r') as f: | ||
readme_content = f.read() | ||
html_readme_content = markdown2.markdown(readme_content) | ||
html_soup = bs4.BeautifulSoup(html_readme_content, "html.parser") | ||
|
||
missed_patterns = find_missed_sections(html_soup, config.required_readme_sections) | ||
|
||
return (readme, missed_patterns) | ||
|
||
# within the entire readme, are there any missing sections that are expected? | ||
def find_missed_sections(html_soup, patterns): | ||
headers = html_soup.find_all(re.compile('^h[1-6]$')) | ||
missed_patterns = [] | ||
observed_patterns = [] | ||
|
||
for header in headers: | ||
observed_patterns.extend(match_regex_set(header, patterns)) | ||
|
||
return list(set(patterns) - set(observed_patterns)) | ||
|
||
# checks a header tag (soup) against a set of configured patterns | ||
def match_regex_set(header, patterns): | ||
matching_patterns = [] | ||
for pattern in patterns: | ||
result = re.search(pattern, header.get_text()) | ||
if result: | ||
matching_patterns.append(pattern) | ||
return matching_patterns | ||
|
||
# boilerplate for translating RST | ||
class HTMLFragmentTranslator(HTMLTranslator): | ||
def __init__(self, document): | ||
HTMLTranslator.__init__(self, document) | ||
self.head_prefix = ['','','','',''] | ||
self.body_prefix = [] | ||
self.body_suffix = [] | ||
self.stylesheet = [] | ||
def astext(self): | ||
return ''.join(self.body) | ||
|
||
html_fragment_writer = Writer() | ||
html_fragment_writer.translator_class = HTMLFragmentTranslator | ||
|
||
# utilize boilerplate | ||
def rst_to_html(input_rst): | ||
return core.publish_string(input_rst, writer = html_fragment_writer) |
Oops, something went wrong.