Beispiel #1
0
 def _replace_file(src, dst):
     # True atomic replace, if supported
     try:
         win32api.MoveFileEx(src, dst, win32con.MOVEFILE_REPLACE_EXISTING)
     except:
         # Sometimes it fails - we play stupid and try again...
         time.sleep(0.5)
         win32api.MoveFileEx(src, dst, win32con.MOVEFILE_REPLACE_EXISTING)
Beispiel #2
0
def create_script(filename, templatename, mode=_EXEC_MODE, **kwargs):
    """This Creates a file from a JINJA template.

    The templates exist in our lib/python/treadmill/templates directory.

    :param ``str`` filename:
        Name of the file to generate.
    :param ``str`` templatename:
        The name of the template file.
    :param ``int`` mode:
        The mode for the file (Defaults to +x).
    :param ``dict`` kwargs:
        key/value passed into the template.
    """
    filepath = os.path.dirname(filename)
    with tempfile.NamedTemporaryFile(dir=filepath,
                                     delete=False,
                                     mode='w') as f:
        for data in generate_template(templatename, **kwargs):
            f.write(data)
        if os.name == 'posix':
            # cast to int required in order for default _EXEC_MODE to work
            os.fchmod(f.fileno(), int(mode))
    if sys.version_info[0] < 3:
        # TODO: os.rename cannot replace on windows
        # (use os.replace in python 3.4)
        # copied from fs as utils cannot have this dependency
        if os.name == 'nt':
            win32api.MoveFileEx(f.name, filename,
                                win32con.MOVEFILE_REPLACE_EXISTING)
        else:
            os.rename(f.name, filename)
    else:
        os.replace(f.name, filename)
Beispiel #3
0
def replace(path_from, path_to):
    """Temp replace/rename for windows."""
    if sys.version_info[0] < 3:
        # TODO: os.rename cannot replace on windows
        # (use os.replace in python 3.4)
        if os.name == 'nt':
            win32api.MoveFileEx(path_from, path_to,
                                win32con.MOVEFILE_REPLACE_EXISTING)
        else:
            os.rename(path_from, path_to)
    else:
        os.replace(path_from, path_to)
Beispiel #4
0
def delete_locked_file(pathname):
    """Delete a file that is currently in use"""
    if os.path.exists(pathname):
        try:
            win32api.MoveFileEx(pathname, None,
                                win32con.MOVEFILE_DELAY_UNTIL_REBOOT)
        except pywintypes.error, e:
            if not 5 == e.winerror:
                raise e
            if shell.IsUserAnAdmin():
                logger = logging.getLogger(__name__)
                logger.warning(
                    'Unable to queue locked file for deletion, even with administrator rights: %s'
                    % pathname)
                return
            # show more useful message than "error: (5, 'MoveFileEx', 'Access
            # is denied.')"
            raise RuntimeError(
                'Access denied when attempting to delete locked file without administrator rights: %s'
                % pathname)
Beispiel #5
0
    def process5(self):
        answer5 = 5

        while answer5 != '10':
            print('1 - To copy file\n'
                  '2 - To move file\n'
                  '3 - To advanced move file\n'
                  '10 - To exit to menu')

            answer5 = input()

            if answer5 == '1':
                print("Enter the name of file to copy and the name of new file\n")
                win32api.CopyFile(input(), input(), 0)

            elif answer5 == '2':
                print("Enter the name of file and the directory to move\n")
                win32api.MoveFile(input(), input())
                # win32file.MoveFileWithProgress(input(), input())
            elif answer5 == '3':
                print("Enter the name of file and the directory to move\n")
                win32api.MoveFileEx(input(),
                                    input(),
                                    11)
Beispiel #6
0
 def _replace(s, d):
     win32api.MoveFileEx(
         s,
         d,
         win32con.MOVEFILE_REPLACE_EXISTING,
     )
Beispiel #7
0
 def replace(src, dst):
     win32api.MoveFileEx(src, dst, win32con.MOVEFILE_REPLACE_EXISTING)
Beispiel #8
0
 def replace(src, dst):
     """Atomic move `src` to `dst`"""
     win32api.MoveFileEx(src, dst, win32con.MOVEFILE_REPLACE_EXISTING)
Beispiel #9
0
def delete_locked_file(pathname):
    """Delete a file that is currently in use"""
    if os.path.exists(pathname):
        win32api.MoveFileEx(
            pathname, None, win32con.MOVEFILE_DELAY_UNTIL_REBOOT)