~ubuntu-branches/ubuntu/trusty/pyinotify/trusty-proposed

« back to all changes in this revision

Viewing changes to src/examples/autopath.py

  • Committer: Bazaar Package Importer
  • Author(s): Hans Ulrich Niedermann
  • Date: 2006-04-02 02:35:31 UTC
  • Revision ID: james.westby@ubuntu.com-20060402023531-3fj27jbd4x9s4752
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# The MIT License
 
2
#
 
3
# Permission is hereby granted, free of charge, to any person 
 
4
# obtaining a copy of this software and associated documentation 
 
5
# files (the "Software"), to deal in the Software without 
 
6
# restriction, including without limitation the rights to use, 
 
7
# copy, modify, merge, publish, distribute, sublicense, and/or 
 
8
# sell copies of the Software, and to permit persons to whom the 
 
9
# Software is furnished to do so, subject to the following conditions:
 
10
#
 
11
# The above copyright notice and this permission notice shall be included 
 
12
# in all copies or substantial portions of the Software.
 
13
#
 
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 
16
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 
17
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 
18
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 
19
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 
20
# DEALINGS IN THE SOFTWARE.
 
21
 
 
22
 
 
23
"""
 
24
-----------
 
25
README:
 
26
 
 
27
This source file is a modified version of autopath.py taken
 
28
from pypy :
 
29
 
 
30
http://codespeak.net/svn/pypy/dist/pypy/tool/autopath.py
 
31
 
 
32
It has just been modified to fit with pyinotify.
 
33
 
 
34
USAGE: in order to use this script, edit it
 
35
- root_dir: set the root directory of your project
 
36
- this_dir: set the location of writable version of this script
 
37
 
 
38
------------
 
39
self cloning, automatic path configuration
 
40
 
 
41
copy this into any subdirectory of pypy from which scripts need
 
42
to be run, typically all of the test subdirs.
 
43
The idea is that any such script simply issues
 
44
 
 
45
    import autopath
 
46
 
 
47
and this will make sure that the parent directory containing 'src'
 
48
is in sys.path.
 
49
 
 
50
If you modify the master version (in src/autopath.py) you can
 
51
directly run it which will copy itself on all autopath.py files
 
52
it finds under the src root directory.
 
53
 
 
54
This module always provides these attributes:
 
55
 
 
56
    root_dir    src root directory path
 
57
    this_dir   directory where this autopath.py resides
 
58
 
 
59
 
 
60
"""
 
61
 
 
62
 
 
63
def __dirinfo(part):
 
64
    """ return (partdir, this_dir) and insert parent of partdir
 
65
    into sys.path.  If the parent directories don't have the part
 
66
    an EnvironmentError is raised."""
 
67
 
 
68
    import sys, os
 
69
    try:
 
70
        head = this_dir = os.path.realpath(os.path.dirname(__file__))
 
71
    except NameError:
 
72
        head = this_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
 
73
 
 
74
    while head:
 
75
        partdir = head
 
76
        head, tail = os.path.split(head)
 
77
        if tail == part:
 
78
            break
 
79
    else:
 
80
        raise EnvironmentError, "'%s' missing in '%r'" % (partdir, this_dir)
 
81
 
 
82
    checkpaths = sys.path[:]
 
83
    d_root = os.path.join(head, '')
 
84
 
 
85
    while checkpaths:
 
86
        orig = checkpaths.pop()
 
87
        fullorig = os.path.join(os.path.realpath(orig), '')
 
88
        if fullorig.startswith(d_root):
 
89
            if os.path.exists(os.path.join(fullorig, '__init__.py')):
 
90
                sys.path.remove(orig)
 
91
    if head not in sys.path:
 
92
        sys.path.insert(0, head)
 
93
 
 
94
    munged = {}
 
95
    for name, mod in sys.modules.items():
 
96
        fn = getattr(mod, '__file__', None)
 
97
        if '.' in name or not isinstance(fn, str):
 
98
            continue
 
99
        newname = os.path.splitext(os.path.basename(fn))[0]
 
100
        if not newname.startswith(part + '.'):
 
101
            continue
 
102
        path = os.path.join(os.path.dirname(os.path.realpath(fn)), '')
 
103
        if path.startswith(d_root) and newname != part:
 
104
            modpaths = os.path.normpath(path[len(d_root):]).split(os.sep)
 
105
            if newname != '__init__':
 
106
                modpaths.append(newname)
 
107
            modpath = '.'.join(modpaths)
 
108
            if modpath not in sys.modules:
 
109
                munged[modpath] = mod
 
110
 
 
111
    for name, mod in munged.iteritems():
 
112
        if name not in sys.modules:
 
113
            sys.modules[name] = mod
 
114
        if '.' in name:
 
115
            prename = name[:name.rfind('.')]
 
116
            postname = name[len(prename)+1:]
 
117
            if prename not in sys.modules:
 
118
                __import__(prename)
 
119
                if not hasattr(sys.modules[prename], postname):
 
120
                    setattr(sys.modules[prename], postname, mod)
 
121
 
 
122
    return partdir, this_dir
 
123
 
 
124
def __clone():
 
125
    """ clone master version of autopath.py into all subdirs """
 
126
    from os.path import join, walk
 
127
    if not this_dir.endswith(join('src','pyinotify')):
 
128
        raise EnvironmentError("can only clone master version "
 
129
                               "'%s'" % join(root_dir, 'pyinotify',_myname))
 
130
 
 
131
 
 
132
    def sync_walker(arg, dirname, fnames):
 
133
        if _myname in fnames:
 
134
            fn = join(dirname, _myname)
 
135
            f = open(fn, 'rwb+')
 
136
            try:
 
137
                if f.read() == arg:
 
138
                    print "checkok", fn
 
139
                else:
 
140
                    print "syncing", fn
 
141
                    f = open(fn, 'w')
 
142
                    f.write(arg)
 
143
            finally:
 
144
                f.close()
 
145
    s = open(join(root_dir, 'pyinotify', _myname), 'rb').read()
 
146
    walk(root_dir, sync_walker, s)
 
147
 
 
148
_myname = 'autopath.py'
 
149
 
 
150
# set guaranteed attributes
 
151
 
 
152
root_dir, this_dir = __dirinfo('src')
 
153
 
 
154
if __name__ == '__main__':
 
155
    __clone()