ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/ryzzyj/uploadDropboxFiles.py
Revision: 1.7
Committed: Wed Jun 29 13:41:07 2011 UTC (13 years, 10 months ago) by diguida
Content type: text/x-python
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +3 -2 lines
Log Message:
changed to ORA

File Contents

# User Rev Content
1 ryzzyj 1.1 import os
2     import os.path
3     import sys
4     from optparse import OptionParser
5     import urllib
6     import stat
7     import sqlite3
8     import atexit
9     import tarfile
10     import uuid
11     import shutil
12    
13     datafile = "templateForDropbox.txt"
14     timetypes = ("runnumber", "lumiid", "timestamp", "hash", "userid")
15 diguida 1.7 iovchecks = ("offline", "hlt", "express ", "prompt", "pcl", "All")
16 ryzzyj 1.4 dbfile = ""
17 ryzzyj 1.1 isProd = False
18     nameOfTarball = ""
19     nameOfFileDB = ""
20     nameOfFileTXT = ""
21     rezOk = False
22 ryzzyj 1.3 IOVCheckAll = False
23     duplicateEntered = False
24     tag = ""
25 ryzzyj 1.1
26     def tarball(prodTag, nameOfTemplateFile, nameOfOldDBFile):
27     try:
28     if prodTag == "":
29     return ""
30    
31     uuidOfFiles = str(uuid.uuid1())
32    
33     global nameOfFileDB
34     global nameOfFileTXT
35     global nameOfTarball
36     nameOfFileDB = prodTag + "@" + uuidOfFiles + ".db"
37     nameOfFileTXT = prodTag + "@" + uuidOfFiles + ".txt"
38     nameOfTarball = prodTag + "@" + uuidOfFiles + ".tar.bz2"
39    
40     shutil.copy(nameOfTemplateFile, nameOfFileTXT)
41     shutil.copy(nameOfOldDBFile, nameOfFileDB)
42    
43     os.chmod(nameOfFileTXT, stat.S_IREAD | stat.S_IWRITE | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
44    
45     if isProd:
46     tar = tarfile.open(nameOfTarball,"w:bz2")
47    
48     for name in [nameOfFileDB, nameOfFileTXT]:
49     tar.add(name)
50     tar.close()
51     os.chmod(nameOfTarball, stat.S_IREAD | stat.S_IWRITE | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
52    
53    
54     except:
55     print "Failed to generate files."
56    
57    
58     def deleteOldFile(path):
59     if os.path.isfile(str(path)):
60     os.remove(datafile)
61    
62     def downloadFile(path, where):
63     print "Downloading file %s..." % path
64     err = urllib.urlretrieve(path, where)
65     if err == 0:
66     print "Error downloading file %s! Closing program." % path
67     sys.exit()
68    
69     def uploadFiles():
70     try:
71     if nameOfTarball == "":
72     raise Exception()
73    
74     localfile = nameOfTarball
75     remotehost = "webcondvm.cern.ch"
76     remotefile = "/tmp"
77    
78     print "Uploading files..."
79     commandBeginning = '"mv /tmp/'
80     if isProd:
81 ryzzyj 1.5 commandEnding = '/DropBox"'
82 diguida 1.6 if os.path.exists(nameOfTarball) and os.path.isfile(nameOfTarball) and os.path.getsize(nameOfTarball) != 0:
83 ryzzyj 1.5 os.system('scp -p "%s" "%s":"%s"' % (nameOfTarball, remotehost, remotefile) )
84     #os.system('scp -p "%s" "%s":"%s"' % (nameOfTarball, remotehost, remotefile) )
85     os.system('ssh "%s" "%s""%s" "%s" ' % (remotehost, commandBeginning, nameOfTarball, commandEnding) )
86     else:
87     print 'ERROR: Could not upload file because it has 0 size.'
88 ryzzyj 1.1 else:
89 ryzzyj 1.5 commandEnding = '/DropBox_test"'
90 diguida 1.6 if os.path.exists(nameOfFileDB) and os.path.isfile(nameOfFileDB) and os.path.getsize(nameOfFileDB) != 0 and os.path.exists(nameOfFileTXT) and os.path.isfile(nameOfFileTXT) and os.path.getsize(nameOfFileTXT) != 0:
91 ryzzyj 1.5 os.system('scp -p "%s" "%s":"%s"' % (nameOfFileDB, remotehost, remotefile) )
92     os.system('scp -p "%s" "%s":"%s"' % (nameOfFileTXT, remotehost, remotefile) )
93     os.system('ssh "%s" "%s""%s" "%s" ' % (remotehost, commandBeginning, nameOfFileDB, commandEnding) )
94     os.system('ssh "%s" "%s""%s" "%s" ' % (remotehost, commandBeginning, nameOfFileTXT, commandEnding) )
95     else:
96     print 'ERROR: Could not upload files because one or more files have 0 size.'
97 ryzzyj 1.1 except:
98     print "Error uploading file %s" % nameOfTarball
99    
100     def getDestDB(line):
101     print "Choose dropbox type:"
102     print "0) Production"
103     print "1) Preparation"
104     while True:
105     try:
106     answ = int(raw_input("Your choice: "))
107     except:
108     answ = -1
109     if (answ < 0) or (answ > 1):
110     print "Wrong number. Number has to be between %d and %d. Please try again." % (0, 1)
111     else:
112     while True:
113     answ1 = raw_input("ENTER your username: ")
114     if answ1.find(" ") != -1:
115     print "Username can't contain whitespaces. Please make sure you typed your username correctly."
116     elif len(answ1) > 0:
117 ryzzyj 1.3 break
118     while True:
119     answ2 = raw_input("ENTER the Destination Database schema name: ")
120     if answ2.find(" ") != -1:
121     print "Database schema name can't contain whitespaces. Please make sure you typed the Database schema name correctly."
122     elif len(answ2) > 0:
123     break
124 ryzzyj 1.1 global isProd
125     if answ == 0:
126     isProd = True
127 diguida 1.2 return "oracle://cms_orcon_prod/%s" % answ2
128 ryzzyj 1.1 else:
129     isProd = False
130 diguida 1.2 return "oracle://cms_orcoff_prep/%s" % answ2
131 ryzzyj 1.1
132     def getInputtag(line):
133     try:
134 ryzzyj 1.3 connection = sqlite3.connect(dbfile)
135 ryzzyj 1.1 cur = connection.cursor()
136 diguida 1.7 #cur.execute("select name from metadata")
137     cur.execute("SELECT OBJECT_NAME FROM ORA_NAMING_SERVICE")
138 ryzzyj 1.1 except ValueError:
139     print "Couldn't connect to database. Closing program."
140     sys.exit()
141     except:
142 ryzzyj 1.3 print "Couldn't read input tags from %s. Closing program." % dbfile
143 ryzzyj 1.1 sys.exit()
144     rows = cur.fetchall()
145     if len(rows) <= 0:
146 ryzzyj 1.3 print "There are no input tags in %s. Closing program." % dbfile
147 ryzzyj 1.1 sys.exit()
148     if len(rows) == 1:
149     print "Only one input tag found. Setting inputtag value as '%s'" % rows[0][0]
150     return rows[0][0]
151     else:
152     i = 0
153     for c in rows:
154     print "%d) %s" % (i, c[0])
155     i += 1
156     while True:
157     try:
158     answ = int(raw_input("Your choice: "))
159     except:
160     answ = -1
161     if (answ < 0) or (answ >= len(rows)):
162     print "Wrong number. Number has to be between %d and %d. Please try again." % (0, len(rows) - 1)
163     else:
164     return rows[answ][0]
165    
166     def getTimetype(line):
167     i = 0
168     for i in range(len(timetypes)):
169     print "%d) %s" % (i, timetypes[i])
170     while True:
171     try:
172     answ = int(raw_input("Your choice: "))
173     except:
174     answ = -1
175     if (answ < 0) or (answ >= len(timetypes)):
176     print "Wrong number. Number has to be between %d and %d. Please try again." % (0, len(timetypes) - 1)
177     else:
178     return timetypes[answ]
179    
180     def getIOVCheck(line):
181     i = 0
182     for i in range(len(iovchecks)):
183     print "%d) %s" % (i, iovchecks[i])
184     while True:
185     try:
186     answ = int(raw_input("Your choice: "))
187     except:
188     answ = -1
189     if (answ < 0) or (answ >= len(iovchecks)):
190     print "Wrong number. Number has to be between %d and %d. Please try again." % (0, len(iovchecks) - 1)
191     else:
192 ryzzyj 1.3 if iovchecks[answ].lower() == "all":
193     global IOVCheckAll
194     IOVCheckAll = True
195 ryzzyj 1.1 return iovchecks[answ]
196    
197     def getTag(line):
198     answ = raw_input()
199 ryzzyj 1.3 if len(answ.strip()) <= 0:
200 ryzzyj 1.1 return getInput(line)
201 ryzzyj 1.3 global tag
202     tag = answ
203     return answ
204    
205     def getDuplicate(line):
206     answ = raw_input()
207     if len(answ.strip()) > 0:
208     global duplicateEntered
209     duplicateEntered = True
210 ryzzyj 1.1 return answ
211    
212     def getInput(line):
213     print "ENTER %s: " % line,
214     if line.lower() == "destdb":
215     print " "
216     return getDestDB(line)
217     elif line.lower() == "inputtag":
218     print " "
219     return getInputtag(line)
220     elif line.lower() == "timetype":
221     print " "
222     return getTimetype(line)
223     elif line.lower() == "iovcheck":
224     print " "
225     return getIOVCheck(line)
226     elif line.lower() == "tag":
227     return getTag(line)
228 ryzzyj 1.3 elif line.lower().find("duplicate") == 0:
229     return getDuplicate(line)
230 ryzzyj 1.1 else:
231     return raw_input()
232    
233     def populateFile():
234     os.chmod(datafile, stat.S_IREAD | stat.S_IWRITE | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
235     tf = open(datafile, "r")
236 ryzzyj 1.3 values = {}
237     donotify = True
238     duplicateTags = []
239 ryzzyj 1.1 for line in tf:
240     value = line[0:line.find(" ")]
241 ryzzyj 1.3 if value.lower().find("duplicate") == 0 and IOVCheckAll:
242     if donotify:
243     print "INFO: Value 'All' for 'IOVCheck' property requires you to enter at least 1 duplicate tag."
244     donotify = False
245     duplicateTags.append(value)
246     #value = value + " " + getInput(value) + "\n"
247     values[value] = getInput(value)
248     while not duplicateEntered and IOVCheckAll:
249     print "ERROR: Can't submit the metadata file while none of duplicate tags are filled."
250     for dt in duplicateTags:
251     print "ENTER %s: " % dt,
252     values[dt] = getDuplicate(dt)
253 ryzzyj 1.1 global rezOk
254     rezOk = True
255     tf.close()
256     df = open(datafile, "w")
257 ryzzyj 1.3 for key in values.keys():
258     df.write("%s %s\n" % (key, values[key]))
259 ryzzyj 1.1 df.close()
260    
261     def checkPkg():
262     if os.path.isfile("$CMSSW_BASE/lib/$SCRAM_ARCH/libCondFormats*.so"):
263     print "You have compiled version of CondFormats. Please make sure, that you haven't compiled the package. If you don't know how to do it, please contact system administrator."
264    
265     def validateArgs():
266 ryzzyj 1.3 global dbfile
267 ryzzyj 1.1 if options.dbfile == None:
268     cont = True
269     while cont:
270     print "You didn't specify DB file. Would you like to do it now (if not, program will close)? (y/n): ",
271     answ = raw_input()
272     if answ.lower() == "y":
273     answ1 = raw_input("ENTER name of DB file: ")
274     if not os.path.isfile(answ1):
275     print "File %s not found." % answ1
276     else:
277 ryzzyj 1.3 dbfile = answ1
278 ryzzyj 1.1 cont = False
279     elif answ.lower() == "n":
280     sys.exit()
281     else:
282     print "Answer has to be Y or N."
283     elif not os.path.isfile(options.dbfile):
284     print "File %s not found. Closing program." % options.dbfile
285     sys.exit()
286 ryzzyj 1.3 elif os.path.isfile(options.dbfile):
287     dbfile = options.dbfile
288 ryzzyj 1.1
289     def prepare_exit():
290     if not rezOk:
291     sys.exit()
292     if os.path.isfile(datafile):
293     os.remove(datafile)
294     if os.path.isfile(nameOfTarball):
295     os.remove(nameOfTarball)
296     if os.path.isfile(nameOfFileDB):
297     os.remove(nameOfFileDB)
298     if os.path.isfile(nameOfFileTXT):
299     os.remove(nameOfFileTXT)
300 ryzzyj 1.3
301 ryzzyj 1.1 #======= Main script
302     parser = OptionParser()
303     parser.add_option("-f", "--force", dest = "force", default = False, action = "store_true", help = "forces script to skip CondFormats and CVS tag check", )
304 ryzzyj 1.3 parser.add_option("-s", "--sql", dest = "dbfile", type="string", help = "name of the DB file")
305 ryzzyj 1.1 (options, args) = parser.parse_args()
306    
307     atexit.register(prepare_exit)
308     validateArgs()
309     deleteOldFile("templateForDropbox.txt")
310     downloadFile("http://condb.web.cern.ch/condb/DropBoxOffline/templateForDropbox.txt", datafile)
311     populateFile()
312     if not options.force:
313     checkPkg()
314 ryzzyj 1.3 tarball(tag, datafile, dbfile)
315 ryzzyj 1.1 uploadFiles()
316     #======= End of script
317    
318    
319    
320    
321    
322    
323 diguida 1.2