1 # Copyright (C) 2006, Thomas Leonard
2 # See the README file for details, or visit http://0install.net.
5 from zeroinstall
.zerostore
.manifest
import generate_manifest
, verify
6 from zeroinstall
import zerostore
, SafeException
14 stores
= zerostore
.Stores()
16 class UsageError(SafeException
): pass
18 def do_manifest(args
):
19 """manifest DIRECTORY"""
20 if len(args
) != 1: raise UsageError("Wrong number of arguments")
23 for line
in generate_manifest(args
[0]):
25 digest
.update(line
+ '\n')
26 print "sha1=" + digest
.hexdigest()
31 if len(args
) != 1: raise UsageError("Wrong number of arguments")
33 print stores
.lookup(args
[0])
35 except zerostore
.BadDigest
, ex
:
36 print >>sys
.stderr
, ex
37 except zerostore
.NotStored
, ex
:
38 print >>sys
.stderr
, ex
42 """add DIGEST (DIRECTORY | (ARCHIVE [EXTRACT])"""
43 if len(args
) < 2: raise UsageError("Missing arguments")
45 if os
.path
.isdir(args
[1]):
46 if len(args
) > 2: raise UsageError("Too many arguments")
47 stores
.add_dir_to_cache(digest
, args
[1])
48 elif os
.path
.isfile(args
[1]):
49 if len(args
) > 3: raise UsageError("Too many arguments")
54 stores
.add_archive_to_cache(digest
, file(args
[1]), args
[1], extract
)
56 raise UsageError("No such file or directory '%s'" % args
[1])
59 """verify (DIGEST | DIRECTORY)"""
60 if len(args
) != 1: raise UsageError("Missing DIGEST or DIRECTORY")
61 root
= get_stored(args
[0])
63 print "Verifying", root
67 except zerostore
.BadDigest
, ex
:
74 def show_changes(actual
, saved
):
76 for line
in difflib
.unified_diff(saved
, actual
, 'Recorded', 'Actual'):
81 if args
: raise UsageError("List takes no arguments")
82 print "User store (writable) : " + stores
.stores
[0].dir
83 for s
in stores
.stores
[1:]:
84 print "System store : " + s
.dir
85 if len(stores
.stores
) < 2:
86 print "No system stores."
88 def get_stored(dir_or_digest
):
89 if os
.path
.isdir(dir_or_digest
):
93 return stores
.lookup(dir_or_digest
)
94 except zerostore
.NotStored
, ex
:
95 print >>sys
.stderr
, ex
98 commands
= [do_add
, do_find
, do_list
, do_manifest
, do_verify
]