Python os.fchdir() Examples

The following are code examples for showing how to use os.fchdir(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: pkgscripts-ng   Author: SynologyOpenSource   File: chroot.py    (license) View Source Project 5 votes vote down vote up
def __enter__(self):
        self.mount()
        os.chroot(self.chroot)
        os.fchdir(self.chroot_fd)
        return self 
Example 2
Project: pkgscripts-ng   Author: SynologyOpenSource   File: chroot.py    (license) View Source Project 5 votes vote down vote up
def __exit__(self, exc_type, exc_val, exc_tb):
        os.fchdir(self.orig_fd)
        os.chroot(".")
        os.close(self.orig_fd)
        os.close(self.chroot_fd)
        self.umount() 
Example 3
Project: Coffer   Author: Max00355   File: templateUtils.py    (license) View Source Project 5 votes vote down vote up
def executeCommand(command):
    cwd = os.getcwd()
    rr = os.open("/", os.O_RDONLY)
    os.chroot(getRootDir.getEnvsDir() + getEnvName())
    os.chdir("/")
    os.system(command)
    os.fchdir(rr)
    os.chroot(".")
    os.chdir(cwd) 
Example 4
Project: integration   Author: mendersoftware   File: release_tool.py    (Apache License 2.0) View Source Project 4 votes vote down vote up
def execute_git(state, repo_git, args, capture=False, capture_stderr=False):
    """Executes a Git command in the given repository, with args being a list
    of arguments (not including git itself). capture and capture_stderr
    arguments causes it to return stdout or stdout+stderr as a string.

    state can be None, but if so, then repo_git needs to be an absolute path.

    The function automatically takes into account Git commands with side effects
    and applies push simulation and dry run if those are enabled."""

    is_push = (args[0] == "push")
    is_change = (is_push
                 or (args[0] == "tag" and len(args) > 1)
                 or (args[0] == "branch" and len(args) > 1)
                 or (args[0] == "config" and args[1] != "-l")
                 or (args[0] == "checkout")
                 or (args[0] == "commit")
                 or (args[0] == "fetch")
                 or (args[0] == "init")
                 or (args[0] == "reset"))

    if os.path.isabs(repo_git):
        git_dir = repo_git
    else:
        git_dir = os.path.join(state['repo_dir'], repo_git)

    if (not PUSH and is_push) or (DRY_RUN and is_change):
        print("Would have executed: cd %s && git %s"
              % (git_dir, " ".join(args)))
        return None

    fd = os.open(".", flags=os.O_RDONLY)
    os.chdir(git_dir)
    if capture_stderr:
        stderr = subprocess.STDOUT
    else:
        stderr = None

    try:
        if capture:
            output = subprocess.check_output(["git"] + args, stderr=stderr).decode().strip()
        else:
            output = None
            subprocess.check_call(["git"] + args, stderr=stderr)
    finally:
        os.fchdir(fd)
        os.close(fd)

    return output