| 1 | # -*- test-case-name: twisted.conch.test.test_cftp -*- |
|---|
| 2 | # Copyright (c) Twisted Matrix Laboratories. |
|---|
| 3 | # See LICENSE for details. |
|---|
| 4 | |
|---|
| 5 | import array |
|---|
| 6 | import stat |
|---|
| 7 | |
|---|
| 8 | from time import time, strftime, localtime |
|---|
| 9 | |
|---|
| 10 | # locale-independent month names to use instead of strftime's |
|---|
| 11 | _MONTH_NAMES = dict(zip( |
|---|
| 12 | range(1, 13), |
|---|
| 13 | "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split())) |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | def lsLine(name, s): |
|---|
| 17 | """ |
|---|
| 18 | Build an 'ls' line for a file ('file' in its generic sense, it |
|---|
| 19 | can be of any type). |
|---|
| 20 | """ |
|---|
| 21 | mode = s.st_mode |
|---|
| 22 | perms = array.array('c', '-'*10) |
|---|
| 23 | ft = stat.S_IFMT(mode) |
|---|
| 24 | if stat.S_ISDIR(ft): perms[0] = 'd' |
|---|
| 25 | elif stat.S_ISCHR(ft): perms[0] = 'c' |
|---|
| 26 | elif stat.S_ISBLK(ft): perms[0] = 'b' |
|---|
| 27 | elif stat.S_ISREG(ft): perms[0] = '-' |
|---|
| 28 | elif stat.S_ISFIFO(ft): perms[0] = 'f' |
|---|
| 29 | elif stat.S_ISLNK(ft): perms[0] = 'l' |
|---|
| 30 | elif stat.S_ISSOCK(ft): perms[0] = 's' |
|---|
| 31 | else: perms[0] = '!' |
|---|
| 32 | # user |
|---|
| 33 | if mode&stat.S_IRUSR:perms[1] = 'r' |
|---|
| 34 | if mode&stat.S_IWUSR:perms[2] = 'w' |
|---|
| 35 | if mode&stat.S_IXUSR:perms[3] = 'x' |
|---|
| 36 | # group |
|---|
| 37 | if mode&stat.S_IRGRP:perms[4] = 'r' |
|---|
| 38 | if mode&stat.S_IWGRP:perms[5] = 'w' |
|---|
| 39 | if mode&stat.S_IXGRP:perms[6] = 'x' |
|---|
| 40 | # other |
|---|
| 41 | if mode&stat.S_IROTH:perms[7] = 'r' |
|---|
| 42 | if mode&stat.S_IWOTH:perms[8] = 'w' |
|---|
| 43 | if mode&stat.S_IXOTH:perms[9] = 'x' |
|---|
| 44 | # suid/sgid |
|---|
| 45 | if mode&stat.S_ISUID: |
|---|
| 46 | if perms[3] == 'x': perms[3] = 's' |
|---|
| 47 | else: perms[3] = 'S' |
|---|
| 48 | if mode&stat.S_ISGID: |
|---|
| 49 | if perms[6] == 'x': perms[6] = 's' |
|---|
| 50 | else: perms[6] = 'S' |
|---|
| 51 | |
|---|
| 52 | lsresult = [ |
|---|
| 53 | perms.tostring(), |
|---|
| 54 | str(s.st_nlink).rjust(5), |
|---|
| 55 | ' ', |
|---|
| 56 | str(s.st_uid).ljust(9), |
|---|
| 57 | str(s.st_gid).ljust(9), |
|---|
| 58 | str(s.st_size).rjust(8), |
|---|
| 59 | ' ', |
|---|
| 60 | ] |
|---|
| 61 | |
|---|
| 62 | # need to specify the month manually, as strftime depends on locale |
|---|
| 63 | ttup = localtime(s.st_mtime) |
|---|
| 64 | sixmonths = 60 * 60 * 24 * 7 * 26 |
|---|
| 65 | if s.st_mtime + sixmonths < time(): # last edited more than 6mo ago |
|---|
| 66 | strtime = strftime("%%s %d %Y ", ttup) |
|---|
| 67 | else: |
|---|
| 68 | strtime = strftime("%%s %d %H:%M ", ttup) |
|---|
| 69 | lsresult.append(strtime % (_MONTH_NAMES[ttup[1]],)) |
|---|
| 70 | |
|---|
| 71 | lsresult.append(name) |
|---|
| 72 | return ''.join(lsresult) |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | __all__ = ['lsLine'] |
|---|