Beispiel #1
0
def push(self, cmdl):
    """ Pulls files from a remote directory. """
    from getpass import getpass
    from paramiko import SSHClient, AutoAddPolicy
    from pylada.ladabase import Manager
    from pylada.ladabase.extracted import generate_extracted
    from hashlib import sha512

    try:
        from .. import fullname
    except ImportError:
        print "Could not import fullname with which to tag files in database.\n" "Please add `fullname = 'my full name'` in ~/.pylada.\n"
        return
    if len(fullname) == 0:
        print "Username with which to tag files in database is empty.\n" "Please add `fullname = 'my full name'` in ~/.pylada.\n"
        return

    if __name__ == "__main__":
        from sys import argv

        cmdl = " ".join(argv)
        try:
            args = _get_push_parser().parse_args()
        except SystemExit:
            return None
    else:
        try:
            args = _get_push_parser().parse_args(cmdl.split())
        except SystemExit:
            return None

    if args.algo == "gw":
        print "Pushing GW data is no yet implemented."
        return

    # gets comment.
    comment = _getcomment(self, cmdl)
    if comment is None:
        return

    # try connecting to host if requested.
    if getattr(args, "hostname", None) is not None:
        client = SSHClient()
        client.set_missing_host_key_policy(AutoAddPolicy())
        try:
            username, hostname = args.hostname.split("@")
        except:
            print "Remote hostname should be given an username@hostname."
            return
        found = False
        for i in range(3):
            client.password = getpass("Pass for {0}@{1}:".format(username, hostname))
            try:
                client.connect(hostname, username=username, password=client.password, timeout=5)
            except KeyboardInterrupt:
                print "Aborting."
                return
            except Exception as e:
                print "error", e
                continue
            else:
                found = True
                break
        if not found:
            print "Could not connect to {0}".format(args.remote)
            return
        # sets up environment for iterating over files.
        client_sftp = client.open_sftp()
        iglob = lambda x: remote_iglob(x, client, client_sftp)
        walk = lambda x: remote_walk(x, client_sftp)

        def context(other):
            @contextmanager
            def _context(path):
                with repatriate_file(path, client_sftp) as filepath:
                    with other(filepath) as result:
                        yield result

            return _context

    # otherwise, look for local files.
    else:
        # sets up environment for iterating over files.
        from os import walk as local_walk

        iglob = local_iglob
        walk = local_walk
        context = lambda x: x

    # Now performs work.
    manager = Manager()
    outcardb = manager.files
    if args.algo == "fere":
        from pylada.ladabase.fere import check_fere_context, generate_fere_summary

        found = False
        for extract, path in walk_calc_files(args, context(check_fere_context), iglob, walk):
            hash = sha512(extract.__outcar__().read()).hexdigest()
            if outcardb.find_one({"sha512": hash}) != None:
                print path, "is already in the database."
                continue
            with extract.__outcar__() as file:
                outcar = file.read()
            item = manager.push(
                path, outcar, comment, compression="bz2", is_dft=extract.is_dft, is_gw=extract.is_gw, uploader=username
            )
            found = True
            print "Pushing", path, "."
            try:
                generate_extracted(filter={"_id": item})
            except:
                print "Could not extract values from ", path, "."
                pass
        if not found:
            print "No new OUTCAR found. "
            return
        generate_fere_summary(2)
Beispiel #2
0
def push(self, cmdl):
  """ Pulls files from a remote directory. """
  from getpass import getpass
  from paramiko import SSHClient, AutoAddPolicy
  from pylada.ladabase import Manager
  from pylada.ladabase.extracted import generate_extracted
  from hashlib import sha512
  try: from .. import fullname
  except ImportError:
    print "Could not import fullname with which to tag files in database.\n"\
          "Please add `fullname = 'my full name'` in ~/.pylada.\n"
    return
  if len(fullname) == 0:
    print "Username with which to tag files in database is empty.\n"\
          "Please add `fullname = 'my full name'` in ~/.pylada.\n"
    return

  if __name__ == "__main__":
    from sys import argv
    cmdl = " ".join(argv)
    try: args = _get_push_parser().parse_args()
    except SystemExit: return None
  else: 
    try: args = _get_push_parser().parse_args(cmdl.split())
    except SystemExit: return None

  if args.algo == "gw": 
    print "Pushing GW data is no yet implemented."
    return
 
  # gets comment. 
  comment = _getcomment(self, cmdl)
  if comment is None: return

  # try connecting to host if requested.
  if getattr(args, 'hostname', None) is not None:
    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy())
    try: username, hostname = args.hostname.split('@')
    except: 
      print "Remote hostname should be given an username@hostname."
      return 
    found = False
    for i in range(3):
      client.password = getpass("Pass for {0}@{1}:".format(username, hostname))
      try: client.connect(hostname, username=username, password=client.password, timeout=5)
      except KeyboardInterrupt:
        print "Aborting."
        return
      except Exception as e: print 'error', e; continue
      else: found = True; break
    if not found: 
      print "Could not connect to {0}".format(args.remote)
      return
    # sets up environment for iterating over files.
    client_sftp = client.open_sftp()
    iglob = lambda x: remote_iglob(x, client, client_sftp)
    walk = lambda x: remote_walk(x, client_sftp)
    def context(other): 
      @contextmanager
      def _context(path):
        with repatriate_file(path, client_sftp) as filepath:
          with other(filepath) as result: yield result
      return _context