-
-
Save pao/2280993 to your computer and use it in GitHub Desktop.
#!/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, quoting fixed. Not sure about the other thing yet. Standby...
Okay, tried it with a better testcase and made a couple more small updates. Let's see how we did...
Testing: Deleting a folder.
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!
I'm getting an error message on the newest version of the script.
http://i.imgur.com/ct3IR.png
Blargh. Will look at this shortly.
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.
Okay, now Python 3ified!
Works perfectly!
Thanks!
Woah. I think the script works too well xD
So I made a test situation where I deleted a folder. I ran the script, checked the .bat and .sh, and ran it.
It ended up deleting the entire folder xD
SET /P M=Do you want to continue? [Y/N]:
IF %M%==Y GOTO DELETE
IF %M%==N GOTO EOF
:DELETE
rmdir /s /q .\Fanmade\Artists&I
Instead of deleting just the "&I" folder, it deleted the entire "Artists" folder. Strange.
Also, the python script creates this
del .\Fanmade\Album Art\4-Bit Insurgence.png
When i tried that, it didn't delete the file, but if I add quotation marks...
del ".\Fanmade\Album Art\4-Bit Insurgence.png"
it does delete the file.
Finally, the command prompt looks weird, and it doesn't stop when it says that the task has been completed.
ORIGINAL BAT
ECHO OFF
CLS
:MENU
ECHO ==============================================
ECHO = MLP MUSIC ARCHIVE UPDATER =
ECHO ==============================================
ECHO = v008a: "Insert Title Here!" =
ECHO ==============================================
ECHO.
ECHO This program will delete any unnecessary, duplicated, or buggy files from the
ECHO previous update.
ECHO.
ECHO Be sure this file is at the ROOT of the Archive folder (With the README.TXT,
ECHO CHANGELOG.TXT, Fanmade and Official folders).
ECHO.
ECHO Apply these updates before adding the new songs!
ECHO.
ECHO Example process of installing update packs:
ECHO 1: Apply Update 1.bat
ECHO 2: Add New Songs 1.rar
ECHO 3: Apply Update 2.bat
ECHO 4: Add New Songs 2.rar
ECHO 5: Etc...
ECHO.
ECHO Install this bat before adding the new songs contained in this update!
ECHO.
SET /P M=Do you want to continue? [Y/N]:
IF %M%==Y GOTO DELETE
IF %M%==N GOTO EOF
:DELETE
DEL "Fanmade\Artists------"
rd /s /q "Fanmade\Artists\Emmaeiou"
rd /s /q "Fanmade\Artists---"
ECHO Operation completed!
ECHO.
PAUSE
PYTHON SCRIPT GENERATED BAT
echo ==============================================
echo = MLP MUSIC ARCHIVE UPDATER =
echo ==============================================
echo = v000X "New Update!" =
echo ==============================================
echo.
echo This program will delete any unnecessary, duplicated, or buggy files from the
echo previous update.
echo.
echo Be sure this file is at the ROOT of the Archive folder (With the README.TXT,
echo CHANGELOG.TXT, Fanmade and Official folders).
echo.
echo Apply these updates before adding the new songs!
echo.
echo Example process of installing update packs:
echo 1: Apply Update 1.bat
echo 2: Add New Songs 1.rar
echo 3: Apply Update 2.bat
echo 4: Add New Songs 2.rar
echo 5: Etc...
SET /P M=Do you want to continue? [Y/N]:
IF %M%==Y GOTO DELETE
IF %M%==N GOTO EOF
:DELETE
del .\Fanmade\Album Art\4-Bit Insurgence.png
echo Operation completed.