How to pass the parameters to a script?

Reply

Join Date: Dec 2007
Posts: 2
Reputation: balance is an unknown quantity at this point 
Solved Threads: 0
balance balance is offline Offline
Newbie Poster

How to pass the parameters to a script?

 
0
  #1
Dec 6th, 2007
Hi. I don't know anything about python but I need to use a script which I have found on the internet. I don't know where to start. I have installed python on my windows xp desktop and I have also downloaded and put in the Lib folder the file BeautifulSoup.py (http://www.crummy.com/software/BeautifulSoup/) since I know that the script needs it. The aim of the script is to generate an html file with a list of links based on the name of a linux package. Infact I need to use this script to download all the *.deb files of a given package plus all the .deb files which depend upon that a package from http://packages.ubuntu.com/. I guess that it's very easy to use this script but I don't know how! Help would be really appreciated.

  1.  
  2. """ubuntu deb digger"""
  3.  
  4. from BeautifulSoup import BeautifulSoup
  5. import urllib
  6. import urlparse
  7. import re
  8.  
  9. _depgif = '../../Pics/dep.gif'
  10. _deps = {}
  11.  
  12. def get_debs(url, arch="i386", packages=None):
  13. """grab the deb defined by url from packages.ubuntu.com and all its dependencies"""
  14. if packages is None:
  15. packages = {}
  16. source = urllib.urlopen(url).read()
  17. soup = BeautifulSoup(source)
  18. downloadheader = soup('div', {'id': 'pdownload'})[0].h2
  19. name = downloadheader.string.replace('Download ','')
  20. if name in packages:
  21. return {}
  22. print name
  23.  
  24. # update the packages dictionary with the download link for this package
  25. archlinks = [link for link in soup('a') if link.string in (arch, 'all')]
  26. archlink = urlparse.urljoin(url,archlinks[0]['href'])
  27. mirrorpage = urllib.urlopen(archlink).read()
  28. mirrorsoup = BeautifulSoup(mirrorpage)
  29. downloadlink = mirrorsoup.firstText('archive.ubuntu.com/ubuntu').parent['href']
  30. packages.update({name: downloadlink})
  31.  
  32. # get dependencies
  33. deplinks = [dt.a for dt in soup('dt') if dt.img['src'] == _depgif]
  34. for link in deplinks:
  35. get_debs(urlparse.urljoin(url,link['href']), packages=packages)
  36.  
  37. return packages
  38.  
  39. if __name__ == '__main__':
  40. import sys
  41. packages = get_debs(sys.argv[0])
  42. html = "\n".join(["<a href='%s'>%s</a><br/>" % (value,key) for key,value in packages.iteritems()])
  43. print 'writing packages.html'
  44. open('packages.html','w').write(html)

If i try to run it I get:

  1. Traceback (most recent call last):
  2. File "C:\Documents and Settings\Andrea\Desktop\UbuntuPackageGrabber.py", line 40, in <module>
  3. packages = get_debs(sys.argv[0])
  4. File "C:\Documents and Settings\Andrea\Desktop\UbuntuPackageGrabber.py", line 15, in get_debs
  5. source = urllib.urlopen(url).read()
  6. File "C:\Python25\lib\urllib.py", line 82, in urlopen
  7. return opener.open(url)
  8. File "C:\Python25\lib\urllib.py", line 187, in open
  9. return self.open_unknown(fullurl, data)
  10. File "C:\Python25\lib\urllib.py", line 199, in open_unknown
  11. raise IOError, ('url error', 'unknown url type', type)
  12. IOError: [Errno url error] unknown url type: 'c'
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: How to pass the parameters to a script?

 
0
  #2
Dec 6th, 2007
Hi balance,

How are you running it?

The error occurs because the "downloader" module, urllib, has a urlopen() function which doesn't recognize the URL being passed in. If you step backwards, you see that the error is in line 15 of UbuntuPackageGrabber.py, the main script. That line says:
  1. source = urllib.urlopen(url).read()
where 'url' is an input parameter to the get_debs() function. If we then look for where the get_debs() function is called, we see that it's at line 40 of the same program, which is:
  1. packages = get_debs(sys.argv[0])
So 'url' is really sys.argv[0]. In case you didn't know, sys.argv is the list of command-line tokens, and sys.argv[0] ought to be the first of those tokens, the program name (all other tokens start at 1 and continue in that fashion). So the script tells urllib to open a URL with the same name as the script? This seems like it should be a bug!

What is the input to this script supposed to be? A package name? Because it seems like it's expecting a URL. Is that URL supposed to be http://packages.ubuntu.com? Is it supposed to be the link for the package? I have no idea what's going on, and I can't find this script through Google.

Do you have a link to the script's documentation?
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2
Reputation: balance is an unknown quantity at this point 
Solved Threads: 0
balance balance is offline Offline
Newbie Poster

Re: How to pass the parameters to a script?

 
0
  #3
Dec 6th, 2007
First of all thank you for your kind reply!
Unfortunately I don't have the documentation of the script!

You're right I should pass an url to the script. For example it should be possible to pass an url like this:

http://packages.ubuntu.com/gutsy/base/adduser

My problem is that I don't know how to pass it. Maybe sys.argv[0] is used to pass the url directly from the command line? But also if this was true, I wouldn't know how to do it!

Hoping that you can tell me how to pass the url to the script

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: How to pass the parameters to a script?

 
0
  #4
Dec 6th, 2007
Hi balance,

You should open a command prompt, navigate to the directory where the script is, and invoke it by saying:
  1. python UbuntuPackageGrabber.py URL_GOES_HERE
Substitute the package URL you have in mind. But I still think it will misfire, unless you change sys.argv[0] to sys.argv[1]. Try it both ways and see what happens.
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC