Skip to content

Instantly share code, notes, and snippets.

@fintanmm
Last active June 22, 2018 05:28
Show Gist options
  • Save fintanmm/659b7bb44e138b25bdf9fe71ade4719c to your computer and use it in GitHub Desktop.
Save fintanmm/659b7bb44e138b25bdf9fe71ade4719c to your computer and use it in GitHub Desktop.
Parses log file from alfresco bulk import tool then runs convmv on files
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse, sys, re, signal
from subprocess import call
parser = argparse.ArgumentParser()
parser.add_argument("--file", help="The log file to parse.")
parser.add_argument(
"--notest", help="Use --notest to finally rename the files.", action="store_true")
parser.add_argument("--printtest", help="Just print out line contents")
args = parser.parse_args()
# Print iterations progress
def progbar(curr, total, full_progbar):
frac = curr/total
filled_progbar = round(frac*full_progbar)
print('\r', '#' * filled_progbar + '-' * (full_progbar-filled_progbar), '[{:>7.2%} ]'.format(frac), "")
sys.stdout.flush()
def parse_logfile(logfile, callback=None, lineCount=0):
with open(logfile, "rb") as fileWithPaths:
x = 0
for line in fileWithPaths:
try:
path = re.search(b"(/.*/.*(?=[\']))", line).group()
x += 1
except AttributeError:
continue
if callback:
callback(path, x, lineCount)
if not callback:
return x
def convmv(path, x, lineCount):
if args.notest:
convmv = "convmv --notest -f iso-8859-1 -t utf8 -r"
else:
convmv = "convmv -f iso-8859-1 -t utf8 -r"
convmv_cmd = convmv.split(" ")
if args.printtest:
convmv_cmd.append("{0}".format(path))
print(" ".join(convmv_cmd))
else:
convmv_cmd.append(path)
call(convmv_cmd)
progbar(x, lineCount, 20)
if __name__ == '__main__':
lineCount = parse_logfile(args.file)
parse_logfile(args.file, convmv, lineCount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment