source: publico/il.spdo/trunk/PasteScript-1.7.4.2-py2.6.egg/paste/script/pluginlib.py @ 5327

Última Alteração nesse arquivo desde 5327 foi 5327, incluída por fabianosantos, 8 anos atrás

Import inicial.

File size: 4.2 KB
Linha 
1# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
2# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
3import os
4import pkg_resources
5
6def add_plugin(egg_info_dir, plugin_name):
7    """
8    Add the plugin to the given distribution (or spec), in
9    .egg-info/paster_plugins.txt
10    """
11    fn = os.path.join(egg_info_dir, 'paster_plugins.txt')
12    if not os.path.exists(fn):
13        lines = []
14    else:
15        f = open(fn)
16        lines = [l.strip() for l in f.readlines() if l.strip()]
17        f.close()
18    if plugin_name in lines:
19        # Nothing to do
20        return
21    lines.append(plugin_name)
22    if not os.path.exists(os.path.dirname(fn)):
23        os.makedirs(os.path.dirname(fn))
24    f = open(fn, 'w')
25    for line in lines:
26        f.write(line)
27        f.write('\n')
28    f.close()
29
30def remove_plugin(egg_info_dir, plugin_name):
31    """
32    Remove the plugin to the given distribution (or spec), in
33    .egg-info/paster_plugins.txt.  Raises ValueError if the
34    plugin is not in the file.
35    """
36    fn = os.path.join(egg_info_dir, 'paster_plugins.txt')
37    if not os.path.exists(fn):
38        raise ValueError(
39            "Cannot remove plugin from %s; file does not exist"
40            % fn)
41    f = open(fn)
42    lines = [l.strip() for l in f.readlines() if l.strip()]
43    f.close()
44    for line in lines:
45        # What about version specs?
46        if line.lower() == plugin_name.lower():
47            break
48    else:
49        raise ValueError(
50            "Plugin %s not found in file %s (from: %s)"
51            % (plugin_name, fn, lines))
52    lines.remove(line)
53    print 'writing', lines
54    f = open(fn, 'w')
55    for line in lines:
56        f.write(line)
57        f.write('\n')
58    f.close()
59
60def find_egg_info_dir(dir):
61    while 1:
62        try:
63            filenames = os.listdir(dir)
64        except OSError:
65            # Probably permission denied or something
66            return None
67        for fn in filenames:
68            if (fn.endswith('.egg-info')
69                and os.path.isdir(os.path.join(dir, fn))):
70                return os.path.join(dir, fn)
71        parent = os.path.dirname(dir)
72        if parent == dir:
73            # Top-most directory
74            return None
75        dir = parent
76
77def resolve_plugins(plugin_list):
78    found = []
79    while plugin_list:
80        plugin = plugin_list.pop()
81        try:
82            pkg_resources.require(plugin)
83        except pkg_resources.DistributionNotFound, e:
84            msg = '%sNot Found%s: %s (did you run python setup.py develop?)'
85            if str(e) != plugin:
86                e.args = (msg % (str(e) + ': ', ' for', plugin)),
87            else:
88                e.args = (msg % ('', '', plugin)),
89            raise
90        found.append(plugin)
91        dist = get_distro(plugin)
92        if dist.has_metadata('paster_plugins.txt'):
93            data = dist.get_metadata('paster_plugins.txt')
94            for add_plugin in parse_lines(data):
95                if add_plugin not in found:
96                    plugin_list.append(add_plugin)
97    return map(get_distro, found)
98
99def get_distro(spec):
100    return pkg_resources.get_distribution(spec)
101
102def load_commands_from_plugins(plugins):
103    commands = {}
104    for plugin in plugins:
105        commands.update(pkg_resources.get_entry_map(
106            plugin, group='paste.paster_command'))
107    return commands
108
109def parse_lines(data):
110    result = []
111    for line in data.splitlines():
112        line = line.strip()
113        if line and not line.startswith('#'):
114            result.append(line)
115    return result
116
117def load_global_commands():
118    commands = {}
119    for p in pkg_resources.iter_entry_points('paste.global_paster_command'):
120        commands[p.name] = p
121    return commands
122
123def egg_name(dist_name):
124    return pkg_resources.to_filename(pkg_resources.safe_name(dist_name))
125
126def egg_info_dir(base_dir, dist_name):
127    all = []
128    for dir_extension in ['.'] + os.listdir(base_dir):
129        full = os.path.join(base_dir, dir_extension,
130                            egg_name(dist_name)+'.egg-info')
131        all.append(full)
132        if os.path.exists(full):
133            return full
134    raise IOError("No egg-info directory found (looked in %s)"
135                  % ', '.join(all))
Note: Veja TracBrowser para ajuda no uso do navegador do trac.
 

The contents and data of this website are published under license:
Creative Commons 4.0 Brasil - Atribuir Fonte - Compartilhar Igual.