Exemplo n.º 1
0
def main(args):
  options = app.get_options()
  if options.show_help:
    app.help()

  if options.show_version or options.just_version:
    print >> sys.stdout, 'Python NailGun client version 0.0.1'
    if options.just_version:
      sys.exit(0)


  # Assume ng.pex has been aliased to the command name
  command = re.compile('.pex$').sub('', os.path.basename(sys.argv[0]))
  args_index = 0

  # Otherwise the command name is the 1st arg
  if command == 'ng':
    if not args:
      app.help()

    command = args[0]
    args_index = 1

  ng = NailgunClient(host=options.ng_host, port=options.ng_port)
  result = ng(command, *args[args_index:], **os.environ)
  sys.exit(result)
Exemplo n.º 2
0
def main(args):
  """Given .thermos configs, loads them and prints out information about them."""

  if len(args) == 0:
    app.help()

  for arg in args:
    print('\nparsing %s\n' % arg)
    tc = ThermosConfigLoader.load(arg)

    for task_wrapper in tc.tasks():
      task = task_wrapper.task
      if not task.has_name():
        print('Found unnamed task!  Skipping...')
        continue

      print('Task: %s [check: %s]' % (task.name(), task.check()))
      if not task.processes():
        print('  No processes.')
      else:
        print('  Processes:')
        for proc in task.processes():
          print('    %s' % proc)

      ports = task_wrapper.ports()
      if not ports:
        print('  No unbound ports.')
      else:
        print('  Ports:')
        for port in ports:
          print('    %s' % port)

      print('raw:')
      pprint.pprint(json.loads(task_wrapper.to_json()))
Exemplo n.º 3
0
def main(args):
    options = app.get_options()
    if options.show_help:
        app.help()

    if options.show_version or options.just_version:
        print('Python NailGun client version 0.0.1', file=sys.stdout)
        if options.just_version:
            sys.exit(0)

    # Assume ng.pex has been aliased to the command name
    command = re.compile('.pex$').sub('', os.path.basename(sys.argv[0]))
    args_index = 0

    # Otherwise the command name is the 1st arg
    if command == 'ng':
        if not args:
            app.help()

        command = args[0]
        args_index = 1

    ng = NailgunClient(host=options.ng_host, port=options.ng_port)
    result = ng(command, *args[args_index:], **os.environ)
    sys.exit(result)
Exemplo n.º 4
0
def main(args, options):
  if len(args) != 1:
    app.help()

  torrent = Torrent.from_file(args[0])

  print('Torrent: %s' % args[0])
  print('Announce url: %s' % torrent.announce)

  print('Metainfo:')
  files = list(torrent.info.files(rooted_at=''))
  print('  name: %s' % torrent.info.name)
  print('  size: %d' % torrent.info.length)
  print('  files: %d' % len(files))
  print('  pieces: %d' % torrent.info.num_pieces)
  print('  piece size: %d' % torrent.info.piece_size)
  print()

  print('Hash manifest:')
  for index, hash in enumerate(torrent.info.piece_hashes):
    print('   [%4d]: %s' % (index, binascii.hexlify(hash)))
  print()

  print('File manifest:')
  for mif in files:
    print('  offset: [%9d-%9d] size: [%9d] filename: %s' % (mif.start, mif.end, mif.length, mif.name))
Exemplo n.º 5
0
def main(args):
    """Given .thermos configs, loads them and prints out information about them."""

    if len(args) == 0:
        app.help()

    for arg in args:
        print('\nparsing %s\n' % arg)
        tc = ThermosConfigLoader.load(arg)

        for task_wrapper in tc.tasks():
            task = task_wrapper.task
            if not task.has_name():
                print('Found unnamed task!  Skipping...')
                continue

            print('Task: %s [check: %s]' % (task.name(), task.check()))
            if not task.processes():
                print('  No processes.')
            else:
                print('  Processes:')
                for proc in task.processes():
                    print('    %s' % proc)

            ports = task_wrapper.ports()
            if not ports:
                print('  No unbound ports.')
            else:
                print('  Ports:')
                for port in ports:
                    print('    %s' % port)

            print('raw:')
            pprint.pprint(json.loads(task_wrapper.to_json()))
Exemplo n.º 6
0
Arquivo: ng.py Projeto: alfss/commons
def main(args):
  options = app.get_options()
  if options.show_help:
    app.help()

  if options.show_version or options.just_version:
    print('Python NailGun client version %s' % VERSION)
    if options.just_version:
      sys.exit(0)

  # Assume ng.pex has been aliased to the command name
  command = re.compile('.pex$').sub('', os.path.basename(sys.argv[0]))
  args_index = 0

  # Otherwise the command name is the 1st arg
  if command == 'ng':
    if not args:
      app.help()

    command = args[0]
    args_index = 1

  ng = NailgunClient(host=options.ng_host, port=options.ng_port)
  try:
    result = ng(command, *args[args_index:], **os.environ)
    sys.exit(result)
  except ng.NailgunError as e:
    print('Problem executing command: %s' % e, file=sys.stderr)
    sys.exit(1)
Exemplo n.º 7
0
def help_command(args, options):
  """Get help about a specific command.
  """
  if len(args) == 0:
    app.help()
  for (command, doc) in app.get_commands_and_docstrings():
    if args[0] == command:
      print('command %s:' % command)
      print(doc)
      app.quit(0)
  print('unknown command: %s' % args[0], file=sys.stderr)
Exemplo n.º 8
0
    def main(_, opts):
        """Main"""

        if not opts.bucket:
            log.error('--bucket is required.')
            app.help()

        server = S3Web(bucket=opts.bucket,
                       prefix=opts.prefix,
                       access_key_id=opts.access_key_id,
                       secret_key=opts.secret_key)
        thread = ExceptionalThread(
            target=lambda: server.run(opts.listen,
                                      opts.port,
                                      server='cherrypy'))
        thread.daemon = True
        thread.start()

        log.info('Ready.')
        app.wait_forever()
Exemplo n.º 9
0
def main(args):
    values = app.get_options()

    if len(args) > 0:
        print >> sys.stderr, "ERROR: unrecognized arguments: %s\n" % (
            " ".join(args))
        app.help()
        sys.exit(1)

    if not values.ckpt:
        print >> sys.stderr, "ERROR: must supply --checkpoint"
        app.help()
        sys.exit(1)

    fp = file(values.ckpt, "r")
    rr = ThriftRecordReader(fp, RunnerCkpt)
    wrs = RunnerState(processes={})
    dispatcher = CheckpointDispatcher()
    try:
        for wts in rr:
            print 'Recovering: ', wts
            if values.assemble is True:
                dispatcher.dispatch(wrs, wts)
    except RecordIO.Error as err:
        print 'Error recovering checkpoint stream: %s' % err
        return
    print '\n\n\n'
    if values.assemble:
        print 'Recovered Task Header'
        pprint.pprint(wrs.header, indent=4)

        print '\nRecovered Task States'
        for task_status in wrs.statuses:
            print '  %s [pid: %d] => %s' % (
                time.asctime(time.localtime(task_status.timestamp_ms /
                                            1000.0)), task_status.runner_pid,
                TaskState._VALUES_TO_NAMES[task_status.state])

        print '\nRecovered Processes'
        pprint.pprint(wrs.processes, indent=4)
Exemplo n.º 10
0
def main(args):
  values = app.get_options()

  if len(args) > 0:
    print("ERROR: unrecognized arguments: %s\n" % (" ".join(args)), file=sys.stderr)
    app.help()
    sys.exit(1)

  if not values.ckpt:
    print("ERROR: must supply --checkpoint", file=sys.stderr)
    app.help()
    sys.exit(1)

  fp = file(values.ckpt, "r")
  rr = ThriftRecordReader(fp, RunnerCkpt)
  wrs = RunnerState(processes={})
  dispatcher = CheckpointDispatcher()
  try:
    for wts in rr:
      print('Recovering: %s' % wts)
      if values.assemble is True:
        dispatcher.dispatch(wrs, wts)
  except RecordIO.Error as err:
    print('Error recovering checkpoint stream: %s' % err, file=sys.stderr)
    return
  print('\n\n\n')
  if values.assemble:
    print('Recovered Task Header')
    pprint.pprint(wrs.header, indent=4)

    print('\nRecovered Task States')
    for task_status in wrs.statuses:
      print('  %s [pid: %d] => %s' % (
          time.asctime(time.localtime(task_status.timestamp_ms / 1000.0)),
          task_status.runner_pid,
          TaskState._VALUES_TO_NAMES[task_status.state]))

    print('\nRecovered Processes')
    pprint.pprint(wrs.processes, indent=4)
Exemplo n.º 11
0
def main(args, options):
    if len(args) <= 2:
        app.help()

    torrent_name, tracker = args[0:2]

    filelist = []

    def add_file(filename):
        print("Adding %s" % filename)
        filelist.append(os.path.realpath(filename))

    def add_dir(dirname):
        for root, _, files in os.walk(dirname):
            for filename in files:
                add_file(os.path.join(root, filename))

    def add(file_or_dir):
        if os.path.isfile(file_or_dir):
            add_file(file_or_dir)
        elif os.path.isdir(file_or_dir):
            add_dir(file_or_dir)
        else:
            app.error("Unknown or non-existent file: %s" % file_or_dir)

    for path in args[2:]:
        add(path)

    if options.relpath:
        relpath = os.path.realpath(options.relpath)
    else:
        relpath = os.path.realpath(".")

    mib = MetaInfoBuilder(filelist, relpath=relpath)
    torrent = Torrent.from_metainfo(mib.build(), tracker)

    print("Writing torrent to file: %s" % torrent_name)
    torrent.to_file(torrent_name)
Exemplo n.º 12
0
def main(args, opts):
  if args:
    print("ERROR: unrecognized arguments: %s\n" % (" ".join(args)), file=sys.stderr)
    app.help()
    sys.exit(1)

  root_server = HttpServer()
  root_server.mount_routes(DiagnosticsEndpoints())

  task_observer = TaskObserver(opts.root)
  task_observer.start()

  bottle_wrapper = BottleObserver(task_observer)

  root_server.mount_routes(bottle_wrapper)

  def run():
    root_server.run('0.0.0.0', opts.port, 'cherrypy')

  et = ExceptionalThread(target=run)
  et.daemon = True
  et.start()
  et.join()
Exemplo n.º 13
0
    def main(args, opts):
        if args:
            print("ERROR: unrecognized arguments: %s\n" % (" ".join(args)),
                  file=sys.stderr)
            app.help()
            sys.exit(1)

        root_server = HttpServer()
        root_server.mount_routes(DiagnosticsEndpoints())

        task_observer = TaskObserver(opts.root)
        task_observer.start()

        bottle_wrapper = BottleObserver(task_observer)

        root_server.mount_routes(bottle_wrapper)

        def run():
            root_server.run('0.0.0.0', opts.port, 'cherrypy')

        et = ExceptionalThread(target=run)
        et.daemon = True
        et.start()
        et.join()
Exemplo n.º 14
0
def main():
    app.help()
Exemplo n.º 15
0
 def main(args, options):
   app.help()
Exemplo n.º 16
0
def main():
  app.help()
Exemplo n.º 17
0
def main(args, options):
    # Requests vendors its own urllib3, which emits annoying messages
    # when using insecure mode
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

    if len(args) != 0:
        app.help()

    if options.gen_token:
        generate_token_interactive()
        app.shutdown(0)

    # Kind of a hack, but whatever
    option_definitions = app.Application.active()._main_options

    if not required_options_present(option_definitions, options):
        app.help()

    download_path = os.path.expanduser(options.download_path)

    base_params = dict(
        t = options.token,
        u = options.username,
        s = options.salt,
        c = app.name(),
        f = 'json',
        v = constants.API_VERSION)

    session = requests.Session()
    session.params.update(base_params)
    session.verify = not options.insecure

    # Get starred songs
    try:
        get_starred_response = handle_request(
            lambda: session.get("{}/rest/getStarred.view".format(options.subsonic_uri)))
    except RequestError as e:
        log.error("Bad response from Subsonic while fetching starred songs list:\n{}".format(e))
        raise

    # Extract songs from response
    try:
        starred_songs = filter(lambda song: song['contentType'].startswith('audio'),
                               get_starred_response.json()['subsonic-response']['starred']['song'])
    except (KeyError, ValueError) as e:
        reraise_as_exception_type(RequestError, e)

    # Do nothing if no songs are starred
    if len(starred_songs) == 0:
        app.shutdown(0)

    # Sort the songs by starred date so we can sync them in chronological order
    sorted_starred_songs = sorted(starred_songs, key=song_to_starred_time_struct)

    start_date = get_start_date(download_path, sorted_starred_songs, songs_sorted=True,
                                since=options.since)

    sync_file_path = get_sync_file_path(download_path)

    # Sync each song in chronological order by starred date
    for song in sorted_starred_songs:
        song_full_path = os.path.join(download_path, song['path'])
        if song_to_starred_time_struct(song) >= start_date and not os.path.exists(song_full_path):
            create_directory_if_missing_from_path(song_full_path)
            try:
                download_params = {'id': song['id']}
                download_response = handle_request(
                    lambda: session.get("{}/rest/download.view".format(options.subsonic_uri),
                                        params=download_params),
                    validate_json=False)
            except RequestError as e:
                log.error("Failed downloading the following song: {}\n{}".format(song['path'], e))
                raise
            with open_tempfile_with_atomic_write_to(song_full_path, mode='wb') as download_file:
                download_file.write(download_response.content)
            starred_date = song_to_starred_time_struct(song)
            if starred_date != time.gmtime(0):
                sync_file_is_stale = True
                # Try to read most recent sync date from sync file.
                try:
                    if read_time_struct_from_sync_file(sync_file_path) > starred_date:
                        sync_file_is_stale = False
                except SyncFileError:
                    pass
                # Write starred date of downloaded file if newer than existing date.
                if sync_file_is_stale:
                    try:
                        write_time_struct_to_sync_file(sync_file_path, starred_date)
                    except SyncFileError:
                        pass
Exemplo n.º 18
0
 def main(args, options):
     app.help()