Skip to content

Commit 1a183c4

Browse files
committed
Code refactoring
1 parent 4f28ea4 commit 1a183c4

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

about_code_tool/about.py

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747

4848

4949
on_windows = 'win32' in sys.platform
50-
UNC_PREFIX = u'\\\\?\\'
5150

5251
__version__ = '2.0.4'
5352

@@ -505,6 +504,7 @@ def repr_problem(obj):
505504
'ZLIB License',
506505
)
507506

507+
508508
def posix_path(path):
509509
"""
510510
Return a path using the posixpath separator given a path that may
@@ -513,6 +513,30 @@ def posix_path(path):
513513
return path.replace(ntpath.sep, posixpath.sep)
514514

515515

516+
UNC_PREFIX = u'\\\\?\\'
517+
UNC_PREFIX_POSIX = posix_path(UNC_PREFIX)
518+
UNC_PREFIXES = (UNC_PREFIX_POSIX, UNC_PREFIX,)
519+
520+
def add_unc(location):
521+
"""
522+
Convert a location to an absolute Window UNC path to support long paths on
523+
Windows. Return the location unchanged if not on Windows. See
524+
https://msdn.microsoft.com/en-us/library/aa365247.aspx
525+
"""
526+
if on_windows and not location.startswith(UNC_PREFIXES):
527+
return UNC_PREFIX + os.path.abspath(location)
528+
return location
529+
530+
531+
def remove_unc(location):
532+
"""
533+
Remove UNC prefix from location if present.
534+
"""
535+
if on_windows and location.startswith(UNC_PREFIXES):
536+
return location[len(UNC_PREFIX):]
537+
return location
538+
539+
516540
def is_about_file(path):
517541
"""
518542
Return True if the path represents a valid ABOUT file name.
@@ -523,7 +547,7 @@ def is_about_file(path):
523547
def resource_name(resource_path):
524548
"""
525549
Return a resource name based on a posix path (either the filename or
526-
directory name). Recurse to handle paths that ends with a path separator
550+
directory name). Recurse to handle paths that ends with a path separator.
527551
"""
528552
left, right = posixpath.split(resource_path)
529553
if right:
@@ -1117,8 +1141,7 @@ def check_invalid_chars(field_name, line):
11171141
warnings = Warn(IGNORED, field_name, line, msg)
11181142
return invalid_chars, warnings
11191143

1120-
def posix_unc_prefix():
1121-
return posix_path(u'\\\\?\\')
1144+
11221145

11231146
class Collector(object):
11241147
"""
@@ -1162,13 +1185,7 @@ def collect(location):
11621185

11631186
if on_windows:
11641187
location = unicode(location)
1165-
"""
1166-
Convert a location to an absolute Window UNC path to support long paths
1167-
on Windows. Return the location unchanged if not on Windows.
1168-
See https://msdn.microsoft.com/en-us/library/aa365247.aspx
1169-
"""
1170-
if on_windows and not location.startswith(UNC_PREFIX):
1171-
location = UNC_PREFIX + os.path.abspath(location)
1188+
location = add_unc(location)
11721189
location = os.path.expanduser(location)
11731190
location = os.path.expandvars(location)
11741191
location = os.path.normpath(location)
@@ -1274,36 +1291,23 @@ def write_to_csv(self, output_path):
12741291

12751292
def get_about_context(self, about_object):
12761293
about_content = about_object.validated_fields
1277-
if '\n' in about_object.get_dje_license_name():
1278-
msg = ('Multiple licenses is not supported. '
1279-
'Skipping License generation.')
1280-
if on_windows:
1281-
if (about_object.location.startswith(posix_unc_prefix())
1282-
or about_object.location.startswith(UNC_PREFIX)):
1283-
about_object.location = about_object.location.strip(posix_unc_prefix()).strip(UNC_PREFIX)
1284-
err = Error(GENATTRIB, 'dje_license',
1285-
about_object.location, msg)
1294+
has_multiple_licenses = '\n' in about_object.get_dje_license_name()
1295+
if has_multiple_licenses:
1296+
msg = 'Multiple licenses is not supported. Skipping License generation.'
1297+
about_object.location = remove_unc(about_object.location)
1298+
err = Error(GENATTRIB, 'dje_license', about_object.location, msg)
12861299
self.genattrib_errors.append(err)
12871300

1288-
lic_text = unicode(about_object.license_text(),
1289-
errors='replace')
1290-
notice_text = unicode(about_object.notice_text(),
1291-
errors='replace')
1301+
lic_text = unicode(about_object.license_text(), errors='replace')
1302+
notice_text = unicode(about_object.notice_text(), errors='replace')
12921303
about_content['license_text'] = lic_text
12931304
about_content['notice_text'] = notice_text
12941305

12951306
# report error if no license_text is found
1296-
if not about_content.get('license_text')\
1297-
and not about_content.get('notice_text')\
1298-
and not '\n' in about_object.get_dje_license_name():
1299-
msg = ('No license_text found. '
1300-
'Skipping License generation.')
1301-
if on_windows:
1302-
if (about_object.location.startswith(posix_unc_prefix())
1303-
or about_object.location.startswith(UNC_PREFIX)):
1304-
about_object.location = about_object.location.strip(posix_unc_prefix()).strip(UNC_PREFIX)
1305-
err = Error(GENATTRIB, 'license_text_file',
1306-
about_object.location, msg)
1307+
if not lic_text and not notice_text and not has_multiple_licenses:
1308+
msg = 'No license_text found. Skipping License generation.'
1309+
about_object.location = remove_unc(about_object.location)
1310+
err = Error(GENATTRIB, 'license_text_file', about_object.location, msg)
13071311
self.genattrib_errors.append(err)
13081312
return about_content
13091313

@@ -1358,9 +1362,7 @@ def generate_attribution(self, template_path=None, limit_to=None, verification=N
13581362
break
13591363

13601364
if not component_exist:
1361-
if on_windows:
1362-
if self.location.startswith(posix_unc_prefix()):
1363-
self.location = self.location.strip(posix_unc_prefix())
1365+
self.location = remove_unc(self.location)
13641366
loc = self.location + component
13651367
msg = ('The requested ABOUT file: %r does not exist. '
13661368
'No attribution generated for this file.' % loc)
@@ -1411,9 +1413,7 @@ def check_paths(self, paths):
14111413
for path in paths:
14121414
path = posix_path(path)
14131415
afp = join(self.location, path)
1414-
if on_windows:
1415-
if afp.startswith(posix_unc_prefix()):
1416-
afp = afp.strip(posix_unc_prefix())
1416+
afp = remove_unc(afp)
14171417
msg = ('The requested ABOUT file: %(afp)r does not exist. '
14181418
'No attribution generated for this file.' % locals())
14191419
err = Error(GENATTRIB, 'about_file', path, msg)

about_code_tool/genabout.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,7 @@ def format_output(input_list):
626626
@staticmethod
627627
def write_output(output):
628628
for about_file_location, context in output:
629-
if about.on_windows:
630-
about_file_location = about.UNC_PREFIX + os.path.abspath(about_file_location)
629+
about_file_location = about.add_unc(about_file_location)
631630
if _exists(about_file_location):
632631
os.remove(about_file_location)
633632
with open(about_file_location, 'wb') as output_file:

0 commit comments

Comments
 (0)