CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
popcon2dropbox.py
Go to the documentation of this file.
1 import subprocess
2 import json
3 import netrc
4 from os import rename
5 from os import remove
6 from os import path
7 
8 confFileName ='popcon2dropbox.json'
9 fileNameForDropBox = 'input_for_dropbox'
10 dbFileForDropBox = '%s.db' %fileNameForDropBox
11 dbLogFile = '%s_log.db' %fileNameForDropBox
12 
13 import upload_popcon
14 
15 class CondMetaData(object):
16  def __init__( self ):
17  self.md = {}
18  with open(confFileName) as jf:
19  self.md = json.load(jf)
20 
21  def authPath( self ):
22  return self.md.get('authenticationPath')
23 
24  def authSys( self ):
25  return self.md.get('authenticationSystem')
26 
27  def destinationDatabase( self ):
28  return self.md.get('destinationDatabase')
29 
30  def logDbFileName( self ):
31  return self.md.get('logDbFileName')
32 
33  def records( self ):
34  return self.md.get('records')
35 
36  def dumpMetadataForUpload( self, inputtag, desttag, comment ):
37  uploadMd = {}
38  uploadMd['destinationDatabase'] = self.destinationDatabase()
39  tags = {}
40  tagInfo = {}
41  tags[ desttag ] = tagInfo
42  uploadMd['destinationTags'] = tags
43  uploadMd['inputTag'] = inputtag
44  uploadMd['since'] = None
45  uploadMd['userText'] = comment
46  with open( '%s.txt' %fileNameForDropBox, 'wb') as jf:
47  jf.write( json.dumps( uploadMd, sort_keys=True, indent = 2 ) )
48  jf.write('\n')
49 
50 def runO2O( cmsswdir, releasepath, release, arch, jobfilename, logfilename, *p ):
51  # first remove any existing metadata file...
52  if path.exists( '%s.db' %fileNameForDropBox ):
53  print "Removing files with name %s" %fileNameForDropBox
54  remove( '%s.db' %fileNameForDropBox )
55  if path.exists( '%s.txt' %fileNameForDropBox ):
56  remove( '%s.txt' %fileNameForDropBox )
57  command = 'export SCRAM_ARCH=%s;' %arch
58  command += 'CMSSWDIR=%s;' %cmsswdir
59  command += 'source ${CMSSWDIR}/cmsset_default.sh;'
60  command += 'cd %s/%s/src;' %(releasepath,release)
61  command += 'eval `scramv1 runtime -sh`;'
62  command += 'cd -;'
63  command += 'pwd;'
64  command += 'cmsRun %s ' %jobfilename
65  command += ' '.join(p)
66  command += ' 2>&1'
67  pipe = subprocess.Popen( command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
68  stdout_val = pipe.communicate()[0]
69  return stdout_val
70 
71 def upload_to_dropbox( backend ):
72  md = CondMetaData()
73  # check if the expected input file is there...
74  if not path.exists( dbFileForDropBox ):
75  print 'The input sqlite file has not been produced.'
76  return False
77  # first remove any existing metadata file...
78  if path.exists( '%s.txt' %fileNameForDropBox ):
79  remove( '%s.txt' %fileNameForDropBox )
80  try:
81  dropBox = upload_popcon.ConditionsUploader(upload_popcon.defaultHostname, upload_popcon.defaultUrlTemplate)
82  # Try to find the netrc entry
83  try:
84  (username, account, password) = netrc.netrc().authenticators(upload_popcon.defaultNetrcHost)
85  except Exception:
86  print 'Netrc entry "DropBox" not found.'
87  return False
88  print 'signing in...'
89  dropBox.signIn(username, password)
90  print 'signed in'
91  ret = True
92  for k,v in md.records().items():
93  destTag = v.get("destinationTag")
94  inputTag = v.get("sqliteTag")
95  if inputTag == None:
96  inputTag = destTag
97  comment = v.get("comment")
98  metadata = md.dumpMetadataForUpload( inputTag, destTag, comment )
99  ret &= dropBox.uploadFile(dbFileForDropBox, backend, upload_popcon.defaultTemporaryFile)
100  dropBox.signOut()
101  return ret
102  except upload_popcon.HTTPError as e:
103  print e
104  return False
105 
static std::string join(char **cmd)
Definition: RemoteFile.cc:18