22# -*- coding: utf8 -*-
33
44# ============================================================================
5- # Copyright (c) 2014 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+ # Copyright (c) 2013-2015 nexB Inc. http://www.nexb.com/ - All rights reserved.
66# Licensed under the Apache License, Version 2.0 (the "License");
77# you may not use this file except in compliance with the License.
88# You may obtain a copy of the License at
3232import csv
3333from datetime import datetime
3434from email .parser import HeaderParser
35- from os .path import basename , dirname , join , normpath , realpath
3635import errno
3736import httplib
3837import logging
38+ import ntpath
3939import optparse
4040import os
41+ from os .path import basename , dirname , join , normpath , realpath
4142import posixpath
4243import socket
4344import string
4445import sys
4546import urlparse
46- import ntpath
4747
4848
49- __version__ = '2.0.1'
49+ on_windows = 'win32' in sys .platform
50+ UNC_PREFIX = u'\\ \\ ?\\ '
51+
52+ __version__ = '2.0.2'
5053
5154# See http://dejacode.org
5255__about_spec_version__ = '1.0'
5356
5457
5558__copyright__ = """
56- Copyright (c) 2013-2014 nexB Inc. All rights reserved.
59+ Copyright (c) 2013-2015 nexB Inc. All rights reserved.
5760
5861Licensed under the Apache License, Version 2.0 (the "License");
5962you may not use this file except in compliance with the License.
@@ -554,7 +557,14 @@ class AboutFile(object):
554557 """
555558 def __init__ (self , location = None ):
556559 self .about_resource = None
557- self .location = location
560+ # The os.path.abspath(None) will cause error in linux system.
561+ # See https://bugs.python.org/issue22587
562+ # Note that the os.path.abspath is needed for windows when there
563+ # is long path/filename.
564+ if on_windows :
565+ self .location = os .path .abspath (location )
566+ else :
567+ self .location = location
558568
559569 self .parsed = None
560570 self .parsed_fields = None
@@ -1082,7 +1092,10 @@ def get_dje_license_name(self):
10821092 """
10831093 Return the about object's dje_license_name.
10841094 """
1085- return self .parsed .get ('dje_license_name' , '' )
1095+ try :
1096+ return self .parsed .get ('dje_license_name' , '' )
1097+ except :
1098+ return ''
10861099
10871100def check_invalid_chars (field_name , line ):
10881101 """
@@ -1104,6 +1117,8 @@ def check_invalid_chars(field_name, line):
11041117 warnings = Warn (IGNORED , field_name , line , msg )
11051118 return invalid_chars , warnings
11061119
1120+ def posix_unc_prefix ():
1121+ return posix_path (u'\\ \\ ?\\ ' )
11071122
11081123class Collector (object ):
11091124 """
@@ -1144,6 +1159,23 @@ def collect(location):
11441159 """
11451160 # FIXME: we should not accept both a file and dir location as input
11461161 paths = []
1162+
1163+ if on_windows :
1164+ 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 )
1172+ location = os .path .expanduser (location )
1173+ location = os .path .expandvars (location )
1174+ location = os .path .normpath (location )
1175+ location = os .path .abspath (location )
1176+
1177+ assert os .path .exists (location )
1178+
11471179 if location :
11481180 if os .path .isfile (location ) and is_about_file (location ):
11491181 paths .append (location )
@@ -1197,6 +1229,10 @@ def get_relative_path(self, location):
11971229 """
11981230 user_loc = normpath (self .location )
11991231 if os .path .isdir (self .normalized_location ):
1232+ # Making sure both are in posix path format before
1233+ # doing any string partition.
1234+ location = posix_path (location )
1235+ user_loc = posix_path (user_loc )
12001236 parent_name = basename (user_loc )
12011237 subpath = '/' + parent_name + location .partition (user_loc )[2 ]
12021238 if user_loc [- 1 ] == '/' :
@@ -1241,6 +1277,10 @@ def get_about_context(self, about_object):
12411277 if '\n ' in about_object .get_dje_license_name ():
12421278 msg = ('Multiple licenses is not supported. '
12431279 '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 )
12441284 err = Error (GENATTRIB , 'dje_license' ,
12451285 about_object .location , msg )
12461286 self .genattrib_errors .append (err )
@@ -1258,6 +1298,10 @@ def get_about_context(self, about_object):
12581298 and not '\n ' in about_object .get_dje_license_name ():
12591299 msg = ('No license_text found. '
12601300 '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 )
12611305 err = Error (GENATTRIB , 'license_text_file' ,
12621306 about_object .location , msg )
12631307 self .genattrib_errors .append (err )
@@ -1314,6 +1358,9 @@ def generate_attribution(self, template_path=None, limit_to=None, verification=N
13141358 break
13151359
13161360 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 ())
13171364 loc = self .location + component
13181365 msg = ('The requested ABOUT file: %r does not exist. '
13191366 'No attribution generated for this file.' % loc )
@@ -1364,6 +1411,9 @@ def check_paths(self, paths):
13641411 for path in paths :
13651412 path = posix_path (path )
13661413 afp = join (self .location , path )
1414+ if on_windows :
1415+ if afp .startswith (posix_unc_prefix ()):
1416+ afp = afp .strip (posix_unc_prefix ())
13671417 msg = ('The requested ABOUT file: %(afp)r does not exist. '
13681418 'No attribution generated for this file.' % locals ())
13691419 err = Error (GENATTRIB , 'about_file' , path , msg )
0 commit comments