-
Notifications
You must be signed in to change notification settings - Fork 0
/
sjcommon.py
103 lines (83 loc) · 2.61 KB
/
sjcommon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
#filename=common.py
import os
import sys
import tarfile
import socket
import string
import subprocess
import random
import time
Lid=[]
num=1
purduesquid="http://squid.rcac.purdue.edu"
purduename="purdue.edu"
numgetretries=3
osgsqquid=os.getenv("OSG_SQUID_LOCATION")
print "osg squid is set to:"+str(osgsquid)
hostname=socket.gethostname()
print "hostname is set to:"+str(hostname)
#if string.find(hostname,purduename)>=0:
# os.putenv("http_proxy",purduesquid)
#elif osgsquid != None and string.find(osgsquid.upper(),"UNAVAILABLE")<0:
# os.putenv("http_proxy",osgsquid)
#else:
# os.unsetenv("http_proxy")
#successdownload=False
if osgsquid != None:
das
else:
b
def download(urladdress)
for i in range(numgetretries):
print "Trial #: "+str(i)
child=subprocess.Popen(["wget","-q",urladdress],shell=True)
child.wait()
returncode=child.poll()
if returncode ==0:
successdownload ==1
break
else:
#after sleepfor second, try to download again
timeofsleep=(random.random())*180
time.sleep(timeofsleep)
@classmethod
def downloadfile(urladdress):
# urladdress="wget -q"+urladdress
purduesquid="http://squid.rcac.purdue.edu"
purduename="purdue.edu"
numgetretries=3
osgsqquid=os.getenv("OSG_SQUID_LOCATION")
print "osg squid is set to:"+str(osgsquid)
hostname=socket.gethostname()
print "hostname is set to:"+str(hostname)
#if
if string.find(hostname,purduename)>=0:
os.putenv("http_proxy",purduesquid)
elif osgsquid != None and string.find(osgsquid.upper(),"UNAVAILABLE")<0:
os.putenv("http_proxy",osgsquid)
else:
os.unsetenv("http_proxy")
successdownload=False
print "using proxy: "+str(os.getenv("http_proxy"))
print "Max Download trials is: "+str(numgetretries)
#try to download three times from http_proxy
for i in range(numgetretries):
print "Trial #: "+str(i)
child=subprocess.Popen(["wget","-q",urladdress],shell=True)
child.wait()
returncode=child.poll()
if returncode ==0:
successdownload ==1
break
else:
#after sleepfor second, try to download again
timeofsleep=(random.random())*180
time.sleep(timeofsleep)
#after three times , still can't download,then unset the suqid, go to download directly
if not successdownload:
os.unsetenv("http_proxy")
print "Do Not Use Proxy"
print "Max Download Trials #: "+str(numgetretries)
for i in range(numgetretries)
print "Unable to download :"+urladdress