73 - dest : destination directory to copy to |
73 - dest : destination directory to copy to |
74 |
74 |
75 Returns: |
75 Returns: |
76 - Nothing |
76 - Nothing |
77 ''' |
77 ''' |
78 self.logger.debug("Executing: Copy %s to %s", source, dest) |
78 self.logger.debug("Executing: Copy file %s to %s", source, dest) |
79 # Copy the file, if it exists. |
79 # Copy the file, if it exists. |
80 if os.access(source, os.F_OK): |
80 if os.access(source, os.F_OK): |
81 shutil.copy2(source, dest) |
81 shutil.copy2(source, dest) |
|
82 else: |
|
83 self.logger.debug('%s not found -- skipping', source) |
|
84 |
|
85 def copy_dir(self, source, dest): |
|
86 ''' |
|
87 Class method to copy a source directory to a destination |
|
88 only if the source directory exists. This method uses |
|
89 shutil.copytree() to copy the directory, so the desitnation |
|
90 must not already exist. |
|
91 |
|
92 Paramters: |
|
93 - source : Source directory to be copied |
|
94 - dest : destination directory to copy to |
|
95 |
|
96 Returns: |
|
97 - Nothing |
|
98 ''' |
|
99 self.logger.debug("Executing: Copy dir %s to %s", source, dest) |
|
100 # Copy the directory, if it exists. |
|
101 if os.path.isdir(source): |
|
102 shutil.copytree(source, dest) |
82 else: |
103 else: |
83 self.logger.debug('%s not found -- skipping', source) |
104 self.logger.debug('%s not found -- skipping', source) |
84 |
105 |
85 def execute(self, dry_run=False): |
106 def execute(self, dry_run=False): |
86 ''' |
107 ''' |
116 dest = os.path.join(self.target_dir, dest.lstrip("/")) |
137 dest = os.path.join(self.target_dir, dest.lstrip("/")) |
117 if not dry_run: |
138 if not dry_run: |
118 |
139 |
119 # Check for source dir, if so copy entire contents |
140 # Check for source dir, if so copy entire contents |
120 if os.path.isdir(source): |
141 if os.path.isdir(source): |
121 if not os.access(dest, os.F_OK): |
142 self.copy_dir(source, dest) |
122 os.makedirs(dest) |
|
123 |
|
124 for listfile in os.listdir(source): |
|
125 srcfile = os.path.join(source, listfile) |
|
126 self.copy_file(srcfile, dest) |
|
127 else: |
143 else: |
128 # Create the destination if it does not exist. |
144 # Create the destination if it does not exist. |
129 if not os.access(os.path.dirname(dest), os.F_OK): |
145 if not os.access(os.path.dirname(dest), os.F_OK): |
130 os.makedirs(os.path.dirname(dest)) |
146 os.makedirs(os.path.dirname(dest)) |
131 self.copy_file(source, dest) |
147 self.copy_file(source, dest) |