|
| 1 | +import asyncio |
| 2 | +import sys |
| 3 | +import typing |
| 4 | +from concurrent.futures import ThreadPoolExecutor |
| 5 | +from functools import partial |
| 6 | +from os import cpu_count |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from binaryornot.check import is_binary |
| 10 | +from plumbum import cli |
| 11 | + |
| 12 | +from .core import CombinedReplacerFactory, ReplaceContext |
| 13 | +from .core.InBufferReplacer import InBufferReplacer |
| 14 | +from .core.InFileReplacer import InFileReplacer |
| 15 | +from .replacers.HEReplacer import HEReplacer |
| 16 | +from .replacers.HSTSPreloadReplacer import HSTSPreloadReplacer |
| 17 | + |
| 18 | + |
| 19 | +class OurInBufferReplacer(InBufferReplacer): |
| 20 | + __slots__ = () |
| 21 | + FACS = CombinedReplacerFactory( |
| 22 | + { |
| 23 | + "preloads": HSTSPreloadReplacer, |
| 24 | + "heRulesets": HEReplacer, |
| 25 | + } |
| 26 | + ) |
| 27 | + |
| 28 | + def __init__(self, preloads=None, heRulesets=None): |
| 29 | + super().__init__(preloads=preloads, heRulesets=heRulesets) |
| 30 | + |
| 31 | + |
| 32 | +class OurInFileReplacer(InFileReplacer): |
| 33 | + def __init__(self, preloads=None, heRulesets=None): |
| 34 | + super().__init__(OurInBufferReplacer(preloads=preloads, heRulesets=heRulesets)) |
| 35 | + |
| 36 | + |
| 37 | +class CLI(cli.Application): |
| 38 | + """HTTPSEverywhere-like URI rewriter""" |
| 39 | + |
| 40 | + |
| 41 | +class FileClassifier: |
| 42 | + __slots__ = ("noSkipDot", "noSkipBinary") |
| 43 | + |
| 44 | + def __init__(self, noSkipDot: bool, noSkipBinary: bool): |
| 45 | + self.noSkipDot = noSkipDot |
| 46 | + self.noSkipBinary = noSkipBinary |
| 47 | + |
| 48 | + def __call__(self, p: Path) -> str: |
| 49 | + for pa in p.parts: |
| 50 | + if not self.noSkipDot and pa[0] == ".": |
| 51 | + return "dotfile" |
| 52 | + |
| 53 | + if not p.is_dir(): |
| 54 | + if p.is_file(): |
| 55 | + if self.noSkipBinary or not is_binary(p): |
| 56 | + return "" |
| 57 | + else: |
| 58 | + return "binary" |
| 59 | + else: |
| 60 | + return "not regular file" |
| 61 | + |
| 62 | + |
| 63 | +class FilesEnumerator: |
| 64 | + __slots__ = ("classifier", "disallowedReportingCallback") |
| 65 | + |
| 66 | + def __init__(self, classifier, disallowedReportingCallback): |
| 67 | + self.classifier = classifier |
| 68 | + self.disallowedReportingCallback = disallowedReportingCallback |
| 69 | + |
| 70 | + def __call__(self, fileOrDir: Path): |
| 71 | + reasonOfDisallowal = self.classifier(fileOrDir) |
| 72 | + if not reasonOfDisallowal: |
| 73 | + if fileOrDir.is_dir(): |
| 74 | + for f in fileOrDir.iterdir(): |
| 75 | + yield from self(f) |
| 76 | + else: |
| 77 | + yield fileOrDir |
| 78 | + else: |
| 79 | + self.disallowedReportingCallback(fileOrDir, reasonOfDisallowal) |
| 80 | + |
| 81 | + |
| 82 | +@CLI.subcommand("bulk") |
| 83 | +class FileRewriteCLI(cli.Application): |
| 84 | + """Rewrites URIs in files. Use - to consume list of files from stdin. Don't use `find`, it is a piece of shit which is impossible to configure to skip .git dirs.""" |
| 85 | + |
| 86 | + __slots__ = ("_repl",) |
| 87 | + |
| 88 | + @property |
| 89 | + def repl(self) -> InFileReplacer: |
| 90 | + if self._repl is None: |
| 91 | + self._repl = OurInFileReplacer() |
| 92 | + print( |
| 93 | + len(self._repl.inBufferReplacer.singleURIReplacer.children[0].preloads), |
| 94 | + "HSTS preloads", |
| 95 | + ) |
| 96 | + print(len(self._repl.inBufferReplacer.singleURIReplacer.children[1].rulesets), "HE rules") |
| 97 | + return self._repl |
| 98 | + |
| 99 | + def processEachFileName(self, ctx: ReplaceContext, l: str) -> Path: |
| 100 | + l = l.strip() |
| 101 | + if l: |
| 102 | + l = l.decode("utf-8") |
| 103 | + p = Path(l).resolve().absolute() |
| 104 | + self.processEachFilePath(ctx, p) |
| 105 | + |
| 106 | + def processEachFilePath(self, ctx: ReplaceContext, p: Path) -> None: |
| 107 | + for pp in self.fe(p): |
| 108 | + if self.trace: |
| 109 | + print("Processing", pp, file=sys.stderr) |
| 110 | + self.repl(ctx, pp) |
| 111 | + if self.trace: |
| 112 | + print("Processed", pp, file=sys.stderr) |
| 113 | + |
| 114 | + @asyncio.coroutine |
| 115 | + def asyncMainPathsFromStdIn(self): |
| 116 | + conc = [] |
| 117 | + asyncStdin = asyncio.StreamReader(loop=self.loop) |
| 118 | + yield from self.loop.connect_read_pipe( |
| 119 | + lambda: asyncio.StreamReaderProtocol(asyncStdin, loop=self.loop), sys.stdin |
| 120 | + ) |
| 121 | + with ThreadPoolExecutor(max_workers=cpu_count()) as pool: |
| 122 | + while not asyncStdin.at_eof(): |
| 123 | + l = yield from asyncStdin.readline() |
| 124 | + yield from self.loop.run_in_executor(pool, partial(self.processEachFileName, l)) |
| 125 | + |
| 126 | + @asyncio.coroutine |
| 127 | + def asyncMainPathsFromCLI(self, filesOrDirs: typing.Iterable[typing.Union[Path, str]]): |
| 128 | + try: |
| 129 | + from tqdm import tqdm |
| 130 | + except ImportError: |
| 131 | + |
| 132 | + def tqdm(x): |
| 133 | + return x |
| 134 | + |
| 135 | + ctx = ReplaceContext(None) |
| 136 | + replaceInEachFileWithContext = partial(self.repl, ctx) |
| 137 | + |
| 138 | + with tqdm(filesOrDirs) as pb: |
| 139 | + for fileOrDir in pb: |
| 140 | + fileOrDir = Path(fileOrDir).resolve().absolute() |
| 141 | + |
| 142 | + files = tuple(self.fe(fileOrDir)) |
| 143 | + |
| 144 | + if files: |
| 145 | + with ThreadPoolExecutor(max_workers=cpu_count()) as pool: |
| 146 | + for f in files: |
| 147 | + if self.trace: |
| 148 | + print("Processing", f, file=pb) |
| 149 | + yield from self.loop.run_in_executor(pool, partial(replaceInEachFileWithContext, f)) |
| 150 | + if self.trace: |
| 151 | + print("Processed", f, file=pb) |
| 152 | + |
| 153 | + noSkipBinary = cli.Flag( |
| 154 | + ["--no-skip-binary", "-n"], |
| 155 | + help="Don't skip binary files. Allows usage without `binaryornot`", |
| 156 | + default=False, |
| 157 | + ) |
| 158 | + noSkipDot = cli.Flag( |
| 159 | + ["--no-skip-dotfiles", "-d"], |
| 160 | + help="Don't skip files and dirs which name stem begins from dot.", |
| 161 | + default=False, |
| 162 | + ) |
| 163 | + trace = cli.Flag( |
| 164 | + ["--trace", "-t"], |
| 165 | + help="Print info about processing of regular files", |
| 166 | + default=False, |
| 167 | + ) |
| 168 | + noReportSkipped = cli.Flag( |
| 169 | + ["--no-report-skipped", "-s"], |
| 170 | + help="Don't report about skipped files", |
| 171 | + default=False, |
| 172 | + ) |
| 173 | + |
| 174 | + def disallowedReportingCallback(self, fileOrDir: Path, reasonOfDisallowal: str) -> None: |
| 175 | + if not self.noReportSkipped: |
| 176 | + print("Skipping ", fileOrDir, ":", reasonOfDisallowal) |
| 177 | + |
| 178 | + def main(self, *filesOrDirs): |
| 179 | + self._repl = None # type: OurInFileReplacer |
| 180 | + self.loop = asyncio.get_event_loop() |
| 181 | + |
| 182 | + self.fc = FileClassifier(self.noSkipDot, self.noSkipBinary) |
| 183 | + self.fe = FilesEnumerator(self.fc, self.disallowedReportingCallback) |
| 184 | + |
| 185 | + if len(filesOrDirs) == 1 and filesOrDirs[0] == "0": |
| 186 | + t = self.loop.create_task(self.asyncMainPathsFromStdIn()) |
| 187 | + else: |
| 188 | + t = self.loop.create_task(self.asyncMainPathsFromCLI(filesOrDirs)) |
| 189 | + self.loop.run_until_complete(t) |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == "__main__": |
| 193 | + CLI.run() |
0 commit comments