Python os.F_OK Examples
The following are 30 code examples for showing how to use os.F_OK(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
os
, or try the search function
.
Example 1
Project: jbox Author: jpush File: base.py License: MIT License | 6 votes |
def __init__(self, dir, file_template=_default_file_template, truncate_slug_length=40, version_locations=None, sourceless=False, output_encoding="utf-8"): self.dir = dir self.file_template = file_template self.version_locations = version_locations self.truncate_slug_length = truncate_slug_length or 40 self.sourceless = sourceless self.output_encoding = output_encoding self.revision_map = revision.RevisionMap(self._load_revisions) if not os.access(dir, os.F_OK): raise util.CommandError("Path doesn't exist: %r. Please use " "the 'init' command to create a new " "scripts folder." % dir)
Example 2
Project: jbox Author: jpush File: env.py License: MIT License | 6 votes |
def write_script( scriptdir, rev_id, content, encoding='ascii', sourceless=False): old = scriptdir.revision_map.get_revision(rev_id) path = old.path content = textwrap.dedent(content) if encoding: content = content.encode(encoding) with open(path, 'wb') as fp: fp.write(content) pyc_path = util.pyc_file_from_path(path) if os.access(pyc_path, os.F_OK): os.unlink(pyc_path) script = Script._from_path(scriptdir, path) old = scriptdir.revision_map.get_revision(script.revision) if old.down_revision != script.down_revision: raise Exception("Can't change down_revision " "on a refresh operation.") scriptdir.revision_map.add_revision(script, _replace=True) if sourceless: make_sourceless(path)
Example 3
Project: ChromaTerm Author: hSaria File: cli.py License: MIT License | 6 votes |
def read_file(location): """Returns the contents of a file or None on error. The error is printed to stderr. Args: location (str): The location of the file to be read. """ location = os.path.expandvars(location) if not os.access(location, os.F_OK): eprint('Configuration file', location, 'not found') return None try: with open(location, 'r') as file: return file.read() except PermissionError: eprint('Cannot read configuration file', location, '(permission)') return None
Example 4
Project: rekall Author: google File: cache.py License: GNU General Public License v2.0 | 6 votes |
def GetCacheDir(session): """Returns the path of a usable cache directory.""" cache_dir = session.GetParameter("cache_dir") if cache_dir == None: return cache_dir cache_dir = os.path.expandvars(cache_dir) if not cache_dir: raise io_manager.IOManagerError( "Local profile cache is not configured - " "add a cache_dir parameter to ~/.rekallrc.") # Cache dir may be specified relative to the home directory. cache_dir = os.path.join(config.GetHomeDir(session), cache_dir) if not os.access(cache_dir, os.F_OK | os.R_OK | os.W_OK | os.X_OK): try: os.makedirs(cache_dir) except (IOError, OSError): raise io_manager.IOManagerError( "Unable to create or access cache directory %s" % cache_dir) return cache_dir
Example 5
Project: MatchingMarkets.py Author: QuantEcon File: solvers.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def setTmpDir(self): """Set the tmpDir attribute to a reasonnable location for a temporary directory""" if os.name != 'nt': # On unix use /tmp by default self.tmpDir = os.environ.get("TMPDIR", "/tmp") self.tmpDir = os.environ.get("TMP", self.tmpDir) else: # On Windows use the current directory self.tmpDir = os.environ.get("TMPDIR", "") self.tmpDir = os.environ.get("TMP", self.tmpDir) self.tmpDir = os.environ.get("TEMP", self.tmpDir) if not os.path.isdir(self.tmpDir): self.tmpDir = "" elif not os.access(self.tmpDir, os.F_OK + os.W_OK): self.tmpDir = ""
Example 6
Project: GTDWeb Author: lanbing510 File: creation.py License: GNU General Public License v2.0 | 6 votes |
def _create_test_db(self, verbosity, autoclobber, keepdb=False): test_database_name = self._get_test_db_name() if keepdb: return test_database_name if not self.connection.is_in_memory_db(test_database_name): # Erase the old test database if verbosity >= 1: print("Destroying old test database '%s'..." % self.connection.alias) if os.access(test_database_name, os.F_OK): if not autoclobber: confirm = input( "Type 'yes' if you would like to try deleting the test " "database '%s', or 'no' to cancel: " % test_database_name ) if autoclobber or confirm == 'yes': try: os.remove(test_database_name) except Exception as e: sys.stderr.write("Got an error deleting the old test database: %s\n" % e) sys.exit(2) else: print("Tests cancelled.") sys.exit(1) return test_database_name
Example 7
Project: ironpython2 Author: IronLanguages File: uuid.py License: Apache License 2.0 | 6 votes |
def _popen(command, args): import os path = os.environ.get("PATH", os.defpath).split(os.pathsep) path.extend(('/sbin', '/usr/sbin')) for dir in path: executable = os.path.join(dir, command) if (os.path.exists(executable) and os.access(executable, os.F_OK | os.X_OK) and not os.path.isdir(executable)): break else: return None # LC_ALL to ensure English output, 2>/dev/null to prevent output on # stderr (Note: we don't have an example where the words we search for # are actually localized, but in theory some system could do so.) cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args) return os.popen(cmd)
Example 8
Project: oversteer Author: berarma File: wheels.py License: GNU General Public License v3.0 | 6 votes |
def check_permissions(self, device_id): if not os.access(self.devices[device_id]['path'], os.F_OK | os.R_OK | os.X_OK): return False if not self.check_file_permissions(self.device_file(device_id, 'alternate_modes')): return False if not self.check_file_permissions(self.device_file(device_id, 'range')): return False if not self.check_file_permissions(self.device_file(device_id, 'combine_pedals')): return False if not self.check_file_permissions(self.device_file(device_id, 'gain')): return False if not self.check_file_permissions(self.device_file(device_id, 'autocenter')): return False if not self.check_file_permissions(self.device_file(device_id, 'spring_level')): return False if not self.check_file_permissions(self.device_file(device_id, 'damper_level')): return False if not self.check_file_permissions(self.device_file(device_id, 'friction_level')): return False if not self.check_file_permissions(self.device_file(device_id, 'ffb_leds')): return False if not self.check_file_permissions(self.device_file(device_id, 'peak_ffb_level')): return False return True
Example 9
Project: psutil Author: giampaolo File: _pslinux.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def is_storage_device(name): """Return True if the given name refers to a root device (e.g. "sda", "nvme0n1") as opposed to a logical partition (e.g. "sda1", "nvme0n1p1"). If name is a virtual device (e.g. "loop1", "ram") return True. """ # Readapted from iostat source code, see: # https://github.com/sysstat/sysstat/blob/ # 97912938cd476645b267280069e83b1c8dc0e1c7/common.c#L208 # Some devices may have a slash in their name (e.g. cciss/c0d0...). name = name.replace('/', '!') including_virtual = True if including_virtual: path = "/sys/block/%s" % name else: path = "/sys/block/%s/device" % name return os.access(path, os.F_OK)
Example 10
Project: bioforum Author: reBiocoder File: creation.py License: MIT License | 6 votes |
def _clone_test_db(self, suffix, verbosity, keepdb=False): source_database_name = self.connection.settings_dict['NAME'] target_database_name = self.get_test_db_clone_settings(suffix)['NAME'] # Forking automatically makes a copy of an in-memory database. if not self.is_in_memory_db(source_database_name): # Erase the old test database if os.access(target_database_name, os.F_OK): if keepdb: return if verbosity >= 1: print("Destroying old test database for alias %s..." % ( self._get_database_display_str(verbosity, target_database_name), )) try: os.remove(target_database_name) except Exception as e: sys.stderr.write("Got an error deleting the old test database: %s\n" % e) sys.exit(2) try: shutil.copy(source_database_name, target_database_name) except Exception as e: sys.stderr.write("Got an error cloning the test database: %s\n" % e) sys.exit(2)
Example 11
Project: Computable Author: ktraunmueller File: catalog.py License: MIT License | 6 votes |
def get_writable_file(self,existing_only=0): """ Return the name of the first writable catalog file. Its parent directory must also be writable. This is so that compiled modules can be written to the same directory. """ # note: both file and its parent directory must be writeable if existing_only: files = self.get_existing_files() else: files = self.get_catalog_files() # filter for (file exists and is writable) OR directory is writable def file_test(x): from os import access, F_OK, W_OK return (access(x,F_OK) and access(x,W_OK) or access(os.path.dirname(x),W_OK)) writable = filter(file_test,files) if writable: file = writable[0] else: file = None return file
Example 12
Project: oss-ftp Author: aliyun File: uuid.py License: MIT License | 6 votes |
def _popen(command, args): import os path = os.environ.get("PATH", os.defpath).split(os.pathsep) path.extend(('/sbin', '/usr/sbin')) for dir in path: executable = os.path.join(dir, command) if (os.path.exists(executable) and os.access(executable, os.F_OK | os.X_OK) and not os.path.isdir(executable)): break else: return None # LC_ALL to ensure English output, 2>/dev/null to prevent output on # stderr (Note: we don't have an example where the words we search for # are actually localized, but in theory some system could do so.) cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args) return os.popen(cmd)
Example 13
Project: pex Author: pantsbuild File: common.py License: Apache License 2.0 | 6 votes |
def can_write_dir(path): """Determines if the directory at path can be written to by the current process. If the directory doesn't exist, determines if it can be created and thus written to. N.B.: This is a best-effort check only that uses permission heuristics and does not actually test that the directory can be written to with and writes. :param str path: The directory path to test. :return: `True` if the given path is a directory that can be written to by the current process. :rtype boo: """ while not os.access(path, os.F_OK): parent_path = os.path.dirname(path) if not parent_path or (parent_path == path): # We've recursed up to the root without success, which shouldn't happen, return False path = parent_path return os.path.isdir(path) and os.access(path, os.R_OK | os.W_OK | os.X_OK)
Example 14
Project: teleport Author: tp4a File: _pslinux.py License: Apache License 2.0 | 6 votes |
def is_storage_device(name): """Return True if the given name refers to a root device (e.g. "sda", "nvme0n1") as opposed to a logical partition (e.g. "sda1", "nvme0n1p1"). If name is a virtual device (e.g. "loop1", "ram") return True. """ # Readapted from iostat source code, see: # https://github.com/sysstat/sysstat/blob/ # 97912938cd476645b267280069e83b1c8dc0e1c7/common.c#L208 # Some devices may have a slash in their name (e.g. cciss/c0d0...). name = name.replace('/', '!') including_virtual = True if including_virtual: path = "/sys/block/%s" % name else: path = "/sys/block/%s/device" % name return os.access(path, os.F_OK)
Example 15
Project: teleport Author: tp4a File: _pslinux.py License: Apache License 2.0 | 6 votes |
def is_storage_device(name): """Return True if the given name refers to a root device (e.g. "sda", "nvme0n1") as opposed to a logical partition (e.g. "sda1", "nvme0n1p1"). If name is a virtual device (e.g. "loop1", "ram") return True. """ # Readapted from iostat source code, see: # https://github.com/sysstat/sysstat/blob/ # 97912938cd476645b267280069e83b1c8dc0e1c7/common.c#L208 # Some devices may have a slash in their name (e.g. cciss/c0d0...). name = name.replace('/', '!') including_virtual = True if including_virtual: path = "/sys/block/%s" % name else: path = "/sys/block/%s/device" % name return os.access(path, os.F_OK)
Example 16
Project: QCElemental Author: MolSSI File: importing.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def which( command: str, *, return_bool: bool = False, raise_error: bool = False, raise_msg: str = None, env: str = None ) -> Union[bool, None, str]: """Test to see if a command is available. Returns ------- str or None By default, returns command path if command found or `None` if not. Environment is $PATH or `os.pathsep`-separated `env`, less any None values. bool When `return_bool=True`, returns whether or not found. Raises ------ ModuleNotFoundError When `raises_error=True` and command not found. Raises generic message plus any `raise_msg`. """ if env is None: lenv = {"PATH": os.pathsep + os.environ.get("PATH", "") + os.path.dirname(sys.executable)} else: lenv = {"PATH": os.pathsep.join([os.path.abspath(x) for x in env.split(os.pathsep) if x != ""])} lenv = {k: v for k, v in lenv.items() if v is not None} ans = shutil.which(command, mode=os.F_OK | os.X_OK, path=lenv["PATH"]) if raise_error and ans is None: raise ModuleNotFoundError( f"Command '{command}' not found in envvar PATH.{' ' + raise_msg if raise_msg else ''}" ) if return_bool: return bool(ans) else: return ans
Example 17
Project: iAI Author: aimuch File: ez_setup.py License: MIT License | 5 votes |
def _clean_check(cmd, target): """ Run the command to download target. If the command fails, clean up before re-raising the error. """ try: subprocess.check_call(cmd) except subprocess.CalledProcessError: if os.access(target, os.F_OK): os.unlink(target) raise
Example 18
Project: astroalign Author: toros-astro File: ez_setup.py License: MIT License | 5 votes |
def _clean_check(cmd, target): """ Run the command to download target. If the command fails, clean up before re-raising the error. """ try: subprocess.check_call(cmd) except subprocess.CalledProcessError: if os.access(target, os.F_OK): os.unlink(target) raise
Example 19
Project: bugbuzz-python Author: fangpenlin File: ez_setup.py License: MIT License | 5 votes |
def _clean_check(cmd, target): """ Run the command to download target. If the command fails, clean up before re-raising the error. """ try: subprocess.check_call(cmd) except subprocess.CalledProcessError: if os.access(target, os.F_OK): os.unlink(target) raise
Example 20
Project: feets Author: quatrope File: ez_setup.py License: MIT License | 5 votes |
def _clean_check(cmd, target): """ Run the command to download target. If the command fails, clean up before re-raising the error. """ try: subprocess.check_call(cmd) except subprocess.CalledProcessError: if os.access(target, os.F_OK): os.unlink(target) raise
Example 21
Project: CapsLayer Author: naturomics File: reader.py License: Apache License 2.0 | 5 votes |
def __init__(self, path=None, num_works=1, splitting="TVT", one_hot=False, name="create_inputs"): """ Args: path: Path to store data. name: Name for the operations. """ # path exists and is writable? if path is None: path = os.path.join(os.environ["HOME"], ".cache", "capslayer", "datasets", "fashion_mnist") os.makedirs(path, exist_ok=True) elif os.access(path, os.F_OK): path = path if os.path.basename(path) == "fashion_mnist" else os.path.join(path, "fashion_mnist") os.makedirs(path, exist_ok=True) elif os.access(path, os.W_OK): raise IOError("Permission denied! Path %s is not writable." % (str(path))) # data downloaded and data extracted? maybe_download_and_extract("fashion-mnist", path) # data tfrecorded? tfrecord_runner(path, force=False) self.handle = tf.placeholder(tf.string, shape=[]) self.next_element = None self.path = path self.name = name
Example 22
Project: CapsLayer Author: naturomics File: reader.py License: Apache License 2.0 | 5 votes |
def __init__(self, path=None, num_works=1, splitting="TVT", one_hot=False, name="create_inputs"): """ Args: path: Path to store data. name: Name for the operations. """ # path exists and is writable? if path is None: path = os.path.join(os.environ["HOME"], ".cache", "capslayer", "datasets", "mnist") os.makedirs(path, exist_ok=True) elif os.access(path, os.F_OK): path = path if os.path.basename(path) == "mnist" else os.path.join(path, "mnist") os.makedirs(path, exist_ok=True) elif os.access(path, os.W_OK): raise IOError("Permission denied! Path %s is not writable." % (str(path))) # data downloaded and data extracted? maybe_download_and_extract("mnist", path) # data tfrecorded? tfrecord_runner(path, force=False) self.handle = tf.placeholder(tf.string, shape=[]) self.next_element = None self.path = path self.name = name
Example 23
Project: browserscope Author: elsigh File: blob_upload_test.py License: Apache License 2.0 | 5 votes |
def setUp(self): """Configure test harness.""" # Configure os.environ to make it look like the relevant parts of the # CGI environment that the stub relies on. self.original_environ = dict(os.environ) os.environ.update({ 'APPLICATION_ID': 'app', 'SERVER_NAME': 'localhost', 'SERVER_PORT': '8080', 'AUTH_DOMAIN': 'abcxyz.com', 'USER_EMAIL': 'user@abcxyz.com', }) # Set up mox. self.mox = mox.Mox() # Use a fresh file datastore stub. self.tmpdir = tempfile.mkdtemp() self.datastore_file = os.path.join(self.tmpdir, 'datastore_v3') self.history_file = os.path.join(self.tmpdir, 'history') for filename in [self.datastore_file, self.history_file]: if os.access(filename, os.F_OK): os.remove(filename) self.stub = datastore_file_stub.DatastoreFileStub( 'app', self.datastore_file, self.history_file, use_atexit=False) self.apiproxy = apiproxy_stub_map.APIProxyStubMap() apiproxy_stub_map.apiproxy = self.apiproxy apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', self.stub)
Example 24
Project: browserscope Author: elsigh File: blob_download_test.py License: Apache License 2.0 | 5 votes |
def setUp(self): """Configure test harness.""" # Configure os.environ to make it look like the relevant parts of the # CGI environment that the stub relies on. self.original_environ = dict(os.environ) os.environ.update({ 'APPLICATION_ID': 'app', 'SERVER_NAME': 'localhost', 'SERVER_PORT': '8080', 'AUTH_DOMAIN': 'abcxyz.com', 'USER_EMAIL': 'user@abcxyz.com', }) # Set up testing blobstore files. self.tmpdir = tempfile.mkdtemp() storage_directory = os.path.join(self.tmpdir, 'blob_storage') self.blob_storage = file_blob_storage.FileBlobStorage(storage_directory, 'appid1') self.blobstore_stub = blobstore_stub.BlobstoreServiceStub(self.blob_storage) # Use a fresh file datastore stub. self.datastore_file = os.path.join(self.tmpdir, 'datastore_v3') self.history_file = os.path.join(self.tmpdir, 'history') for filename in [self.datastore_file, self.history_file]: if os.access(filename, os.F_OK): os.remove(filename) self.datastore_stub = datastore_file_stub.DatastoreFileStub( 'app', self.datastore_file, self.history_file, use_atexit=False) self.apiproxy = apiproxy_stub_map.APIProxyStubMap() apiproxy_stub_map.apiproxy = self.apiproxy apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', self.datastore_stub) apiproxy_stub_map.apiproxy.RegisterStub('blobstore', self.blobstore_stub)
Example 25
Project: flyover Author: jeremybmerrill File: ez_setup.py License: MIT License | 5 votes |
def _clean_check(cmd, target): """ Run the command to download target. If the command fails, clean up before re-raising the error. """ try: subprocess.check_call(cmd) except subprocess.CalledProcessError: if os.access(target, os.F_OK): os.unlink(target) raise
Example 26
Project: jbox Author: jpush File: command.py License: MIT License | 5 votes |
def init(config, directory, template='generic'): """Initialize a new scripts directory.""" if os.access(directory, os.F_OK): raise util.CommandError("Directory %s already exists" % directory) template_dir = os.path.join(config.get_template_directory(), template) if not os.access(template_dir, os.F_OK): raise util.CommandError("No such template %r" % template) util.status("Creating directory %s" % os.path.abspath(directory), os.makedirs, directory) versions = os.path.join(directory, 'versions') util.status("Creating directory %s" % os.path.abspath(versions), os.makedirs, versions) script = ScriptDirectory(directory) for file_ in os.listdir(template_dir): file_path = os.path.join(template_dir, file_) if file_ == 'alembic.ini.mako': config_file = os.path.abspath(config.config_file_name) if os.access(config_file, os.F_OK): util.msg("File %s already exists, skipping" % config_file) else: script._generate_template( file_path, config_file, script_location=directory ) elif os.path.isfile(file_path): output_file = os.path.join(directory, file_) script._copy_file( file_path, output_file ) util.msg("Please edit configuration/connection/logging " "settings in %r before proceeding." % config_file)
Example 27
Project: jbox Author: jpush File: env.py License: MIT License | 5 votes |
def env_file_fixture(txt): dir_ = os.path.join(_get_staging_directory(), 'scripts') txt = """ from alembic import context config = context.config """ + txt path = os.path.join(dir_, "env.py") pyc_path = util.pyc_file_from_path(path) if os.access(pyc_path, os.F_OK): os.unlink(pyc_path) with open(path, 'w') as f: f.write(txt)
Example 28
Project: jbox Author: jpush File: env.py License: MIT License | 5 votes |
def _testing_config(): from alembic.config import Config if not os.access(_get_staging_directory(), os.F_OK): os.mkdir(_get_staging_directory()) return Config(os.path.join(_get_staging_directory(), 'test_alembic.ini'))
Example 29
Project: Adafruit_Python_BMP Author: adafruit File: ez_setup.py License: MIT License | 5 votes |
def _clean_check(cmd, target): """ Run the command to download target. If the command fails, clean up before re-raising the error. """ try: subprocess.check_call(cmd) except subprocess.CalledProcessError: if os.access(target, os.F_OK): os.unlink(target) raise
Example 30
Project: ChromaTerm Author: hSaria File: test_default_config.py License: MIT License | 5 votes |
def test_write_default_config(): """Write config file.""" name = __name__ + '1' assert chromaterm.default_config.write_default_config('.', name) assert os.access(os.path.join('.', name), os.F_OK) os.remove(__name__ + '1')