Created
April 2, 2012 05:26
-
-
Save pao/2280993 to your computer and use it in GitHub Desktop.
Helper to write the MLP Music Archive update script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
''' | |
Run me like so: | |
python write_update_scripts.py OLD_DIRECTORY NEW_DIRECTORY BANNERFILE | |
''' | |
import sys | |
import os.path as op | |
import ntpath as wp | |
import posixpath as pp | |
from filecmp import dircmp | |
from functools import partial | |
u8 = partial(bytes, encoding='utf-8') | |
bat_ask = ''' | |
SET /P M=Do you want to continue? [Y/N]: | |
IF %M%==Y GOTO DELETE | |
IF %M%==y GOTO DELETE | |
EXIT | |
:DELETE | |
''' | |
sh_ask = ''' | |
echo "Do you want to continue? [Y/N]:" | |
read CONTINUE | |
if [ $CONTINUE = "y" -o $CONTINUE = "Y" ]; then | |
''' | |
def write_delete_lines(dc, basedir, dirlevel, batscr, shscr): | |
for fn in dc.left_only: | |
filepath = dirlevel+[fn] | |
if op.isdir(op.join(basedir, *filepath)): | |
batscr.write(u8(''.join(['rmdir /s /q "', wp.join(*filepath), '"\r\n']))) | |
shscr.write(u8(''.join(['rm -rf "', pp.join(*filepath), '"\n']))) | |
else: | |
batscr.write(u8(''.join(['del "', wp.join(*filepath), '"\r\n']))) | |
shscr.write(u8(''.join(['rm -f "', pp.join(*filepath), '"\n']))) | |
for (subdir, subdc) in dc.subdirs.items(): | |
write_delete_lines(subdc, basedir, dirlevel + [subdir], batscr, shscr) | |
def main(dirold, dirnew, bannerfile=None): | |
with open('update.bat', 'wb') as batscr, open('update.sh', 'wb') as shscr: | |
batscr.write(u8("@echo off\n")) | |
shscr.write(u8("#!/bin/sh\n")) | |
if bannerfile is not None: | |
with open(bannerfile) as f: | |
for line in f: | |
if line.strip(): | |
batscr.write(u8(' '.join(['echo', line]))) | |
else: | |
batscr.write(u8('echo.\r\n')) | |
shscr.write(u8(''.join(['echo "', line.strip(), '"\n']))) | |
batscr.write(u8(bat_ask)) | |
shscr.write(u8(sh_ask)) | |
write_delete_lines(dircmp(dirold, dirnew), dirold, ['.'], batscr, shscr) | |
batscr.write(u8('echo Operation completed.\r\n')) | |
shscr.write(u8('echo "Operation completed."\nfi\n')) | |
if __name__ == "__main__": | |
if len(sys.argv) > 3: | |
main(sys.argv[1], sys.argv[2], sys.argv[3]) | |
else: | |
main(sys.argv[1], sys.argv[2]) |
Okay, now Python 3ified!
Works perfectly!
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like you're using Python 3? I wrote this for 2.7, so you're running into this, which cropped up when I opened the files in binary mode (to get around the Windows line break problem).
Shouldn't take long to port, but got to download and install Python 3.x first.