- initial import of revision 374 from cnc
[apt.git] / contrib / apt-files / convert.py
1 #!/usr/bin/python
2 import sys, os
3 import rpm
4
5 def main():
6     if len(sys.argv) == 1:
7         sys.exit("Usage: convert.py <hdlist file|rpms dir> ...")
8
9     if not hasattr(rpm, "headerFromPackage"):
10         ts = rpm.TransactionSet()
11     else:
12         ts = None
13
14     for entry in sys.argv[1:]:
15         if os.path.isfile(entry):
16             for header in rpm.readHeaderListFromFile(entry):
17                 name = header[rpm.RPMTAG_NAME]
18                 for filename in header[rpm.RPMTAG_FILENAMES]:
19                     print filename, name
20         if os.path.isdir(entry):
21             for filename in os.listdir(entry):
22                 if filename.endswith(".rpm"):
23                     filepath = os.path.join(entry, filename)
24                     file = open(filepath)
25                     if ts:
26                         header = ts.hdrFromFdno(file.fileno())
27                     else:
28                         header = rpm.headerFromPackage(file.fileno())[0]
29                     name = header[rpm.RPMTAG_NAME]
30                     for filename in header[rpm.RPMTAG_FILENAMES]:
31                         print filename, name
32                     file.close()
33
34 if __name__ == "__main__":
35     main()
36