Example #1
0
 def open_selection(self, context, results, selection, source):
     text = selection.get_text()
     got_results = False
     
     # Checks whether the text looks like a proper URI
     # .netloc would be "python.org" for http://python.org/, "cakes" for
     # file://cakes/cake.bmp but "" for file://cake.bmp, so it needs to
     # have either that or a "file" scheme.
     parse_result = urlparse(text)
     if (parse_result.scheme and parse_result.path
             and (parse_result.netloc or parse_result.scheme == "file")):
         results.sources.append(URISource(text, parent=source))
         got_results = True
         file_uri = parse_result.scheme == "file"
     else:
         # If not we assume it's an absolute filepath
         # Expanding "~"
         text = os_path.expanduser(text)
         if os_path.isabs(text):
             # Wildly assuming this is a valid filename
             # just because it starts with a slash
             text = "file://" + os_path.normcase(os_path.normcase(text))
             results.sources.append(URISource(text, parent=source))
             got_results = file_uri = True
     
     # Setting source name
     if got_results:
         if file_uri:
             source.setFileContentName()
         else:
             source.setURIContentName()
     
     results.complete()
Example #2
0
    def test_relocated_distribution(self):
        root = mkdtemp(self)
        dummyns_path = join(root, 'dummyns')

        make_dummy_dist(self, ((
            'namespace_packages.txt',
            'dummyns\n',
        ), (
            'entry_points.txt',
            '[dummyns]\n'
            'dummyns = dummyns:attr\n',
        ),), 'dummyns', '1.0', working_dir=root)
        working_set = pkg_resources.WorkingSet([
            root,
            self.ds_egg_root,
        ])
        # activate this as the working set
        stub_item_attr_value(self, pkg_resources, 'working_set', working_set)
        dummyns_ep = next(working_set.iter_entry_points('dummyns'))
        with pretty_logging(stream=StringIO()) as fd:
            p = indexer.resource_filename_mod_entry_point(
                'dummyns', dummyns_ep)
        # since the actual location is not created)
        self.assertIsNone(p)
        self.assertIn("does not exist", fd.getvalue())

        # retry with the module directory created at the expected location
        os.mkdir(dummyns_path)
        with pretty_logging(stream=StringIO()) as fd:
            p = indexer.resource_filename_mod_entry_point(
                'dummyns', dummyns_ep)
        self.assertEqual(normcase(p), normcase(dummyns_path))
        self.assertEqual('', fd.getvalue())
Example #3
0
def test_setup_open_files(setup_editor, last_focused_filename,
                          expected_current_filename, python_files):
    """Test Editor plugin open files setup.

    Test that the file order is preserved during the Editor plugin setup and
    that the current file correspond to the last focused file.
    """
    editor, qtbot = setup_editor
    expected_filenames, tmpdir = python_files
    expected_current_filename = osp.join(tmpdir, expected_current_filename)

    def get_option(option, default=None):
        splitsettings = [(False,
                          osp.join(tmpdir, last_focused_filename),
                          [1, 1, 1, 1])]
        return {'layout_settings': {'splitsettings': splitsettings},
                'filenames': expected_filenames,
                'max_recent_files': 20
                }.get(option)
    editor.get_option = get_option

    editor.setup_open_files()
    current_filename = editor.get_current_editorstack().get_current_filename()
    current_filename = osp.normcase(current_filename)
    assert current_filename == expected_current_filename
    filenames = editor.get_current_editorstack().get_filenames()
    filenames = [osp.normcase(f) for f in filenames]
    assert filenames == expected_filenames
Example #4
0
def parse(root, describe_command=DEFAULT_DESCRIBE):
    real_root, _, ret = do_ex('git rev-parse --show-toplevel', root)
    if ret:
        return
    trace('real root', real_root)
    if (normcase(abspath(realpath(real_root))) !=
            normcase(abspath(realpath(root)))):
        return
    rev_node, _, ret = do_ex('git rev-parse --verify --quiet HEAD', root)
    if ret:
        return meta('0.0')
    rev_node = rev_node[:7]
    out, err, ret = do_ex(describe_command, root)
    if '-' not in out and '.' not in out:
        revs = do('git rev-list HEAD', root)
        count = revs.count('\n')
        if ret:
            out = rev_node
        return meta('0.0', distance=count + 1, node=out)
    if ret:
        return
    dirty = out.endswith('-dirty')
    if dirty:
        out = out.rsplit('-', 1)[0]

    tag, number, node = out.rsplit('-', 2)
    number = int(number)
    if number:
        return meta(tag, distance=number, node=node, dirty=dirty)
    else:
        return meta(tag, dirty=dirty, node=node)
Example #5
0
    def test_uninstall_from_usersite_with_dist_in_global_site(
            self, virtualenv, script):
        """
        Test uninstall from usersite (with same dist in global site)
        """
        _patch_dist_in_site_packages(virtualenv)

        script.pip_install_local('pip-test-package==0.1', '--no-binary=:all:')

        result2 = script.pip_install_local(
            '--user', 'pip-test-package==0.1.1', '--no-binary=:all:')
        result3 = script.pip('uninstall', '-vy', 'pip-test-package')

        # uninstall console is mentioning user scripts, but not global scripts
        assert normcase(script.user_bin_path) in result3.stdout, str(result3)
        assert normcase(script.bin_path) not in result3.stdout, str(result3)

        # uninstall worked
        assert_all_changes(result2, result3, [script.venv / 'build', 'cache'])

        # site still has 0.2 (can't look in result1; have to check)
        egg_info_folder = (
            script.base_path / script.site_packages /
            'pip_test_package-0.1-py%s.egg-info' % pyversion
        )
        assert isdir(egg_info_folder)
Example #6
0
    def test_standard(self):
        d_egg_root = join(mkdtemp(self), 'dummyns')

        make_dummy_dist(self, ((
            'namespace_packages.txt',
            'dummyns\n',
        ), (
            'entry_points.txt',
            '[dummyns]\n'
            'dummyns = dummyns:attr\n',
        ),), 'dummyns', '1.0', working_dir=d_egg_root)
        working_set = pkg_resources.WorkingSet([
            d_egg_root,
            self.ds_egg_root,
        ])
        # ensure the working_set is providing the distributions being
        # mocked here so that resource_filename will resolve correctly
        stub_item_attr_value(self, pkg_resources, 'working_set', working_set)

        moddir = join(d_egg_root, 'dummyns')
        os.makedirs(moddir)

        # make this also a proper thing
        with open(join(moddir, '__init__.py'), 'w') as fd:
            fd.write('')

        dummyns_ep = next(working_set.iter_entry_points('dummyns'))
        p = indexer.resource_filename_mod_entry_point('dummyns', dummyns_ep)

        # finally, this should work.
        self.assertEqual(normcase(p), normcase(moddir))
Example #7
0
 def get_relative_path(self, filename):
    project_path = normpath(normcase(self.project_path))
    filename = normpath(normcase(filename))
    ind = filename.find(project_path + os.sep)
    if ind != -1:
        filename = filename[ind + len(project_path) + 1:]
    return filename
Example #8
0
 def loadHistoryFromDisk(self, currDownloadDir):
     path = self.DOWNLOADED_IMAGE_PATH_BASE
     printDBG('+++++++++++++++++++++++++++++++++++++++ IconMenager.loadHistoryFromDisk path[%s]' % path)
     iconsDirs = GetIconsDirs(path)
     for item in iconsDirs:
         if os_path.normcase(path + '/' + item + '/') != os_path.normcase(currDownloadDir + '/'):
             self.loadIconsFromPath(os_path.join(path, item))
Example #9
0
def symlink_conda(prefix, root_dir, shell=None):
    # do not symlink root env - this clobbers activate incorrectly.
    # prefix should always be longer than, or outside the root dir.
    if normcase(normpath(prefix)) in normcase(normpath(root_dir)):
        return

    if shell is None:
        shell = "bash.msys"

    if on_win:
        where = 'Scripts'
    else:
        where = 'bin'

    if on_win:
        if shell in ["cmd.exe", "powershell.exe"]:
            symlink_fn = functools.partial(win_conda_bat_redirect, shell=shell)
        else:
            symlink_fn = functools.partial(win_conda_unix_redirect, shell=shell)
    else:
        symlink_fn = os.symlink

    if not isdir(join(prefix, where)):
        os.makedirs(join(prefix, where))
    symlink_conda_hlp(prefix, root_dir, where, symlink_fn)
 def view_in_db(self, workspace, open_tab, index_db, extension):
     workspace = path.normcase(str(workspace))
     open_tab_normcase = path.normcase(str(open_tab))
     if open_tab_normcase.startswith(workspace):
         return self.is_rf_file(open_tab, extension, index_db)
     else:
         return False
Example #11
0
    def find_latest(self, **conditions):
        """Return the config for the latest registered build matching
        the given conditions. Otherwise it returns None.

        Valid conditions are any of the Mozilla-devel config variables.
        Typical ones are:
            buildType       release|debug
            komodoVersion   "X.Y"
            mozApp          komodo|xulrunner|suite|browser
            blessed         True|False (i.e. was this build blessed for
                            Komodo production builds)

        "Latest" sorting is determined by the 'changenum' config var.
        """
        sorted_configs = [v for k, v in 
                          sorted(self.configs.items(), reverse=True)]
        for config in sorted_configs:
            for attr, value in conditions.items():
                if value is None: continue
                try:
                    if sys.platform.startswith("win") and attr == "buildDir":
                        # on Windows, paths are not case sensitive...
                        if normcase(getattr(config, attr)) != normcase(value):
                            break
                    elif getattr(config, attr) != value:
                        break
                except AttributeError, ex:
                    # This attr is a likely a new configuration items
                    # since this mozbuild config was registered.
                    break
            else:
                return config
Example #12
0
    def __init__(self, filename, workspace=None, contents=''):
        """
        Initialize the FileProxy instance with the passed
        parameters. A FileProxy instance always starts at
        a fresh state with a negative version indicating
        that no updating operation has been performed on it.

        :param filename:
            The name of the file to create a FileProxy of.
            The filename is internally normcased.
        :param workspace:
            The workspace/project this file belongs to.
            Can be None.
        :param contents:
            The contents of the file to initialize the
            instance with. Integrity of the content or the
            sync state is never checked during initialization.
        """
        logging.debug('File proxy for {} created'.format(filename))

        # The file may not exist yet, hence there is no
        # reliable way of knowing if it is a file on the
        # disk or a directory.
        if not path.isabs(filename) or filename.endswith(path.sep):
            raise ValueError('expecting absolute filename')

        self._version = -1
        self._contents = contents
        self._filename = path.normcase(filename)
        self._workspace = workspace and path.normcase(workspace)
Example #13
0
    def test_result_exporter(self):
        polarity = -1
        exp_settings = ExperimentalSettings(10, polarity, is_dims_exp=False)

        peakels = PeakListReader(op.normcase("mzos/ressources/peaks_matrix_NEG.tsv"), exp_settings).get_peakels()

        # annotation
        peakels_annotator = PeakelsAnnotator(peakels, exp_settings)
        peakels_annotator.annotate()

        # database finding
        db_search = DatabaseSearch('hmdb', exp_settings)
        adducts_l = ['H1']
        db_search.assign_formula(peakels, adducts_l, exp_settings.mz_tol_ppm)

        # scoring
        # first simplistic
        model = StatsModel(peakels, exp_settings.mz_tol_ppm * 1.5)
        # populate annotations objects
        model.calculate_score()

        # scoring 2
        bi = BayesianInferer(peakels, exp_settings)
        # populate annotations object
        bi.infer_assignment_probabilities()

        exporter = ResultsExporter(op.normcase("annotations.tsv"), peakels)
        exporter.write()
Example #14
0
def refresh():
    root_location = get_root_location()
    conf_sites, db_sites = {}, {}
    REPOSITORY_SITES = conf.CONF.get('REPOSITORY_SITES')
    for site_name in REPOSITORY_SITES:
        hidden = REPOSITORY_SITES[site_name].get('hidden', False)
        if 'location' not in REPOSITORY_SITES[site_name]:   
            #not specifying location means using relative path
            site_real_path = normcase(normpath(join(root_location, site_name)))
            site_path = ""
            site_url = ""
        else:
            #specifying location means using absolute path
            site_real_path = normcase(normpath(REPOSITORY_SITES[site_name]['location']))
            site_path = site_real_path
            site_url = REPOSITORY_SITES[site_name]['update_site']
        #put site info in conf_sites
        conf_sites[site_name] = {'site_real_path':site_real_path, 
                                 'site_path': site_path,
                                 'site_url': site_url,
                                 'hidden': hidden}
    
    for site in Site.objects.all():
        db_sites[site.name] = site
        if site.name not in conf_sites:
            remove_site_from_db(site)   #delete the site in db which doesn't exist in file system
    for site_name in conf_sites:
        if site_name not in db_sites:
            create_site(site_name, conf_sites[site_name]['site_path'], conf_sites[site_name]['site_url'],
                        conf_sites[site_name]['hidden'], conf_sites[site_name]['site_real_path'])
        else:
            update_site(db_sites[site_name], hidden)
    update_composites()
def copyFile(sSrcFilePath, sDestPath, **kwargs):

    if osp.normcase(sSrcFilePath) == osp.normcase(sDestPath):
        raise ValueError, "Path of source and destination files are the same."

    logMsg(u"Copying '{}'\n    >> '{}'\n".format(sSrcFilePath, sDestPath))

    return file_util.copy_file(sSrcFilePath, sDestPath, **kwargs)
Example #16
0
 def test_getsourcefile(self):
     self.assertEqual(normcase(inspect.getsourcefile(mod.spam)), modfile)
     self.assertEqual(normcase(inspect.getsourcefile(git.abuse)), modfile)
     fn = "_non_existing_filename_used_for_sourcefile_test.py"
     co = compile("None", fn, "exec")
     self.assertEqual(inspect.getsourcefile(co), None)
     linecache.cache[co.co_filename] = (1, None, "None", co.co_filename)
     self.assertEqual(normcase(inspect.getsourcefile(co)), fn)
Example #17
0
def winpathitems():
    srchpath = environ.get('PATH','')
    windir = environ.get('WINDIR','')
    if not (srchpath and windir):
        return ()
    windir = path.normcase(windir)
    return (p for p in srchpath.split(path.pathsep) if 
              _utils._startswith(path.normcase(p), windir))
Example #18
0
File: tree.py Project: pyzo/pyzo
    def setPathUp(self):
        """ Go one directory up.
        """
        newPath = op.dirname(self.path())

        if op.normcase(newPath) == op.normcase(self.path()):
            self.setPath(cleanpath(MOUNTS))
        else:
            self.setPath(newPath)
 def file_in_workspace(self, open_tab):
     workspace = get_setting(SettingObject.workspace)
     workspace = path.normcase(workspace)
     open_tab = path.normcase(open_tab)
     extension = get_setting(SettingObject.extension)
     if open_tab.endswith(extension):
         return open_tab.startswith(workspace)
     else:
         return False
Example #20
0
def is_subdir(path, directory):
    """
    Returns true if *path* in a subdirectory of *directory*.
    Both paths must be absolute.
    """
    from os.path import normpath, normcase
    path = normpath(normcase(path))
    directory = normpath(normcase(directory))
    return path.startswith(directory)
 def get_table_name(self, open_tab):
     workspace = get_setting(SettingObject.workspace)
     workspace_norm = path.normcase(workspace)
     open_tab_norm = path.normcase(open_tab)
     extension = get_setting(SettingObject.extension)
     if open_tab_norm.endswith(extension):
         if open_tab_norm.startswith(workspace_norm):
             return rf_table_name(normalise_path(open_tab))
     else:
         return False
Example #22
0
    def test_which_is_set(self):
        stub_os_environ(self)
        tmpdir = mkdtemp(self)
        mgr_bin = self.create_fake_mgr_bin(tmpdir)
        driver = cli.PackageManagerDriver(pkg_manager_bin='mgr')
        driver.env_path = tmpdir
        self.assertEqual(normcase(driver.which()), normcase(mgr_bin))

        driver.env_path = None
        self.assertIsNone(driver.which())
Example #23
0
 def assertFilepathsEqual(self, p1, p2):
     if sys.platform == "win32":
         if isinstance(p1, (list, tuple)):
             p1 = [normcase(normpath(x)) for x in p1]
             p2 = [normcase(normpath(x)) for x in p2]
         else:
             assert isinstance(p1, (str, unicode))
             p1 = normcase(normpath(p1))
             p2 = normcase(normpath(p2))
     self.assertEqual(p1, p2)
 def _is_inside_project(self, filename):
     filename = path.normcase(path.realpath(filename))
     for folder in self.window.folders():
         folder = path.normcase(path.realpath(folder))
         if folder == filename:
             return True
         folder += os.sep
         if path.commonprefix([folder, filename]) == folder:
             return True
     return False
Example #25
0
def paths_equal(path1, path2):
    """
    Examples:
        >>> paths_equal('/a/b/c', '/a/b/c/d/..')
        True

    """
    if on_win:
        return normcase(abspath(path1)) == normcase(abspath(path2))
    else:
        return abspath(path1) == abspath(path2)
Example #26
0
def find_rplugins(context, source, loaded_paths):
    """Find available modules from runtimepath and yield

    It searches modules from rplugin/python3/denite/{source} recursvely
    and yields path and module path (dot separated path)

    Search will be performed with the following rules

    1.  Ignore {source,kind,filter}/__init__.py while it has no
        implementation
    2.  Ignore {source,filter}/base.py while it only has an abstract
        implementation. Note that kind/base.py DOES have an
        implementation so it should NOT be ignored
    3   {source,kind,filter}/{foo.py,foo/__init__.py} is handled as
        a module foo
    4.  {source,kind,filter}/foo/{bar.py,bar/__init__.py} is handled
        as a module foo.bar

    Args:
        context (dict): A context dictionary
        source (str): 'source', 'kind', or 'filter'
        loaded_paths: (str[]): Loaded module path list

    Yields:
        (path, module_path)

        path (str): A path of the module
        module_path (str): A dot separated module path used to import
    """
    base = join('rplugin', 'python3', 'denite', source)
    for runtime in context.get('runtimepath', '').split(','):
        root = normcase(normpath(join(runtime, base)))
        for r, _, fs in os.walk(root):
            for f in fs:
                path = normcase(normpath(join(r, f)))
                if not path.endswith('.py') or path in loaded_paths:
                    continue
                module_path = os.path.relpath(path, root)
                module_path = os.path.splitext(module_path)[0]
                if module_path == '__init__':
                    # __init__.py in {root} does not have implementation
                    # so skip
                    continue
                elif module_path == 'base' and source != 'kind':
                    # base.py in {root} does not have implementation so skip
                    # NOTE: kind/base.py DOES have implementation so do
                    # NOT skip
                    continue
                if os.path.basename(module_path) == '__init__':
                    # 'foo/__init__.py' should be loaded as a module 'foo'
                    module_path = os.path.dirname(module_path)
                # Convert IO path to module path
                module_path = module_path.replace(os.sep, '.')
                yield (path, module_path)
Example #27
0
 def dictForStarredDir(self, path):
     """ Return the dict of the starred dir corresponding to
     the given path, or None if no starred dir was found.
     """
     if not path:
         return None
     for d in self.parent().config.starredDirs:
         if op.normcase(d['path']) == op.normcase(path):
             return d
     else:
         return None
Example #28
0
    def __init__(self, bank, exp_design):
        self.exp_design = exp_design
        self.metabolites_by_feature = {}
        self.bank = 'hmdb' if bank not in ['hmdb, kegg'] else bank  #self.exp_design.databases

        #unzip database file  du to heroku filesystem
        if not op.exists(DatabaseSearch.HMDB_FILE):
            z = ZipFile(op.normcase('mzos/ressources/hmdb.zip'))
            z.extract('hmdb.sqlite', path=op.normcase('mzos/ressources/'))

        logging.info("Performing database search in {} {}".format(self.bank, 'v3.5'))
Example #29
0
 def removeStarredDir(self, path):
     """ Remove the given path from the starred directories.
     The path must exactlty match.
     """
     # Remove
     starredDirs = self.parent().config.starredDirs
     pathn = op.normcase(path)
     for d in starredDirs:
         if op.normcase(pathn) == op.normcase(d.path):
             starredDirs.remove(d)
     # Update list
     self._projects.updateProjectList()
Example #30
0
    def test_which_is_set_env_path(self):
        stub_os_environ(self)
        tmpdir = mkdtemp(self)
        mgr_bin = self.create_fake_mgr_bin(tmpdir)
        driver = cli.PackageManagerDriver(pkg_manager_bin='mgr')
        # With both env_path attr and environ PATH set
        os.environ['PATH'] = driver.env_path = tmpdir
        self.assertEqual(normcase(driver.which()), normcase(mgr_bin))

        # the autodetection should still work through ENV_PATH
        driver.env_path = None
        self.assertEqual(normcase(driver.which()), normcase(mgr_bin))
Example #31
0
def python_files(tmpdir_factory):
    """Create and save some python codes in temporary files."""
    tmpdir = tmpdir_factory.mktemp("files")
    tmpdir = osp.normcase(tmpdir.strpath)

    filenames = [osp.join(tmpdir, f) for f in
                 ('file1.py', 'file2.py', 'file3.py', 'file4.py')]
    for filename in filenames:
        with open(filename, 'w') as f:
            f.write("# -*- coding: utf-8 -*-\n"
                    "print(Hello World!)\n")

    return filenames, tmpdir
Example #32
0
    def test_relocated_distribution(self):
        root = mkdtemp(self)
        dummyns_path = join(root, 'dummyns')

        make_dummy_dist(self, (
            (
                'namespace_packages.txt',
                'dummyns\n',
            ),
            (
                'entry_points.txt',
                '[dummyns]\n'
                'dummyns = dummyns:attr\n',
            ),
        ),
                        'dummyns',
                        '1.0',
                        working_dir=root)
        working_set = pkg_resources.WorkingSet([
            root,
            self.ds_egg_root,
        ])
        # activate this as the working set
        stub_item_attr_value(self, pkg_resources, 'working_set', working_set)
        dummyns_ep = next(working_set.iter_entry_points('dummyns'))
        with pretty_logging(stream=StringIO()) as fd:
            p = indexer.resource_filename_mod_entry_point(
                'dummyns', dummyns_ep)
        # since the actual location is not created)
        self.assertIsNone(p)
        self.assertIn("does not exist", fd.getvalue())

        # retry with the module directory created at the expected location
        os.mkdir(dummyns_path)
        with pretty_logging(stream=StringIO()) as fd:
            p = indexer.resource_filename_mod_entry_point(
                'dummyns', dummyns_ep)
        self.assertEqual(normcase(p), normcase(dummyns_path))
        self.assertEqual('', fd.getvalue())
Example #33
0
def save(image):
    try:
        with NamedTemporaryFile(prefix='tess_') as f:
            if isinstance(image, str):
                yield f.name, realpath(normpath(normcase(image)))
                return

            image, extension = prepare(image)
            input_file_name = f.name + extsep + extension
            image.save(input_file_name, format=extension, **image.info)
            yield f.name, input_file_name
    finally:
        cleanup(f.name)
Example #34
0
    def __init__(self):
        TreeView.__init__(self)  # for debug logging: , debug="catalogs")
        self._rows = []

        koDirSvc = components.classes["@activestate.com/koDirs;1"].\
                   getService(components.interfaces.koIDirs)
        self.norm_user_apicatalogs_dir \
            = normpath(normcase(join(koDirSvc.userDataDir, "apicatalogs")))

        # Atoms for styling the checkboxes.
        self.atomSvc = components.classes["@mozilla.org/atom-service;1"].\
                  getService(components.interfaces.nsIAtomService)
        self._sortColAtom = self.atomSvc.getAtom("sort-column")
Example #35
0
 async def cont() -> _SortOfMonoid:
     assert pip
     p = await call(
         pip,
         "install",
         "--upgrade",
         "--user",
         "--",
         *specs,
         check_returncode=set(),
         env={"PYTHONUSERBASE": normcase(PIP_DIR)},
     )
     return (("", p), )
Example #36
0
def safe_join(base, *paths):
    """
    Joins one or more path components to the base path component intelligently.
    Returns a normalized, absolute version of the final path.

    The final path must be located inside of the base path component (otherwise
    a ValueError is raised).
    """
    base = force_unicode(base)
    paths = [force_unicode(p) for p in paths]
    final_path = abspathu(join(base, *paths))
    base_path = abspathu(base)
    base_path_len = len(base_path)
    # Ensure final_path starts with base_path (using normcase to ensure we
    # don't false-negative on case insensitive operating systems like Windows)
    # and that the next character after the final path is os.sep (or nothing,
    # in which case final_path must be equal to base_path).
    if not normcase(final_path).startswith(normcase(base_path)) \
       or final_path[base_path_len:base_path_len+1] not in ('', sep):
        raise ValueError('The joined path (%s) is located outside of the base '
                         'path component (%s)' % (final_path, base_path))
    return final_path
Example #37
0
class TestPeakListReader(unittest.TestCase):
    FILE = op.normcase("mzos/ressources/peaks_matrix_NEG.tsv")

    def test_read_tsv(self):
        pkl_reader = PeakListReader(
            self.FILE,
            ExperimentalSettings(mz_tol_ppm=10.0,
                                 polarity='negative',
                                 is_dims_exp=False))
        peakels = pkl_reader.get_peakels()
        self.assertEqual(len(peakels), 3238)
        self.assertTrue(all(isinstance(p.moz, float) for p in peakels))
        self.assertTrue(all(p.moz != 0.0 for p in peakels))
Example #38
0
def norm_path(path):
    # python may return Windows short paths, normalize
    path = realpath(path)
    if virtualenv.IS_WIN:
        from ctypes import create_unicode_buffer, windll

        buffer_cont = create_unicode_buffer(256)
        get_long_path_name = windll.kernel32.GetLongPathNameW
        get_long_path_name(six.text_type(path), buffer_cont, 256)  # noqa: F821
        result = buffer_cont.value
    else:
        result = path
    return normcase(result)
Example #39
0
    def get_path(self, p_path):
        """
        Validate path exists and convert it to absolute, normal and real for the environment.
        Works for directory, file, symlink or mount.

        @DEV: Consider the pathlib.Path library. It is quite a robust extension over the
              os.path library. Can probably do away with this function altogether. It doesn't
              add to the native capability and I don't think I have ever used more than one of
              the returned values in the calling object.

        :Args: {string} path to file or dir expressed in any legit notation

        :Return: {namedtuple}

        - .exists   {boolean} whether the dir or file exists
        - .rqst     {string} requested path
        - .abs      {path} absolute path using Posix syntax
        - .isDir    {boolean}
        - .isFile   {boolean}
        - .isLink   {boolean}
        - .isMount  {boolean}
        - .parent   {string} parent path
        - .item     {string} name of file or directory
        """
        path_a = False
        path_a = path.abspath(p_path)  # .abs
        path_p = {
            pattr: False
            for pattr in
            ['isDir', 'isFile', 'isLink', 'isMount', 'parent', 'item']
        }
        path_e = True if path.exists(path_a) else False  # .exists
        if path_e:
            path_p['isDir'] = True if path.isdir(p_path) else path_p['isDir']
            path_p['isFile'] = True if path.isfile(
                p_path) else path_p['isFile']
            path_p['isLink'] = True if path.islink(
                p_path) else path_p['isLink']
            path_p['isMount'] = True if path.ismount(
                p_path) else path_p['isMount']
            path_a = path.normpath(path.normcase(
                path.realpath(path_a)))  # .abs
            v_parts = path.split(path_a)
            path_p['parent'] = v_parts[0]
            path_p['item'] = v_parts[1]

        fpath = namedtuple(
            'fpath', 'rqst exists abs isDir isFile isLink isMount parent item')
        return fpath(p_path, path_e, path_a, path_p['isDir'], path_p['isFile'],
                     path_p['isLink'], path_p['isMount'], path_p['parent'],
                     path_p['item'])
Example #40
0
def create_server(
    binding: Union[PurePath, Tuple[Union[IPAddress, Literal[""], str], int]],
    handler: Type[BaseHTTPRequestHandler],
) -> HTTPServer:

    dualstack_ipv6 = False
    if isinstance(binding, PurePath):
        addr_fam = AddressFamily.AF_UNIX
        srv_addr: Any = normcase(binding)
    elif isinstance(binding, tuple):
        ip, port = binding
        srv_addr = (str(ip), port)

        if isinstance(ip, IPv4Address):
            addr_fam = AddressFamily.AF_INET
        elif isinstance(ip, IPv6Address) or not ip:
            dualstack_ipv6 = True
            addr_fam = AddressFamily.AF_INET6
        else:
            ipv = ip_address(ip)
            if isinstance(ipv, IPv4Address):
                addr_fam = AddressFamily.AF_INET
            elif isinstance(ipv, IPv6Address):
                dualstack_ipv6 = True
                addr_fam = AddressFamily.AF_INET6
            else:
                never(ipv)

    else:
        never(binding)

    class Server(ThreadingHTTPServer):
        address_family = addr_fam

        def server_bind(self) -> None:
            if dualstack_ipv6:
                self.socket.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 0)
            TCPServer.server_bind(self)

            if isinstance(binding, PurePath):
                self.server_name = self.socket.getsockname()
            elif isinstance(binding, tuple):
                self.server_name = getfqdn().encode("idna").decode()
                if isinstance(self, HTTPServer):
                    _, actual_port, *_ = self.socket.getsockname()
                    self.server_port = actual_port
            else:
                never(binding)

    server = Server(srv_addr, handler)
    return server
Example #41
0
def old_cp(src, dst, log=None, dry_run=False):
    """Copy src to dst.
    
    DEPRECATED: use the new cp().
    """
    if log: log.info("cp `%s' `%s'", src, dst)
    if dry_run:
        return

    #if src.find('*') != -1 or src.find('?') != -1 or src.find('[') != -1:
    #    raise ValueError("globbing not yet supported: %r" % src)

    norm_src = normcase(normpath(abspath(src)))
    norm_dst = normcase(normpath(abspath(dst)))
    if norm_src == norm_dst:
        raise OSError("cannot copy file onto itself: src=%r, dst=%r" %
                      (src, dst))

    if sys.platform == "win32":
        src = src.replace("/", "\\")
        dst = dst.replace("/", "\\")
        #TODO: cp("somedir/*", "destdir") does not do the expected here
        #      b/c 'isdir(src) is False'.
        if isdir(src):
            run('xcopy /e/i/y/q "%s" "%s"' % (src, dst))
        else:
            run('copy /y "%s" "%s" >nul' % (src, dst))
        # Problem: 'copy' and 'xcopy' preserve the mtime by default
        # (with no option to not do so). As a result, any make target
        # with two copies will always end up being out of date. By
        # default 'cp' on other platforms does NOT preserve the mtime.
        # We want that behaviour here -- but that means using 'xcopy'
        # is a pain.
    else:
        if isdir(src):
            run('cp -R "%s" "%s"' % (src, dst))
        else:
            run('cp "%s" "%s"' % (src, dst))
    def test_uninstall_from_usersite_with_dist_in_global_site(
            self, script, virtualenv):
        """
        Test uninstall from usersite (with same dist in global site)
        """
        # the test framework only supports testing using virtualenvs.
        # the sys.path ordering for virtualenvs with --system-site-packages is
        # this: virtualenv-site, user-site, global-site.
        # this test will use 2 modifications to simulate the
        #   user-site/global-site relationship
        # 1) a monkey patch which will make it appear piptestpackage is not in
        #    the virtualenv site if we don't patch this, pip will return an
        #    installation error:  "Will not install to the usersite because it
        #    will lack sys.path precedence..."
        # 2) adding usersite to PYTHONPATH, so usersite has sys.path precedence
        #    over the virtualenv site

        virtualenv.system_site_packages = True
        script.environ["PYTHONPATH"] = script.base_path / script.user_site
        _patch_dist_in_site_packages(script)

        script.pip_install_local('pip-test-package==0.1', '--no-binary=:all:')

        result2 = script.pip_install_local('--user', 'pip-test-package==0.1.1',
                                           '--no-binary=:all:')
        result3 = script.pip('uninstall', '-vy', 'pip-test-package')

        # uninstall console is mentioning user scripts, but not global scripts
        assert normcase(script.user_bin_path) in result3.stdout, str(result3)
        assert normcase(script.bin_path) not in result3.stdout, str(result3)

        # uninstall worked
        assert_all_changes(result2, result3, [script.venv / 'build', 'cache'])

        # site still has 0.2 (can't look in result1; have to check)
        egg_info_folder = (script.base_path / script.site_packages /
                           'pip_test_package-0.1-py%s.egg-info' % pyversion)
        assert isdir(egg_info_folder)
Example #43
0
    def filterAcceptsRow(self, row, parent_index):
        """Reimplement Qt method."""
        if self.root_path is None:
            return True
        index = self.sourceModel().index(row, 0, parent_index)
        path = osp.normcase(
            osp.normpath(str(self.sourceModel().filePath(index))))

        if osp.normcase(self.root_path).startswith(path):
            # This is necessary because parent folders need to be scanned
            return True
        else:
            for p in [osp.normcase(p) for p in self.path_list]:
                if path == p or path.startswith(p + os.sep):
                    if not any([d in path for d in self.PATHS_TO_SHOW]):
                        if any([d in path for d in self.PATHS_TO_HIDE]):
                            return False
                        else:
                            return True
                    else:
                        return True
            else:
                return False
Example #44
0
def safe_join(base, *paths):
    """
    Joins one or more path components to the base path component intelligently.
    Returns a normalized, absolute version of the final path.

    The final path must be located inside of the base path component (otherwise
    a ValueError is raised).
    """
    # We need to use normcase to ensure we don't false-negative on case
    # insensitive operating systems (like Windows).
    base = force_unicode(base)
    paths = [force_unicode(p) for p in paths]
    final_path = normcase(abspathu(join(base, *paths)))
    base_path = normcase(abspathu(base))
    base_path_len = len(base_path)
    # Ensure final_path starts with base_path and that the next character after
    # the final path is os.sep (or nothing, in which case final_path must be
    # equal to base_path).
    if not final_path.startswith(base_path) \
       or final_path[base_path_len:base_path_len+1] not in ('', sep):
        raise ValueError('the joined path is located outside of the base path'
                         ' component')
    return final_path
Example #45
0
def commonpath(a, b):
    """Returns the longest common to 'paths' path.

    Unlike the strange commonprefix:
    - this returns valid path
    - accepts only two arguments
    """
    a = normpath(normcase(a))
    b = normpath(normcase(b))

    if a == b:
        return a

    while len(a) > 0:
        if a == b:
            return a

        if len(a) > len(b):
            a = dirname(a)
        else:
            b = dirname(b)

    return None
Example #46
0
def eval_snips(
    nvim: Nvim,
    stack: Stack,
    visual: bool,
    maybe_grammar: str = REPL_GRAMMAR,
) -> None:
    try:
        grammar = SnippetGrammar[maybe_grammar]
    except KeyError:
        grammar = SnippetGrammar.lsp
        log.warn("%s", "bad snippet grammar -- reverting to LSP")

    win = cur_win(nvim)
    buf = win_get_buf(nvim, win=win)
    line_count = buf_line_count(nvim, buf=buf)
    path = PurePath(normcase(buf_name(nvim, buf=buf)))
    comment_str = buf_commentstr(nvim, buf=buf)
    clipboard = nvim.funcs.getreg()
    info = ParseInfo(visual="", clipboard=clipboard, comment_str=comment_str)

    if visual:
        (lo, _), (hi, _) = operator_marks(nvim, buf=buf, visual_type=None)
        hi = min(line_count, hi + 1)
    else:
        lo, hi = 0, line_count

    lines = buf_get_lines(nvim, buf=buf, lo=lo, hi=hi)

    try:
        compiled = compile_one(
            stack,
            grammar=grammar,
            path=path,
            info=info,
            lines=enumerate(lines, start=lo + 1),
        )
    except (LoadError, ParseError) as e:
        preview = str(e).splitlines()
        with hold_win_pos(nvim, win=win):
            set_preview(nvim, syntax="", preview=preview)
        write(nvim, LANG("snip parse fail"))

    else:
        preview = _pprn(compiled).splitlines()
        with hold_win_pos(nvim, win=win):
            set_preview(nvim, syntax="yaml", preview=preview)
        if preview:
            write(nvim, LANG("snip parse succ"))
        else:
            write(nvim, LANG("no snippets found"))
Example #47
0
 def __init__(self, root_dir, cache_max_age=3600, package_name=None,
              use_subpath=False, index='index.html'):
     # package_name is for bw compat; it is preferred to pass in a
     # package-relative path as root_dir
     # (e.g. ``anotherpackage:foo/static``).
     self.cache_max_age = cache_max_age
     if package_name is None:
         package_name = caller_package().__name__
     package_name, docroot = resolve_asset_spec(root_dir, package_name)
     self.use_subpath = use_subpath
     self.package_name = package_name
     self.docroot = docroot
     self.norm_docroot = normcase(normpath(docroot))
     self.index = index
Example #48
0
def rename_duplicate(top_dir, dry_run=True):
    top = path.normcase(top_dir)
    for root, _, files in os.walk(top):
        for name in files:
            base, ext = path.splitext(name)
            if not ext or ext.lower() not in ['.jpg', 'jpeg', '.tiff']:
                continue
            print(base, ext)
            rel_path = path.join(root, name)
            src_path = path.abspath(rel_path)
            dst_name = "{}_1{}".format(base, ext)
            dst_path = path.join(root, dst_name)
            os.rename(src_path, dst_path)
            print("Rename {} to {}".format(name, dst_name))
Example #49
0
    def setup(self, new_path_to_graphviz=None):
        """
        Method to setup the path to dot.exe on the executing computer. Used for both pydot or
        pyreverse pretty printing.
        :param new_path_to_graphviz: the full path to dot.exe. If no path is
        provided, assumes the graphviz source is in the working directory under graphviz and
        sets the path to that.
        """
        if new_path_to_graphviz is None:  # pragma: no cover
            dir_path = path.dirname(path.realpath(__file__))
            full_path = path.join(dir_path, self.path_to_graphviz)
            new_path_to_graphviz = path.normcase(full_path)

        environ["PATH"] += pathsep + new_path_to_graphviz
Example #50
0
def build_parent_child_relationship(nodes, site):
    for node in nodes:
        nodePath = normcase(normpath(node.get_full_path()))
        deep = nodePath.split(os.sep)
        parentNode = None
        parentNodeDeep = -1
        for iNode in nodes:
            if (iNode.type == 'R'
                ):  # repository can not be a parent of another repository
                continue
            iNodePath = normcase(normpath(iNode.get_full_path()))
            iDeep = iNodePath.split(os.sep)
            #find the parent node whose path is more close to node's path
            if iDeep > parentNodeDeep and iDeep < deep and nodePath.find(
                    iNodePath) != -1:
                parentNode = iNode
                parentNodeDeep = iDeep
        if parentNode is None:
            node.parent = site.node
            node.save()
        else:
            node.parent = parentNode
            node.save()
Example #51
0
async def _git_show_many(it: Iterable[Tuple[str, PurePath]]) -> None:
    tmp = Path(mkdtemp())
    for sha, path in it:
        temp = tmp / path
        proc = await call(
            "git",
            "show",
            f"{sha}:{path}",
            capture_stderr=False,
        )
        temp.parent.mkdir(parents=True, exist_ok=True)
        temp.write_bytes(proc.out)

    log.info("%s", normcase(tmp))
Example #52
0
def relpath(target, base_path=os.curdir):
    """\
    Return a relative path to the target from either the current directory
    or an optional base directory.

    Base can be a directory specified either as absolute or relative
    to current directory."""
    # http://code.activestate.com/recipes/302594/

    base_path = normcase(abspath(normpath(base_path)))
    target = normcase(abspath(normpath(target)))

    if base_path == target:
        return '.'

    # On the windows platform the target may be on a different drive.
    if splitdrive(base_path)[0] != splitdrive(target)[0]:
        return None

    common_path_len = len(commonpath(base_path, target))

    # If there's no common prefix decrease common_path_len should be less by 1
    base_drv, base_dir = splitdrive(base_path)
    if common_path_len == len(base_drv) + 1:
        common_path_len -= 1

    # if base_path is root directory - no directories up
    if base_dir == os.sep:
        dirs_up = 0
    else:
        dirs_up = base_path[common_path_len:].count(os.sep)

    ret = os.sep.join([os.pardir] * dirs_up)
    if len(target) > common_path_len:
        ret = path_join(ret, target[common_path_len + 1:])

    return ret
Example #53
0
    def test_standard(self):
        d_egg_root = join(mkdtemp(self), 'dummyns')

        make_dummy_dist(self, (
            (
                'namespace_packages.txt',
                'dummyns\n',
            ),
            (
                'entry_points.txt',
                '[dummyns]\n'
                'dummyns = dummyns:attr\n',
            ),
        ),
                        'dummyns',
                        '1.0',
                        working_dir=d_egg_root)
        working_set = pkg_resources.WorkingSet([
            d_egg_root,
            self.ds_egg_root,
        ])
        # ensure the working_set is providing the distributions being
        # mocked here so that resource_filename will resolve correctly
        stub_item_attr_value(self, pkg_resources, 'working_set', working_set)

        moddir = join(d_egg_root, 'dummyns')
        os.makedirs(moddir)

        # make this also a proper thing
        with open(join(moddir, '__init__.py'), 'w') as fd:
            fd.write('')

        dummyns_ep = next(working_set.iter_entry_points('dummyns'))
        p = indexer.resource_filename_mod_entry_point('dummyns', dummyns_ep)

        # finally, this should work.
        self.assertEqual(normcase(p), normcase(moddir))
Example #54
0
def find_rplugins(context, source, loaded_paths):
    """Find available modules from runtimepath and yield

    It searches modules from rplugin/python3/denite/{source} recursvely
    and yields path and module path (dot separated path)

    Search will be performed with the following rules

    1.  Ignore {source,kind,filter}/__init__.py while it has no
        implementation
    2.  Ignore {source,filter}/base.py while it only has an abstract
        implementation. Note that kind/base.py DOES have an
        implementation so it should NOT be ignored
    3   {source,kind,filter}/{foo.py,foo/__init__.py} is handled as
        a module foo
    4.  {source,kind,filter}/foo/{bar.py,bar/__init__.py} is handled
        as a module foo.bar

    Args:
        context (dict): A context dictionary
        source (str): 'source', 'kind', or 'filter'
        loaded_paths: (str[]): Loaded module path list

    Yields:
        (path, module_path)

        path (str): A path of the module
        module_path (str): A dot separated module path used to import
    """
    base = join('rplugin', 'python3', 'denite', source)
    for runtime in context.get('runtimepath', '').split(','):
        root = os.path.normpath(join(runtime, base))
        for path in filter(lambda p: normcase(normpath(p)) not in loaded_paths,
                           iglob(join(root, '**', '*.py'), recursive=True)):
            module_path = os.path.relpath(str(path), root)
            module_path = os.path.splitext(module_path)[0]
            if module_path == '__init__':
                # __init__.py in {root} does not have implementation so skip
                continue
            elif module_path == 'base' and source != 'kind':
                # base.py in {root} does not have implementation so skip
                # NOTE: kind/base.py DOES have implementation so do NOT skip
                continue
            if os.path.basename(module_path) == '__init__':
                # 'foo/__init__.py' should be loaded as a module 'foo'
                module_path = os.path.dirname(module_path)
            # Convert IO path to module path
            module_path = module_path.replace(os.sep, '.')
            yield (path, module_path)
Example #55
0
    def test_uninstall_from_usersite_with_dist_in_global_site(
            self, virtualenv, script):
        """
        Test uninstall from usersite (with same dist in global site)
        """
        _patch_dist_in_site_packages(virtualenv)

        script.pip_install_local('pip-test-package==0.1', '--no-binary=:all:')

        result2 = script.pip_install_local('--user', 'pip-test-package==0.1.1',
                                           '--no-binary=:all:')
        result3 = script.pip('uninstall', '-vy', 'pip-test-package')

        # uninstall console is mentioning user scripts, but not global scripts
        assert normcase(script.user_bin_path) in result3.stdout, str(result3)
        assert normcase(script.bin_path) not in result3.stdout, str(result3)

        # uninstall worked
        assert_all_changes(result2, result3, [script.venv / 'build', 'cache'])

        # site still has 0.2 (can't look in result1; have to check)
        egg_info_folder = (script.base_path / script.site_packages /
                           'pip_test_package-0.1-py%s.egg-info' % pyversion)
        assert isdir(egg_info_folder)
Example #56
0
def breakpoint_path_match(vs_path, local_path):
    vs_path_norm = path.normcase(vs_path)
    local_path_norm = path.normcase(local_path)
    if local_path_to_vs_path.get(local_path_norm) == vs_path_norm:
        return True

    # Walk the local filesystem from local_path up, matching agains win_path component by component,
    # and stop when we no longer see an __init__.py. This should give a reasonably close approximation
    # of matching the package name.
    while True:
        local_path, local_name = path.split(local_path)
        vs_path, vs_name = ntpath.split(vs_path)
        # Match the last component in the path. If one or both components are unavailable, then
        # we have reached the root on the corresponding path without successfully matching.
        if not local_name or not vs_name or path.normcase(local_name) != path.normcase(vs_name):
            return False
        # If we have an __init__.py, this module was inside the package, and we still need to match
        # thatpackage, so walk up one level and keep matching. Otherwise, we've walked as far as we
        # needed to, and matched all names on our way, so this is a match.
        if not path.exists(path.join(local_path, '__init__.py')):
            break

    local_path_to_vs_path[local_path_norm] = vs_path_norm
    return True
Example #57
0
    def norm_path(path):
        # python may return Windows short paths, normalize
        if not isinstance(path, Path):
            path = Path(path)
        path = ensure_text(str(path.resolve()))
        if sys.platform != "win32":
            result = path
        else:
            from ctypes import create_unicode_buffer, windll

            buffer_cont = create_unicode_buffer(256)
            get_long_path_name = windll.kernel32.GetLongPathNameW
            get_long_path_name(six.text_type(path), buffer_cont, 256)
            result = buffer_cont.value or path
        return normcase(result)
    def trim_path(path):
        """coverts to Zip format.

    Args:
      path: path to be trimmed

    Returns:
      Path to the zip archive
    """
        archive_path = path.replace(parent_dir, "", 1)
        if parent_dir:
            archive_path = archive_path.replace(sep, "", 1)
        if not include_dir_in_path:
            archive_path = archive_path.replace(dir_to_zip + sep, "", 1)
        return normcase(archive_path)
    def canonical_path(input_path, folder=''):
        """Return a canonical path of the file.

        Args:
            input_path (str): path to convert.
            folder (str, optional): parent folder.

        Returns:
            str: canonical path
        """
        if not input_path:
            return None
        if not path.isabs(input_path):
            input_path = path.join(folder, input_path)
        return path.normcase(path.normpath(input_path))
Example #60
0
    def test_FindExecutables(self):
        self.prefs.setStringPref(self.defaultInterpreterPrefName, "")

        # Check it without a default pref.
        exe_paths = self._getPathsForInterpreters(self.exenames)
        self.assertFilepathsEqual(self.freshAppInfo.FindExecutables(), exe_paths)
        self.assertFilepathsEqual(self.cachedAppInfo.FindExecutables(), exe_paths)

        # Set the pref and then check it.
        expected_executablePath = normcase(normpath(abspath(__file__)))
        expected_installationPath = dirname(expected_executablePath)
        expected_exe_paths = [expected_executablePath] + exe_paths
        self.prefs.setStringPref(self.defaultInterpreterPrefName, expected_executablePath)
        self.assertFilepathsEqual(self.freshAppInfo.FindExecutables(), expected_exe_paths)
        self.assertFilepathsEqual(self.cachedAppInfo.FindExecutables(), expected_exe_paths)