Exemple #1
0
def find_cached(files, match, replace = None, replace_match = None):
    """Finds and replaces inside of files. """

    # Make sure match and replace are arrays
    match = [match] if is_string(match) else match
    if replace != None:
        replace = [replace] if is_string(replace) else replace

        if len(replace) != len(match):
            err("Find and Replace patterns are not of the same length. ")
            return False


    rep = False
    for file in files:
        repo = os.path.relpath(find_repo_dir(file), lmh_locate("content"))
        matcher = [Template(m).substitute(repo=repo) for m in match]
        if replace != None:
            rep = find_and_replace_file(file, matcher, replace, replace_match = replace_match) or rep
        else:
            rep = find_file(file, matcher) or rep
    return rep
Exemple #2
0
def match_repos(repos, root=os.getcwd(), abs=False):
    """
        Matches a list of specefiers to repositories.

        @param {string[]} repos - Specefiers to match
        @param {string} root - Root directory to use
        @param {boolean} [abs=False] - Should absolute paths be returned?

        @returns string[] - repository paths
    """

    # For each element do the following:
    # 1) Check if given directory exists relatively from current root.
    #       1a) If it is a repository or repository subdir, return that
    #        1b) If it is inside the data_dir, return all repo subdirectories
    # 2) If it does not exist, resolve it as glob.glob from install_dir
    # 3) For each of the found directories, run 1)

    # If the repositories are just a string, we want an array.
    if is_string(repos):
        repos = [repos]
    # it is already an array, so remove doubles please.
    else:
        repos = remove_doubles(repos)

    # the glob expressions we want to use
    # and the repo directories we will use.
    results = set()

    def match_repo_name(r):

        # make an absolute path
        # this will also work with globs
        names = os.path.abspath(os.path.join(root, r))

        # it is not inside the data directory
        # so try again next time
        if not is_in_data(names):
            return True

        # find the relative path of it to the root.
        names = os.path.relpath(os.path.abspath(names), lmh_locate("content"))

        # make sure everything ends with a slash
        # so that we can count properly
        if names[-1] != "/":
            names += "/"

        # Now figure out which level we are at
        # by counting slashes
        num_slashes = 0
        for c in names:
            # count the slashes
            # by increasing the level.
            if c == "/":
                num_slashes += 1

        # if we have no slashes
        # we are toplevel
        # and can pretty much exit everything
        if names == "./":
            names = lmh_locate("content", "*", "*")

        # if we have 1 slash
        # we are one level deep
        elif num_slashes == 1:
            names = lmh_locate("content", names, "*")
        else:
            names = lmh_locate("content", names)

        # now expand with the help of globs
        names = glob.glob(names)

        # and check if they exist
        names = list(filter(is_repo, names))

        # if we found something
        # we should through the item
        if len(names) > 0:
            results.update(names)
            return False
        else:
            return True

    # now filter the repos
    repos = list(filter(match_repo_name, repos))

    # repeat again with data_dir as root
    root = lmh_locate("content")
    repos = list(filter(match_repo_name, repos))

    # if we want the relative paths we need to set them properly.
    if not abs:
        return [os.path.relpath(d, lmh_locate("content")) for d in results]

    return list(results)
Exemple #3
0
def run_shell(shell=None, quiet=False, args=""):
    """
    Runs a shell in which all external programs can run
    """

    # If args is a list, join it by " "s
    if not is_string(args):
        args = " ".join(args)

    if shell == None:
        try:
            shell = os.environ["SHELL"] or which("bash")
        except:
            shell = which("bash")

            if shell == None:
                err("Unable to find bash shell, please provide another one. ")
                return 127
    else:
        shell = which(shell)
        if shell == None:
            return 127

    # Make a perl 5 environment
    _env = perl5env(os.environ)

    # pdf inputs
    def genTEXInputs():
        res = ".:" + stydir + ":"
        for (root, files, dirs) in os.walk(stexstydir):
            res += root + ":"
        for (root, files, dirs) in os.walk(latexmlstydir):
            res += root + ":"
        return res + ":" + latexmlstydir + ":" + stexstydir

    _env["TEXINPUTS"] = genTEXInputs()
    _env["PATH"] = stexstydir + ":" + _env["PATH"]

    try:
        runner = Popen([shell] + shlex.split(args),
                       env=_env,
                       stdin=sys.stdin,
                       stdout=sys.stdout,
                       stderr=sys.stderr)
    except Exception:
        # we could not find that
        return 127

    try:
        # If possible propagnate the sigterm (for #184)
        def handle_term(s, f):
            runner.send_signal(signal.SIGTERM)

        signal.signal(signal.SIGTERM, handle_term)
    except:
        pass

    def do_the_run():
        try:
            runner.wait()
        except KeyboardInterrupt:
            runner.send_signal(signal.SIGINT)
            do_the_run()

    if not quiet:
        std("Opening a shell ready to compile for you. ")

    do_the_run()

    return runner.returncode
Exemple #4
0
def match_repos(repos, root=os.getcwd(), abs=False):
    """
        Matches a list of specefiers to repositories.

        @param {string[]} repos - Specefiers to match
        @param {string} root - Root directory to use
        @param {boolean} [abs=False] - Should absolute paths be returned?

        @returns string[] - repository paths
    """

    # For each element do the following:
    # 1) Check if given directory exists relatively from current root.
    #       1a) If it is a repository or repository subdir, return that
    #        1b) If it is inside the data_dir, return all repo subdirectories
    # 2) If it does not exist, resolve it as glob.glob from install_dir
    # 3) For each of the found directories, run 1)

    # If the repositories are just a string, we want an array.
    if is_string(repos):
        repos = [repos]
    # it is already an array, so remove doubles please.
    else:
        repos = remove_doubles(repos)

    # the glob expressions we want to use
    # and the repo directories we will use.
    results = set()

    def match_repo_name(r):

        # make an absolute path
        # this will also work with globs
        names = os.path.abspath(os.path.join(root, r))

        # it is not inside the data directory
        # so try again next time
        if not is_in_data(names):
            return True

        # find the relative path of it to the root.
        names = os.path.relpath(
            os.path.abspath(names),
            lmh_locate("content")
        )

        # make sure everything ends with a slash
        # so that we can count properly
        if names[-1] != "/":
            names += "/"

        # Now figure out which level we are at
        # by counting slashes
        num_slashes = 0
        for c in names:
            # count the slashes
            # by increasing the level.
            if c == "/":
                num_slashes += 1


        # if we have no slashes
        # we are toplevel
        # and can pretty much exit everything
        if names == "./":
            names = lmh_locate("content", "*", "*")

        # if we have 1 slash
        # we are one level deep
        elif num_slashes == 1:
            names = lmh_locate("content", names, "*")
        else:
            names = lmh_locate("content", names)

        # now expand with the help of globs
        names = glob.glob(names)

        # and check if they exist
        names = list(filter(is_repo, names))

        # if we found something
        # we should through the item
        if len(names) > 0:
            results.update(names)
            return False
        else:
            return True

    # now filter the repos
    repos = list(filter(match_repo_name, repos))

    # repeat again with data_dir as root
    root = lmh_locate("content")
    repos = list(filter(match_repo_name, repos))


    # if we want the relative paths we need to set them properly.
    if not abs:
        return [os.path.relpath(d, lmh_locate("content")) for d in results]

    return list(results)