euscan: euscan_patch_metadata --diff option

Signed-off-by: volpino <fox91@anche.no>
This commit is contained in:
volpino 2012-07-22 10:26:40 +02:00
parent edf549bc40
commit 634c2db389

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import os import os
import sys
import re import re
import urllib import urllib
from tempfile import mkstemp from tempfile import mkstemp
@ -8,6 +9,7 @@ import tarfile
import gzip import gzip
import logging import logging
import shutil import shutil
import subprocess
from gentoolkit.query import Query from gentoolkit.query import Query
from BeautifulSoup import BeautifulSoup, SoupStrainer from BeautifulSoup import BeautifulSoup, SoupStrainer
@ -130,7 +132,7 @@ def get_deb_url(name):
return deb_url, deb_type return deb_url, deb_type
def patch_metadata(metadata_path, watch_data): def patch_metadata(metadata_path, watch_data, diff=False):
watch_data = "\n".join([line for line in watch_data.split("\n") watch_data = "\n".join([line for line in watch_data.split("\n")
if not line.startswith("#")]) # comments if not line.startswith("#")]) # comments
watch_data = watch_data.replace("\\\n", "") # remove backslashes watch_data = watch_data.replace("\\\n", "") # remove backslashes
@ -167,10 +169,20 @@ def patch_metadata(metadata_path, watch_data):
(rindent, watch_tag, rindent) (rindent, watch_tag, rindent)
data = data.replace('</pkgmetadata>', rep, 1) data = data.replace('</pkgmetadata>', rep, 1)
print data if not diff:
return data
else:
_, data_path = mkstemp()
with open(data_path, "w") as f:
f.write(data)
sub = subprocess.Popen(["diff", metadata_path, data_path],
stdout=subprocess.PIPE)
content = sub.stdout.read()
os.unlink(data_path)
return content
def process_package(query): def process_package(query, diff=False):
matches = Query(query).smart_find( matches = Query(query).smart_find(
in_installed=True, in_installed=True,
in_porttree=True, in_porttree=True,
@ -194,7 +206,7 @@ def process_package(query):
if watch_data is None: if watch_data is None:
logger.error(" No watch file found") logger.error(" No watch file found")
else: else:
patch_metadata(metadata_path, watch_data) return patch_metadata(metadata_path, watch_data, diff=diff)
def main(): def main():
@ -202,13 +214,17 @@ def main():
p = optparse.OptionParser( p = optparse.OptionParser(
usage="usage: %prog <package> [<package> [...]]", usage="usage: %prog <package> [<package> [...]]",
) )
p.add_option('-d', '--diff', action="store_true", dest="diff",
default=False,
help="Outputs a diff")
opts, packages = p.parse_args() opts, packages = p.parse_args()
logging.basicConfig(level=logging.INFO, format='%(message)s') logging.basicConfig(stream=sys.stderr, level=logging.INFO,
format='%(message)s')
for package in packages: for package in packages:
logger.info("Processing %s..." % package) logger.info("Processing %s..." % package)
process_package(package) print process_package(package, opts.diff)
if __name__ == "__main__": if __name__ == "__main__":
main() main()