コード例 #1
0
def create_case_as_link(cases, tmpdir, case_name) -> Tuple[str, str]:
    target_original = cases.get_path(case_name)
    import os

    target_link = str(tmpdir.join(case_name))
    os.symlink(target_original, target_link, target_is_directory=True)
    return target_original, target_link
コード例 #2
0
ファイル: __init__.py プロジェクト: TREE-Ind/wake-word-skill
 def download_sounds(self):
     if self.settings["soundbackup"] is True:
         import py7zr
         name = self.settings["name"]
         if not os.path.isfile(self.file_system.path + "/nonesounds.7z"):
             self.log.info("downloading soundbackup")
             wget.download(
                 'http://downloads.tuxfamily.org/pdsounds/pdsounds_march2009.7z',
                 self.file_system.path + "/nonesounds.7z")
         if not os.path.isdir(self.settings["file_path"] + "/" + name +
                              "/not-wake-word/noises"):
             if not os.path.isdir(self.file_system.path + "/noises"):
                 os.makedirs(self.file_system.path + "/noises")
             if not os.path.isdir(self.file_system.path + "/noises/mp3"):
                 self.log.info("unzip soundbackup")
                 py7zr.unpack_7zarchive(
                     self.file_system.path + "/nonesounds.7z",
                     self.file_system.path + "/noises")
                 self.log.info("download sucess, start convert")
             if not os.path.isdir(self.file_system.path + "/noises/noises"):
                 for root, dirs, files in os.walk(self.file_system.path +
                                                  "/noises/mp3/"):
                     for f in files:
                         filename = os.path.join(root, f)
                         if filename.endswith('.mp3'):
                             self.log.info("Filename: " + filename)
                             if not os.path.isdir(self.file_system.path +
                                                  "/noises/noises"):
                                 os.makedirs(self.file_system.path +
                                             "/noises/noises")
                             self.soundbackup_convert = subprocess.Popen(
                                 [
                                     "ffmpeg -i " + filename +
                                     " -acodec pcm_s16le -ar 16000 -ac 1 -f wav "
                                     + self.file_system.path +
                                     "/noises/noises/noises-" +
                                     str(uuid.uuid1()) + ".wav"
                                 ],
                                 preexec_fn=os.setsid,
                                 shell=True)
                             self.log.info("extratct: " + filename)
                 self.log.info("Make Filelink")
             if not os.path.isdir(self.settings["file_path"] + "/" + name +
                                  "/not-wake-word"):
                 os.makedirs(self.settings["file_path"] + "/" + name +
                             "/not-wake-word")
             os.symlink(
                 self.file_system.path + "/noises/noises/",
                 self.settings["file_path"] + "/" + name +
                 "/not-wake-word/noises")
     else:
         return True
コード例 #3
0
 def download_sounds(self):
     if self.settings["soundbackup"] is True:
         import py7zr
         name = self.settings["Name"]
         if not os.path.isfile(self.file_system.path+"/nonesounds.7z"):
             free_mb = psutil.disk_usage('/')[2] / 1024 / 1024
             if free_mb <= 1500:
                 self.settings["soundbackup"] = False
                 self.log.info("no space: Sound Download not possible")
                 return
             else:
                 self.log.info("downloading soundbackup")
                 wget.download('http://downloads.tuxfamily.org/pdsounds/pdsounds_march2009.7z', self.file_system.path+"/nonesounds.7z")
         #onlyfiles = next(os.walk(self.settings["file_path"]+name+"/not-wake-word/noises"))[2]
         #if len(onlyfiles) <= 30:
             if not os.path.isdir(self.file_system.path+"/noises"):
                 os.makedirs(self.file_system.path+"/noises")
             if not os.path.isdir(self.file_system.path+"/noises/mp3"):
                 self.log.info("unzip soundbackup")
                 py7zr.unpack_7zarchive(self.file_system.path+"/nonesounds.7z", self.file_system.path+"/noises")
                 self.log.info("download sucess, start convert")
             onlyfiles = next(os.walk(self.file_system.path+"/noises/noises"))[2]
             if len(onlyfiles) <= 30:
                 folder = self.file_system.path+"/noises/mp3/"
                 fileformat = '.mp3'
                 i = 1
                 while i <= 2:
                     for root, dirs, files in os.walk(folder):
                         for f in files:
                             filename = os.path.join(root, f)
                             if filename.endswith(fileformat):
                                 self.log.info("Filename: "+filename)
                                 soundfile = filename.replace(fileformat, '').replace(folder, '')
                                 if not os.path.isdir(self.file_system.path+"/noises/noises"):
                                     os.makedirs(self.file_system.path+"/noises/noises")
                                 subprocess.call(["ffmpeg -i "+filename+" -acodec pcm_s16le -ar 16000 -ac 1 -f wav "+
                                                 self.file_system.path+"/noises/noises/"+soundfile+".wav"],
                                                 preexec_fn=os.setsid, shell=True)
                                 self.log.info("extratct: "+filename)
                     folder = self.file_system.path+"/noises/otherformats/"
                     fileformat = '.flac'
                     i = i + 1
                 self.speak_dialog("download.success")
             if not os.path.isdir(self.settings["file_path"]+name+"/not-wake-word"):
                 os.makedirs(self.settings["file_path"]+"/"+name+"/not-wake-word")
         if not os.path.isdir(self.settings["file_path"]+name+"/not-wake-word/noises"):
             self.log.info("Make Filelink")
             os.symlink(self.file_system.path+"/noises/noises/", self.settings["file_path"]+name+"/not-wake-word/noises")
     else:
         return True
コード例 #4
0
ファイル: util_links.py プロジェクト: Silicontx-wang-yu/ubelt
def symlink(real_path, link_path, overwrite=False, verbose=0):
    """
    Create a symbolic link.

    This will work on linux or windows, however windows does have some corner
    cases. For more details see notes in :mod:`ubelt._win32_links`.

    Args:
        path (PathLike): path to real file or directory

        link_path (PathLike): path to desired location for symlink

        overwrite (bool, default=False): overwrite existing symlinks.
            This will not overwrite real files on systems with proper symlinks.
            However, on older versions of windows junctions are
            indistinguishable from real files, so we cannot make this
            guarantee.

        verbose (int, default=0): verbosity level

    Returns:
        PathLike: link path

    Example:
        >>> import ubelt as ub
        >>> dpath = ub.ensure_app_cache_dir('ubelt', 'test_symlink0')
        >>> real_path = join(dpath, 'real_file.txt')
        >>> link_path = join(dpath, 'link_file.txt')
        >>> [ub.delete(p) for p in [real_path, link_path]]
        >>> ub.writeto(real_path, 'foo')
        >>> result = symlink(real_path, link_path)
        >>> assert ub.readfrom(result) == 'foo'
        >>> [ub.delete(p) for p in [real_path, link_path]]

    Example:
        >>> import ubelt as ub
        >>> from os.path import dirname
        >>> dpath = ub.ensure_app_cache_dir('ubelt', 'test_symlink1')
        >>> ub.delete(dpath)
        >>> ub.ensuredir(dpath)
        >>> _dirstats(dpath)
        >>> real_dpath = ub.ensuredir((dpath, 'real_dpath'))
        >>> link_dpath = ub.augpath(real_dpath, base='link_dpath')
        >>> real_path = join(dpath, 'afile.txt')
        >>> link_path = join(dpath, 'afile.txt')
        >>> [ub.delete(p) for p in [real_path, link_path]]
        >>> ub.writeto(real_path, 'foo')
        >>> result = symlink(real_dpath, link_dpath)
        >>> assert ub.readfrom(link_path) == 'foo', 'read should be same'
        >>> ub.writeto(link_path, 'bar')
        >>> _dirstats(dpath)
        >>> assert ub.readfrom(link_path) == 'bar', 'very bad bar'
        >>> assert ub.readfrom(real_path) == 'bar', 'changing link did not change real'
        >>> ub.writeto(real_path, 'baz')
        >>> _dirstats(dpath)
        >>> assert ub.readfrom(real_path) == 'baz', 'very bad baz'
        >>> assert ub.readfrom(link_path) == 'baz', 'changing real did not change link'
        >>> ub.delete(link_dpath, verbose=1)
        >>> _dirstats(dpath)
        >>> assert not exists(link_dpath), 'link should not exist'
        >>> assert exists(real_path), 'real path should exist'
        >>> _dirstats(dpath)
        >>> ub.delete(dpath, verbose=1)
        >>> _dirstats(dpath)
        >>> assert not exists(real_path)
    """
    path = normpath(real_path)
    link = normpath(link_path)

    if not os.path.isabs(path):
        # if path is not absolute it must be specified relative to link
        if _can_symlink():
            path = os.path.relpath(path, os.path.dirname(link))
        else:  # nocover
            # On windows, we need to use absolute paths
            path = os.path.abspath(path)

    if verbose:
        print('Symlink: {path} -> {link}'.format(path=path, link=link))
    if islink(link):
        if verbose:
            print('... already exists')
        pointed = _readlink(link)
        if pointed == path:
            if verbose > 1:
                print('... and points to the right place')
            return link
        if verbose > 1:
            if not exists(link):
                print('... but it is broken and points somewhere else: {}'.
                      format(pointed))
            else:
                print('... but it points somewhere else: {}'.format(pointed))
        if overwrite:
            util_io.delete(link, verbose=verbose > 1)
    elif exists(link):
        if _win32_links is None:
            if verbose:
                print('... already exists, but its a file. This will error.')
            raise FileExistsError(
                'cannot overwrite a physical path: "{}"'.format(path))
        else:  # nocover
            if verbose:
                print('... already exists, and is either a file or hard link. '
                      'Assuming it is a hard link. '
                      'On non-win32 systems this would error.')

    if _win32_links is None:
        os.symlink(path, link)
    else:  # nocover
        _win32_links._symlink(path, link, overwrite=overwrite, verbose=verbose)

    return link
コード例 #5
0
def symlink(real_path, link_path, overwrite=False, verbose=0):
    """
    Create a symbolic link.

    This will work on linux or windows, however windows does have some corner
    cases. For more details see notes in `ubelt._win32_links`.

    Args:
        path (str): path to real file or directory
        link_path (str): path to desired location for symlink
        overwrite (bool): overwrite existing symlinks.
            This will not overwrite real files on systems with proper symlinks.
            However, on older versions of windows junctions are
            indistinguishable from real files, so we cannot make this
            guarantee.  (default = False)
        verbose (int):  verbosity level (default=0)

    Returns:
        str: link path

    CommandLine:
        python -m ubelt.util_links symlink:0

    Example:
        >>> import ubelt as ub
        >>> dpath = ub.ensure_app_cache_dir('ubelt', 'test_symlink0')
        >>> real_path = join(dpath, 'real_file.txt')
        >>> link_path = join(dpath, 'link_file.txt')
        >>> [ub.delete(p) for p in [real_path, link_path]]
        >>> ub.writeto(real_path, 'foo')
        >>> result = symlink(real_path, link_path)
        >>> assert ub.readfrom(result) == 'foo'
        >>> [ub.delete(p) for p in [real_path, link_path]]

    Example:
        >>> import ubelt as ub
        >>> from os.path import dirname
        >>> dpath = ub.ensure_app_cache_dir('ubelt', 'test_symlink1')
        >>> ub.delete(dpath)
        >>> ub.ensuredir(dpath)
        >>> _dirstats(dpath)
        >>> real_dpath = ub.ensuredir((dpath, 'real_dpath'))
        >>> link_dpath = ub.augpath(real_dpath, base='link_dpath')
        >>> real_path = join(dpath, 'afile.txt')
        >>> link_path = join(dpath, 'afile.txt')
        >>> [ub.delete(p) for p in [real_path, link_path]]
        >>> ub.writeto(real_path, 'foo')
        >>> result = symlink(real_dpath, link_dpath)
        >>> assert ub.readfrom(link_path) == 'foo', 'read should be same'
        >>> ub.writeto(link_path, 'bar')
        >>> _dirstats(dpath)
        >>> assert ub.readfrom(link_path) == 'bar', 'very bad bar'
        >>> assert ub.readfrom(real_path) == 'bar', 'changing link did not change real'
        >>> ub.writeto(real_path, 'baz')
        >>> _dirstats(dpath)
        >>> assert ub.readfrom(real_path) == 'baz', 'very bad baz'
        >>> assert ub.readfrom(link_path) == 'baz', 'changing real did not change link'
        >>> ub.delete(link_dpath, verbose=1)
        >>> _dirstats(dpath)
        >>> assert not exists(link_dpath), 'link should not exist'
        >>> assert exists(real_path), 'real path should exist'
        >>> _dirstats(dpath)
        >>> ub.delete(dpath, verbose=1)
        >>> _dirstats(dpath)
        >>> assert not exists(real_path)
    """
    path = normpath(real_path)
    link = normpath(link_path)

    if not os.path.isabs(path):
        # if path is not absolute it must be specified relative to link
        if _can_symlink():
            path = os.path.relpath(path, os.path.dirname(link))
        else:  # nocover
            # On windows, we need to use absolute paths
            path = os.path.abspath(path)

    if verbose:
        print('Creating symlink: path={} link={}'.format(path, link))
    if islink(link):
        if verbose:
            print('symlink already exists')
        if _readlink(link) == path:
            if verbose > 1:
                print('... and points to the right place')
            return link
        if verbose > 1:
            if not exists(link):
                print('... but it is broken and points somewhere else')
            else:
                print('... but it points somewhere else')
        if overwrite:
            util_io.delete(link, verbose > 1)

    if _win32_links:  # nocover
        _win32_links._symlink(path, link, overwrite=overwrite, verbose=verbose)
    else:
        os.symlink(path, link)

    return link