File tree 1 file changed +52
-0
lines changed
1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+
3
+ import os
4
+ import sys
5
+ import re
6
+ import requests
7
+
8
+ PATTERN = (
9
+ "\[!\[View code\]\(https://www\.mbed\.com/embed/\?type=library\)\]"
10
+ "\((.*)\)"
11
+ )
12
+
13
+ def main (path ):
14
+ bad = False
15
+
16
+ for dir , dirs , files in os .walk (path ):
17
+ for file in files :
18
+ if not file .endswith ('.md' ):
19
+ continue
20
+
21
+ path = os .path .join (dir , file )
22
+ with open (path ) as file :
23
+ for i , line in enumerate (file ):
24
+ for match in re .findall (PATTERN , line ):
25
+ if match .startswith ('https:' ):
26
+ sys .stderr .write ("\n %s:%s: Bad URL (https):\n " % (path , i ))
27
+ sys .stderr .write (match + '\n ' )
28
+ bad = True
29
+
30
+ try :
31
+ code = requests .get (match ).status_code
32
+ except requests .exceptions .RequestException :
33
+ code = - 1
34
+
35
+ if code != 200 :
36
+ sys .stderr .write ("\n %s:%s: Bad URL (%d):\n " % (path , i , code ))
37
+ sys .stderr .write (match + '\n ' )
38
+ bad = True
39
+
40
+ sys .stdout .write ('.' )
41
+ sys .stdout .flush ()
42
+
43
+ for dir in dirs :
44
+ if dir .startswith ('.' ):
45
+ dirs .remove (dir )
46
+
47
+ sys .stdout .write ('\n Done!\n ' )
48
+ return 0 if not bad else 1
49
+
50
+
51
+ if __name__ == "__main__" :
52
+ sys .exit (main (* sys .argv [1 :]))
You can’t perform that action at this time.
0 commit comments