Łukasz Langa avatar Łukasz Langa committed 19d27b2

copy option introduced; bumped version to 1.0c3

Comments (0)

Files changed (2)

 
 setup (
     name = 'rename',
-    version = '1.0c2',
+    version = '1.0c3',
     author = 'Lukasz Langa',
     author_email = 'support@langacore.org, lukasz@langa.pl',
     description = "Renames files using regular expressions",
 
 import os
 import re
+import shutil
 import sys
 
 
 
 class Renamer(object):
     def __init__(self, test=False, case_insensitive=False, xform=None,
-                 quiet=False, **kwargs):
+                 quiet=False, copy=False, **kwargs):
         self.case_insensitive = case_insensitive
+        self.copy = copy
         self.xform = xform
         self.test = test
         self.quiet = quiet
                 return 2
         # actual rename
         for target, sources in self.targets.iteritems():
-            if self.test:
-                print >>INFO, ("Would run os.rename(%s, %s)" % (sources[0],
-                                                                target))
+            if self.copy:
+                if self.test:
+                    print >>INFO, ("Would run shutil.copy(%s, %s) (with "
+                            "copymode and copystat)" % (sources[0], target))
+                else:
+                    shutil.copy(sources[0], target)
+                    shutil.copymode(sources[0], target)
+                    shutil.copystat(sources[0], target)
             else:
-                os.rename(sources[0], target)
+                if self.test:
+                    print >>INFO, ("Would run os.rename(%s, %s)" % (sources[0],
+                                                                    target))
+                else:
+                    os.rename(sources[0], target)
         return 0
 
 
 def run():
     import argparse
     parser = argparse.ArgumentParser(prog='rename')
+    parser.add_argument('-c', '--copy', action='store_true',
+                        help='copy files instead of renaming')
     parser.add_argument('-i', '--case-insensitive', action='store_true',
                         help='treat the regular expression as '
                              'case-insensitive')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.