def __init__(self, path): self.path = path self.title = path.stem self.slug = self._slugify(self.title) # If the file is not present create an empty one. if not path.exists(): path.touch() self.updated = self._getmtime() self.age = self._calc_age()
def __init__(self, path): self.path = path self.title = path.name if path.exists(): with path.open() as f: self.content = f.read() else: path.touch() self.updated = self._getmtime()
def touch(filename): """ Creates file if it doesn't exist. Always modifies utime. Returns a Path(filename) """ path = pathify(filename) path.touch(exist_ok=True) return path
def __init__(self, path): self.path = path self.title = path.stem self._content = b'' # If the file is not present create an empty one. if not path.exists(): path.touch() self.is_encrypted = self._check_encrypted() self.updated = self._getmtime() self.age = self._calc_age()
def create_open_file(fn): """ vim always opens a buffer, and creates a file on write if the file doesn't exist. This hopefully creates a more consistent experience: the file will always 'physically' exist on disk. """ path = Path(fn) if not path.exists(): path.parent.mkdir(parents=True, exist_ok=True) path.touch() vim.command("silent e " + fn)
def isValid(self): """ Validate the name of the log file. The log filename is valid if: 1) The file exists. 2) The file can be created. Try to create the log file if it does not exist. """ if (self._flags & self.FLAG_CLEAR_HIGHLIGHT_BEFORE_VALIDATING): self.clearHighlight() if ((self._flags & self.FLAG_DISABLED_WIDGET_ALWAYS_VALID) and (not self._widget.isEnabled())): return True log = SingletonLog() path = pathlib.Path(self._widget.text()) if (path.is_file()): return True result = QMessageBox.question( QApplication.instance().mainWindow, 'Create Log File?', 'Log file {} does not exist. Do you want to create it?'.format( path.name)) if (result == QMessageBox.Yes): try: path.touch(exist_ok=False) QMessageBox.information( QApplication.instance().mainWindow, 'Create Log File', 'Log file {} was created.'.format(path.name)) return True except (FileExistsError, Exception) as err: QMessageBox.critical( QApplication.instance().mainWindow, 'Create Log File Error', 'Unable to create log file "{}".\n{}'.format( path.name, err)) if (self._flags & self.FLAG_HIGHLIGHT_WIDGETS_WITH_ERRORS): self.setHighlight() if (self._flags & self.FLAG_SHOW_ERROR_MESSAGE): QMessageBox.critical(QApplication.instance().mainWindow, self._errorTitle, self._errorMessage) return False
def ensure_overwritable(*paths): """ This is a (nasty) workaround for the permissions issues we hit when switching between running the job-runner inside Docker and running it natively on Windows. The issue is that the Docker process creates files which the Windows native process then doesn't have permission to delete or replace. We work around this here by using Docker to delete the offending files for us. Note for the potentially confused: Windows permissions work nothing like POSIX permissions. We can create new files in directories created by Docker, we just can't modify or delete existing files. """ if not config.ENABLE_PERMISSIONS_WORKAROUND: return non_writable = [] for path in paths: path = Path(path) if path.exists(): # It would be nice to have a read-only way of determining if we # have write access but I can't seem to find one that works on # Windows try: path.touch() except PermissionError: non_writable.append(path.resolve()) if not non_writable: return root = os.path.commonpath([f.parent for f in non_writable]) rel_paths = [f.relative_to(root) for f in non_writable] rel_posix_paths = [str(f).replace(os.path.sep, "/") for f in rel_paths] subprocess_run( [ "docker", "run", "--rm", "--volume", f"{root}:/workspace", "--workdir", "/workspace", docker.MANAGEMENT_CONTAINER_IMAGE, "rm", *rel_posix_paths, ], check=True, capture_output=True, )
def log_to_file(file, line, with_timestamp=True): """Append line to file. If file is a single dash, write to stdout instead. """ line = f'{line.rstrip()}\n' if with_timestamp: timestamp = datetime.datetime.now().isoformat() line = f'[{timestamp}] {line}' if file == '-': sys.stdout.write(line) else: path = Path(file) if not path.exists(): path.touch(mode=0o664) with path.open('a') as fp: fp.write(line)
def reload(): '''reload web app at pythonanywhere.com''' paths = [ Path('/var/www/macke_eu_pythonanywhere_com_wsgi.py'), Path('/var/www/test-macke_eu_pythonanywhere_com_wsgi.py') ] found = False for path in paths: if path.exists(): print(f"Touching {path}") path.touch() found = True else: print(f"Ignoring WSGI file {path} as it doesn't exist here.") if found: run(['python', 'manage.py', 'check', '--deploy'], shell=SHELL)
def test_messy_name(self, tmp_path: Path) -> None: # http://bitbucket.org/hpk42/py-trunk/issue/129 path = tmp_path / "foo__init__.py" path.touch() module = import_path(path) assert module.__name__ == "foo__init__"