Advertisement
Guest User

hg-touch.py

a guest
Oct 11th, 2012
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Copyright (c) 2011 Ernesto Mendez (der-design.com)
  4. # Dual licensed under the MIT and GPL licenses:
  5. # http://www.opensource.org/licenses/mit-license.php
  6. # http://www.gnu.org/licenses/gpl.html
  7.  
  8. # Version 1.0.0
  9. # - Initial Release
  10.  
  11. from __future__ import generators
  12. import sys
  13. from optparse import OptionParser
  14. import os
  15.  
  16. def main():
  17.     # Process arguments
  18.  
  19.     if len(args) > 1:
  20.         parser.error('Too many arguments')
  21.         sys.exit()
  22.  
  23.     elif len(args) == 0:
  24.         parser.error('Missing filename')
  25.         sys.exit()
  26.  
  27.     if not os.path.exists(options.directory):
  28.         parser.error("%s: No such directory" % options.directory)
  29.         sys.exit()
  30.  
  31.     filename = args[0]
  32.  
  33.     # Create generator
  34.  
  35.     filetree = dirwalk(os.path.abspath(options.directory))
  36.  
  37.     # Walk directory tree, create files
  38.  
  39.     if options.remove == True:
  40.  
  41.         removed = ['Removing the following files: \n']
  42.         cmd = "rm"
  43.  
  44.         for file in filetree:
  45.             if (os.path.basename(file) == filename):
  46.                 removed.append(file)
  47.                 cmd += " %s" % fixpath(file)
  48.  
  49.         if cmd != "rm":
  50.             for f in removed: print f
  51.             os.system(cmd)
  52.         else:
  53.             print "No files named '%s' found" % filename
  54.             sys.exit()
  55.  
  56.     # Walk directory tree, delete files
  57.  
  58.     else:
  59.  
  60.         created = ["Creating the following files:\n"]
  61.         cmd = "touch"
  62.  
  63.         for file in filetree:
  64.             if (os.path.isdir(file)):
  65.                 created.append("%s%s" % (file, filename))
  66.                 cmd += " " + fixpath("%s%s" % (file, filename))
  67.  
  68.         if cmd != "touch":
  69.             for f in created: print f
  70.             os.system(cmd)
  71.         else:
  72.             print "No empty directories found"
  73.             sys.exit()
  74.  
  75.  
  76. def dirwalk(dir, giveDirs=1):
  77.     # http://code.activestate.com/recipes/105873-walk-a-directory-tree-using-a-generator/
  78.     for f in os.listdir(dir):
  79.         fullpath = os.path.join(dir, f)
  80.         if os.path.isdir(fullpath) and not os.path.islink(fullpath):
  81.             if not len(os.listdir(fullpath)):
  82.                 yield fullpath + os.sep
  83.             else:
  84.                 for x in dirwalk(fullpath):  # recurse into subdir
  85.                     if os.path.isdir(x):
  86.                         if giveDirs:
  87.                             yield x
  88.                     else:
  89.                         yield x
  90.         else:
  91.             yield fullpath
  92.  
  93.  
  94. def wrap(text, width):
  95.     return reduce(lambda line, word, width=width: '%s%s%s' % (line, ' \n'[(len(line)-line.rfind('\n')-1 + len(word.split('\n', 1)[0] ) >= width)], word), text.split(' ') )
  96.  
  97.  
  98. def fixpath(p):
  99.     return shellquote(os.path.normpath(p))
  100.  
  101.  
  102. def shellquote(s):
  103.     return "'" + s.replace("'", "'\\''") + "'"
  104.  
  105.  
  106. def init_options():
  107.     global parser, options, args
  108.     parser = OptionParser(usage="usage: %prog [options] filename", description="Add or Remove placeholder files for SCM (Source Control Management) tools that do not support empty directories.")
  109.     parser.add_option("-p", "--path", dest="directory", help="search within PATH", metavar="PATH")
  110.     parser.add_option("-r", "--remove", dest="remove", action="store_true", help="remove FILE from PATH, if it's the only file on PATH")
  111.  
  112.     (options, args) = parser.parse_args()
  113.  
  114. if __name__ == '__main__':
  115.     print
  116.     init_options()
  117.     main()
  118.     print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement