Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """Scan the files in a directory sub-tree and set directory modification times
  4. to be the modification time of the newest non directory file within the
  5. sub-tree. This is work around the Office 2016 practice of creating a temporary
  6. file in the same directory as each file when opening that file (which of
  7. course updates the directory modification time."""
  8.  
  9. import argparse, os, sys, time
  10.  
  11. loglevel = 1
  12.  
  13. def scan_directory(dirpath, mtimes={}, level=0):
  14. """Scan directory sub-tree...
  15.  
  16. dirpath: root of directory sub-tree
  17. mtimes: dictionary...
  18. level: indentation level (used only for reporting)"""
  19.  
  20. mtime0 = os.stat(dirpath).st_mtime
  21. mtimes[dirpath] = {"mtime0": mtime0, "mtime": 0.0}
  22.  
  23. for direntry in os.scandir(dirpath):
  24. name = direntry.name
  25. path = direntry.path
  26. is_file = direntry.is_file()
  27. is_dir = direntry.is_dir()
  28. is_symlink = direntry.is_symlink()
  29.  
  30. if loglevel > 2:
  31. print("%s%s %s" % (level * " ", name,
  32. time.ctime(direntry.stat().st_mtime)))
  33.  
  34. if not is_file and not is_dir:
  35. continue
  36.  
  37. if is_dir and name == ".git":
  38. continue
  39.  
  40. if is_dir and not is_symlink:
  41. mtime = scan_directory(path, level=level+1, mtimes=mtimes)
  42. else:
  43. mtime = direntry.stat().st_mtime
  44.  
  45. if mtime > mtimes[dirpath]["mtime"]:
  46. mtimes[dirpath]["mtime"] = mtime
  47.  
  48. return mtimes[dirpath]["mtime"]
  49.  
  50. def main(argv=None):
  51. """Main program."""
  52.  
  53. if argv is None: argv = sys.argv
  54.  
  55. # XXX might be better to use positional argument for directory?
  56. global loglevel
  57. parser = argparse.ArgumentParser(
  58. prog=argv[0], description="update directory modification time")
  59. parser.add_argument("-d", "--directory",
  60. help="root of directory sub-tree; default is current directory")
  61. parser.add_argument("-l", "--loglevel", type=int,
  62. help="logging level; default: %d" % loglevel)
  63. parser.add_argument("-n", "--dryrun", action="store_true",
  64. help="dry run; report what would have been done but don't do it")
  65.  
  66. args = parser.parse_args(argv[1:])
  67.  
  68. directory = args.directory if args.directory else os.getcwd()
  69. dryrun = args.dryrun
  70. if args.loglevel is not None: loglevel = args.loglevel
  71.  
  72. mtimes = {}
  73. scan_directory(directory, mtimes)
  74.  
  75. for dirname in sorted(mtimes.keys()):
  76. mtime0 = mtimes[dirname]["mtime0"]
  77. mtime = mtimes[dirname]["mtime"]
  78. if mtime > 0 and mtime < mtime0:
  79.  
  80. if loglevel > 0:
  81. print("%s: %s -> %s" % (dirname, time.ctime(mtime0),
  82. time.ctime(mtime)))
  83.  
  84. if dryrun or loglevel > 1:
  85. print("os.utime(\"%s\", (%d, %d))" % (dirname, mtime, mtime))
  86.  
  87. if not dryrun:
  88. os.utime(dirname, (mtime, mtime))
  89.  
  90. if __name__ == "__main__":
  91. sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement