CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Public Attributes | Private Member Functions
upload_popcon.DropBox Class Reference
Inheritance diagram for upload_popcon.DropBox:

Public Member Functions

def __init__
 
def signIn
 
def signInSSO
 
def signOut
 
def uploadFile
 

Public Attributes

 hostname
 
 http
 

Private Member Functions

def _checkForUpdates
 

Detailed Description

A dropBox API class.

Definition at line 411 of file upload_popcon.py.

Constructor & Destructor Documentation

def upload_popcon.DropBox.__init__ (   self,
  hostname = defaultHostname,
  urlTemplate = defaultUrlTemplate 
)

Definition at line 415 of file upload_popcon.py.

416  def __init__(self, hostname = defaultHostname, urlTemplate = defaultUrlTemplate):
417  self.hostname = hostname
418  self.http = HTTP()
419  self.http.setBaseUrl(urlTemplate % hostname)
420 

Member Function Documentation

def upload_popcon.DropBox._checkForUpdates (   self)
private
Updates this script, if a new version is found.

Definition at line 471 of file upload_popcon.py.

References upload_popcon.DropBox.hostname, and upload_popcon.DropBox.signOut().

472  def _checkForUpdates(self):
473  '''Updates this script, if a new version is found.
474  '''
475 
476  logging.info('%s: Checking for updates...', self.hostname)
477  version = int(self.http.query('getUploadScriptVersion'))
478 
479  if version <= __version__:
480  logging.info('%s: Up to date.', self.hostname)
481  return
482 
483  logging.info('%s: There is a newer version (%s) than the current one (%s): Updating...', self.hostname, version, __version__)
484 
485  logging.info('%s: Downloading new version...', self.hostname)
486  uploadScript = self.http.query('getUploadScript')
487 
488  self.signOut()
489 
490  logging.info('%s: Saving new version...', self.hostname)
491  with open(sys.argv[0], 'wb') as f:
492  f.write(uploadScript)
493 
494  logging.info('%s: Executing new version...', self.hostname)
495  os.execl(sys.executable, *([sys.executable] + sys.argv))
496 
def upload_popcon.DropBox.signIn (   self,
  username,
  password 
)
Signs in the server.

Definition at line 452 of file upload_popcon.py.

References upload_popcon.DropBox.hostname.

Referenced by cmstc.TagCollector.signInInteractive().

453  def signIn(self, username, password):
454  '''Signs in the server.
455  '''
456 
457  logging.info('%s: Signing in...', self.hostname)
458  self.http.query('signIn', {
459  'username': username,
460  'password': password,
461  })
462 
def upload_popcon.DropBox.signInSSO (   self,
  secure = True 
)
Signs in the server via CERN SSO.

Definition at line 421 of file upload_popcon.py.

References upload_popcon.DropBox.hostname.

422  def signInSSO(self, secure = True):
423  '''Signs in the server via CERN SSO.
424  '''
425 
426  if secure:
427  logging.info('%s: Signing in via CERN SSO...', self.hostname)
428  else:
429  logging.info('%s: Signing in via CERN SSO (insecure)...', self.hostname)
430 
431  # FIXME: Insecure connection to -prod until the certificates are fixed.
432  # The connection to the CERN SSO is still secure by default.
433  # On -dev and -int the certificates are installed properly.
434  secureTarget = True
435  if 'cms-conddb-prod' in self.hostname:
436  secureTarget = False
437 
438  # We also use the CERN CA certificate to verify the targets,
439  # so if we are not connecting securely to CERN SSO is because
440  # we do not have the CERN-CA-certs package, so we need to skip
441  # this as well.
442  #
443  # i.e. right now we have these options:
444  # secure == True, secureTarget == True with CERN CA cert, -dev and -int
445  # secure == True, secureTarget == False with CERN CA cert, -prod
446  # secure == False, secureTarget == False without CERN CA cert
447  if not secure:
448  secureTarget = False
449 
450  self.http.addCERNSSOCookies('signInSSO', secureTarget, secure)
451 
def upload_popcon.DropBox.signOut (   self)
Signs out the server.

Definition at line 463 of file upload_popcon.py.

References upload_popcon.DropBox.hostname.

Referenced by upload_popcon.DropBox._checkForUpdates().

464  def signOut(self):
465  '''Signs out the server.
466  '''
467 
468  logging.info('%s: Signing out...', self.hostname)
469  self.http.query('signOut')
470 
def upload_popcon.DropBox.uploadFile (   self,
  filename,
  backend = defaultBackend,
  temporaryFile = defaultTemporaryFile 
)
Uploads a file to the dropBox.

The filename can be without extension, with .db or with .txt extension.
It will be stripped and then both .db and .txt files are used.

Definition at line 497 of file upload_popcon.py.

References upload_popcon.addToTarFile(), and upload_popcon.DropBox.hostname.

498  def uploadFile(self, filename, backend = defaultBackend, temporaryFile = defaultTemporaryFile):
499  '''Uploads a file to the dropBox.
500 
501  The filename can be without extension, with .db or with .txt extension.
502  It will be stripped and then both .db and .txt files are used.
503  '''
504 
505  basepath = filename.rsplit('.db', 1)[0].rsplit('.txt', 1)[0]
506  basename = os.path.basename(basepath)
507 
508  logging.info('%s: %s: Creating tar file...', self.hostname, basename)
509 
510  tarFile = tarfile.open(temporaryFile, 'w:bz2')
511 
512  with open('%s.db' % basepath, 'rb') as data:
513  addToTarFile(tarFile, data, 'data.db')
514 
515  with tempfile.NamedTemporaryFile() as metadata:
516  with open('%s.txt' % basepath, 'rb') as originalMetadata:
517  json.dump(json.load(originalMetadata), metadata, sort_keys = True, indent = 4)
518 
519  metadata.seek(0)
520  addToTarFile(tarFile, metadata, 'metadata.txt')
521 
522  tarFile.close()
523 
524  logging.info('%s: %s: Calculating hash...', self.hostname, basename)
525 
526  fileHash = hashlib.sha1()
527  with open(temporaryFile, 'rb') as f:
528  while True:
529  data = f.read(4 * 1024 * 1024)
530 
531  if not data:
532  break
533 
534  fileHash.update(data)
535 
536  fileHash = fileHash.hexdigest()
537 
538  logging.info('%s: %s: Hash: %s', self.hostname, basename, fileHash)
539 
540  logging.info('%s: %s: Uploading file for the %s backend...', self.hostname, basename, backend)
541  os.rename(temporaryFile, fileHash)
542  self.http.query('uploadFile', {
543  'backend': backend,
544  'fileName': basename,
545  }, files = {
546  'uploadedFile': fileHash,
547  })
548  os.unlink(fileHash)
549 

Member Data Documentation

upload_popcon.DropBox.hostname

Definition at line 416 of file upload_popcon.py.

Referenced by upload_popcon.DropBox._checkForUpdates(), upload_popcon.DropBox.signIn(), upload_popcon.DropBox.signInSSO(), upload_popcon.DropBox.signOut(), and upload_popcon.DropBox.uploadFile().

upload_popcon.DropBox.http

Definition at line 417 of file upload_popcon.py.