| 1 |
"""Tools for helping with Subversion.""" |
|---|
| 2 |
|
|---|
| 3 |
import glob |
|---|
| 4 |
import os |
|---|
| 5 |
import sys |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
def _sitepackages(): |
|---|
| 9 |
"""Return absolute site-packages/site-python.""" |
|---|
| 10 |
if sys.platform == 'darwin' and 'Python.framework' in sys.prefix: |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
home = os.environ.get('HOME') |
|---|
| 15 |
if home: |
|---|
| 16 |
return os.path.join(home, 'Library', 'Python', |
|---|
| 17 |
sys.version[:3], 'site-packages') |
|---|
| 18 |
|
|---|
| 19 |
if sys.platform in ('os2emx', 'riscos'): |
|---|
| 20 |
return os.path.join(sys.prefix, "Lib", "site-packages") |
|---|
| 21 |
elif os.sep == '/': |
|---|
| 22 |
return os.path.join(sys.prefix, "lib", "python" + sys.version[:3], |
|---|
| 23 |
"site-packages") |
|---|
| 24 |
else: |
|---|
| 25 |
return os.path.join(sys.prefix, "lib", "site-packages") |
|---|
| 26 |
return None |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
def native_eol(path, linesep=None, pattern="*.py"): |
|---|
| 30 |
"""Assert native EOL in the given file (or all matches in the given package). |
|---|
| 31 |
|
|---|
| 32 |
Uses os.linesep for the new output. |
|---|
| 33 |
""" |
|---|
| 34 |
|
|---|
| 35 |
if not os.path.isabs(path): |
|---|
| 36 |
path = os.path.join(_sitepackages(), path) |
|---|
| 37 |
|
|---|
| 38 |
if os.path.isdir(path): |
|---|
| 39 |
paths = [] |
|---|
| 40 |
pat = os.path.join(path, pattern) |
|---|
| 41 |
paths.extend(glob.glob(pat)) |
|---|
| 42 |
for root, dirs, files in os.walk(path): |
|---|
| 43 |
for d in dirs: |
|---|
| 44 |
pat = os.path.join(root, d, pattern) |
|---|
| 45 |
paths.extend(glob.glob(pat)) |
|---|
| 46 |
else: |
|---|
| 47 |
paths = [path] |
|---|
| 48 |
|
|---|
| 49 |
for path in paths: |
|---|
| 50 |
print path |
|---|
| 51 |
data = open(path, 'rb').read() |
|---|
| 52 |
|
|---|
| 53 |
if linesep is None: |
|---|
| 54 |
linesep = os.linesep |
|---|
| 55 |
|
|---|
| 56 |
if linesep == '\r': |
|---|
| 57 |
data = data.replace('\r\n', '\r').replace('\n', '\r') |
|---|
| 58 |
elif linesep == '\n': |
|---|
| 59 |
data = data.replace('\r\n', '\n').replace('\r', '\n') |
|---|
| 60 |
elif linesep == '\r\n': |
|---|
| 61 |
data = data.replace('\r\n', '\n').replace('\r', '\n').replace('\n', '\r\n') |
|---|
| 62 |
else: |
|---|
| 63 |
raise ValueError("Unknown linesep %r" % linesep) |
|---|
| 64 |
|
|---|
| 65 |
open(path, 'wb').write(data) |
|---|
| 66 |
|
|---|