def potree_dir(self, value): from vsi.tools.dir_util import is_subdir import posixpath storage = is_subdir(value, os.environ['VIP_STORAGE_DIR']) if storage[0]: value = posixpath.join('${VIP_STORAGE_DIR}', posixpath.normpath(storage[1])) else: image = is_subdir(value, os.environ['VIP_IMAGE_DIR']) if image[0]: value = posixpath.join('${VIP_IMAGE_DIR}', posixpath.normpath(image[1])) else: value = posixpath.normpath(value) self._potree_dir = value
def test_is_subdir(self): os.makedirs(os.path.join(self.temp_dir.name, 'a', 'b')) os.makedirs(os.path.join(self.temp_dir.name, 'c', 'd')) ans = is_subdir(os.path.join(self.temp_dir.name, 'a', 'b'), self.temp_dir.name) self.assertTrue(ans[0]) self.assertEqual(ans[1], os.path.join('a', 'b')) ans = is_subdir(os.path.join(self.temp_dir.name, 'a', '.', 'b'), self.temp_dir.name) self.assertTrue(ans[0]) self.assertEqual(ans[1], os.path.join('a', 'b')) ans = is_subdir(os.path.join(self.temp_dir.name, 'q', '..', 'a', 'b'), self.temp_dir.name) self.assertTrue(ans[0]) self.assertEqual(ans[1], os.path.join('a', 'b')) ans = is_subdir(os.path.join(self.temp_dir.name, 'a'), self.temp_dir.name) self.assertTrue(ans[0]) self.assertEqual(ans[1], 'a') ans = is_subdir(self.temp_dir.name, self.temp_dir.name) self.assertTrue(ans[0]) self.assertEqual(ans[1], ".") ans = is_subdir(self.temp_dir.name, os.path.join(self.temp_dir.name, 'a')) self.assertFalse(ans[0]) self.assertEqual(ans[1], "..") ans = is_subdir(os.path.join(self.temp_dir.name, 'a', 'b'), os.path.join(self.temp_dir.name, 'c', 'd')) self.assertFalse(ans[0]) self.assertEqual(ans[1], os.path.join('..', '..', 'a', 'b')) if os.name != 'nt': os.symlink(os.path.join(self.temp_dir.name, 'a', 'b'), os.path.join(self.temp_dir.name, 'e')) ans = is_subdir(os.path.join(self.temp_dir.name, 'e'), os.path.join(self.temp_dir.name, 'a')) self.assertTrue(ans[0]) self.assertEqual(ans[1], 'b') ans = is_subdir(os.path.join(self.temp_dir.name, 'e'), os.path.join(self.temp_dir.name, 'a'), False) self.assertFalse(ans[0]) self.assertEqual(ans[1], os.path.join('..', 'e'))
def delete_file_post_delete(sender, instance, using, **kwargs): for field in instance._meta.get_fields(): if isinstance(field, FileNameField): filename = os.path.expandvars(getattr(instance, field.name)) if is_subdir(filename, path): if os.path.isfile(filename): os.remove(filename) prune_dir(os.path.dirname(filename)) elif os.path.isdir(filename): shutil.rmtree(filename) prune_dir(filename) else: logger.error('Deletion of a non-existing file attempted: {}'.format( filename)) else: logger.warning('Deletion outside of {} attempted: {}'.format(path, filename))
def run_service(self, service_info): ''' Run a command, usually in a virtual env. If ``settings.compute.virtualenv_dir`` is set, then that directory is automatically prepended to the ``PATH`` for the command to be executed. Arguments --------- env : :class:`dict`, optional Sets environment variables. Same as Popen's ``env``, except ``JUSTFILE`` is programatically set, and cannot be overridden any other way than chaning the ``justfile`` variable *args : List of arguments to be pass to ``Popen`` **kwargs : Arguments sent to ``Popen`` command ''' logger.debug('Running: ' + ' '.join([quote(x) for x in service_info.command])) env = service_info.env # Replace 'python' command with virtual environment python executable if settings.compute.virtualenv_dir: env['PATH'] = settings.compute.virtualenv_dir \ + os.path.pathsep + service_info.env['PATH'] # TODO: Not sure if I want this or not # if "_OLD_VIRTUAL_PATH" in service_info.env: # service_info.env['PATH'] = service_info.env["_OLD_VIRTUAL_PATH"] if logger.getEffectiveLevel() <= DEBUG1: dd = dict_diff(os.environ, env)[3] if dd: logger.debug4('Environment Modification:\n' + '\n'.join(dd)) # Similar (but different) to a bug in docker compute, the right python # executable is not found on the path, possibly because Popen doesn't # search the env's path, but this will manually search and find the right # command executable = distutils.spawn.find_executable(service_info.command[0], path=env['PATH']) # Check if the executable was found in the virtualenv_dir. # If it wasn't, warn the user in case they made a mistake if settings.compute.virtualenv_dir is not None and \ not is_subdir(executable, settings.compute.virtualenv_dir)[0]: logger.warning( f"Couldn't find command {service_info.command[0]} in " f"virtualenv_dir {settings.compute.virtualenv_dir}. " f"Using {executable} instead. If you meant to bypass the " "virtualenv dir, then feel free to ignore this message. " "If you weren't expecting this, then make sure the " "compute.virtualenv_dir is correct.") # run command -- command must be a list of strings pid = Popen(service_info.command, env=env, executable=executable) if pid.wait() != 0: raise ServiceRunFailed()