OpenSDE Framework (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.1 KiB

  1. import urllib2, re, os,sys,optparse
  2. from BeautifulSoup import BeautifulSoup
  3. from HTMLParser import HTMLParser
  4. from info import *
  5. class cParseHTML(HTMLParser):
  6. val=None
  7. def handle_starttag(self, tag, attrs):
  8. self.val=re.sub(self.get_starttag_text()+"|\n|\r",
  9. " ",self.val)
  10. def parse(self,string):
  11. self.val=string
  12. self.feed(self.val)
  13. return self.val
  14. cParse=cParseHTML()
  15. parse=cParse.parse
  16. repo=None
  17. pkg=None
  18. bdescd=False
  19. parser = optparse.OptionParser(usage)
  20. parser.add_option("-b", "--base", dest="base",
  21. help="""Change base i.e from
  22. http://packages.debian.org to http://packages.ubuntu.com""")
  23. parser.add_option("-d", "--distro",dest="distro",
  24. help="""chose distro i.e stable (default) testing,unstable
  25. oldstable experimental etc. """)
  26. parser.add_option("-f", "--force",
  27. action="store_true", dest="force",
  28. help="""Toggle force values on/off. Default can be set in
  29. lib/sde-download/info.py.""")
  30. parser.add_option("-g", "--guess",
  31. action="store_true", dest="guess",
  32. help="""Toggle guess values on/off. Default can be set in
  33. lib/sde-download/info.py.""")
  34. parser.add_option("-o", "--outpkg",dest="outpkg",
  35. help="""Write desc to package.""")
  36. (options, args) = parser.parse_args()
  37. argn=0
  38. if len(args) != 1:
  39. parser.error("incorrect number of arguments")
  40. if options.base:
  41. base=options.base
  42. if options.distro:
  43. distro=options.distro
  44. if options.force:
  45. if force:
  46. force=False
  47. else: force=True
  48. if options.guess:
  49. if guess:
  50. guess=False
  51. else: force=True
  52. nargs=len(sys.argv)-1
  53. if sys.argv[nargs].startswith("-") or sys.argv[nargs].startswith("--"):
  54. parse.error("Invalid options")
  55. opts=sys.argv[nargs].split("/")
  56. if len(opts) == 1:
  57. pkg=sys.argv[nargs]
  58. for cat in categories:
  59. url=base + "/" + distro + "/" + cat + "/" + pkg
  60. try:
  61. urllib2.urlopen(url)
  62. repo=cat
  63. except:pass
  64. elif len(opts)==2:
  65. repo=opts[0]
  66. pkg=opts[1]
  67. else:
  68. parse.error("Invalid options.")
  69. if not repo:parser.error("Package doesn't exist.")
  70. if not pkg:parse.error("Invalid options.")
  71. url=base + "/" + distro + "/" + repo + "/" + pkg
  72. try:
  73. page=urllib2.urlopen(url).read()
  74. except:
  75. print "Error: package '%s' does not exist." % sys.argv[nargs]
  76. sys.exit()
  77. soup = BeautifulSoup(page)
  78. copy=copynote(pkg)
  79. for link in soup("a"):
  80. link=str(link)
  81. if re.search("tar\.gz",link):
  82. buf=re.sub("^.|.$","",
  83. re.search('("|\').*("|\')',link).group())
  84. descd+=" " + buf.split("/").pop()
  85. descd+=" " + re.sub(buf.split("/").pop()+"$","",buf)
  86. bdescd=True
  87. try:
  88. descv="[V] " + re.sub("^.|.$","",re.search("\(.*\)",
  89. str(soup("h1"))).group())
  90. except: pass
  91. try:
  92. desci="[I] " + parse("".join(soup("h2")[0])).capitalize()+"."
  93. except: pass
  94. try:
  95. desct=parse(str(soup("p")[1]))
  96. except: pass
  97. lcat=soup("span")
  98. if len(lcat) > 0:
  99. lcat=re.sub("^.|.$","",str(lcat))
  100. if force:
  101. descu="[U] " + url
  102. desca="[A] Unknown"
  103. descl="[L] Unknown"
  104. descs="[S] Stable"
  105. if guess:
  106. try:
  107. stat=status[distro]
  108. descs="[S] " + stat
  109. except: pass
  110. try:
  111. cat=categories[repo]
  112. descc="[C] " + cat
  113. except: pass
  114. try:
  115. license=licenses[lcat]
  116. descl="[L] " + license
  117. except:pass
  118. sdesct=""
  119. cnt=0
  120. cnt2=0
  121. desct=re.sub("^\s*","",desct)
  122. for word in desct.split(" "):
  123. cnt+=len(word)+1
  124. if cnt < 76:
  125. if cnt2 == 0:
  126. sdesct+="[T] %s" % word
  127. cnt2=1
  128. else:sdesct+=" %s" % word
  129. else:
  130. sdesct+="\n[T] %s" % word
  131. cnt=1
  132. formatteddesc="""%(copy)s
  133. %(i)s
  134. %(t)s
  135. %(u)s
  136. %(a)s
  137. %(m)s
  138. %(c)s
  139. %(l)s
  140. %(s)s
  141. %(v)s
  142. %(p)s
  143. %(d)s
  144. """%{ "copy": copy,"i": desci,"t":sdesct,"u":descu,"a":desca,
  145. "m":descm,"c":descc,"l":descl,"s":descs,"v":descv,"p":descp,
  146. "d":descd}
  147. if options.outpkg:
  148. output=options.outpkg.split("/")
  149. optcnt=len(output) - output.count("")
  150. out=""
  151. if optcnt == 1:
  152. os.system("echo -e \"\033[33;1m=>\033[0m assuming reqested repository as 'wip.'\"")
  153. out="package/wip/" +output[0]+"/"+output[0]+".desc"
  154. dir="package/wip/" +output[0]
  155. if os.path.isdir(dir):
  156. if os.path.isfile(out):
  157. print """failed
  158. package %s belongs to wip!""" % output[0]
  159. sys.exit(1)
  160. else:
  161. os.mkdir(dir)
  162. elif optcnt==2:
  163. repository="package/" + output[0]
  164. if not os.path.isdir(repository):
  165. print "Error:invalid repository '%s'!" % dir
  166. sys.exit(1)
  167. dir=repository + "/" + output[1]
  168. if os.path.isdir(dir):
  169. out=dir+"/"+output[1]+".desc"
  170. if os.path.isfile(out):
  171. print "failed\n package %(p)s belongs to %(c)s!" %{"p":output[1],
  172. "c":output[0]}
  173. sys.exit(1)
  174. else:
  175. os.mkdir(dir)
  176. else:parser.error("Inavlid Option for --outpkg (-o)")
  177. try:
  178. os.system("echo -n 'Writing desc to file...'")
  179. outf=open(out,"w")
  180. outf.write(formatteddesc)
  181. outf.close()
  182. os.system("echo 'ok'")
  183. except:
  184. print"Error writing to file " + out
  185. if bdescd:
  186. os.system("echo -n 'Patching cksum...'")
  187. if os.popen("sde pkg up " + output[len(output)-1]+"&>/dev/null"):
  188. os.system("echo 'ok'")
  189. else:os.system("echo 'failed'")
  190. todo=""
  191. for line in formatteddesc.split("\n"):
  192. if re.search("^\[.\] TODO:",line):todo+="\n" + line
  193. if not todo == "":
  194. print "The following needs completing:"
  195. print todo
  196. else:
  197. print formatteddesc