Skip to contentSkip to conversation

source-checker: usability tweaks

PATCH Superseded Michael Smith 3 participants 7 messages Jun 27, 2009 – Jun 30, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 source-checker: usability tweaks

checksum sorter, source checker: exit(1) on failure; send usage to stderr; make executable

checksum sorter: read from stdin if no arg; accept --inplace option to read and write from the same file.

---
 contrib/source-checker/oe-checksums-sorter.py |   59 +++++++++++++++++++++----
 contrib/source-checker/oe-source-checker.py   |    4 +-
 2 files changed, 52 insertions(+), 11 deletions(-)
 mode change 100644 => 100755 contrib/source-checker/oe-checksums-sorter.py
 mode change 100644 => 100755 contrib/source-checker/oe-source-checker.py
contrib/source-checker/oe-checksums-sorter.py +50 -9
old mode 100644
new mode 100755
index 2bab58c..7388c62
@@ -29,20 +29,57 @@
 #
 
 
+import ConfigParser
+import getopt
+import os
 import sys
+import tempfile
 
-if len(sys.argv) < 2:
-    print """
-    OpenEmbedded source checksums script require argument:
+def usage():
+    print >> sys.stderr, \
+    """usage: %s [--inplace|-i] conf/checksums.ini
 
-    1. location of conf/checksums.ini
-    """
-    sys.exit(0)
+    --inplace, -i: update file in place (default is to write to stdout)
 
-import ConfigParser, os
+    If no input file is given, will read from standard input.
+    """ % sys.argv[0]
+    sys.exit(1)
+
+try:
+    optlist, args = getopt.getopt(sys.argv[1:], "ih", ["inplace", "help"])
+except getopt.GetoptError, e:
+    print >> sys.stderr, "%s: %s" % (sys.argv[0], e)
+    usage()
+
+inplace = False
+readstdin = False
+for opt, val in optlist:
+    if opt == '-i' or opt == '--inplace':
+        inplace = True
+    else:
+        usage()
+
+if len(args) == 0:
+    readstdin = True
+    if inplace:
+        print >> sys.stderr, "%s: --inplace requires a filename" % sys.argv[0]
+        usage()
+
+if len(args) > 1:
+    usage()
+
+out = sys.stdout
+outfn = None
+if inplace:
+    outfd, outfn = tempfile.mkstemp(prefix='cksums')
+    out = os.fdopen(outfd, 'w')
 
 checksums_parser = ConfigParser.ConfigParser()
-checksums_parser.read(sys.argv[1])
+
+if readstdin:
+    checksums_parser.readfp(sys.stdin)
+else:
+    checksums_parser.read(args[0])
 
 item = 1;
 files_total   = len(checksums_parser.sections())
@@ -60,4 +97,8 @@ for source in checksums_parser.sections():
 new_list.sort()
 
 for entry in new_list:
-    print "[%s]\nmd5=%s\nsha256=%s\n" % (entry[1], entry[2], entry[3])
+    print >> out, "[%s]\nmd5=%s\nsha256=%s\n" % (entry[1], entry[2], entry[3])
+
+if inplace:
+    out.close()
+    os.rename(outfn, args[0])
contrib/source-checker/oe-source-checker.py +2 -2
old mode 100644
new mode 100755
index 0ae3563..79e988b
@@ -39,13 +39,13 @@
 import sys
 
 if len(sys.argv) < 3:
-    print """
+    print >> sys.stderr, """
     OpenEmbedded source checker script require two arguments:
 
     1. location of conf/checksums.ini
     2. path to DL_DIR (without "/" at the end)
     """
-    sys.exit(0)
+    sys.exit(1)
 
 import ConfigParser, os, itertools
 
Signature · 1 line
1.6.3
#1 lore raw

PATCH 2/2 oe-checksums-sorter.py: speed up about 4X using a hash

OK, this saves a grand total of one second a few times a week, but it was bugging me...

---
 contrib/source-checker/oe-checksums-sorter.py |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
contrib/source-checker/oe-checksums-sorter.py +5 -2
index 7388c62..821d247 100755
@@ -85,14 +85,17 @@ item = 1;
 files_total   = len(checksums_parser.sections())
 
 new_list = []
+seen = {}
 
 for source in checksums_parser.sections():
     archive = source.split("/")[-1]
     md5 = checksums_parser.get(source, "md5")
     sha = checksums_parser.get(source, "sha256")
 
-    if new_list.count([archive, source, md5, sha]) < 1:
-        new_list += [[archive, source, md5, sha]]
+    tup = (archive, source, md5, sha)
+    if not seen.has_key(tup):
+        new_list.append(tup)
+        seen[tup] = 1
 
 new_list.sort()
 
Signature · 1 line
1.6.3
#2 lore raw
In reply to Michael Smith

Dnia sobota, 27 czerwca 2009 o 23:29:38 Michael Smith napisał(a):

Quoted 3 lines

checksum sorter, source checker:

exit(1) on failure;

OK with it

Quoted 1 line

send usage to stderr;

NACK - most of tools which I use use stdout for --help/usage stuff.

Quoted 1 line

make executable

OK

Quoted 3 lines

checksum sorter:

read from stdin if no arg;

OK

Quoted 1 line

accept --inplace option to read and write from the same file.

Idea nice but:

12:00 hrw@home:openembedded$ contrib/source-checker/oe-checksums- sorter.py -i conf/checksums.ini
Traceback (most recent call last):
File "contrib/source-checker/oe-checksums-sorter.py", line 104, in
<module>

    os.rename(outfn, args[0])

OSError: [Errno 18] Invalid cross-device link

/tmp/ is tmpfs, /home/ is ext3 and Python documentation says "The operation may fail on some Unix flavors if src and dst are on different filesystems".

Regards,

Signature · 3 lines
JID:      [email protected]
Website:  http://marcin.juszkiewicz.com.pl/
LinkedIn: http://www.linkedin.com/in/marcinjuszkiewicz
#3 lore raw
Quoted 34 lines · Marcin Juszkiewicz

Dnia sobota, 27 czerwca 2009 o 23:29:38 Michael Smith napisał(a):

Quoted 3 lines

checksum sorter, source checker:

exit(1) on failure;

OK with it

Quoted 1 line

send usage to stderr;

NACK - most of tools which I use use stdout for --help/usage stuff.

Quoted 1 line

make executable

OK

Quoted 3 lines

checksum sorter:

read from stdin if no arg;

OK

Quoted 1 line

accept --inplace option to read and write from the same file.

Idea nice but:

12:00 hrw@home:openembedded$ contrib/source-checker/oe-checksums- sorter.py -i conf/checksums.ini
Traceback (most recent call last):
 File "contrib/source-checker/oe-checksums-sorter.py", line 104, in <module>
   os.rename(outfn, args[0])
OSError: [Errno 18] Invalid cross-device link

Quoted 3 lines

/tmp/ is tmpfs, /home/ is ext3 and Python documentation says "The operation may fail on some Unix flavors if src and dst are on different filesystems".

This should be using shutil.move(), which knows to use rename when possible and copy/unlink when not.

Signature · 7 lines
Chris Larson
clarson at kergoth dot com
clarson at mvista dot com
Founder - BitBake, OpenEmbedded, OpenZaurus
Maintainer - Tslib
Software Engineer
MontaVista Software, Inc.
#4 lore raw
In reply to Marcin Juszkiewicz
Quoted 3 lines · Marcin Juszkiewicz

Dnia sobota, 27 czerwca 2009 o 23:29:38 Michael Smith napisał(a):

Quoted 1 line

send usage to stderr;

NACK - most of tools which I use use stdout for --help/usage stuff.

OK, right, errors should go to stderr and usage to stdout.

Quoted 2 lines
Quoted 1 line

accept --inplace option to read and write from the same file.

OSError: [Errno 18] Invalid cross-device link

Yes, the tmpfile needs to be in the same directory as the checksums.ini. I'll fix that. I think os.rename is still the right way to do it (it's guaranteed to be atomic, or fall flat on its face :).

Mike

#5 lore raw

PATCH v2 1/2 source-checker: usability tweaks

checksum sorter, source checker:
* exit(1) on failure;
* send errors to stderr;
* make executable

checksum sorter:
* read from stdin if no arg;
* accept --inplace option to read and write from the same file;
* check that input file exists as ConfigParser.read() doesn't treat missing files as an error.

---
 contrib/source-checker/oe-checksums-sorter.py |   65 +++++++++++++++++++++----
 contrib/source-checker/oe-source-checker.py   |    3 +-
 2 files changed, 58 insertions(+), 10 deletions(-)
 mode change 100644 => 100755 contrib/source-checker/oe-checksums-sorter.py
 mode change 100644 => 100755 contrib/source-checker/oe-source-checker.py
contrib/source-checker/oe-checksums-sorter.py +56 -9
old mode 100644
new mode 100755
index 2bab58c..ccf76d4
@@ -29,20 +29,63 @@
 #
 
 
+import ConfigParser
+import getopt
+import os
 import sys
+import tempfile
 
-if len(sys.argv) < 2:
-    print """
-    OpenEmbedded source checksums script require argument:
+def usage(rc):
+    print """usage: %s [--inplace|-i] conf/checksums.ini
 
-    1. location of conf/checksums.ini
-    """
-    sys.exit(0)
+    --inplace, -i: update file in place (default is to write to stdout)
 
-import ConfigParser, os
+    If no input file is given, will read from standard input.
+    """ % sys.argv[0]
+    sys.exit(rc)
+
+try:
+    optlist, args = getopt.getopt(sys.argv[1:], "ih", ["inplace", "help"])
+except getopt.GetoptError, e:
+    print >> sys.stderr, "%s: %s" % (sys.argv[0], e)
+    usage(1)
+
+inplace = False
+infp = sys.stdin
+filename = None
+for opt, val in optlist:
+    if opt == '-i' or opt == '--inplace':
+        inplace = True
+    elif opt == 'h' or opt == '--help':
+        usage(0)
+    else:
+        print >> sys.stderr, "%s: %s: invalid argument" % (sys.argv[0], opt)
+        usage(1)
+
+if len(args) == 0:
+    if inplace:
+        print >> sys.stderr, "%s: --inplace requires a filename" % sys.argv[0]
+        usage(1)
+elif len(args) == 1:
+    filename = args[0]
+    try:
+        infp = open(filename, "r")
+    except Exception, e:
+        print >> sys.stderr, "%s: %s" % (sys.argv[0], e)
+        sys.exit(1)
+else:
+    print >> sys.stderr, "%s: extra arguments" % sys.argv[0]
+    usage(1)
+
+out = sys.stdout
+tmpfn = None
+if inplace:
+    outfd, tmpfn = tempfile.mkstemp(prefix='cksums',
+                                    dir=os.path.dirname(filename) or '.')
+    out = os.fdopen(outfd, 'w')
 
 checksums_parser = ConfigParser.ConfigParser()
-checksums_parser.read(sys.argv[1])
+checksums_parser.readfp(infp)
 
 item = 1;
 files_total   = len(checksums_parser.sections())
@@ -60,4 +103,8 @@ for source in checksums_parser.sections():
 new_list.sort()
 
 for entry in new_list:
-    print "[%s]\nmd5=%s\nsha256=%s\n" % (entry[1], entry[2], entry[3])
+    print >> out, "[%s]\nmd5=%s\nsha256=%s\n" % (entry[1], entry[2], entry[3])
+
+if inplace:
+    out.close()
+    os.rename(tmpfn, filename)
contrib/source-checker/oe-source-checker.py +2 -1
old mode 100644
new mode 100755
index 0ae3563..bb14c87
@@ -39,13 +39,14 @@
 import sys
 
 if len(sys.argv) < 3:
+    print >> sys.stderr, "%s: missing argument" % sys.argv[0]
     print """
     OpenEmbedded source checker script require two arguments:
 
     1. location of conf/checksums.ini
     2. path to DL_DIR (without "/" at the end)
     """
-    sys.exit(0)
+    sys.exit(1)
 
 import ConfigParser, os, itertools
 
Signature · 1 line
1.6.3
#6 lore raw

PATCH v2 2/2 oe-checksums-sorter.py: speed up about 4X using a hash

OK, this saves a grand total of one second a few times a week, but it was bugging me...

---
 contrib/source-checker/oe-checksums-sorter.py |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
contrib/source-checker/oe-checksums-sorter.py +5 -5
index ccf76d4..a72b202 100755
@@ -87,18 +87,18 @@ if inplace:
 checksums_parser = ConfigParser.ConfigParser()
 checksums_parser.readfp(infp)
 
-item = 1;
-files_total   = len(checksums_parser.sections())
-
 new_list = []
+seen = {}
 
 for source in checksums_parser.sections():
     archive = source.split("/")[-1]
     md5 = checksums_parser.get(source, "md5")
     sha = checksums_parser.get(source, "sha256")
 
-    if new_list.count([archive, source, md5, sha]) < 1:
-        new_list += [[archive, source, md5, sha]]
+    tup = (archive, source, md5, sha)
+    if not seen.has_key(tup):
+        new_list.append(tup)
+        seen[tup] = 1
 
 new_list.sort()
 
Signature · 1 line
1.6.3