Changeset 388 for python/vendor/current/Lib/netrc.py
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Lib/netrc.py
r2 r388 3 3 # Module and documentation by Eric S. Raymond, 21 Dec 1998 4 4 5 import os, shlex 5 import os, stat, shlex 6 if os.name == 'posix': 7 import pwd 6 8 7 9 __all__ = ["netrc", "NetrcParseError"] … … 22 24 class netrc: 23 25 def __init__(self, file=None): 26 default_netrc = file is None 24 27 if file is None: 25 28 try: … … 27 30 except KeyError: 28 31 raise IOError("Could not find .netrc: $HOME is not set") 29 fp = open(file)30 32 self.hosts = {} 31 33 self.macros = {} 34 with open(file) as fp: 35 self._parse(file, fp, default_netrc) 36 37 def _parse(self, file, fp, default_netrc): 32 38 lexer = shlex.shlex(fp) 33 39 lexer.wordchars += r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" 40 lexer.commenters = lexer.commenters.replace('#', '') 34 41 while 1: 35 42 # Look for a machine, default, or macdef top-level keyword … … 37 44 if not tt: 38 45 break 46 elif tt[0] == '#': 47 # seek to beginning of comment, in case reading the token put 48 # us on a new line, and then skip the rest of the line. 49 pos = len(tt) + 1 50 lexer.instream.seek(-pos, 1) 51 lexer.instream.readline() 52 continue 39 53 elif tt == 'machine': 40 54 entryname = lexer.get_token() … … 62 76 while 1: 63 77 tt = lexer.get_token() 64 if (tt =='' or tt == 'machine'or65 tt == 'default' or tt =='macdef'):78 if (tt.startswith('#') or 79 tt in {'', 'machine', 'default', 'macdef'}): 66 80 if password: 67 81 self.hosts[entryname] = (login, account, password) … … 78 92 account = lexer.get_token() 79 93 elif tt == 'password': 94 if os.name == 'posix' and default_netrc: 95 prop = os.fstat(fp.fileno()) 96 if prop.st_uid != os.getuid(): 97 try: 98 fowner = pwd.getpwuid(prop.st_uid)[0] 99 except KeyError: 100 fowner = 'uid %s' % prop.st_uid 101 try: 102 user = pwd.getpwuid(os.getuid())[0] 103 except KeyError: 104 user = 'uid %s' % os.getuid() 105 raise NetrcParseError( 106 ("~/.netrc file owner (%s) does not match" 107 " current user (%s)") % (fowner, user), 108 file, lexer.lineno) 109 if (prop.st_mode & (stat.S_IRWXG | stat.S_IRWXO)): 110 raise NetrcParseError( 111 "~/.netrc access too permissive: access" 112 " permissions must restrict access to only" 113 " the owner", file, lexer.lineno) 80 114 password = lexer.get_token() 81 115 else:
Note:
See TracChangeset
for help on using the changeset viewer.