Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: netrc.py
"""An object-oriented interface to .netrc files."""
[0] Fix | Delete
[1] Fix | Delete
# Module and documentation by Eric S. Raymond, 21 Dec 1998
[2] Fix | Delete
[3] Fix | Delete
import os, stat, shlex
[4] Fix | Delete
if os.name == 'posix':
[5] Fix | Delete
import pwd
[6] Fix | Delete
[7] Fix | Delete
__all__ = ["netrc", "NetrcParseError"]
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
class NetrcParseError(Exception):
[11] Fix | Delete
"""Exception raised on syntax errors in the .netrc file."""
[12] Fix | Delete
def __init__(self, msg, filename=None, lineno=None):
[13] Fix | Delete
self.filename = filename
[14] Fix | Delete
self.lineno = lineno
[15] Fix | Delete
self.msg = msg
[16] Fix | Delete
Exception.__init__(self, msg)
[17] Fix | Delete
[18] Fix | Delete
def __str__(self):
[19] Fix | Delete
return "%s (%s, line %s)" % (self.msg, self.filename, self.lineno)
[20] Fix | Delete
[21] Fix | Delete
[22] Fix | Delete
class netrc:
[23] Fix | Delete
def __init__(self, file=None):
[24] Fix | Delete
default_netrc = file is None
[25] Fix | Delete
if file is None:
[26] Fix | Delete
try:
[27] Fix | Delete
file = os.path.join(os.environ['HOME'], ".netrc")
[28] Fix | Delete
except KeyError:
[29] Fix | Delete
raise IOError("Could not find .netrc: $HOME is not set")
[30] Fix | Delete
self.hosts = {}
[31] Fix | Delete
self.macros = {}
[32] Fix | Delete
with open(file) as fp:
[33] Fix | Delete
self._parse(file, fp, default_netrc)
[34] Fix | Delete
[35] Fix | Delete
def _parse(self, file, fp, default_netrc):
[36] Fix | Delete
lexer = shlex.shlex(fp)
[37] Fix | Delete
lexer.wordchars += r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
[38] Fix | Delete
lexer.commenters = lexer.commenters.replace('#', '')
[39] Fix | Delete
while 1:
[40] Fix | Delete
# Look for a machine, default, or macdef top-level keyword
[41] Fix | Delete
toplevel = tt = lexer.get_token()
[42] Fix | Delete
if not tt:
[43] Fix | Delete
break
[44] Fix | Delete
elif tt[0] == '#':
[45] Fix | Delete
# seek to beginning of comment, in case reading the token put
[46] Fix | Delete
# us on a new line, and then skip the rest of the line.
[47] Fix | Delete
pos = len(tt) + 1
[48] Fix | Delete
lexer.instream.seek(-pos, 1)
[49] Fix | Delete
lexer.instream.readline()
[50] Fix | Delete
continue
[51] Fix | Delete
elif tt == 'machine':
[52] Fix | Delete
entryname = lexer.get_token()
[53] Fix | Delete
elif tt == 'default':
[54] Fix | Delete
entryname = 'default'
[55] Fix | Delete
elif tt == 'macdef': # Just skip to end of macdefs
[56] Fix | Delete
entryname = lexer.get_token()
[57] Fix | Delete
self.macros[entryname] = []
[58] Fix | Delete
lexer.whitespace = ' \t'
[59] Fix | Delete
while 1:
[60] Fix | Delete
line = lexer.instream.readline()
[61] Fix | Delete
if not line or line == '\012':
[62] Fix | Delete
lexer.whitespace = ' \t\r\n'
[63] Fix | Delete
break
[64] Fix | Delete
self.macros[entryname].append(line)
[65] Fix | Delete
continue
[66] Fix | Delete
else:
[67] Fix | Delete
raise NetrcParseError(
[68] Fix | Delete
"bad toplevel token %r" % tt, file, lexer.lineno)
[69] Fix | Delete
[70] Fix | Delete
# We're looking at start of an entry for a named machine or default.
[71] Fix | Delete
login = ''
[72] Fix | Delete
account = password = None
[73] Fix | Delete
self.hosts[entryname] = {}
[74] Fix | Delete
while 1:
[75] Fix | Delete
tt = lexer.get_token()
[76] Fix | Delete
if (tt.startswith('#') or
[77] Fix | Delete
tt in {'', 'machine', 'default', 'macdef'}):
[78] Fix | Delete
if password:
[79] Fix | Delete
self.hosts[entryname] = (login, account, password)
[80] Fix | Delete
lexer.push_token(tt)
[81] Fix | Delete
break
[82] Fix | Delete
else:
[83] Fix | Delete
raise NetrcParseError(
[84] Fix | Delete
"malformed %s entry %s terminated by %s"
[85] Fix | Delete
% (toplevel, entryname, repr(tt)),
[86] Fix | Delete
file, lexer.lineno)
[87] Fix | Delete
elif tt == 'login' or tt == 'user':
[88] Fix | Delete
login = lexer.get_token()
[89] Fix | Delete
elif tt == 'account':
[90] Fix | Delete
account = lexer.get_token()
[91] Fix | Delete
elif tt == 'password':
[92] Fix | Delete
if os.name == 'posix' and default_netrc:
[93] Fix | Delete
prop = os.fstat(fp.fileno())
[94] Fix | Delete
if prop.st_uid != os.getuid():
[95] Fix | Delete
try:
[96] Fix | Delete
fowner = pwd.getpwuid(prop.st_uid)[0]
[97] Fix | Delete
except KeyError:
[98] Fix | Delete
fowner = 'uid %s' % prop.st_uid
[99] Fix | Delete
try:
[100] Fix | Delete
user = pwd.getpwuid(os.getuid())[0]
[101] Fix | Delete
except KeyError:
[102] Fix | Delete
user = 'uid %s' % os.getuid()
[103] Fix | Delete
raise NetrcParseError(
[104] Fix | Delete
("~/.netrc file owner (%s) does not match"
[105] Fix | Delete
" current user (%s)") % (fowner, user),
[106] Fix | Delete
file, lexer.lineno)
[107] Fix | Delete
if (prop.st_mode & (stat.S_IRWXG | stat.S_IRWXO)):
[108] Fix | Delete
raise NetrcParseError(
[109] Fix | Delete
"~/.netrc access too permissive: access"
[110] Fix | Delete
" permissions must restrict access to only"
[111] Fix | Delete
" the owner", file, lexer.lineno)
[112] Fix | Delete
password = lexer.get_token()
[113] Fix | Delete
else:
[114] Fix | Delete
raise NetrcParseError("bad follower token %r" % tt,
[115] Fix | Delete
file, lexer.lineno)
[116] Fix | Delete
[117] Fix | Delete
def authenticators(self, host):
[118] Fix | Delete
"""Return a (user, account, password) tuple for given host."""
[119] Fix | Delete
if host in self.hosts:
[120] Fix | Delete
return self.hosts[host]
[121] Fix | Delete
elif 'default' in self.hosts:
[122] Fix | Delete
return self.hosts['default']
[123] Fix | Delete
else:
[124] Fix | Delete
return None
[125] Fix | Delete
[126] Fix | Delete
def __repr__(self):
[127] Fix | Delete
"""Dump the class data in the format of a .netrc file."""
[128] Fix | Delete
rep = ""
[129] Fix | Delete
for host in self.hosts.keys():
[130] Fix | Delete
attrs = self.hosts[host]
[131] Fix | Delete
rep += "machine {host}\n\tlogin {attrs[0]}\n".format(host=host, attrs=attrs)
[132] Fix | Delete
if attrs[1]:
[133] Fix | Delete
rep += "\taccount {attrs[1]}\n".format(attrs=attrs)
[134] Fix | Delete
rep += "\tpassword {attrs[2]}\n".format(attrs=attrs)
[135] Fix | Delete
for macro in self.macros.keys():
[136] Fix | Delete
rep += "macdef {macro}\n".format(macro=macro)
[137] Fix | Delete
for line in self.macros[macro]:
[138] Fix | Delete
rep += line
[139] Fix | Delete
rep += "\n"
[140] Fix | Delete
return rep
[141] Fix | Delete
[142] Fix | Delete
if __name__ == '__main__':
[143] Fix | Delete
print netrc()
[144] Fix | Delete
[145] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function