def pivot_root(new, old): """Move the filesystem specfied by `new` and mount it at '/' and move the old '/' to `old` Arguments ---------- :param str new: Path to a mounted filesystem to make the new '/' :param str old: Location where current '/' should be mounted Returns -------- No return value Exceptions ----------- :raises ValueError: `new` or `old` does not refer to a directory :raises ValueError: `new` or `old` are on the current root filesystem or filesystem already mounted on `old` :raises ValueError: `old` is not a folder underneath `new` :raises PermissionError: No permission to pivot_root to new location """ assert len(new) > 0 assert len(old) > 0 assert isinstance(new, (str, bytes)), "new must be a string" assert isinstance(old, (str, bytes)), "old must be a string" if isinstance(new, str): new = new.encode() if isinstance(old, str): old = old.encode() err = _lib.pivot_root(new, old) if err < 0: err = _ffi.errno if err == _errno.EINVAL: raise ValueError("{} is not a sub-directory of {}".format( old, new)) elif err == _errno.EBUSY: raise ValueError( "old or new are on the current root filesystem or filesystem already mounted on {}" .format(old)) elif err == _errno.ENOTDIR: if _isdir(new): raise ValueError("{} is not a Directory".format(new)) elif _isdir(old): raise ValueError("{} is not a Directory".format(old)) else: # this is a bug but testing for this case just in case, let us know if you # hit it raise ValueError( "old or new is not a dir but could not work out which one") elif err == _errno.EPERM: raise PermissionError( "Permission denied, CAP_SYS_ADMIN not in capability bits") else: # If you are here, its a bug. send us the traceback raise UnknownError(err)
def pivot_root(new, old): """Move the filesystem specfied by `new` and mount it at '/' and move the old '/' to `old` Arguments ---------- :param str new: Path to a mounted filesystem to make the new '/' :param str old: Location where current '/' should be mounted Returns -------- No return value Exceptions ----------- :raises ValueError: `new` or `old` does not refer to a directory :raises ValueError: `new` or `old` are on the current root filesystem or filesystem already mounted on `old` :raises ValueError: `old` is not a folder underneath `new` :raises PermissionError: No permission to pivot_root to new location """ assert len(new) > 0 assert len(old) > 0 assert isinstance(new, (str, bytes)), "new must be a string" assert isinstance(old, (str, bytes)), "old must be a string" if isinstance(new, str): new = new.encode() if isinstance(old, str): old = old.encode() err = _C.pivot_root(new, old) if err < 0: err = _ffi.errno if err == _errno.EINVAL: raise ValueError("{} is not a sub-directory of {}".format(old, new)) elif err == _errno.EBUSY: raise ValueError("old or new are on the current root filesystem or filesystem already mounted on {}".format(old)) elif err == _errno.ENOTDIR: if _isdir(new): raise ValueError("{} is not a Directory".format(new)) elif _isdir(old): raise ValueError("{} is not a Directory".format(old)) else: # this is a bug but testing for this case just in case, let us know if you # hit it raise ValueError("old or new is not a dir but could not work out which one") elif err == _errno.EPERM: raise PermissionError("Permission denied, CAP_SYS_ADMIN not in capability bits") else: # If you are here, its a bug. send us the traceback raise UnknownError(err)
def Check(FilePath): """Check(FilPath) Check the permission of file and return value """ if (_isfile(FilePath) or _isdir(FilePath)): chmodValue = ((oct(_stat(FilePath).st_mode & 0O777))[-3:]) return (int(chmodValue)) else: chmodValue = "File Not Found" return (chmodValue)
def enum_files(root,*allow_ext): allow_ext=list(map(lambda s:s.lower(),allow_ext)) if len(allow_ext)==0: allow_ext=[''] if not _isdir(root): return for di,fd,fl in _walk(root): for f in fl: for ext in allow_ext: if f.lower().endswith(ext): yield _join(di,f)
def loader(path): """ Returns a loader object for a given path, with the correct finder class (``ModuleFinder`` for directories and ``FileFinder`` for single file modules). :param path: The path to the module(s). """ finder = ModuleFinder if _isdir(path) else FileFinder return Loader(finder(path))
def dir_exists(dir_path, create=False): """ :param dir_path: :param create: :return: """ exists = _isdir(dir_path) if create and not exists: _makedir(dir_path) return True return exists
def symlink(src, dst): """ Extended "os.symlink" that: - Autodetect if target is directory. - Ignore error if file already exists. - Ensure to link to real absolute path of the source. Args: src (path-like object): Source path. dst (path-like object): Destination path. """ src = _realpath(_fsdecode(src)) try: _symlink(src, _fsdecode(dst), target_is_directory=_isdir(src)) except FileExistsError: pass
def create_virtualenv(directory=None, pyver=None, interpreter='python'): """ Create a virtual environment for inyoka. :param directory: Where to create this virtual environment (folder must not exist). :param pyver: Which Python Version to use. """ if directory is None: workon_home = os.environ.get('WORKON_HOME') if workon_home: directory = _path.join(workon_home, 'inyoka') else: directory = '../inyoka-testsuite' if not _isdir(directory): _mkdir(_path.expanduser(_path.expandvars(directory))) local('%s %s > %s' % (interpreter, _j('extra/make-bootstrap.py'), _j('bootstrap.py')), capture=False) local('%s ./bootstrap.py --no-site-packages -r %s %s' % (interpreter, _j('extra/requirements.txt'), directory), capture=False)
def put(sources, host=None, expiry="24h", archive=False, compression=None, name=None): """ Args: sources (iterable of str): The files or directory to share. archive (bool): If True, create an archive before putting on the share. Automatic if "sources" is a directory or multiples files. expiry (str or int): The download expiry time. In hours, or with a specific time unit by appending: "s" for seconds, "m" for minutes, "h" for hours or "d" for days (For instance: "7d" for 7 days, "30m" for 30 minutes). compression (str): The compression method to use when archiving. Possible values: "gz", "bz2", "xz". Default to value saved in configuration or "gz". host (str): The remote storage host. name (str): Rename the file being put. Returns: str: The shareable URL to the shared object. """ config = _get_host_config(host) expiry = _parse_expiry(expiry) max_expiry = config["max_expiry_sec"] if max_expiry and expiry > max_expiry: raise ValueError(f"Expiry can't be more than {config['max_expiry']}.") if archive or len(sources) > 1 or any(_isdir(path) for path in sources): compression = compression or config["compression"] dst = _set_dst_path(name or f"archive.tar.{compression}", config) with _open(dst, "wb") as fileobj: with _tarfile_open(fileobj=fileobj, mode=f"w:{compression}") as dst_archive: for src in sources: dst_archive.add(src, arcname=_basename(src)) else: dst = _set_dst_path(name or _basename(sources[0]), config) _copy(sources[0], dst) return _shareable_url(dst, expiry)
def test_dir(directory): if not _isdir(directory): try: _mkdir(directory) except: pass
def isdir(self, path): return _isdir(self.getsyspath(path))
def make_all_dirs(*file_paths): for fp in file_paths: if not _isdir(fp): _make_dirs(fp)
from os.path import isdir as _isdir, join as _join AUTHOR = 'CeroProgramming' LICENSE = 'MIT' VERSION = '0.0.1' DATAFP = '/var/local/cxapps/' LOGFP = '/var/log/cxapps/' LOGFILENAME = 'default.log' APPFP = '/usr/local/cxapps/' APPSFP = '/usr/local/cxapps/modules/' CONFIGFP = '/usr/local/etc/cxapps/' LIBFP = '/usr/local/lib/cxlibs/' LOG_LEVEL = 'DEBUG' MODE = 'dev' if MODE == 'dev': DATAFP = '/tmp' + DATAFP LOGFP = '/tmp' + LOGFP APPFP = '/tmp' + APPFP APPSFP = '/tmp' + APPSFP CONFIGFP = '/tmp' + CONFIGFP for fp in (DATAFP, LOGFP, APPFP, APPSFP, CONFIGFP): if not _isdir(fp): _make_dirs(fp) def get_application_install_path(name): return _join(APPSFP, name.lower())
# maybe set GDAL_DATA variable from os import environ as _environ from os.path import join as _join, dirname as _dirname, basename as _basename # from warnings import warn as _warn if not "GDAL_DATA" in _environ: from os.path import isdir as _isdir from sys import executable as _executable for d in [ _join(_dirname(_executable), "Library", "share", "gdal"), # Common location on windows _join(_dirname(_executable), "..", "share", "gdal"), ]: # Common location on linux if _isdir(d): # _warn("Setting GDAL_DATA to: "+d, UserWarning) _environ["GDAL_DATA"] = d break if not "GDAL_DATA" in _environ: raise RuntimeError( "Could not locate GDAL_DATA folder. Please set this as an environment variable pointing to the GDAL static files" ) # import the utilities import geokit.util import geokit.srs import geokit.geom import geokit.raster import geokit.vector
def mkdir(x: str): if _isdir(x): return False _i_mkdir(x) return True