Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #19 - Convert shell script to python #71

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 61 additions & 0 deletions app/merge_badges.py
@@ -0,0 +1,61 @@
import os

if os.system('which rsvg-convert') != 0:
os.system('sudo apt-get -y install librsvg2-bin')
if os.system('which python3') != 0:
os.system('sudo apt-get -y install python3')
if os.system('which pdftk') != 0:
os.system('sudo apt-get -y install pdftk')

os.system('python3 generate-badges.py')

BADGES_FOLDER = 'static/badges'


input_folders = [file for file in os.listdir(BADGES_FOLDER)
if file.lower().endswith(".badges")]

def generate_pdfs(folder_path):
svgs = [file for file in os.listdir(folder_path)
if file.lower().endswith('.svg')]
for svg in svgs:
svg_path = os.path.join(folder_path, svg)
pdf_path = os.path.splitext(svg_path)[0] + '.pdf'
print('svg: {}'.format(svg_path))
print('pdf: {}'.format(pdf_path))
os.system('rsvg-convert -f pdf -o {} {}'.format(pdf_path, svg_path))

# Generating PDF files from svg.
for folder in input_folders:
folder_path = os.path.join(BADGES_FOLDER, folder)
generate_pdfs(folder_path)

# Merge badges of different types
input_folders = [file for file in os.listdir(BADGES_FOLDER)
if file.lower().endswith(".badges")]

print ('Merging badges of different types.')

for folder in input_folders:
folder_path = os.path.join(BADGES_FOLDER, folder)
out = folder + '.pdf'
out_path = os.path.join(BADGES_FOLDER, out)
os.system('pdftk ' + folder_path + '/*.pdf cat output ' + out_path)

final_path = os.path.join(BADGES_FOLDER, 'all-badges.pdf')
os.system('pdftk ' + BADGES_FOLDER + '/*.pdf cat output ' + final_path)

print ("Created {}".format(final_path))
print ("Generating ZIP file")

os.system('find static/badges/ \( -iname \*.svg -o -iname \*.pdf \) | zip -@ static/badges/all-badges.zip')

print ('Generating ZIP of SVG files')

input_folders = [file for file in os.listdir(BADGES_FOLDER)
if file.lower().endswith(".badges")]

for folder in input_folders:
folder_path = os.path.join(BADGES_FOLDER, folder)
file_name = folder_path + '.svg.zip'
os.system('find ' + folder_path + ' -type f -name *.svg | zip -@ ' + file_name)