okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b7bcbcde9
main
1"""Is this a folder Strata can keep model files in? Said in plain words before anything is downloaded (the Folder step, --data-dir, setup). 2 3`problem(path)` returns (what is wrong, what to do) or None. Nothing is created and nothing is left behind: a missing folder is judged by the 4nearest folder above it that exists (can a file be made there, so the folder could be created), and "can be written" is tried by creating and 5deleting a small file, never read from the permission bits. 6""" 7 8from __future__import annotations 9 10import os 11import re 12import uuid 13from pathlib import Path 14 15NEXT = "Pick another folder, for example one on a drive you own, like D:\\Strata-models. Nothing was downloaded or changed." 16NEXT_POSIX = "Pick another folder you own, for example ~/strata-models. Nothing was downloaded or changed." 17 18_WIN_BAD = re .compile (r'[<>"|?*\x00-\x1f]' ) 19_POSIX_SYSTEM = ("/bin" ,"/boot" ,"/dev" ,"/etc" ,"/lib" ,"/lib32" ,"/lib64" ,"/proc" ,"/root" ,"/run" ,"/sbin" ,"/sys" ,"/usr" ,"/var/lib" ,"/System" , 20"/Library" ,"/private/etc" ) 21# Where a desktop (udisks, GNOME, KDE) mounts the drives a person plugs in. It sits inside /run, a system folder, but it is not system 22# state: a model drive there is normal. Whether it is the person's to write to is the write test below, not the name. 23_POSIX_MOUNTS = ("/run/media" ,) 24SAID_NOTHING_CHANGED = "Nothing was downloaded or changed." 25 26 27def _next ()-> str : 28return NEXT if os .name == "nt" else NEXT_POSIX 29 30 31def _system_roots ()-> list [str ]: 32if os .name != "nt" : 33return list (_POSIX_SYSTEM ) 34roots = [] 35for var in ("SystemRoot" ,"windir" ,"ProgramFiles" ,"ProgramFiles(x86)" ,"ProgramW6432" ): 36value = os .environ .get (var ) 37if value : 38roots .append (value ) 39drive = os .environ .get ("SystemDrive" ,"C:" ) 40roots += [drive + "\\Windows" ,drive + "\\Program Files" ,drive + "\\Program Files (x86)" ] 41return roots 42 43 44def _norm (path :str )-> str : 45return os .path .normcase (os .path .normpath (path )).rstrip ("\\/" ) 46 47 48def _inside (path :str ,root :str )-> bool : 49p ,r = _norm (path ),_norm (root ) 50return p == r or p .startswith (r + os .sep ) 51 52 53def _is_system (full :str )-> bool : 54for root in _system_roots (): 55if not _inside (full ,root )or _inside (full ,os .path .expanduser ("~" )):# the running user's own home is theirs (root's /root included) 56continue 57if any (_inside (full ,mount )and _norm (full )!= _norm (mount )for mount in _POSIX_MOUNTS ): 58continue # /run/media/<user>/<drive>/...: a mounted drive 59return True 60return False 61 62 63def _can_write (folder :Path )-> bool : 64probe = folder / f".strata-write-test- { uuid . uuid4 (). hex [: 8 ] } " 65try : 66with open (probe ,"wb" )as fh : 67fh .write (b"x" ) 68except OSError : 69return False 70try : 71probe .unlink () 72except OSError : 73pass 74return True 75 76 77def problem (raw :str | os .PathLike | None )-> tuple [str ,str ]| None : 78"""(what is wrong, what to do) for a folder meant for model files, None when it is usable (or empty: the default folder is used).""" 79text = os .fspath (raw ).strip ().strip ('"' )if raw else "" 80if not text : 81return None 82if _WIN_BAD .search (text .replace ("\\\\?\\" ,"" ,1 ))or (os .name == "nt" and ":" in text [2 :]and not text .startswith ("\\\\?\\" )): 83return (f"This does not look like one folder path: { text } ." , 84"Type one full path, like D:\\Strata-models, with nothing else in front of or behind it. Nothing was downloaded or changed." 85if os .name == "nt" else "Type one full path, like ~/strata-models. Nothing was downloaded or changed." ) 86path = Path (text ).expanduser () 87if not path .is_absolute (): 88return (f"This is not a full path: { text } ." , 89"Type the whole path, starting with the drive, like D:\\Strata-models. Nothing was downloaded or changed." 90if os .name == "nt" else "Type the whole path, starting with /, like /data/strata-models. Nothing was downloaded or changed." ) 91full = os .path .abspath (path ) 92if _is_system (full ): 93return (f" { full } is a system folder, and Strata should not keep model files there." ,_next ()) 94try : 95if path .exists (): 96if not path .is_dir (): 97return (f" { full } is a file, not a folder." ,_next ()) 98if not _can_write (path ): 99return (f"Strata cannot write to { full } : Windows or the folder's owner does not allow it." if os .name == "nt" 100else f"Strata cannot write to { full } : you do not have permission." ,_next ()) 101return None 102above = path 103while not above .exists (): 104if above .parent == above : 105return (f"The drive or folder for { full } is not there. Check the spelling, and that the drive is connected." ,_next ()) 106above = above .parent 107if not above .is_dir (): 108return (f" { above } is a file, so { full } cannot be created inside it." ,_next ()) 109if not _can_write (above ): 110return (f" { full } does not exist yet and cannot be created: Strata cannot write to { above } ." ,_next ()) 111except OSError as e : 112return (f"Strata cannot look at { full } ( { type ( e ). __name__ } )." ,_next ()) 113return None 114 115 116def words (problem_ :tuple [str ,str ])-> str : 117return f" { problem_ [ 0 ] } { problem_ [ 1 ] } " .strip () 118 119 120def without_nothing_changed (problem_ :tuple [str ,str ])-> str : 121"""The same words with the closing "Nothing was downloaded or changed." left off, for a caller that says it again in its own sentence.""" 122what ,nxt = problem_ 123if nxt .endswith (SAID_NOTHING_CHANGED ): 124nxt = nxt [:- len (SAID_NOTHING_CHANGED )] 125return f" { what } { nxt } " .strip ()