|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright (c) 2023 Open Tech Strategies, LLC |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in all |
| 14 | +# copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +# SOFTWARE. |
| 23 | + |
| 24 | +"""Create directory 'test-tree' containing files with pathological names. |
| 25 | +(If 'test-tree' already exists, delete it and recreate it.) |
| 26 | +You must run this from this directory because sample source data is here. |
| 27 | +""" |
| 28 | + |
| 29 | +import os |
| 30 | +import sys |
| 31 | +import shutil |
| 32 | + |
| 33 | +def main(): |
| 34 | + test_tree_top = "test-tree" |
| 35 | + |
| 36 | + # Filename components. |
| 37 | + front = "front" |
| 38 | + back = "back" |
| 39 | + text_ext = "txt" |
| 40 | + image_ext = "png" |
| 41 | + |
| 42 | + # Ensure that an empty test-tree/ dir exists. |
| 43 | + if os.path.exists(test_tree_top): |
| 44 | + shutil.rmtree(test_tree_top) |
| 45 | + os.makedirs(test_tree_top) |
| 46 | + |
| 47 | + # Create all the files in test-tree/. |
| 48 | + # |
| 49 | + # A really rigorous test would try every UTF-8 character. We're |
| 50 | + # not going to do that, though: it would be too many files and |
| 51 | + # take too long to upload/download over the network. But we will |
| 52 | + # cover all the funny stops on the 7-bit ASCII railroad at least. |
| 53 | + # We try all the non-alphanumerics, including the lower-ASCII |
| 54 | + # control chars, at the front, middle, pre-extension end, |
| 55 | + # post-extension end, post-dot end, and raw end of the basename: |
| 56 | + # |
| 57 | + # %frontback.txt |
| 58 | + # front%back.txt |
| 59 | + # frontback%.txt |
| 60 | + # frontback.txt% |
| 61 | + # frontback.% |
| 62 | + # frontback% |
| 63 | + # |
| 64 | + # We also do the same with ".img" instead of ".txt". |
| 65 | + # |
| 66 | + # Note that we skip '/' because there's no way on a normal Unix |
| 67 | + # filesystem to create a file with '/' in its name. There might |
| 68 | + # be a way to persuade rclone to *send* such a filename, however, |
| 69 | + # and that's something we should try at some point. |
| 70 | + # |
| 71 | + # We skip 0 (NULL) because null bytes cause all sorts of trouble |
| 72 | + # -- which you would think would mean we *want* to include it, but |
| 73 | + # in practice Python's path-accepting functions don't like NULL |
| 74 | + # bytes either and it's quite unlikely to be found in a user's |
| 75 | + # filename... I think? I dunno. <shrug> If we need to test it, |
| 76 | + # we'll figure out how to get it through to disk below. For now, |
| 77 | + # I'm skipping it in the interests of not letting a perfect yak be |
| 78 | + # the enemy of a perfectly okay yak. |
| 79 | + for ch in (list(range(1, ord('/'))) # SOH through '.' |
| 80 | + + list(range(ord(':'), ord('A'))) # ':" through '@' |
| 81 | + + list(range(ord('['), ord('a'))) # '[" through '`' |
| 82 | + + list(range(ord('{'), 128))): # '{" through DEL |
| 83 | + ch = chr(ch) |
| 84 | + for ext in (text_ext, image_ext,): |
| 85 | + for fname in (ch + front + back + "." + ext, |
| 86 | + front + ch + back + "." + ext, |
| 87 | + front + back + ch + "." + ext, |
| 88 | + front + back + "." + ch + ext, |
| 89 | + front + back + "." + ch,): |
| 90 | + if ext == "txt": # text: use the tiny sample text |
| 91 | + src = os.path.join("sample-sources", "text-small.txt") |
| 92 | + else: # image: use the PNG |
| 93 | + src = os.path.join("sample-sources", "single-pixel.png") |
| 94 | + dst = os.path.join(test_tree_top, fname) |
| 95 | + # sys.stderr.write(f"DEBUG: creating {dst}\n") |
| 96 | + shutil.copy(src, dst) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == '__main__': |
| 100 | + main() |
0 commit comments