|
| 1 | +import frontmatter |
| 2 | +from pathlib import Path, PosixPath |
| 3 | +import sys |
| 4 | + |
| 5 | +# 'path' == '_posts' in 'documentation' |
| 6 | +# 'path' == 'build/html' in 'py-docs' |
| 7 | +# 'path' == 'r' in 'r-docs' |
| 8 | +try: |
| 9 | + path = str(sys.argv[1]) |
| 10 | +except: |
| 11 | + raise Exception("You need to specify a path that contains the files with front matter.") |
| 12 | + |
| 13 | +def ci_check(checkList, error_message): |
| 14 | + print("***********************************!") |
| 15 | + print("Checking... {}".format(error_message)) |
| 16 | + if len(checkList) > 0: |
| 17 | + print("NOT PASSED!\n") |
| 18 | + print("List of failed permalinks:") |
| 19 | + print("**{}**".format(checkList)) |
| 20 | + print("\n") |
| 21 | + return False |
| 22 | + else: |
| 23 | + print("Passed!") |
| 24 | + return True |
| 25 | + |
| 26 | +paths = [] |
| 27 | +allPosts = [] |
| 28 | +postsWithNoName = [] |
| 29 | +postsWithTitle = [] |
| 30 | +allPermalinks = [] |
| 31 | +indexOverflow = [] |
| 32 | +postsWithNoThumbnail = [] |
| 33 | +temp = [] |
| 34 | +duplicatePermalinks = [] |
| 35 | +noTrailingSlash = [] |
| 36 | + |
| 37 | +categories = ["file_settings", "basic", "financial", "statistical", "scientific", "maps", "3d_charts", "multiple_axes"] |
| 38 | +languages = ["python", "python/v3", "plotly_js", "r"] |
| 39 | + |
| 40 | +if path == "r": |
| 41 | + for suffix in ["Rmd"]: |
| 42 | + paths += [x for x in Path(path).glob("*."+suffix)] |
| 43 | +else: |
| 44 | + # collect all paths |
| 45 | + for suffix in ["md", "html"]: |
| 46 | + paths += [x for x in Path(path).glob("**/*."+suffix)] |
| 47 | +print("number posts:") |
| 48 | +print (len(paths)) |
| 49 | + |
| 50 | +# collect all posts |
| 51 | +for path in paths: |
| 52 | + post = frontmatter.load(str(path)) |
| 53 | + if len(post.metadata.keys()) > 0: |
| 54 | + allPosts.append(post) |
| 55 | + |
| 56 | +# perform checks |
| 57 | +for post in allPosts: |
| 58 | + meta = post.metadata |
| 59 | + |
| 60 | + # ignore posts with 'jupyter' in front-matter |
| 61 | + if "jupyter" in meta: |
| 62 | + continue |
| 63 | + |
| 64 | + # Check #1 - do all non-redirect posts have names? |
| 65 | + if "name" not in meta and "redirect_to" not in meta: |
| 66 | + postsWithNoName.append(post.metadata['permalink']) |
| 67 | + |
| 68 | + # Check #2 - do any posts have titles? |
| 69 | + if "title" in meta: |
| 70 | + postsWithTitle.append(post.metadata['permalink']) |
| 71 | + |
| 72 | + # Check #3 - are there duplicate permalinks/redirect_froms? |
| 73 | + if "permalink" in meta and meta['permalink'] != '//plot.ly/products/dash/': |
| 74 | + allPermalinks.append(meta['permalink']) |
| 75 | + if "redirect_from" in meta: |
| 76 | + allPermalinks.append(meta['redirect_from']) |
| 77 | + |
| 78 | + # Check #4 - are there posts with order > 5 and 'page_type: example_index'? |
| 79 | + if "display_as" in meta and meta['display_as'] in categories: |
| 80 | + if "language" in meta and meta['language'] in languages: |
| 81 | + if "order" in meta and meta['order'] > 5: |
| 82 | + if "page_type" in meta and meta['page_type'] == "example_index": |
| 83 | + indexOverflow.append(meta['permalink']) |
| 84 | + |
| 85 | + # Check #5 - does every post have a thumbnail? |
| 86 | + if "thumbnail" not in meta: |
| 87 | + if "display_as" in meta and meta['display_as'] in categories: |
| 88 | + if "language" in meta and meta['language'] in languages: |
| 89 | + postsWithNoThumbnail.append(meta['permalink']) |
| 90 | + |
| 91 | + # Check #6 - do any permalinks not end with a trailing slash? |
| 92 | + if "permalink" in meta: |
| 93 | + if meta['permalink'][-1] != "/": |
| 94 | + noTrailingSlash.append(meta['permalink']) |
| 95 | + |
| 96 | + |
| 97 | +for post in allPermalinks: |
| 98 | + if post in temp: |
| 99 | + duplicatePermalinks.append(post) |
| 100 | + continue |
| 101 | + else: |
| 102 | + temp.append(post) |
| 103 | + |
| 104 | +print("Begin CI Checks!\n") |
| 105 | +passed_check_1 = ci_check(postsWithNoName, "do all non-redirect posts have names?") |
| 106 | +passed_check_2 = ci_check(postsWithTitle, "do any posts have titles?") |
| 107 | +passed_check_3 = ci_check(indexOverflow, "are there posts with order > 5 and 'page_type: example_index'?") |
| 108 | +passed_check_4 = ci_check(duplicatePermalinks, "are there duplicate permalinks/redirect_froms?") |
| 109 | +passed_check_5 = ci_check(postsWithNoThumbnail, "does every post have a thumbnail?") |
| 110 | +passed_check_6 = ci_check(noTrailingSlash, "do any permalinks not end with a trailing slash?") |
| 111 | +print("End CI Checks!\n") |
| 112 | + |
| 113 | +if not passed_check_1 or not passed_check_2 or not passed_check_3 or not passed_check_4 or not passed_check_5 or not passed_check_6: |
| 114 | + raise Exception("***********CI Checks Not Passed! Check Error Messages!*********************") |
0 commit comments