Beispiel #1
0
def process(paths):
    v = []
    if len(paths) > MAX_FILES:
        sys.stderr.write(
            "You may open at most %s at once using the open command; truncating list\n"
            % MAX_FILES)
        paths = paths[:MAX_FILES]
    for path in paths:
        if not path:
            continue
        if not os.path.exists(path) and any(c in path for c in '{?*'):
            # If the path doesn't exist and does contain a shell glob character which didn't get expanded,
            # then don't try to just create that file.  See https://github.com/sagemathinc/cocalc/issues/1019
            sys.stderr.write("no match for '%s', so not creating\n" % path)
            continue
        if not os.path.exists(path):
            if '/' in path:
                dir = os.path.dirname(path)
                if not os.path.exists(dir):
                    sys.stderr.write("creating directory '%s'\n" % dir)
                    os.makedirs(dir)
            if path[-1] != '/':
                sys.stderr.write("creating file '%s'\n" % path)
                import new_file
                new_file.new_file(
                    path
                )  # see https://github.com/sagemathinc/cocalc/issues/1476

        if not path.startswith('/'):
            # we use pwd instead of getcwd or os.path.abspath since we want this to
            # work when used inside a directory that is a symlink!  I could find
            # no analogue of pwd directly in Python (getcwd is not it!).
            path = os.path.join(os.popen('pwd').read().strip(), path)

        # Make name be the path to the file, **relative to home directory**
        if path.startswith(home):
            name = path[len(home) + 1:]
        else:
            # use the / symlink -- see https://github.com/sagemathinc/cocalc/issues/4188
            name = ROOT_SYMLINK + path

        # Is it a file or directory?
        if os.path.isdir(path):
            v.append({'directory': name})
        else:
            v.append({'file': name})

    if v:
        mesg = {'event': 'open', 'paths': v}
        print(prefix +
              '\x1b]49;%s\x07' % json.dumps(mesg, separators=(',', ':')) +
              postfix)
Beispiel #2
0
def process(paths):
    v = []
    if len(paths) > MAX_FILES:
        sys.stderr.write(
            "You may open at most %s at once using the open command; truncating list\n"
            % MAX_FILES)
        paths = paths[:MAX_FILES]
    for path in paths:
        if not path:
            continue
        if not os.path.exists(path) and any(c in path for c in '{?*'):
            # If the path doesn't exist and does contain a shell glob character which didn't get expanded,
            # then don't try to just create that file.  See https://github.com/sagemathinc/cocalc/issues/1019
            sys.stderr.write("no match for '%s', so not creating\n" % path)
            continue
        if not os.path.exists(path):
            if '/' in path:
                dir = os.path.dirname(path)
                if not os.path.exists(dir):
                    sys.stderr.write("creating directory '%s'\n" % dir)
                    os.makedirs(dir)
            if path[-1] != '/':
                sys.stderr.write("creating file '%s'\n" % path)
                import new_file
                new_file.new_file(
                    path
                )  # see https://github.com/sagemathinc/cocalc/issues/1476

        if not path.startswith('/'):
            # we use pwd instead of getcwd or os.path.abspath since we want this to
            # work when used inside a directory that is a symlink!  I could find
            # no analogue of pwd directly in Python (getcwd is not it!).
            path = os.path.join(os.popen('pwd').read().strip(), path)

        # determine name relative to home directory
        if path.startswith(home):
            name = path[len(home) + 1:]
        else:
            name = path

        # Is it a file or directory?
        if os.path.isdir(path):
            v.append({'directory': name})
        else:
            v.append({'file': name})

    if len(v) > 0:
        mesg = {'event': 'open', 'paths': v}
        print prefix + '\x1b]49;%s\x07' % json.dumps(
            mesg, separators=(',', ':')) + postfix
Beispiel #3
0
def process(paths):
    v = []
    if len(paths) > MAX_FILES:
        sys.stderr.write(
            "You may open at most %s at once using the open command; truncating list\n"
            % MAX_FILES)
        paths = paths[:MAX_FILES]
    for path in paths:
        if not path:
            continue
        if not os.path.exists(path) and any(c in path for c in '{?*'):
            # If the path doesn't exist and does contain a shell glob character which didn't get expanded,
            # then don't try to just create that file.  See https://github.com/sagemathinc/smc/issues/1019
            sys.stderr.write("no match for '%s', so not creating\n" % path)
            continue
        if not os.path.exists(path):
            if '/' in path:
                dir = os.path.dirname(path)
                if not os.path.exists(dir):
                    sys.stderr.write("creating directory '%s'\n" % dir)
                    os.makedirs(dir)
            if path[-1] != '/':
                sys.stderr.write("creating file '%s'\n" % path)
                import new_file
                new_file.new_file(
                    path)  # see https://github.com/sagemathinc/smc/issues/1476

        path = os.path.abspath(path)

        # determine name relative to home directory
        if path.startswith(home):
            name = path[len(home) + 1:]
        else:
            name = path

        # Is it a file or directory?
        if os.path.isdir(path):
            v.append({'directory': name})
        else:
            v.append({'file': name})

    if len(v) > 0:
        mesg = {'event': 'open', 'paths': v}
        print prefix + '\x1b]49;%s\x07' % json.dumps(
            mesg, separators=(',', ':')) + postfix