Ignore:
Timestamp:
Jun 16, 2011, 4:37:15 PM (9 years ago)
Author:
derek@…
Message:
  • Collect list of active and inactive ports all other relevant data included in the portlist
  • JSON encode all information that is to be submitted (OS data as well as ports)
  • Submit data using curl post
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/gsoc11-statistics/base/src/port/port.tcl

    r78936 r79522  
    24792479
    24802480proc action_stats { action portlist opts } {   
    2481 
     2481    # Build dictionary of os information
     2482    dict set os macports_version [macports::version]
     2483    dict set os osx_version ${macports::macosx_version}
     2484    dict set os os_arch ${macports::os_arch}
     2485    dict set os os_platform ${macports::os_platform}
     2486    dict set os build_arch ${macports::build_arch}
     2487    dict set os gcc_version ${macports::gccversion}
     2488    dict set os xcode_version ${macports::xcodeversion}
     2489   
     2490    # Build dictionary of port information
     2491    dict set ports active   [get_active_ports]
     2492    dict set ports inactive [get_inactive_ports]
     2493   
    24822494    # If no subcommands are given (portlist is empty) print out OS information
    24832495    if {$portlist == ""} {
    2484         # Print information
    2485         puts "Build Information"
    2486         puts "- MacPorts Version [macports::version]"
    2487         puts "- Mac OS X Version ${macports::macosx_version}"
    2488         puts "- Platform ${macports::os_arch} ${macports::os_platform}"
    2489         puts "- Build Arch is ${macports::build_arch}"
    2490         puts "- XCode Version ${macports::xcodeversion}"
     2496        # Print information from os dictionary
     2497        dict for {key values} $os {
     2498            puts "$key: [dict get $os $key]"
     2499        }
    24912500        return 0
    24922501    }
    2493 
     2502   
    24942503    # Make sure there aren't too many subcommands
    24952504    if {[llength $portlist] > 1} {
    2496         puts "Please select only one subcommand. See port help stats"
     2505        ui_error "Please select only one subcommand. See port help stats"
    24972506        return 0
    24982507    }
    24992508
    2500     # Get the command
     2509    # Get the subcommand
    25012510    set cmd [lindex $portlist 0]
    2502        
     2511
     2512    ###### JSON Encoding helper procs ######
     2513   
     2514    # Return JSON encoding of a flat "key":"value" dictionary
     2515    proc json_encode_dict { data } {
     2516        upvar 1 $data db
     2517
     2518        set size [dict size $db]
     2519        set i 1
     2520
     2521        # Initialize the JSON string string
     2522        set json "\{"
     2523
     2524        dict for {key values} $db {
     2525            set line "\"$key\":\"[dict get $db $key]\""
     2526
     2527            # Check if there are any subsequent items
     2528            if {$i < $size} {
     2529                set line "$line, "
     2530            }
     2531
     2532            # Add line to the JSON string
     2533            set json "$json$line"
     2534
     2535            incr i
     2536        }
     2537
     2538        set json "$json\}"
     2539
     2540        return $json
     2541    }
     2542
     2543    # Encodes a list of strings as a JSON array
     2544    proc json_encode_list { data } {   
     2545        set size [llength $data]
     2546        set i 1
     2547
     2548        set json "\["
     2549
     2550        foreach item $data {
     2551            set json "$json$data"
     2552
     2553            # Check if there are any subsequent items
     2554            if {$i < $size} {
     2555                set json "$json, "
     2556            }
     2557
     2558            incr i
     2559        }
     2560
     2561        set json "$json \]"
     2562
     2563        return $json
     2564    }
     2565
     2566    # Encode a port (from a portlist entry) as a JSON object
     2567    proc json_encode_port { port_info } {
     2568        upvar 1 $port_info port
     2569
     2570        set first true
     2571
     2572        set json "\{"
     2573        foreach name [array names port] {
     2574
     2575            # Skip fullname and empty strings
     2576            if {$name == "fullname" || $port($name) == ""} {
     2577                continue
     2578            }
     2579
     2580            # Prepend a comma if this isn't the first item that has been processed
     2581            if {!$first} {
     2582                # Add a comma
     2583                set json "$json, "
     2584           } else {
     2585               set first false
     2586           }
     2587
     2588            # Format the entry as "name_string":"value"
     2589            set entry "\"$name\":\"$port($name)\""
     2590            set json "$json$entry"
     2591
     2592
     2593        }
     2594
     2595        set json "$json\}"
     2596
     2597        return $json
     2598    }
     2599
     2600    # Encode portlist as a JSON array of port objects
     2601    proc json_encode_portlist { portlist } {
     2602        set json "\["
     2603        set first true
     2604
     2605        foreach i $portlist {
     2606            array set port $i
     2607
     2608            set encoded [json_encode_port port]
     2609
     2610            # Prepend a comma if this isn't the first item that has been processed
     2611            if {!$first} {
     2612                # Add a comma
     2613                set json "$json, "
     2614           } else {
     2615               set first false
     2616           }
     2617
     2618            # Append encoded json object
     2619            set json "$json$encoded"
     2620        }
     2621
     2622        set json "$json\]"
     2623
     2624        return $json
     2625    }
     2626
     2627    # Top level container for os and port data
     2628    # Returns a JSON Object with three 
     2629    proc json_encode_stats {os_dict ports_dict} {
     2630        upvar 1 $os_dict os
     2631        upvar 1 $ports_dict ports
     2632
     2633        set os_json [json_encode_dict os]
     2634        set active_ports_json [json_encode_portlist [dict get $ports "active"]]
     2635        set inactive_ports_json [json_encode_portlist [dict get $ports "inactive"]]
     2636
     2637        set json "\{"
     2638        set json "$json \"os\":$os_json,"
     2639        set json "$json \"active_ports\":$active_ports_json,"
     2640        set json "$json \"inactive_ports\":$inactive_ports_json"
     2641        set json "$json\}"
     2642
     2643        return $json
     2644    }
     2645
    25032646    switch $cmd {
    25042647        "submit" {
    2505             # Only submit if the user is participating
    2506             if {[string equal ${macports::stats_participate} "yes"]} {
    2507                 # TODO: proc call which will submit data
    2508                 puts "Will submit collected data"
    2509             }
     2648            # TODO: Get URL from a configuration variable
     2649            set url "http://127.0.0.1/cgi-bin/data.py"
     2650            set json [json_encode_stats os ports]
     2651            curl post "data=$json" $url     
    25102652        }
    25112653        default {
     
    25132655        }
    25142656    }
    2515    
    2516     return 0
     2657
     2658   return 0
    25172659}
    25182660
Note: See TracChangeset for help on using the changeset viewer.