var/opt/nydus/ops/primordial/__pycache__/fileutils.cpython-38.pyc000064400000015264147205563050021122 0ustar00U af@sddlZddlZddlZddlZddlmZmZmZzddlm Z Wne k rXYnXzddl m Z Wne k r~YnXddl mZddlZzddlZddlZWne k rYnXddlmZmZdZdZeeZejed d d Zdeeeeefdd d dZeddddZeddddZ eeddddZ!deeeeddddZ"d eeeeeeddddZ#dS)!N)AnyOptionalTuple)getpwnam)getgrnam)rmtree)get_file_group get_file_userg?)in_timereturncCst|tdddS)zpReturn integer seconds since epoch. :param in_time: Datetime :returns: Integer seconds since epoch i)intdatetime total_seconds)r rE/opt/nydus/tmp/pip-target-53d1vnqk/lib/python/primordial/fileutils.pydatetime_to_epoch_seconds#sr)filenametimesr c Cs&t|dt||W5QRXdS)a0Set the atime and mtime of a file. .. note:: Linux (MAYBE Windows) ONLY. This does not work on Mac OSX. :param filename: The file to touch :param times: a two-tuple of (atime, mtime) where these are integer seconds since epoch; see os.utimes :raises OSError: if file doesn't exist aN)openosutime)rrrrrtouch,s r)pathr c CsBz t|Wn0tk r<}ztd||W5d}~XYnXdS)zySimple rmtree wrapper :param path: path to recursively remove :raises: IOError if the path can't be removed Failed to remove %sN)rOSErrorIOError)rerrrrm_path:s r )rr c CsDzt|Wn0tk r>}ztd||W5d}~XYnXdS)zuSimple remove wrapper :param filename: filename to remove :raises: IOError if the file can't be removed rN)rremoverr)rrrrrrm_fileFsr")rdatar c Cs0tjtj|d\}}t|d}t|}zz|t | dWnft k rz}zt d||f|W5d}~XYn4t k r}zt d||f|W5d}~XYnXW5|Xt|}|j|krt d||j|fzt||Wn6t k r*}zt d||f|W5d}~XYnXdS)z*A utility helper for the atomic file utils)dirwbutf8z$Failed to write data to path %s (%s)Nz1Unexpectedly failed to write data to path %s (%s)z4Failed to write correct number of bytes (%s, %s, %s))tempfilemkstemprrdirnamefdopenlenclosewritestrencoderr Exceptionstatst_sizerename)rr#Zoutfile_handleZ tmpfilenameoutfileZdatalenrZ stat_structrrr _write_fileRs& "(    r5)rr#add_all_access_userr cCstj|rt|t|||dk r~td|\}}}t|tj}| }| tj t j ||d|dt|tj|dS)a4Safely/atomically write a file on Windows. Write a file in such a way that the data is guaranteed to be complete and not intermixed with that of another process. This implementation will NOT clobber the target file if it exists already; instead, it will fail immediately. This is a windows-specific implementation that has Windows locking semantics for open file handles and rather than changing the file's ownership, setting add_all_access_user will cause the file to have a read-write ACE added to the ACL for the directory/file for the target user. Note that in both this function and atomically_write_file, the fact that the file has locking or not at the OS level is NOT being relied on in e.g. archon's file utilities for leasing or exclusive access. Instead, this implementation uses tmpfile to guarantee uniqueness of the source of information, and then a simple atomic mv to replace the destination. Windows, unlike Linux, could support a true OS level file locking layer for exclusive access, and so a future Windows specific file utility would be feasible for true single-host, global locking for use in e.g. archon. From https://stackoverflow.com/questions/12168110/setting-folder-permissions-in-windows-using-python :param filename: filename to write :param data: data to put in file :param add_all_access_user: the user if any to add a FILE_ALL_ACCESS ACE for :raises pywintypes.error: on failure to modify the file's ACL; pywintypes.error on user not found; pywintypes.error on file not found Nr r)rrexistsr"r5 win32securityZLookupAccountNameZGetFileSecurityZDACL_SECURITY_INFORMATIONZGetSecurityDescriptorDaclZAddAccessAllowedAceZ ACL_REVISIONconZFILE_ALL_ACCESSZSetSecurityDescriptorDaclZSetFileSecurity)rr#r6Zuserx_Zfile_sdZdaclrrrwin_atomically_write_filels  r<)rr# file_owner file_groupr cCs\t||d}|pt}|dk r*t|j}d}|p6t}|dk rJt|j}t|||dS)aSafely/atomically write a file. Write to a tmpfile then do os.rename(); Note that this is for Unix systems only, as the implicit contract is that the destination might exist and will be overwritten. This contract is violated on Windows. Also even on Unix, the file system of the source and dest must be the same; since we're in the same directory for this move, that constraint is satisfied. We use tempfile to make it so that we don't collide on the source tmp write -- different processes or threads will not select the same tmpfile. Last man in wins for the move -- there's no general way to sync this across processes or hosts using a filesystem store. :param filename: filename to ultimately write :param data: data to put in file :param file_owner: If set, will change file to this owner if permission is available :param file_group: If set, will change file to this group if permission is available :raises: IOError on failure; OSError on permission change without appropriate permissions N) r5r rpw_uidrrgr_gidrchown)rr#r=r>file_uidZfile_group_gidrrratomically_write_files     rD)N)N)NN)$rloggingros.pathtypingrrrpwdr ImportErrorgrprshutilrr'r9Z ntsecurityconr:Zprimordial.settingsrr ZDELAY_INCREASE_SECSZ MAX_RETRIES getLogger__name__LOGGERrrr.rr r"r5r<rDrrrrsL      &