Tiem Song | cab78e6 | 2019-08-14 11:07:16 -0700 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | |
| 4 | import getopt |
| 5 | import os |
| 6 | import sys |
| 7 | |
| 8 | |
| 9 | # GLOBAL FLAGS |
| 10 | |
| 11 | global stubs |
| 12 | global java_stubs, kotlin_stubs |
| 13 | global work, verbose, show_solo, max_stubs |
| 14 | global java_source_abs_path |
| 15 | global kotlin_source_abs_path |
| 16 | |
| 17 | verbose = False # set True to list all files as they are stubbed (--verbose) |
| 18 | work = False # set True to insert stubs, False to do a dry run for stats (--work) |
| 19 | show_solo = False # set True to list files that only appear in one language, rather than both (--solo) |
| 20 | max_stubs = 0 # set positive to create a limited number of stubs (--max 12) |
| 21 | |
| 22 | |
| 23 | # You must run the script from the refodcs reference/ root directory |
| 24 | |
| 25 | java_ref_root = os.getcwd() |
| 26 | kotlin_ref_root = os.path.join(java_ref_root, "kotlin") |
| 27 | root = os.path.split(java_ref_root)[1] |
| 28 | if root != "reference": |
| 29 | print ("You must cd to the refocs reference/ root directoy") |
| 30 | sys.exit() |
| 31 | |
| 32 | |
| 33 | # This method uses switcher2, which assumes the refdocs stay in their current |
| 34 | # assymetrical dirs (ref/android and ref/kotlin/android) |
| 35 | # And just puts the switcher in the existing docs |
| 36 | def insert_stub(doc, java, both): |
| 37 | global stubs |
| 38 | global java_stubs, kotlin_stubs |
| 39 | global verbose, work, show_solo |
| 40 | global java_source_abs_path |
| 41 | global kotlin_source_abs_path |
| 42 | |
| 43 | stubs = stubs+1 |
| 44 | |
| 45 | if verbose: |
| 46 | print "File: ", stubs, doc |
| 47 | else: |
| 48 | fn = os.path.split(doc) |
| 49 | print "File: ", stubs, fn[1], "\r", |
| 50 | |
| 51 | if (java): |
| 52 | java_stubs = java_stubs + 1 |
| 53 | else: |
| 54 | kotlin_stubs = kotlin_stubs + 1 |
| 55 | |
| 56 | |
| 57 | if (work): |
| 58 | if (java): |
| 59 | file_path = doc[len(java_ref_root)+1:] |
| 60 | stub = doc.replace(java_source_abs_path, kotlin_source_abs_path) |
| 61 | if (both): |
| 62 | slug1 = "sed -i 's/<\/h1>/{}/' {}".format("<\/h1>\\n{% setvar page_path %}_page_path_{% endsetvar %}\\n{% setvar can_switch %}1{% endsetvar %}\\n{% include \"reference\/_java_switcher2.md\" %}",doc) |
| 63 | else: |
| 64 | slug1 = "sed -i 's/<\/h1>/{}/' {}".format("<\/h1>\\n{% include \"reference\/_java_switcher2.md\" %}",doc) |
| 65 | else: |
| 66 | file_path = doc[len(kotlin_ref_root)+1:] |
| 67 | stub = doc.replace(kotlin_source_abs_path, java_source_abs_path) |
| 68 | if (both): |
| 69 | slug1 = "sed -i 's/<\/h1>/{}/' {}".format("<\/h1>\\n{% setvar page_path %}_page_path_{% endsetvar %}\\n{% setvar can_switch %}1{% endsetvar %}\\n{% include \"reference\/_kotlin_switcher2.md\" %}",doc) |
| 70 | else: |
| 71 | slug1 = "sed -i 's/<\/h1>/{}/' {}".format("<\/h1>\\n{% include \"reference\/_kotlin_switcher2.md\" %}",doc) |
| 72 | |
| 73 | os.system(slug1) |
| 74 | if (both): |
| 75 | page_path_slug = "sed -i 's/_page_path_/{}/' {}".format(file_path.replace("/","\/"),doc) |
| 76 | os.system(page_path_slug) |
| 77 | |
| 78 | |
| 79 | def scan_files(stem): |
| 80 | global work, verbose, show_solo, max_stubs |
| 81 | global stubs |
| 82 | global java_stubs, kotlin_stubs |
| 83 | global java_source_abs_path |
| 84 | global kotlin_source_abs_path |
| 85 | |
| 86 | java_source_abs_path = os.path.join(java_ref_root, stem) |
| 87 | kotlin_source_abs_path = os.path.join(kotlin_ref_root, stem) |
| 88 | |
| 89 | # Pass 1 |
| 90 | # Loop over java content, create stubs for java, |
| 91 | # and for corresponding Kotlin (when it exsits) |
| 92 | |
| 93 | # solo is java-only classes |
| 94 | # both is java+kotlin |
| 95 | stubs = 0 |
| 96 | java_stubs = 0 |
| 97 | kotlin_stubs = 0 |
| 98 | solo = 0 |
| 99 | both = 0 |
| 100 | |
| 101 | print "*** PASS1 (Java) ***" |
| 102 | maxed_out = False |
| 103 | for root, dirs, files in os.walk(java_source_abs_path): |
| 104 | if maxed_out: |
| 105 | break; |
| 106 | for file_ in files: |
| 107 | ext = os.path.splitext(file_) |
| 108 | ext = ext[1] |
| 109 | if not ext: |
| 110 | # this catches package-lists with no extension |
| 111 | print "***", os.path.join(root, file_) |
| 112 | elif ext != ".html": |
| 113 | # filter out png, yaml, etc |
| 114 | continue |
| 115 | else: |
| 116 | # we have java content |
| 117 | doc = os.path.join(root, file_) |
| 118 | |
| 119 | |
| 120 | |
| 121 | # look for matching kotlin file |
| 122 | kotlinsource = doc.replace(java_source_abs_path, kotlin_source_abs_path) |
| 123 | if os.path.isfile(kotlinsource): |
| 124 | # corresponding kotlin content exists |
| 125 | insert_stub(doc, True, True) |
| 126 | insert_stub(kotlinsource, False, True) |
| 127 | both = both+1 |
| 128 | else: |
| 129 | # no kotlin content |
| 130 | if (show_solo): |
| 131 | print "solo: ", doc |
| 132 | insert_stub(doc, True, False) |
| 133 | solo = solo+1 |
| 134 | |
| 135 | if max_stubs>0 and stubs>=max_stubs: |
| 136 | print |
| 137 | print "max java stubs: ", max_stubs |
| 138 | maxed_out = True; |
| 139 | break |
| 140 | |
| 141 | print "Java+Kotlin:", both, "Only Java:", solo |
| 142 | print |
| 143 | |
| 144 | |
| 145 | # PASS 2 |
| 146 | # Loop over kotlin content, create stubs for Kotlin-only APIs |
| 147 | print "*** PASS2 (Kotlin) ***" |
| 148 | solo = 0 |
| 149 | both = 0 |
| 150 | maxed_out = False |
| 151 | stubs = 0 |
| 152 | for root, dirs, files in os.walk(kotlin_source_abs_path): |
| 153 | if maxed_out: |
| 154 | break; |
| 155 | for file_ in files: |
| 156 | ext = os.path.splitext (file_) |
| 157 | ext = ext[1] |
| 158 | if not ext: |
| 159 | # this catches package-lists with no extension |
| 160 | print "***", os.path.join(root, file_) |
| 161 | elif ext != ".html": |
| 162 | # filter out png, yaml, etc |
| 163 | continue |
| 164 | else: |
| 165 | # we have kotlin content |
| 166 | doc = os.path.join(root, file_) |
| 167 | javadoc = doc.replace(kotlin_source_abs_path, java_source_abs_path) |
| 168 | file_name = os.path.splitext(file_)[0] |
| 169 | file_path = doc[len(kotlin_source_abs_path)+1:] |
| 170 | include_path = os.path.join("/reference/_kotlin", file_path) |
| 171 | |
| 172 | if os.path.isfile(javadoc): |
| 173 | # corresponding java content exists |
| 174 | # so we already created the kotlin stub file |
| 175 | # nothing to do |
| 176 | both = both+1 |
| 177 | else: |
| 178 | # no java content |
| 179 | # create the kotlin stub file |
| 180 | if (show_solo): |
| 181 | print "solo: ", doc |
| 182 | insert_stub(doc , False, False) |
| 183 | solo = solo+1 |
| 184 | |
| 185 | if (max_stubs>0 and stubs>=max_stubs): |
| 186 | print |
| 187 | print "max koltin stubs: ", max_stubs |
| 188 | maxed_out = True; |
| 189 | break |
| 190 | |
| 191 | |
| 192 | print "Java+Kotlin:", both, "Only Kotlin:", solo |
| 193 | print |
| 194 | print "Java: ", java_stubs, " Kotlin: ", kotlin_stubs, "Total: ", java_stubs + kotlin_stubs |
| 195 | |
| 196 | |
| 197 | def main(argv): |
| 198 | |
| 199 | global work, verbose, show_solo, max_stubs |
| 200 | global java_source_abs_path |
| 201 | global kotlin_source_abs_path |
| 202 | stem = "" |
| 203 | |
| 204 | try: |
| 205 | opts, args = getopt.getopt(argv,"",["work","verbose","solo","max="]) |
| 206 | except getopt.GetoptError: |
| 207 | print 'USAGE: switcher --work --verbose --solo --max=<max_stubs> platform|androidx|support|chrome' |
| 208 | sys.exit(2) |
| 209 | |
| 210 | for opt, arg in opts: |
| 211 | if opt == '--work': |
| 212 | work = True |
| 213 | elif opt == "--verbose": |
| 214 | print "verbose" |
| 215 | verbose = True |
| 216 | elif opt == "--solo": |
| 217 | print "verbose" |
| 218 | show_solo = True |
| 219 | elif opt == "--max": |
| 220 | max_stubs = int(arg) |
| 221 | print "max ", max_stubs |
| 222 | |
| 223 | if len(args)>0: |
| 224 | source = args[0] |
| 225 | if source == "platform": |
| 226 | stem = "android" |
| 227 | print |
| 228 | print "*** PLATFORM PAGES ***" |
| 229 | print "======================" |
| 230 | |
| 231 | elif source == "androidx": |
| 232 | stem = "androidx" |
| 233 | print |
| 234 | print "*** ANDROIDX SUPPORT LIBRARY PAGES ***" |
| 235 | print "======================================" |
| 236 | |
| 237 | elif source == "support": |
| 238 | stem = "android/support/v4/media" |
| 239 | print |
| 240 | print "*** ANDROIDX SUPPORT LIBRARY PAGES ***" |
| 241 | print "======================================" |
| 242 | |
| 243 | elif source == "chrome": |
| 244 | stem = "org/chromium/support_lib_boundary" |
| 245 | print |
| 246 | print "*** ANDROIDX CHROMIUM PAGES ***" |
| 247 | print "===============================" |
| 248 | |
| 249 | if (len(stem)>0): |
| 250 | scan_files(stem) |
| 251 | print " *** DONE ***" |
| 252 | else: |
| 253 | print 'You must specify one of: platform|androidx|support|chrome' |
| 254 | |
| 255 | |
| 256 | |
| 257 | if __name__ == "__main__": |
| 258 | main(sys.argv[1:]) |
| 259 | |