1
1
#
2
- # Copyright (c) 2018 nexB Inc. and others. All rights reserved .
3
- # http://nexb.com and https://github.com/nexB/scancode-toolkit/
4
- # The ScanCode software is licensed under the Apache License version 2.0.
5
- # Data generated with ScanCode require an acknowledgment .
2
+ # Copyright (c) nexB Inc. and others.
3
+ # SPDX-License-Identifier: Apache-2.0
4
+ #
5
+ # Visit https://aboutcode.org and https://github.com/nexB/ for support and download .
6
6
# ScanCode is a trademark of nexB Inc.
7
7
#
8
- # You may not use this software except in compliance with the License.
9
- # You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
10
- # Unless required by applicable law or agreed to in writing, software distributed
11
- # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
- # CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
- # specific language governing permissions and limitations under the License.
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
14
11
#
15
- # When you publish or redistribute any data created with ScanCode or any ScanCode
16
- # derivative work, you must accompany this data with the following acknowledgment:
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
17
19
#
18
- # Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
19
- # OR CONDITIONS OF ANY KIND, either express or implied. No content created from
20
- # ScanCode should be considered or used as legal advice. Consult an Attorney
21
- # for any legal advice.
22
- # ScanCode is a free software code scanning tool from nexB Inc. and others.
23
- # Visit https://github.com/nexB/scancode-toolkit/ for support and download.
24
-
25
- from __future__ import absolute_import
26
- from __future__ import print_function
27
- from __future__ import unicode_literals
28
20
29
21
import logging
30
22
import os
33
25
import shutil
34
26
import sys
35
27
28
+ from os .path import dirname
29
+ from os .path import join
30
+ from os .path import exists
31
+
36
32
from commoncode .fileutils import as_posixpath
37
33
from commoncode .fileutils import create_dir
38
34
from commoncode .fileutils import file_name
39
- from commoncode .fileutils import fsencode
40
35
from commoncode .fileutils import parent_directory
41
36
from commoncode .text import toascii
42
37
from commoncode .system import on_linux
43
- from commoncode .system import py2
44
- from os .path import dirname
45
- from os .path import join
46
- from os .path import exists
47
38
48
39
logger = logging .getLogger (__name__ )
49
40
DEBUG = False
53
44
54
45
root_dir = join (dirname (__file__ ), 'bin' )
55
46
56
- POSIX_PATH_SEP = b'/' if (on_linux and py2 ) else '/'
57
- WIN_PATH_SEP = b'\\ ' if (on_linux and py2 ) else '\\ '
58
- PATHS_SEPS = POSIX_PATH_SEP + WIN_PATH_SEP
59
- EMPTY_STRING = b'' if (on_linux and py2 ) else ''
60
- DOT = b'.' if (on_linux and py2 ) else '.'
61
- DOTDOT = DOT + DOT
62
- UNDERSCORE = b'_' if (on_linux and py2 ) else '_'
63
-
64
47
# Suffix added to extracted target_dir paths
65
- EXTRACT_SUFFIX = b'-extract' if ( on_linux and py2 ) else r '-extract'
48
+ EXTRACT_SUFFIX = '-extract'
66
49
67
50
# high level archive "kinds"
68
51
docs = 1
@@ -103,60 +86,46 @@ def is_extraction_path(path):
103
86
"""
104
87
Return True is the path points to an extraction path.
105
88
"""
106
- if on_linux and py2 :
107
- path = fsencode (path )
108
-
109
- return path and path .rstrip (PATHS_SEPS ).endswith (EXTRACT_SUFFIX )
89
+ return path and path .rstrip ('\\ /' ).endswith (EXTRACT_SUFFIX )
110
90
111
91
112
92
def is_extracted (location ):
113
93
"""
114
94
Return True is the location is already extracted to the corresponding
115
95
extraction location.
116
96
"""
117
- if on_linux and py2 :
118
- location = fsencode (location )
119
97
return location and exists (get_extraction_path (location ))
120
98
121
99
122
100
def get_extraction_path (path ):
123
101
"""
124
102
Return a path where to extract.
125
103
"""
126
- if on_linux and py2 :
127
- path = fsencode (path )
128
- return path .rstrip (PATHS_SEPS ) + EXTRACT_SUFFIX
104
+ return path .rstrip ('\\ /' ) + EXTRACT_SUFFIX
129
105
130
106
131
107
def remove_archive_suffix (path ):
132
108
"""
133
109
Remove all the extracted suffix from a path.
134
110
"""
135
- if on_linux and py2 :
136
- path = fsencode (path )
137
- return re .sub (EXTRACT_SUFFIX , EMPTY_STRING , path )
111
+ return re .sub (EXTRACT_SUFFIX , '' , path )
138
112
139
113
140
114
def remove_backslashes_and_dotdots (directory ):
141
115
"""
142
116
Walk a directory and rename the files if their names contain backslashes.
143
117
Return a list of errors if any.
144
118
"""
145
- if on_linux and py2 :
146
- directory = fsencode (directory )
147
119
errors = []
148
120
for top , _ , files in os .walk (directory ):
149
121
for filename in files :
150
- if not (WIN_PATH_SEP in filename or DOTDOT in filename ):
122
+ if not (' \\ ' in filename or '..' in filename ):
151
123
continue
152
124
try :
153
- new_path = as_posixpath (filename )
154
- new_path = new_path .strip (POSIX_PATH_SEP )
155
- new_path = posixpath .normpath (new_path )
156
- new_path = new_path .replace (DOTDOT , POSIX_PATH_SEP )
157
- new_path = new_path .strip (POSIX_PATH_SEP )
125
+ new_path = as_posixpath (filename ).strip ('/' )
126
+ new_path = posixpath .normpath (new_path ).replace ('..' , '/' ).strip ('/' )
158
127
new_path = posixpath .normpath (new_path )
159
- segments = new_path .split (POSIX_PATH_SEP )
128
+ segments = new_path .split ('/' )
160
129
directory = join (top , * segments [:- 1 ])
161
130
create_dir (directory )
162
131
shutil .move (join (top , filename ), join (top , * segments ))
@@ -180,9 +149,7 @@ def new_name(location, is_dir=False):
180
149
the extension unchanged.
181
150
"""
182
151
assert location
183
- if on_linux and py2 :
184
- location = fsencode (location )
185
- location = location .rstrip (PATHS_SEPS )
152
+ location = location .rstrip ('\\ /' )
186
153
assert location
187
154
188
155
parent = parent_directory (location )
@@ -193,8 +160,8 @@ def new_name(location, is_dir=False):
193
160
filename = file_name (location )
194
161
195
162
# corner case
196
- if filename in (DOT , DOT ):
197
- filename = UNDERSCORE
163
+ if filename in ('.' , '..' ):
164
+ filename = '_'
198
165
199
166
# if unique, return this
200
167
if filename .lower () not in siblings_lower :
@@ -204,19 +171,19 @@ def new_name(location, is_dir=False):
204
171
if is_dir :
205
172
# directories do not have an "extension"
206
173
base_name = filename
207
- ext = EMPTY_STRING
174
+ ext = ''
208
175
else :
209
- base_name , dot , ext = filename .partition (DOT )
176
+ base_name , dot , ext = filename .partition ('.' )
210
177
if dot :
211
- ext = dot + ext
178
+ ext = f'. { ext } '
212
179
else :
213
180
base_name = filename
214
- ext = EMPTY_STRING
181
+ ext = ''
215
182
216
183
# find a unique filename, adding a counter int to the base_name
217
184
counter = 1
218
185
while 1 :
219
- filename = base_name + UNDERSCORE + str ( counter ) + ext
186
+ filename = f' { base_name } _ { counter } { ext } '
220
187
if filename .lower () not in siblings_lower :
221
188
break
222
189
counter += 1
0 commit comments