annotate bin/uptime @ 12137:db60ce0f0bb3 draft

<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
author HackEso <hackeso@esolangs.org>
date Sun, 17 Nov 2019 19:16:20 +0000
parents
children c2f4b787425f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12137
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
1 #!python3
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
2 boottime = 1245511822
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
3 import sys, time
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
4 helpmsg = "\nUsage:\n uptime [options]\n\nOptions:\n -p, --pretty show uptime in pretty format\n -h, --help display this help and exit\n -s, --since system up since\n -V, --version output version information and exit\n\nFor more details see uptime(1)."
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
5 opt_p, opt_s = 0, 0
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
6 for a in sys.argv[1:]:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
7 if "--" == a:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
8 break
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
9 if "-" == a[0]:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
10 for c in a[1:]:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
11 if "p" == c:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
12 opt_p = 1
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
13 elif "s" == c:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
14 opt_s = 1
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
15 elif "V" == c:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
16 print("uptime from procps-ng 3.3.15")
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
17 sys.exit(0)
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
18 elif "h" == c:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
19 print(helpmsg, file = sys.stdout)
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
20 sys.exit(0)
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
21 else:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
22 print("uptime: invalid option -- '" + c + "'\n" + helpmsg, file = sys.stderr)
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
23 sys.exit(0)
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
24 if opt_s:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
25 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(boottime)))
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
26 else:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
27 now = time.time()
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
28 uptime_s = now - boottime
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
29 uptime_d, uptime_md = divmod(uptime_s, 60*60*24)
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
30 uptime_h, uptime_mh = divmod(uptime_md, 60*60)
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
31 uptime_m = uptime_mh // 60
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
32 if opt_p:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
33 print("up %d day, %d hours, %d minutes" %
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
34 (uptime_d, uptime_h, uptime_m))
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
35 else:
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
36 nowfmt = time.strftime("%H:%M:%S", time.localtime(now))
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
37 loadavg = ", ".join(open("/proc/loadavg").read().split()[0:3])
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
38 print(" %s up %d day, %2d:%02d, 0 users, load average: %s" %
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
39 (nowfmt, uptime_d, uptime_h, uptime_m, loadavg))
db60ce0f0bb3 <b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
HackEso <hackeso@esolangs.org>
parents:
diff changeset
40