Skip to content

Commit 3e2bc34

Browse files
author
hartsantler
committed
fixed nodejs package "python-js"
1 parent de8b098 commit 3e2bc34

File tree

9 files changed

+1968
-9
lines changed

9 files changed

+1968
-9
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""
2+
Path operations common to more than one OS
3+
Do not use directly. The OS specific modules import the appropriate
4+
functions from this module themselves.
5+
"""
6+
import os
7+
import stat
8+
9+
__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
10+
'getsize', 'isdir', 'isfile']
11+
12+
13+
# Does a path exist?
14+
# This is false for dangling symbolic links on systems that support them.
15+
def exists(path):
16+
"""Test whether a path exists. Returns False for broken symbolic links"""
17+
try:
18+
os.stat(path)
19+
except os.error:
20+
return False
21+
return True
22+
23+
24+
# This follows symbolic links, so both islink() and isdir() can be true
25+
# for the same path ono systems that support symlinks
26+
def isfile(path):
27+
"""Test whether a path is a regular file"""
28+
try:
29+
st = os.stat(path)
30+
except os.error:
31+
return False
32+
return stat.S_ISREG(st.st_mode)
33+
34+
35+
# Is a path a directory?
36+
# This follows symbolic links, so both islink() and isdir()
37+
# can be true for the same path on systems that support symlinks
38+
def isdir(s):
39+
"""Return true if the pathname refers to an existing directory."""
40+
try:
41+
st = os.stat(s)
42+
except os.error:
43+
return False
44+
return stat.S_ISDIR(st.st_mode)
45+
46+
47+
def getsize(filename):
48+
"""Return the size of a file, reported by os.stat()."""
49+
return os.stat(filename).st_size
50+
51+
52+
def getmtime(filename):
53+
"""Return the last modification time of a file, reported by os.stat()."""
54+
return os.stat(filename).st_mtime
55+
56+
57+
def getatime(filename):
58+
"""Return the last access time of a file, reported by os.stat()."""
59+
return os.stat(filename).st_atime
60+
61+
62+
def getctime(filename):
63+
"""Return the metadata change time of a file, reported by os.stat()."""
64+
return os.stat(filename).st_ctime
65+
66+
67+
# Return the longest prefix of all list elements.
68+
def commonprefix(m):
69+
"Given a list of pathnames, returns the longest common leading component"
70+
if not m: return ''
71+
s1 = min(m)
72+
s2 = max(m)
73+
for i, c in enumerate(s1):
74+
if c != s2[i]:
75+
return s1[:i]
76+
return s1
77+
78+
# Split a path in root and extension.
79+
# The extension is everything starting at the last dot in the last
80+
# pathname component; the root is everything before that.
81+
# It is always true that root + ext == p.
82+
83+
# Generic implementation of splitext, to be parametrized with
84+
# the separators
85+
def _splitext(p, sep, altsep, extsep):
86+
"""Split the extension from a pathname.
87+
88+
Extension is everything from the last dot to the end, ignoring
89+
leading dots. Returns "(root, ext)"; ext may be empty."""
90+
91+
sepIndex = p.rfind(sep)
92+
if altsep:
93+
altsepIndex = p.rfind(altsep)
94+
sepIndex = max(sepIndex, altsepIndex)
95+
96+
dotIndex = p.rfind(extsep)
97+
if dotIndex > sepIndex:
98+
# skip all leading dots
99+
filenameIndex = sepIndex + 1
100+
while filenameIndex < dotIndex:
101+
if p[filenameIndex] != extsep:
102+
return p[:dotIndex], p[dotIndex:]
103+
filenameIndex += 1
104+
105+
return p, ''
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
"""Cache lines from files.
2+
3+
This is intended to read lines from modules imported -- hence if a filename
4+
is not found, it will look down the module search path for a file by
5+
that name.
6+
"""
7+
8+
import sys
9+
import os
10+
11+
__all__ = ["getline", "clearcache", "checkcache"]
12+
13+
def getline(filename, lineno, module_globals=None):
14+
lines = getlines(filename, module_globals)
15+
if 1 <= lineno <= len(lines):
16+
return lines[lineno-1]
17+
else:
18+
return ''
19+
20+
21+
# The cache
22+
23+
cache = {} # The cache
24+
25+
26+
def clearcache():
27+
"""Clear the cache entirely."""
28+
29+
global cache
30+
cache = {}
31+
32+
33+
def getlines(filename, module_globals=None):
34+
"""Get the lines for a file from the cache.
35+
Update the cache if it doesn't contain an entry for this file already."""
36+
37+
if filename in cache:
38+
return cache[filename][2]
39+
else:
40+
return updatecache(filename, module_globals)
41+
42+
43+
def checkcache(filename=None):
44+
"""Discard cache entries that are out of date.
45+
(This is not checked upon each call!)"""
46+
47+
if filename is None:
48+
filenames = cache.keys()
49+
else:
50+
if filename in cache:
51+
filenames = [filename]
52+
else:
53+
return
54+
55+
for filename in filenames:
56+
size, mtime, lines, fullname = cache[filename]
57+
if mtime is None:
58+
continue # no-op for files loaded via a __loader__
59+
try:
60+
stat = os.stat(fullname)
61+
except os.error:
62+
del cache[filename]
63+
continue
64+
if size != stat.st_size or mtime != stat.st_mtime:
65+
del cache[filename]
66+
67+
68+
def updatecache(filename, module_globals=None):
69+
"""Update a cache entry and return its list of lines.
70+
If something's wrong, print a message, discard the cache entry,
71+
and return an empty list."""
72+
73+
if filename in cache:
74+
del cache[filename]
75+
if not filename or (filename.startswith('<') and filename.endswith('>')):
76+
return []
77+
78+
fullname = filename
79+
try:
80+
stat = os.stat(fullname)
81+
except OSError:
82+
basename = filename
83+
84+
# Try for a __loader__, if available
85+
if module_globals and '__loader__' in module_globals:
86+
name = module_globals.get('__name__')
87+
loader = module_globals['__loader__']
88+
get_source = getattr(loader, 'get_source', None)
89+
90+
if name and get_source:
91+
try:
92+
data = get_source(name)
93+
except (ImportError, IOError):
94+
pass
95+
else:
96+
if data is None:
97+
# No luck, the PEP302 loader cannot find the source
98+
# for this module.
99+
return []
100+
cache[filename] = (
101+
len(data), None,
102+
[line+'\n' for line in data.splitlines()], fullname
103+
)
104+
return cache[filename][2]
105+
106+
# Try looking through the module search path, which is only useful
107+
# when handling a relative filename.
108+
if os.path.isabs(filename):
109+
return []
110+
111+
for dirname in sys.path:
112+
# When using imputil, sys.path may contain things other than
113+
# strings; ignore them when it happens.
114+
try:
115+
fullname = os.path.join(dirname, basename)
116+
except (TypeError, AttributeError):
117+
# Not sufficiently string-like to do anything useful with.
118+
continue
119+
try:
120+
stat = os.stat(fullname)
121+
break
122+
except os.error:
123+
pass
124+
else:
125+
return []
126+
try:
127+
with open(fullname, 'rU') as fp:
128+
lines = fp.readlines()
129+
except IOError:
130+
return []
131+
if lines and not lines[-1].endswith('\n'):
132+
lines[-1] += '\n'
133+
size, mtime = stat.st_size, stat.st_mtime
134+
cache[filename] = size, mtime, lines, fullname
135+
return lines

0 commit comments

Comments
 (0)