Server IP : 127.0.0.2 / Your IP : 3.144.149.8 Web Server : Apache/2.4.18 (Ubuntu) System : User : www-data ( ) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : disk_free_space,disk_total_space,diskfreespace,dl,exec,fpaththru,getmyuid,getmypid,highlight_file,ignore_user_abord,leak,listen,link,opcache_get_configuration,opcache_get_status,passthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,php_uname,phpinfo,posix_ctermid,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix,_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_times,posix_ttyname,posix_uname,pclose,popen,proc_open,proc_close,proc_get_status,proc_nice,proc_terminate,shell_exec,source,show_source,system,virtual MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/lib/python3/dist-packages/uaclient/files/ |
Upload File : |
import json import logging import os from typing import Any, Dict, Optional from uaclient import defaults, event_logger, exceptions, system, util event = event_logger.get_event_logger() LOG = logging.getLogger(util.replace_top_level_logger_name(__name__)) class UAFile: def __init__( self, name: str, directory: str = defaults.DEFAULT_DATA_DIR, private: bool = True, ): self._directory = directory self._file_name = name self._is_private = private self._path = os.path.join(self._directory, self._file_name) @property def path(self) -> str: return self._path @property def is_private(self) -> bool: return self._is_private @property def is_present(self): return os.path.exists(self.path) def write(self, content: str): file_mode = ( defaults.ROOT_READABLE_MODE if self.is_private else defaults.WORLD_READABLE_MODE ) # try/except-ing here avoids race conditions the best try: if os.path.basename(self._directory) == defaults.PRIVATE_SUBDIR: os.makedirs(self._directory, mode=0o700) else: os.makedirs(self._directory) except OSError: pass system.write_file(self.path, content, file_mode) def read(self) -> Optional[str]: content = None try: content = system.load_file(self.path) except FileNotFoundError: LOG.debug("Tried to load %s but file does not exist", self.path) return content def delete(self): system.ensure_file_absent(self.path) class ProJSONFile: def __init__( self, pro_file: UAFile, ): self.pro_file = pro_file def write(self, content: Dict[str, Any]): self.pro_file.write( content=json.dumps(content, cls=util.DatetimeAwareJSONEncoder) ) def read(self) -> Optional[Dict[str, Any]]: content = self.pro_file.read() if content: try: return json.loads(content, cls=util.DatetimeAwareJSONDecoder) except json.JSONDecodeError as e: raise exceptions.InvalidJson( source=self.pro_file.path, out="\n" + str(e) ) return None def delete(self): return self.pro_file.delete() @property def is_present(self): return self.pro_file.is_present class UserCacheFile(UAFile): def __init__(self, name: str): super().__init__( name, directory=system.get_user_cache_dir(), private=False )