/usr/share/pyshared/spyderlib/utils/misc.py is in python-spyderlib 2.1.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | # -*- coding: utf-8 -*-
#
# Copyright © 2009-2010 Pierre Raybaut
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)
"""Miscellaneous utilities"""
from __future__ import with_statement
import os
import os.path as osp
import shutil
import sys
def __remove_pyc_pyo(fname):
"""Eventually remove .pyc and .pyo files associated to a Python script"""
if osp.splitext(fname)[1] == '.py':
for ending in ('c', 'o'):
if osp.exists(fname+ending):
os.remove(fname+ending)
def rename_file(source, dest):
"""
Rename file from *source* to *dest*
If file is a Python script, also rename .pyc and .pyo files if any
"""
os.rename(source, dest)
__remove_pyc_pyo(source)
def remove_file(fname):
"""
Remove file *fname*
If file is a Python script, also rename .pyc and .pyo files if any
"""
os.remove(fname)
__remove_pyc_pyo(fname)
def move_file(source, dest):
"""
Move file from *source* to *dest*
If file is a Python script, also rename .pyc and .pyo files if any
"""
shutil.copy(source, dest)
remove_file(source)
def select_port(default_port=20128):
"""Find and return a non used port"""
import socket
while True:
try:
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP)
# sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind( ("127.0.0.1", default_port) )
except socket.error, _msg:
default_port += 1
else:
break
finally:
sock.close()
sock = None
return default_port
def count_lines(path, extensions=None, excluded_dirnames=None):
"""Return number of source code lines for all filenames in subdirectories
of *path* with names ending with *extensions*
Directory names *excluded_dirnames* will be ignored"""
if extensions is None:
extensions = ['.py', '.pyw', '.ipy', '.c', '.h', '.cpp', '.hpp',
'.inc', '.', '.hh', '.hxx', '.cc', '.cxx', '.cl',
'.f', '.for', '.f77', '.f90', '.f95', '.f2k']
if excluded_dirnames is None:
excluded_dirnames = ['build', 'dist', '.hg', '.svn']
def get_filelines(path):
dfiles, dlines = 0, 0
if osp.splitext(path)[1] in extensions:
dfiles = 1
with open(path, 'rb') as textfile:
dlines = len(textfile.read().strip().splitlines())
return dfiles, dlines
lines = 0
files = 0
if osp.isdir(path):
for dirpath, dirnames, filenames in os.walk(path):
for d in dirnames[:]:
if d in excluded_dirnames:
dirnames.remove(d)
if excluded_dirnames is None or \
osp.dirname(dirpath) not in excluded_dirnames:
for fname in filenames:
dfiles, dlines = get_filelines(osp.join(dirpath, fname))
files += dfiles
lines += dlines
else:
dfiles, dlines = get_filelines(path)
files += dfiles
lines += dlines
return files, lines
def fix_reference_name(name, blacklist=None):
"""Return a syntax-valid Python reference name from an arbitrary name"""
import re
name = "".join(re.split(r'[^0-9a-zA-Z_]', name))
while name and not re.match(r'([a-zA-Z]+[0-9a-zA-Z_]*)$', name):
if not re.match(r'[a-zA-Z]', name[0]):
name = name[1:]
continue
name = str(name.lower())
if not name:
name = "data"
if blacklist is not None and name in blacklist:
get_new_name = lambda index: name+('%03d' % index)
index = 0
while get_new_name(index) in blacklist:
index += 1
name = get_new_name(index)
return name
def remove_trailing_single_backslash(text):
"""Remove trailing single backslash in *text*
This is especially useful when formatting path strings on
Windows platforms for which folder paths may end with such
a character"""
if text.endswith('\\') and not text.endswith('\\\\'):
text = text[:-1]
return text
def get_error_match(text):
"""Return error match"""
import re
return re.match(r' File "(.*)", line (\d*)', text)
def get_python_executable():
"""Return path to Python executable"""
executable = sys.executable.replace("pythonw.exe", "python.exe")
if executable.endswith("spyder.exe"):
# py2exe distribution
executable = "python.exe"
return executable
|