ããã£ã¬ã¯ããªãå帰çã«ã³ãã¼ããã«ã¯ãã«ããã¦ã2ã¤ã®ãã£ã¬ã¯ããªã¼ã®å 容ãã ãããä¸è´ãããæ¹æ³ã示ãã¾ãããã§ãããã®æ¹æ³ã¯åãªãã³ãã¼ã§ãå®å ¨ãªåæãåããã®ã§ã¯ããã¾ãããorigin/ ã®å 容ã target/ ã«ã³ãã¼ããã¨ããç¶æ³ã¯æ¬¡ã®ããã«ãªãã¾ãã
- origin/ ã®é ä¸ã«ãããã¡ã¤ã«ã¯å ¨ã¦ target/ ã«ããã*1ï¼ãã¡ã¤ã«ã®ä¸èº«ãåãï¼ã
- target/ ã®é ä¸ã«ã¯ããããorigin/ å´ã«ã¯åå¨ããªããã¡ã¤ã«ãããã
åæï¼ãã©ã¼ãªã³ã°ï¼ããã¨ãã¯ããtarget/ ã®é ä¸ã«ã¯ããããorigin/ å´ã«ã¯åå¨ããªããã¡ã¤ã«ãã¯åé¤ãã¾ããåã®å½é¢ã®ç¨éã§ã¯ãåé¤ããã®ã¯ãã¡ã§ãï¼ã¨ãã§ããªããã¨ã§ãï¼ãã§ããããtarget/ ã®é ä¸ã«ã¯ããããorigin/ å´ã«ã¯åå¨ããªããã¡ã¤ã«ããã©ã®ããããããã¯æ°ã«ãªãã¨ããã§ããããã§ã次ã®ãããªæ©è½ãæã¤ã³ãã³ããæ¢ãã¦ã¿ã¾ããã
- dir1/ 㨠dir2/ ãå帰çã«æ¯è¼ãã¦ãdir2/ ã«ã¯ããããdir1/ ã«ã¯ãªããã¡ã¤ã«ããªã¹ãããã
dircmp, diff, rsync ãªã©ã«ãã®æ©è½ãå«ã¾ãã¦ãããã§ãããmanãã¼ã¸èªãã ãããã®ãåå«ï¼ãªã¤ãï¼ãã·ã§ã«ã¹ã¯ãªããã§ä½ã£ã¦ãã¾ããããã¨ã
ããå¾ ã¦ããã·ã§ã«ã§è¦å´ãããªãã¹ã¯ãªããè¨èªã®ã»ããããããªããLinux/Unixã³ãã³ãã©ã¤ã³ã§ã¡ãã£ã¨ããäºãããã«ã¯ãã§ã¯æ¬¡ã®ãããªååµåã£ã¦ãããã©ã
ãã¹ã¯ãªããè¨èªï¼ä¾ãã°perlï¼ã使ãã°ããããããããã¨ãããæè¦ã»ãææã¯ã´ã¢ããã¢ã ã¨æãã¾ãããä»åã¯èãè³æã¡ã¾ããã
ã´ã¢ããã¢ãªãæè¦ãèãå ¥ãããã¨ã«ãã¾ãããPythonã使ã£ã¦ã¿ã -- ã¨ããããJSONãã¼ã¿ãèªã¿è¾¼ãã以æ¥ãPythonããã¯ã«è§¦ã£ã¦ãªãã®ã§ãPython使ã£ã¦ã¿ãããã¨ã
ã¡ãã£ã¨èª¿ã¹ãããfilecmpã¢ã¸ã¥ã¼ã«ã®dircmpã¯ã©ã¹ã使ãã°ã¨ã£ã¦ããæ軽ã«ã§ãããã
- http://www.python.jp/doc/2.5/lib/module-filecmp.html
- http://www.python.jp/doc/2.5/lib/dircmp-objects.html
ããããå ã¯å¿ è¦ãªç¥èãç®æ¡æ¸ãã§ãã£ããã£ã¨æ¸ãé£ãã¾ãã
ãã¡ã¤ã«ã·ã¹ãã ã¨ãã¹ã«ã¤ãã¦ç¥ã£ã¦ããã¹ããã¨ï¼
- os.listdir(path), os.getcwd(), os.chdir(path)ãããã¯ãPythonã使ã£ã¦ã¿ã -- ã¨ããããJSONãã¼ã¿ãèªã¿è¾¼ããã«ãæ¸ããã
- ãã¹ã®åå¨ã¯ os.path.exits(path)ã
- ãã¹ããã£ã¬ã¯ããªã§ãããã©ãã㯠os.path.isdir(path)ã
次ã«dircmpã®ä½¿ãæ¹ãleftã¨rightããã£ã¬ã¯ããªã¼ã®ãã¹ã ã¨ãã¦ãdcmp = filecmp.dircmp(left, right) ã¨ããã¨ï¼
- dcmp.right_only ã§rightã«ã ãå«ã¾ãããã¡ã¤ã«ï¼ãã£ã¬ã¯ããªã¼ã®ä¸è¦§ãåããã
- ãã¡ã¤ã«ï¼ãã£ã¬ã¯ããªã¼ã®ä¸è¦§ã¯æååã®ãªã¹ãã
- dcmp.subdirs ã¯è¾æ¸ã§ãleftã¨rightã«å ±éãªãµããã£ã¬ã¯ããªã«å¯¾ããdircmpãªãã¸ã§ã¯ããéãããã¦ããã
- dcmp.subdirs ã®ãã¼ã¯ããµããã£ã¬ã¯ããªã®ååã
- è¾æ¸ã®ãã¼ã®ä¸¦ã³ã¯kyes()ã¡ã½ããï¼é¢æ°ãããªãï¼ã§åãåºããã
åã«ã¨ã£ã¦ãããã°ã©ãã³ã°è¨èªã®æ©è½ã§ã¨ãããã欲ãã*2ã®ã¯ãmapé¢æ°ãã©ã ãè¨æ³ãä¸é å¼ããªã¹ãå å 表è¨ãã
- mapé¢æ°ã¯ãmap(ï¼functionï¼, ï¼sequenceï¼) ã
- lambdaè¨æ³ã¯ãlambda ï¼å¤æ°ä¸¦ã³ï¼: ï¼å¼ï¼ ã
- ä¸é å¼ã¯ãï¼then_valueï¼ if ï¼predicateï¼ else ï¼else_valueï¼ ã
- ãªã¹ãå å 表è¨ã¯ã[ï¼å¼ï¼ for ï¼å¤æ°ï¼ in ï¼åï¼ if ï¼æ¡ä»¶ï¼]
ãªã¹ãå å 表è¨ã¯æçµçã«ã¯ä½¿ãã¾ããã§ããããéä¸ã§ã試ãã®ã¨ãã¯ã[f + "/" for f in dcmp.right_only if os.path.isdir(f)] ã¨ãæ¸ãã¦ã¾ãããPythonã¯è¨å·ãå°ãªãã¦ã綴ããæã¤èªãå¤ç¨ãã¾ããããªã¹ãå å 表è¨ã§ã¯ããã£ã¦èªã¿ã«ããæãã§ããã[f + "/" <- f in dcmp.righ_only | os.path.isdir(f)] ã¨ããé©åº¦ã«è¨å·ãæ··ãã£ã¦ããã»ããè¦èªæ§ãããã¨æãã¾ãã
ãã§ã課é¡ã®ããã°ã©ã ã¯ãããªæããæ¬è³ªçãªä½æ¥ã¯dircmpãªãã¸ã§ã¯ãããã£ã¦ãããã®ã§ããã£ãããã ãã§ãã
# -*- coding: utf-8 -*- # rightonly.py import os import filecmp SEP = os.path.sep; def report_right_only(left, right): dcmp = filecmp.dircmp(left, right) _report_right_only(right, dcmp) def _report_right_only(right, dcmp): # ééããå«ã¾ããã®ã§ãããä¸ã®è¿½è¨ãè¦ã¦ãã ãã for p in map(lambda f: right + SEP + f + SEP \ if os.path.isdir(f) else right + SEP + f, dcmp.right_only): print p for k in dcmp.subdirs.keys(): new_right = right + SEP + k new_dcmp = dcmp.subdirs.get(k) _report_right_only(new_right, new_dcmp)
[追è¨]
使ã£ã¦ãã¦æ°ãã¤ããã®ã§ããããã£ã¬ã¯ããªã®ãå°»ã«ã¯ã¹ã©ãã·ã¥ï¼ã¾ãã¯ããã¯ã¹ã©ãã·ã¥ï¼ãä»ãã¯ããä»ãã¦ã¾ããã§ããã次ã®lambdaå¼ã®é¨åãééã£ã¦ãã¾ããã
lambda f: right + SEP + f + SEP \ if os.path.isdir(f) # ãããééã else right + SEP + f
ã¾ããæååã®é£çµã®ä»£ããã«os.path.joinã使ã£ã¦è¦ããããã¦ããã¾ãã
lambda f: os.path.join(right, f) + os.path.sep \ if os.path.isdir(f) # ãããééã else os.path.join(right, f)
ããããééããã®è¡ã®fã os.path.join(right, f) ã§ããã
lambda f: os.path.join(right, f) + os.path.sep \ if os.path.isdir(os.path.join(right, f)) \ else os.path.join(right, f)
ãããªãã¨ãos.path.join(right, f) ãä½åº¦ãç»å ´ãã¦ãããã«ä¸é æ¼ç®åããã«ãã«ãããªãã®ã§ãä»£å ¥ã使ã£ãæç¶ãçé¢æ°ãå¥ã«å®ç¾©ãã¦ãããã»ããè¯ãããã§ãã
def make_path(parent, name): path = os.path.join(parent, name) if os.path.isdir(path): return path + os.path.sep else: return path
çµå±ã_report_right_only ã次ã®ããã«ãã¾ããã
def _report_right_only(right, dcmp): def make_path(parent, name): path = os.path.join(parent, name) if os.path.isdir(path): return path + os.path.sep else: return path for p in map(lambda name: make_path(right, name), dcmp.right_only): print p for k in dcmp.subdirs.keys(): new_right = right + SEP + k new_dcmp = dcmp.subdirs.get(k) _report_right_only(new_right, new_dcmp)
[/追è¨]
rightonly.pyãã³ãã³ãã¨ãã¦ä½¿ãã¨ãã®ãç´æããããããªã®ã§æ¸ã足ãã¾ããããç´æé¨åã®ã»ããé·ããããªã
if __name__ == "__main__": import sys prog = os.path.basename(sys.argv[0]) def usage(): print >>sys.stderr, \ "usage: " + prog + " <left (source dir)> <right (target dir)>" sys.exit(1) def die(message): print >>sys.stderr, prog + ": " + message sys.exit(1) args = sys.argv[1:] if len(args) != 2: usage() if not os.path.isdir(args[0]): die(args[0] + " is not a directory.") if not os.path.isdir(args[1]): die(args[1] + " is not a directory.") report_right_only(args[0], args[1])
ãã®ä»ãæ°ã¥ãããã¨ãã
- 対話çã¤ã³ã¿ããªã¿ããã¢ã¸ã¥ã¼ã«ãimportã§ãã¾ãããä¸åimportããã¨ã½ã¬ãããªãªã®ã§ãreload(module) ã使ãã
- ä»åã¯ãã©ãããã©ã¼ã ã®å¤å®ã¯è¦ããªãã£ãã®ã ãã©ï¼os.path.sepã§éã«åã£ãï¼ãå¤å®æ³ãè²ã ãã£ã¦ã¤ã¤ã ãªãä¾ãã°ãWindowsã§ãããã¨ã¯ï¼ (os.name == 'nt'), (platform.system() == 'Windows'), (sys.platform == 'win32') ã
*1:shiroããã®ãææã®ããã«ãæ¼ããããå¯è½æ§ãããã¾ãã
*2:ãªããã°ãªãã§ææ ¢ãã¾ãããã