Skip to content

Commit

Permalink
added pysed and itunes_diff
Browse files Browse the repository at this point in the history
  • Loading branch information
dgilman committed Mar 21, 2011
1 parent 4a79c50 commit 7aa7f20
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions bashrc
Expand Up @@ -4,6 +4,7 @@ export EDITOR=vim
export PAGER=w3m
export COWPATH=/opt/local/share/cowsay
export LDFLAGS=-L/opt/local/lib/
export LESS=-R

stty erase ^H

Expand Down
2 changes: 2 additions & 0 deletions bin/README
Expand Up @@ -2,4 +2,6 @@ irssinp: AppleScript that returns a string with the current iTunes playing song.
flac2mp3: extract flac metadata, encode to -V 2 mp3, set id3 tags with id3v2
latest_additions.php: RSS feed of changes to a bugzilla query. as-is it shows changes in the firefox 4 blocker list. you can pretty easily change this to any bugzilla or query.
treediff.py: show files not common to two paths
pysed.py: primitive sed with python regex.
itunes_diff.py: given an itunes plist and a dirtree shows you files in dirtree not in itunes. use it to get rid of duplicate songs in itunes after a "Organize Library".

50 changes: 50 additions & 0 deletions bin/itunes_diff.py
@@ -0,0 +1,50 @@
import plistlib
import sys
import os
import urllib
import re

stripper = re.compile("^file://localhost(.*)")

#file://localhost/Volumes/ntfs/Music/Music/Apple%20Computer/Developer%20Helper%20Volume%201-%20Phi/07%20Breaking%20Through.mp3

if len(sys.argv) < 3:
print "Usage: itunes_diff.py iTunes\ Music\ Library.xml /path/to/itunesdir [--ignore-dotfiles]"
sys.exit()

ignore_dotfiles = False
try:
if sys.argv[3] == "--ignore-dotfiles":
ignore_dotfiles = True
except IndexError:
pass

countdict = plistlib.readPlist(sys.argv[1])

walker = os.walk(sys.argv[2])

hard_drive_paths = set()
itunes_library_paths = set()

for directory in walker:
for filename in directory[2]:
hard_drive_paths.add(os.path.join(directory[0], filename))

for track in countdict["Tracks"].iteritems():
if "Location" in track[1]:
unquoted = urllib.unquote(track[1]["Location"])
match = stripper.search(unquoted)
if match:
itunes_library_paths.add(match.group(1))

hd_only = hard_drive_paths - itunes_library_paths

for x in hd_only:
if not ignore_dotfiles:
print x
else:
if not os.path.basename(x).startswith('.'):
print x

#hopefully there aren't any newlines in your mp3 file names!

47 changes: 47 additions & 0 deletions bin/pysed.py
@@ -0,0 +1,47 @@
#!/usr/bin/env python
import re
import fileinput
import sys, os

cheatsheet = """You need two args!
pysed.py: regex replacement [file1, file2...]
*: match 0 or more
+: match 1 or more
?: match 0 or 1
*?, +?, ??: non-greedy matching
\d , \D: any digit, any not digit
\s , \S: any whitespace, any not whitespace
\w , \W: any number, letter or _, any not number, letter or _
{n}: match n repetitions
{m,n}: match m to n repetitions
(?iLmsux): one or more chars from set adjust group matching.
i: ignore case
L: locale dependent
m: does not do what you might expect
s: . also matches newlines
u: unicode dependent
x: ignore spaces except when escaped, comments after #
(?:): group can't be backreferenced
(?P<foo>): you can backreference group with \gfoo in substitution or (?P=foo) in regex
(?(backref)regex|regex): ternary if backref exists"""

if len(sys.argv) < 3:
print >> sys.stderr, cheatsheet
sys.exit()

regex = re.compile(sys.argv[1])
replacement = sys.argv[2]

sedfiles = []
if len(sys.argv) > 3:
for sedfile in sys.argv[3:]:
if os.path.isfile(sedfile):
sedfiles.append(sedfile)

for line in fileinput.input(sedfiles):
sys.stdout.write(re.sub(regex, replacement, line))

0 comments on commit 7aa7f20

Please sign in to comment.