-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit-XML.py
95 lines (70 loc) · 3.57 KB
/
pre-commit-XML.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''
pre-commit-XML.py
Last updated 23/02/2019
Script extracts customUI.xml/customUI14.xml files into a new 'FILENAME.XML' subdirectory within the repository root folder (by default)
With the standard .gitignore entries, only Excel files located within the root directory of the repository will be processed & added to a commit
'''
import os
import shutil
from zipfile import ZipFile
# list excel extensions that will be processed and have VBA modules extracted
excel_file_extensions = ('xlsb', 'xlsx', 'xlsm', 'xlam', 'xltm')
# process Excel files in this directory (not recursive to subdirectories)
directory = '.'
# String to append to end of subdirectory based on filename
XML_suffix = '.XML'
# String to remove everything after in Excel filename
Rev_tag = ' - Rev '
# function to extract the XML files from an Excel archive
def extract_XML_files(archive_name, full_item_name, extract_folder):
with ZipFile(archive_name) as zf:
file_data = zf.read(full_item_name)
with open(os.path.join(extract_folder, os.path.basename(full_item_name)), "wb") as file_out:
file_out.write(file_data)
# remove all previous '.XML' directories including contents
for directories in os.listdir(directory):
if directories.endswith(XML_suffix):
shutil.rmtree(directories)
# loop through files in given directory and process those that are Excel files (excluding temporary Excel files ~$*)
for filename in os.listdir(directory):
if filename.endswith(excel_file_extensions):
# skip temporary excel files ~$*.*
if not filename.startswith('~$'):
# Setup variable for wookbook name
workbook_name = filename
# Find index of standard '- Rev X.xxx' tag in workbook filename if it exists
index_rev = filename.find(Rev_tag)
# If revision tag existed in the filename, remove it based on index position, else remove only the file extension
if index_rev != -1:
filename = filename[0:index_rev]
else:
filename = os.path.splitext(filename)[0]
# Setup directory name based on filename
xml_path = filename + XML_suffix
# Make new directory (to replace existing where it previously existed)
os.mkdir(xml_path)
# print to console for information/debugging purposes
max_len = max(len('Directory name -- ' + xml_path), len('Workbook name -- ' + workbook_name))
print()
print('-' * max_len)
print('Workbook name -- ' + workbook_name)
print('Directory name -- ' + xml_path)
print('-' * max_len)
# Extract customUI.xml & customUI14.xml files from temporary zip file if they exist
try:
extract_XML_files(workbook_name, 'customUI/customUI.xml', xml_path)
print('Extracted -- customUI.xml from ' + workbook_name)
except KeyError:
print('customUI.xml does not exists in ' + workbook_name)
try:
extract_XML_files(workbook_name, 'customUI/customUI14.xml', xml_path)
print('Extracted -- customUI14.xml from ' + workbook_name)
except KeyError:
print('customUI14.xml does not exists in ' + workbook_name)
# print trailing line to separate output
print()
# loop through directories & remove '.XML' directory if nothing was extracted and it is therefore empty
for directories in os.listdir(directory):
if directories.endswith(XML_suffix):
if not os.listdir(directories):
os.rmdir(directories)