Skip to content

Instantly share code, notes, and snippets.

@pao
Created April 2, 2012 05:26
Show Gist options
  • Save pao/2280993 to your computer and use it in GitHub Desktop.
Save pao/2280993 to your computer and use it in GitHub Desktop.
Helper to write the MLP Music Archive update script
==============================================
= MLP MUSIC ARCHIVE UPDATER =
==============================================
= v007ab: "Sneak Attack! Huge Album Update!" =
==============================================
This program will delete any unnecessary, duplicated, or buggy files from the
previous update.
Be sure this file is at the ROOT of the Archive folder (With the README.TXT,
CHANGELOG.TXT, Fanmade and Official folders).
Apply these updates before adding the new songs!
Example process of installing update packs:
1: Apply Update 1.bat
2: Add New Songs 1.rar
3: Apply Update 2.bat
4: Add New Songs 2.rar
5: Etc...
FOR THIS SPECIFIC UPDATE, APPLY THIS BAT BEFORE ADDING THE FILES.
#!/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])
@roflcopter572
Copy link

Testing: Deleting a folder.

@roflcopter572
Copy link

Update:
The CMD output has been fixed, folder deletions and file deletions now work properly.

Still havn't tested it on Linux/Mac OSX. Oh tell. Thanks!

@roflcopter572
Copy link

I'm getting an error message on the newest version of the script.
http://i.imgur.com/ct3IR.png

@pao
Copy link
Author

pao commented Apr 27, 2012

Blargh. Will look at this shortly.

@pao
Copy link
Author

pao commented Apr 27, 2012

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.

@pao
Copy link
Author

pao commented Apr 27, 2012

Okay, now Python 3ified!

@roflcopter572
Copy link

Works perfectly!
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment