def render_repo_filename(self, template, translation): component = translation.component # Render the template filename = render_template(template, translation=translation) # Validate filename (not absolute or linking to parent dir) try: validate_filename(filename) except ValidationError: return None # Absolute path filename = os.path.join(component.full_path, filename) # Check if parent directory exists dirname = os.path.dirname(filename) if not os.path.exists(dirname): os.makedirs(dirname) # Validate if there is not a symlink out of the tree try: component.repository.resolve_symlinks(dirname) if os.path.exists(filename): component.repository.resolve_symlinks(filename) except ValueError: component.log_error('refused to write out of repository: %s', filename) return None return filename
def render_repo_filename(self, template, translation): component = translation.component # Render the template filename = render_template(template, translation=translation) # Validate filename (not absolute or linking to parent dir) try: validate_filename(filename) except ValidationError: return None # Absolute path filename = os.path.join(component.full_path, filename) # Check if parent directory exists dirname = os.path.dirname(filename) if not os.path.exists(dirname): os.makedirs(dirname) # Validate if there is not a symlink out of the tree try: component.repository.resolve_symlinks(dirname) if os.path.exists(filename): component.repository.resolve_symlinks(filename) except ValueError: component.log_error( 'refused to write out of repository: %s', filename ) return None return filename
def pre_commit(self, translation, author): filename = render_template(self.instance.configuration['filename'], translation=translation) try: validate_filename(filename) except ValidationError: return filename = os.path.join(self.instance.component.full_path, filename) content = render_template(self.instance.configuration['template'], translation=translation) dirname = os.path.dirname(filename) if not os.path.exists(dirname): os.makedirs(dirname) with open(filename, 'w') as handle: handle.write(content) translation.addon_commit_files.append(filename)
def clean_filename(self): self.test_render(self.cleaned_data['filename']) validate_filename(self.cleaned_data['filename']) return self.cleaned_data['filename']
def test_empty(self): validate_filename("")
def test_simplification(self): with self.assertRaises(ValidationError): validate_filename("path/./file")
def test_good(self): validate_filename("path/file")
def test_absolute(self): with self.assertRaises(ValidationError): validate_filename("/path")
def test_parent(self): with self.assertRaises(ValidationError): validate_filename("../path")
def clean_path(self): self.test_render(self.cleaned_data["path"]) validate_filename(self.cleaned_data["path"]) return self.cleaned_data["path"]
def test_good(self): validate_filename('path/file')
def test_absolute(self): with self.assertRaises(ValidationError): validate_filename('/path')
def test_parent(self): with self.assertRaises(ValidationError): validate_filename('../path')
def clean_filename(self): self.test_render(self.cleaned_data['filename']) validate_filename(self.cleaned_data['filename']) return self.cleaned_data['filename']