Skip to content

Commit 5669839

Browse files
committed
#210 Added support to run genattrib with a zip file and tests
1 parent 1ced1c8 commit 5669839

15 files changed

+20599
-0
lines changed

about_code_tool/genattrib.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,37 @@ def check_about_file_existence_and_format(input_list):
134134
Create a verification CSV output for the attribution
135135
"""
136136

137+
def extract_zip(location):
138+
"""
139+
Extract a zip file at location in a temp directory and return the temporary
140+
directory where the archive was extracted.
141+
"""
142+
import zipfile
143+
import tempfile
144+
if not zipfile.is_zipfile(location):
145+
raise Exception('Incorrect zip file %(location)r' % locals())
146+
147+
archive_base_name = os.path.basename(location).replace('.zip', '')
148+
base_dir = tempfile.mkdtemp()
149+
target_dir = os.path.join(base_dir, archive_base_name)
150+
os.makedirs(target_dir)
151+
152+
with zipfile.ZipFile(location) as zipf:
153+
for info in zipf.infolist():
154+
name = info.filename
155+
content = zipf.read(name)
156+
target = os.path.join(target_dir, name)
157+
if not os.path.exists(os.path.dirname(target)):
158+
os.makedirs(os.path.dirname(target))
159+
if not content and target.endswith(os.path.sep):
160+
if not os.path.exists(target):
161+
os.makedirs(target)
162+
if not os.path.exists(target):
163+
with open(target, 'wb') as f:
164+
f.write(content)
165+
return target_dir
166+
167+
137168
def main(parser, options, args):
138169
overwrite = options.overwrite
139170
verbosity = options.verbosity
@@ -200,6 +231,10 @@ def main(parser, options, args):
200231
parser.print_help()
201232
sys.exit(errno.EEXIST)
202233

234+
if input_path.lower().endswith('.zip'):
235+
# accept zipped ABOUT files as input
236+
input_path = extract_zip(input_path)
237+
203238
if isdir(output_path):
204239
print('Output must be a HTML file.')
205240
parser.print_help()

about_code_tool/tests/test_genattrib.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
from about_code_tool import genattrib
2323

24+
from about_code_tool.tests.test_about import get_temp_file
25+
2426

2527
TESTS_DIR = os.path.abspath(os.path.dirname(__file__))
2628
TESTDATA_DIR = os.path.join(TESTS_DIR, 'testdata')
@@ -69,3 +71,29 @@ def test_component_subset_to_sublist(self):
6971
expected = ['/tmp/', '/tmp/t1/']
7072
result = genattrib.component_subset_to_sublist(test)
7173
self.assertEqual(expected, result)
74+
75+
def test_genattrib_basic(self):
76+
about_dir = 'about_code_tool/tests/testdata/genattrib/basic/'
77+
generated_attrib = get_temp_file('generated.html')
78+
args = [about_dir, generated_attrib]
79+
options = None
80+
genattrib_command_tester(args, options)
81+
expected = open('about_code_tool/tests/testdata/genattrib/basic.html').read()
82+
result = open(generated_attrib).read()
83+
assert expected ==result
84+
85+
def test_genattrib_from_zipped_dir(self):
86+
about_dir = 'about_code_tool/tests/testdata/genattrib/zipped_about.zip'
87+
generated_attrib = get_temp_file('generated.html')
88+
args = [about_dir, generated_attrib]
89+
options = None
90+
genattrib_command_tester(args, options)
91+
expected = open('about_code_tool/tests/testdata/genattrib/zipped_about.html').read()
92+
result = open(generated_attrib).read()
93+
assert expected ==result
94+
95+
96+
def genattrib_command_tester(args, options):
97+
parser = genattrib.get_parser()
98+
options, args = parser.parse_args(args, options)
99+
genattrib.main(parser, options, args)

0 commit comments

Comments
 (0)