{"id":1157,"date":"2011-06-14T19:16:13","date_gmt":"2011-06-14T10:16:13","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1157"},"modified":"2011-06-14T19:16:13","modified_gmt":"2011-06-14T10:16:13","slug":"pydd%e3%81%ae%e6%94%b9%e8%89%af","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1157","title":{"rendered":"pydd\u306e\u6539\u826f"},"content":{"rendered":"<p>\u4ee5\u524d<a href=\"https:\/\/peta.okechan.net\/blog\/archives\/962\">dd\u30b3\u30de\u30f3\u30c9\u306e\u30e9\u30c3\u30d1\u30fc\u300cpydd\u300d\u3092Python\u3067\u66f8\u3044\u3066\u307f\u305f<\/a>\u3068\u3044\u3046\u8a18\u4e8b\u3092\u66f8\u304d\u307e\u3057\u305f\u304c\uff08\u3046\u308f\u301c\u3082\u3046\u534a\u5e74\u7d4c\u3064\u3093\u3067\u3059\u306d\u301c\uff09\u3001\u5e7e\u3064\u304b\u5c0f\u3055\u3044\u554f\u984c\u70b9\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>1. dd\u3067\u8ee2\u9001\u3055\u308c\u305f\u6700\u7d42\u7684\u306a\u30b5\u30a4\u30ba\u304c\u51fa\u306a\u3044<br \/>\n2. dd\u306e\u51fa\u529b\u30923\u884c\u3068\u6c7a\u3081\u6253\u3061\u3057\u3066\u305f<\/p>\n<p>1\u306b\u3064\u3044\u3066\u306f\u3001dd\u306e\u9032\u6357\u3092\u5f97\u308b\u305f\u3081\u306b0.1\u79d2\u3054\u3068\u306b\u30b7\u30b0\u30ca\u30eb\u3092\u9001\u4fe1\u3057\u3066stderr\u3092\u8aad\u3080\u51e6\u7406\u3092\u884c\u3063\u3066\u307e\u3057\u305f\u304c\u3001\u3053\u306e\u65b9\u6cd5\u3060\u3068\u4e00\u756a\u6700\u5f8c\u306b\u8868\u793a\u3055\u308c\u308b\u8ee2\u9001\u30b5\u30a4\u30ba\u304c\u5b9f\u969b\u306e\u6700\u7d42\u7684\u306a\u30b5\u30a4\u30ba\u3067\u306f\u306a\u304f\u3001\u5b8c\u4e86\u3059\u308b\u3061\u3087\u3063\u3068\u524d\uff08\u5e73\u5747\u3057\u30660.1\/2\u79d2\u524d\uff09\u306e\u9014\u4e2d\u306e\u8ee2\u9001\u30b5\u30a4\u30ba\u3057\u304b\u3068\u308c\u307e\u305b\u3093\u3002<br \/>\n\u3053\u308c\u306f\u6700\u8fd1\u306e\u4e00\u822c\u7684\u306a\u6027\u80fd\u306e\u30de\u30b7\u30f3\u3060\u3068\u6570MB\u301c\u6570\u5341MB\u306e\u305a\u308c\u304c\u751f\u3058\u307e\u3059\u3002<br \/>\ndd\u30b3\u30de\u30f3\u30c9\u306f\u5b8c\u4e86\u6642\u306bstdout\u306b\u7d50\u679c\u3092\u51fa\u529b\u3059\u308b\u306e\u3067\u3001\u51e6\u7406\u306e\u6700\u5f8c\u306b\u305d\u308c&#8221;\u3082&#8221;\u8aad\u3093\u3067\u8868\u793a\u3059\u3079\u304d\u3067\u3059\u3002<\/p>\n<p>2\u306b\u3064\u3044\u3066\u306f\u3001\u975e\u540c\u671f\u306b\u5b9f\u884c\u3055\u308c\u3066\u3044\u308bdd\u304c\u51fa\u529b\u3057\u305fstderr\u3092\u8aad\u307f\u8fbc\u3080\u969b\u306b\u3001\u8aad\u307f\u8fbc\u307f\u3059\u304e\u308b\uff08\u30d0\u30c3\u30d5\u30a1\u306b\u5b58\u5728\u3059\u308b\u30c7\u30fc\u30bf\u3088\u308a\u591a\u304f\u8aad\u307f\u8fbc\u3082\u3046\u3068\u3059\u308b\uff09\u3068\u305d\u3053\u3067\u51e6\u7406\u304c\u30d6\u30ed\u30c3\u30af\u3057\u3066\u3057\u307e\u3044\u3001dd\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u9032\u6357\u306e\u8868\u793a\u304c\u66f4\u65b0\u3055\u308c\u306a\u3044\u3068\u3044\u3046\u554f\u984c\u304c\u8d77\u304d\u308b\u305f\u3081\u306b\u3001dd\u306e\u9032\u6357\u306e\u51fa\u529b\u30923\u884c\u3068\u6c7a\u3081\u6253\u3061\u3057\u3066\u8aad\u307f\u8fbc\u307f\u3059\u304e\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u307e\u3057\u305f\u3002<br \/>\n\u3053\u308c\u3092\u6c7a\u3081\u6253\u3061\u3057\u306a\u304f\u3066\u6e08\u3080\u306a\u3089\u305d\u3046\u3059\u308b\u306b\u8d8a\u3057\u305f\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3053\u308c\u3089\u306e\u554f\u984c\u70b9\u3092\u89e3\u6d88\u3057\u305f\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n\u7d50\u69cb\u524d\u56de\u3068\u69cb\u9020\u304c\u5909\u308f\u3063\u3066\u307e\u3059\u304c\u3001\u7c21\u5358\u306b\u8a00\u3048\u3070\u5909\u66f4\u3057\u305f\u90e8\u5206\u306f<\/p>\n<p>1. dd\u306estdout\u3068stderr\u3092\u6df7\u305c\u3066\u3001\u3055\u3089\u306b\u5b8c\u4e86\u6642\u306e\u51fa\u529b\u3082\u51e6\u7406\u3059\u308b\u3088\u3046\u306b\u3057\u305f<br \/>\n2. \u30ce\u30f3\u30d6\u30ed\u30c3\u30ad\u30f3\u30b0\u3067dd\u306estdout\u3068stderr\u3092\u8aad\u307f\u8fbc\u3080\u3088\u3046\u306b\u3057\u8aad\u307f\u904e\u304e\u3066\u3082\u30d6\u30ed\u30c3\u30af\u3057\u306a\u3044\u3088\u3046\u306b\u3057\u305f<\/p>\n<p>\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3057\u304b\u3057\u554f\u984c\u70b9\u3092\u89e3\u6d88\u3059\u308b\u304b\u308f\u308a\u306b\u3001\u524d\u56de\u306e\u3082\u306e\u3088\u308a\u30bd\u30fc\u30b9\u304c\u6c5a\u304f\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<br \/>\n\u5236\u5fa1\u6587\u304c\u5897\u3048\u305f\u304b\u3089\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u7f8e\u3057\u304f\u7c21\u6f54\u306b\u8868\u73fe\u3059\u308b\u65b9\u6cd5\u306f\u306a\u3044\u3082\u306e\u304b\u2026<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/env python\r\n# -*- coding: utf-8 -*-\r\n\r\n'''\r\ndd command wrapper\r\n'''\r\n\r\nimport sys, os, time, fcntl\r\nfrom subprocess import Popen, PIPE, STDOUT\r\nfrom signal import SIGUSR1, SIGINFO\r\nfrom select import select\r\nimport humansize\r\n\r\ninterval = 0.1\r\n\r\nif 'linux' in os.uname()&#x5B;0].lower():\r\n    SIGPROGRESS = SIGUSR1\r\nelse:\r\n    SIGPROGRESS = SIGINFO\r\n\r\ndef print_progress(lines):\r\n    for line in lines:\r\n        if 'bytes transferred' in line:\r\n            b = int(line.split()&#x5B;0])\r\n            print '\\t', humansize.approximate_size(b, False), '   \\r',\r\n\r\ndef fh2lines(fh):\r\n    lines = &#x5B;]\r\n    while True:\r\n        try:\r\n            line = fh.readline()\r\n            lines.append(line)\r\n        except IOError, e:\r\n            if e.errno == 35: # Resource temporarily unavailable\r\n                break\r\n            else:\r\n                raise\r\n    return lines\r\n\r\ndef set_nonblocking(fh):\r\n    fd = fh.fileno()\r\n    fl = fcntl.fcntl(fd, fcntl.F_GETFL)\r\n    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)\r\n\r\nif __name__ == '__main__':\r\n    dd = Popen(&#x5B;'dd'] + sys.argv&#x5B;1:], stdout = PIPE, stderr = STDOUT)\r\n    set_nonblocking(dd.stdout)\r\n    while not dd.poll():\r\n        lines = &#x5B;]\r\n        try:\r\n            dd.send_signal(SIGPROGRESS)\r\n            info = select((dd.stdout, ), (), (), 0)&#x5B;0]\r\n            if info:\r\n                lines = fh2lines(info&#x5B;0])\r\n            print_progress(lines)\r\n            time.sleep(interval)\r\n        except OSError, e:\r\n            if e.errno == 3: # No such process\r\n                out, _ = dd.communicate()\r\n                print_progress(out.split('\\n'))\r\n                break\r\n            else:\r\n                raise\r\n    print\r\n    sys.exit(dd.returncode)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u524d<a href=\"https:\/\/peta.okechan.net\/blog\/archives\/962\">dd\u30b3\u30de\u30f3\u30c9\u306e\u30e9\u30c3\u30d1\u30fc\u300cpydd\u300d\u3092Python\u3067\u66f8\u3044\u3066\u307f\u305f<\/a>\u3068\u3044\u3046\u8a18\u4e8b\u3092\u66f8\u304d\u307e\u3057\u305f\u304c\uff08\u3046\u308f\u301c\u3082\u3046\u534a\u5e74\u7d4c\u3064\u3093\u3067\u3059\u306d\u301c\uff09\u3001\u5e7e\u3064\u304b\u5c0f\u3055\u3044\u554f\u984c\u70b9\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>1. dd\u3067\u8ee2\u9001\u3055\u308c\u305f\u6700\u7d42\u7684\u306a\u30b5\u30a4\u30ba\u304c\u51fa\u306a\u3044<br \/>\n2. dd\u306e\u51fa\u529b\u30923\u884c\u3068\u6c7a\u3081\u6253\u3061\u3057\u3066\u305f<\/p>\n<p>1\u306b\u3064\u3044\u3066\u306f\u3001dd\u306e\u9032\u6357\u3092\u5f97\u308b\u305f\u3081\u306b0.1\u79d2\u3054\u3068\u306b\u30b7\u30b0\u30ca\u30eb\u3092\u9001\u4fe1\u3057\u3066stderr\u3092\u8aad\u3080\u51e6\u7406\u3092\u884c\u3063\u3066\u307e\u3057\u305f\u304c\u3001\u3053\u306e\u65b9\u6cd5\u3060\u3068\u4e00\u756a\u6700\u5f8c\u306b\u8868\u793a\u3055\u308c\u308b\u8ee2\u9001\u30b5\u30a4\u30ba\u304c\u5b9f\u969b\u306e\u6700\u7d42\u7684\u306a\u30b5\u30a4\u30ba\u3067\u306f\u306a\u304f\u3001\u5b8c\u4e86\u3059\u308b\u3061\u3087\u3063\u3068\u524d\uff08\u5e73\u5747\u3057\u30660.1\/2\u79d2\u524d\uff09\u306e\u9014\u4e2d\u306e\u8ee2\u9001\u30b5\u30a4\u30ba\u3057\u304b\u3068\u308c\u307e\u305b\u3093\u3002<br \/>\n\u3053\u308c\u306f\u6700\u8fd1\u306e\u4e00\u822c\u7684\u306a\u6027\u80fd\u306e\u30de\u30b7\u30f3\u3060\u3068\u6570MB\u301c\u6570\u5341MB\u306e\u305a\u308c\u304c\u751f\u3058\u307e\u3059\u3002<br \/>\ndd\u30b3\u30de\u30f3\u30c9\u306f\u5b8c\u4e86\u6642\u306bstdout\u306b\u7d50\u679c\u3092\u51fa\u529b\u3059\u308b\u306e\u3067\u3001\u51e6\u7406\u306e\u6700\u5f8c\u306b\u305d\u308c&#8221;\u3082&#8221;\u8aad\u3093\u3067\u8868\u793a\u3059\u3079\u304d\u3067\u3059\u3002<\/p>\n<p>2\u306b\u3064\u3044\u3066\u306f\u3001\u975e\u540c\u671f\u306b\u5b9f\u884c\u3055\u308c\u3066\u3044\u308bdd\u304c\u51fa\u529b\u3057\u305fstderr\u3092\u8aad\u307f\u8fbc\u3080\u969b\u306b\u3001\u8aad\u307f\u8fbc\u307f\u3059\u304e\u308b\uff08\u30d0\u30c3\u30d5\u30a1\u306b\u5b58\u5728\u3059\u308b\u30c7\u30fc\u30bf\u3088\u308a\u591a\u304f\u8aad\u307f\u8fbc\u3082\u3046\u3068\u3059\u308b\uff09\u3068\u305d\u3053\u3067\u51e6\u7406\u304c\u30d6\u30ed\u30c3\u30af\u3057\u3066\u3057\u307e\u3044\u3001dd\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u9032\u6357\u306e\u8868\u793a\u304c\u66f4\u65b0\u3055\u308c\u306a\u3044\u3068\u3044\u3046\u554f\u984c\u304c\u8d77\u304d\u308b\u305f\u3081\u306b\u3001dd\u306e\u9032\u6357\u306e\u51fa\u529b\u30923\u884c\u3068\u6c7a\u3081\u6253\u3061\u3057\u3066\u8aad\u307f\u8fbc\u307f\u3059\u304e\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u307e\u3057\u305f\u3002<br \/>\n\u3053\u308c\u3092\u6c7a\u3081\u6253\u3061\u3057\u306a\u304f\u3066\u6e08\u3080\u306a\u3089\u305d\u3046\u3059\u308b\u306b\u8d8a\u3057\u305f\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3053\u308c\u3089\u306e\u554f\u984c\u70b9\u3092\u89e3\u6d88\u3057\u305f\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n\u7d50\u69cb\u524d\u56de\u3068\u69cb\u9020\u304c\u5909\u308f\u3063\u3066\u307e\u3059\u304c\u3001\u7c21\u5358\u306b\u8a00\u3048\u3070\u5909\u66f4\u3057\u305f\u90e8\u5206\u306f<\/p>\n<p>1. dd\u306estdout\u3068stderr\u3092\u6df7\u305c\u3066\u3001\u3055\u3089\u306b\u5b8c\u4e86\u6642\u306e\u51fa\u529b\u3082\u51e6\u7406\u3059\u308b\u3088\u3046\u306b\u3057\u305f<br \/>\n2. \u30ce\u30f3\u30d6\u30ed\u30c3\u30ad\u30f3\u30b0\u3067dd\u306estdout\u3068stderr\u3092\u8aad\u307f\u8fbc\u3080\u3088\u3046\u306b\u3057\u8aad\u307f\u904e\u304e\u3066\u3082\u30d6\u30ed\u30c3\u30af\u3057\u306a\u3044\u3088\u3046\u306b\u3057\u305f<\/p>\n<p>\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3057\u304b\u3057\u554f\u984c\u70b9\u3092\u89e3\u6d88\u3059\u308b\u304b\u308f\u308a\u306b\u3001\u524d\u56de\u306e\u3082\u306e\u3088\u308a\u30bd\u30fc\u30b9\u304c\u6c5a\u304f\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<br \/>\n\u5236\u5fa1\u6587\u304c\u5897\u3048\u305f\u304b\u3089\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u7f8e\u3057\u304f\u7c21\u6f54\u306b\u8868\u73fe\u3059\u308b\u65b9\u6cd5\u306f\u306a\u3044\u3082\u306e\u304b\u2026<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/env python\r\n# -*- coding: utf-8 -*-\r\n\r\n'''\r\ndd command wrapper\r\n'''\r\n\r\nimport sys, os, time, fcntl\r\nfrom subprocess import Popen, PIPE, STDOUT\r\nfrom signal import SIGUSR1, SIGINFO\r\nfrom select import select\r\nimport humansize\r\n\r\ninterval = 0.1\r\n\r\nif 'linux' in os.uname()&#x5B;0].lower():\r\n    SIGPROGRESS = SIGUSR1\r\nelse:\r\n    SIGPROGRESS = SIGINFO\r\n\r\ndef print_progress(lines):\r\n    for line in lines:\r\n        if 'bytes transferred' in line:\r\n            b = int(line.split()&#x5B;0])\r\n            print '\\t', humansize.approximate_size(b, False), '   \\r',\r\n\r\ndef fh2lines(fh):\r\n    lines = &#x5B;]\r\n    while True:\r\n        try:\r\n            line = fh.readline()\r\n            lines.append(line)\r\n        except IOError, e:\r\n            if e.errno == 35: # Resource temporarily unavailable\r\n                break\r\n            else:\r\n                raise\r\n    return lines\r\n\r\ndef set_nonblocking(fh):\r\n    fd = fh.fileno()\r\n    fl = fcntl.fcntl(fd, fcntl.F_GETFL)\r\n    fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)\r\n\r\nif __name__ == '__main__':\r\n    dd = Popen(&#x5B;'dd'] + sys.argv&#x5B;1:], stdout = PIPE, stderr = STDOUT)\r\n    set_nonblocking(dd.stdout)\r\n    while not dd.poll():\r\n        lines = &#x5B;]\r\n        try:\r\n            dd.send_signal(SIGPROGRESS)\r\n            info = select((dd.stdout, ), (), (), 0)&#x5B;0]\r\n            if info:\r\n                lines = fh2lines(info&#x5B;0])\r\n            print_progress(lines)\r\n            time.sleep(interval)\r\n        except OSError, e:\r\n            if e.errno == 3: # No such process\r\n                out, _ = dd.communicate()\r\n                print_progress(out.split('\\n'))\r\n                break\r\n            else:\r\n                raise\r\n    print\r\n    sys.exit(dd.returncode)\r\n<\/pre>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[388,60],"class_list":["post-1157","post","type-post","status-publish","format-standard","hentry","category-tech","tag-dd","tag-python"],"_links":{"self":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/comments?post=1157"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1157\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}