Skip to content

Commit f65d6a4

Browse files
committed
#22 - Working in progress (Need to add tests)
1 parent 0df0d32 commit f65d6a4

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

src/attributecode/cmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def collect_redist_src(location, output, quiet, verbose):
411411
errors.extend(copy_errors)
412412
errors_count = report_errors(errors, quiet, verbose, log_file_loc=output + '-error.log')
413413
if not quiet:
414-
msg = 'Inventory collected in {output}.'.format(**locals())
414+
msg = 'Redistributed sources are copied to {output}.'.format(**locals())
415415
click.echo(msg)
416416
sys.exit(errors_count)
417417

src/attributecode/model.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import posixpath
3737
import traceback
3838

39-
from attributecode.util import python2
39+
from attributecode.util import python2, to_posix
4040

4141
if python2: # pragma: nocover
4242
from itertools import izip_longest as zip_longest # NOQA
@@ -69,6 +69,7 @@
6969
from attributecode.util import filter_errors
7070
from attributecode.util import is_valid_name
7171
from attributecode.util import on_windows
72+
from attributecode.util import norm
7273
from attributecode.util import replace_tab_with_spaces
7374
from attributecode.util import wrap_boolean_value
7475
from attributecode.util import UNC_PREFIX
@@ -1284,7 +1285,14 @@ def copy_redist_src(abouts, location, output):
12841285
continue
12851286
for k in about.about_resource.value:
12861287
from_path = about.about_resource.value.get(k)
1287-
copy_file(from_path, output)
1288+
norm_from_path = norm(from_path)
1289+
relative_from_path = norm_from_path.partition(util.norm(location))[2]
1290+
# Need to strip the '/' to use the join
1291+
if relative_from_path.startswith('/'):
1292+
relative_from_path = relative_from_path.partition('/')[2]
1293+
# Get the directory name of the output path
1294+
output_dir = os.path.dirname(os.path.join(output, util.norm(relative_from_path)))
1295+
copy_file(from_path, output_dir)
12881296
return errors
12891297

12901298

src/attributecode/util.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,24 @@ def get_about_locations(location):
191191
if is_about_file(loc):
192192
yield loc
193193

194+
def norm(p):
195+
"""
196+
Normalize the path
197+
"""
198+
if p.startswith(UNC_PREFIX) or p.startswith(to_posix(UNC_PREFIX)):
199+
p = p.strip(UNC_PREFIX).strip(to_posix(UNC_PREFIX))
200+
p = to_posix(p)
201+
p = p.strip(posixpath.sep)
202+
p = posixpath.normpath(p)
203+
return p
204+
194205

195206
def get_relative_path(base_loc, full_loc):
196207
"""
197208
Return a posix path for a given full location relative to a base location.
198209
The first segment of the different between full_loc and base_loc will become
199210
the first segment of the returned path.
200211
"""
201-
def norm(p):
202-
if p.startswith(UNC_PREFIX) or p.startswith(to_posix(UNC_PREFIX)):
203-
p = p.strip(UNC_PREFIX).strip(to_posix(UNC_PREFIX))
204-
p = to_posix(p)
205-
p = p.strip(posixpath.sep)
206-
p = posixpath.normpath(p)
207-
return p
208-
209212
base = norm(base_loc)
210213
path = norm(full_loc)
211214

@@ -476,8 +479,10 @@ def copy_file(from_path, to_path):
476479
return
477480

478481
if on_windows:
479-
from_path = add_unc(from_path)
480-
to_path = add_unc(to_path)
482+
if not from_path.startswith(UNC_PREFIXES):
483+
from_path = add_unc(from_path)
484+
if not to_path.startswith(UNC_PREFIXES):
485+
to_path = add_unc(to_path)
481486

482487
# Strip the white spaces
483488
from_path = from_path.strip()
@@ -491,9 +496,14 @@ def copy_file(from_path, to_path):
491496
os.makedirs(to_path)
492497
try:
493498
if os.path.isdir(from_path):
494-
print("#############################")
495-
from distutils.dir_util import copy_tree
496-
copy_tree(from_path, to_path)
499+
# Copy the whole directory structure
500+
folder_name = os.path.basename(from_path)
501+
to_path = os.path.join(to_path, folder_name)
502+
# Since we need to copy everything along with the directory structure,
503+
# making sure the directory does not exist will not hurt.
504+
shutil.rmtree(to_path)
505+
# Copy the directory recursively along with its structure
506+
shutil.copytree(from_path, to_path)
497507
else:
498508
shutil.copy2(from_path, to_path)
499509
except Exception as e:

0 commit comments

Comments
 (0)