sources.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import csv
  2. import os
  3. from config import SOURCE_PATH, DL_PATH
  4. """ -- InstallerSource
  5. Read sources to Wise installers.
  6. The file format is CSV with the following columns
  7. (b3, md5, filesize, url). It does not have a header.
  8. """
  9. class InstallerSource:
  10. def __init__(self, data):
  11. self.b3, self.md5, self.filesize, self.dlUrl = data
  12. self.filesize = int(self.filesize)
  13. self.filepath = os.path.join(DL_PATH, self.b3)
  14. def getFilename(self):
  15. return os.path.basename(self.dlUrl)
  16. def InstallerSourceIt(swFile):
  17. with open(swFile, newline='') as fp:
  18. for row in csv.reader(fp):
  19. yield InstallerSource(row)
  20. def InstallerSourceUniqIt(swFile):
  21. sums = []
  22. with open(swFile, newline='') as fp:
  23. for row in csv.reader(fp):
  24. sw = InstallerSource(row)
  25. if sw.b3 in sums:
  26. continue
  27. sums.append(sw.b3)
  28. yield sw
  29. def InstallerSourceAllUniqIt():
  30. for ent in os.listdir(SOURCE_PATH):
  31. abspath = os.path.join(SOURCE_PATH, ent)
  32. if not os.path.isfile(abspath):
  33. continue
  34. if os.path.splitext(ent)[1] != ".csv":
  35. continue
  36. for source in InstallerSourceUniqIt(abspath):
  37. yield source